国产探花免费观看_亚洲丰满少妇自慰呻吟_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; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武乡县| 金昌市| 灌阳县| 抚顺县| 维西| 阳谷县| 克山县| 娄烦县| 宁海县| 绥宁县| 惠东县| 武隆县| 团风县| 新化县| 南雄市| 井研县| 三明市| 金溪县| 包头市| 夹江县| 敦煌市| 崇仁县| 弥勒县| 武川县| 奈曼旗| 南宫市| 内乡县| 莱州市| 龙川县| 周宁县| 临朐县| 长阳| 凤山市| 云安县| 阿鲁科尔沁旗| 嵩明县| 广宁县| 毕节市| 灵台县| 山西省| 阿巴嘎旗|