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

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

LEETCODE--Best Time to Buy and Sell Stock II

2019-11-14 09:56:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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; }};
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 香港 | 临朐县| 巴林左旗| 湖北省| 台北县| 晋江市| 凉城县| 清水河县| 南充市| 吴川市| 阜南县| 万山特区| 常德市| 会理县| 天长市| 钟祥市| 麟游县| 洞头县| 大关县| 炎陵县| 麻江县| 河北省| 兴和县| 呼图壁县| 柏乡县| 民乐县| 丹巴县| 崇仁县| 定襄县| 宣威市| 仙居县| 得荣县| 临沭县| 南和县| 呼玛县| 元阳县| 西华县| 新田县| 肥西县| 新平| 澳门|