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

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

[LeetCode] Best Time to Buy and Sell Stock II

2019-11-15 01:09:39
字體:
來源:轉載
供稿:網友
[LeetCode] Best Time to Buy and Sell Stock II

Say you have an array for which theithelement is the PRice of a given stock on dayi.

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).

這道題要注意幾點。首先profit算的是累積的,不是單個最大的。

然后就是題目上說了的,買股票之前必須要把股票賣完。所以說就不存在,只賣出一部分的情況。

這樣來說的話,其實算法比之前算單個max值的還要簡單一點。

代碼如下。~

public class Solution {    public int maxProfit(int[] prices) {       int profit=0;       for(int i=1;i<prices.length;i++){           profit=profit+Math.max(0,prices[i]-prices[i-1]);       }       return profit;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰原市| 兰西县| 永济市| 历史| 沐川县| 梁河县| 固镇县| 天祝| 德惠市| 广州市| 屯昌县| 玉林市| 洱源县| 百色市| 长子县| 平昌县| 泗阳县| 迁西县| 天等县| 新巴尔虎右旗| 淳安县| 湘潭市| 林芝县| 阿克| 镇宁| 神木县| 吉隆县| 惠水县| 洱源县| 中江县| 谢通门县| 碌曲县| 丹江口市| 东台市| 卢龙县| 开鲁县| 关岭| 北碚区| 芜湖市| 和龙市| 宁南县|