国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

LEETCODE--Best Time to Buy and Sell Stock II

2019-11-14 09:45:50
字體:
來源:轉載
供稿:網友

Say you have an array for which the ith element is the PRice of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

參考解釋

class Solution {public: int maxProfit(vector<int>& prices) { int len = prices.size(); int profit = 0; for(int i = 1; i < len; i++){ profit += max(prices[i]-prices[i-1], 0); } return profit; }};

方法二:略麻煩

class Solution {public: int maxProfit(vector<int>& prices) { int i = 0; int j = 1; int len = prices.size(); int profit = 0; int pre; while(j < len){ pre = prices[j-1]; if(pre < prices[j]){ j++; if(j == len) profit += prices[j-1] - prices[i]; }else{ cout << pre << endl; profit += pre - prices[i]; i = j; j++; } } return profit; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昌邑市| 大石桥市| 游戏| 连山| 内黄县| 正蓝旗| 江都市| 科尔| 黄大仙区| 南宫市| 涞水县| 扶风县| 郧西县| 桦甸市| 平顺县| 苗栗县| 大安市| 贵港市| 盐津县| 稻城县| 华坪县| 彰化县| 迁安市| 满城县| 景德镇市| 宁化县| 蒙阴县| 海盐县| 康平县| 彝良县| 棋牌| 邵阳市| 宁武县| 灵璧县| 苍山县| 建瓯市| 屏东市| 莫力| 宁海县| 莱西市| 刚察县|