国产探花免费观看_亚洲丰满少妇自慰呻吟_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;    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 凤山市| 岳阳县| 湟源县| 永定县| 临桂县| 龙岩市| 抚州市| 阿拉善盟| 谢通门县| 准格尔旗| 太仆寺旗| 汾阳市| 赞皇县| 鸡东县| 肥城市| 博罗县| 若尔盖县| 中宁县| 通化县| 同江市| 麻江县| 平乐县| 鄂尔多斯市| 镇巴县| 建瓯市| 囊谦县| 闽侯县| 保山市| 乡宁县| 乌拉特中旗| 莱西市| 县级市| 凌海市| 阿荣旗| 浠水县| 九江县| 莫力| 红原县| 华安县| 翼城县| 麻城市|