leetcode ——Best Time to Buy and Sell Stock II 遍历价格vector,把每段上升期间的利润加起来,注意最后结束时记得再计算最后一次的利润 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 classSolution { // 把每段上升期间的利润加起来 // mi
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple transactions at t...
【leetcode】43-best-time-to-buy-and-sell-stock-iv 力扣 188. 买卖股票的最佳时机 IV 【leetcode】44-best-time-to-buy-and-sell-stock-with-cooldown 力扣 309. 买卖股票的最佳时机包含冷冻期 【leetcode】45-best-time-to-buy-and-sell-stock-with-cooldown 力扣 714. 买卖股票的最佳时机包含手续费 ...
Leetcode: Best Time to Buy and Sell Stock II 题目: 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 ...
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 因为可以进行无限次交易,并且在下一次buy之前必须已经sell。所以只需要把所有price曲线价格上涨的部分加起来就行。 class Solution(object): def maxProfit(self, prices): """
int buyPrice =Integer.MAX_VALUE; for(int i=0;i<prices.length-1;i++) { if(prices[i]<buyPrice) {//今天小于buyPrice buyPrice = prices[i]; }else {//今天大于buyPrice且今天大于明天 if(prices[i]>=prices[i+1]) { gain = gain+prices[i]-buyPrice; ...
classSolution{publicintmaxProfit(int[]prices){intmoney=0;for(inti=0;i<prices.length-1;i++){money=(prices[i+1]>prices[i]?prices[i+1]-prices[i]:0)+money;}returnmoney;}} 0 0 1 2 3 4 5 6 classSolution{ public: intmaxProfit(vector<int>&prices) { ...
Best Time to Buy and Sell Stock IV中是求某个给定k次交易的最大收益,和Maximum Subarray III完全一样 本题由于是可以操作任意次数,只为获得最大收益,而且对于一个上升子序列,比如:[5, 1, 2, 3, 4]中的1, 2, 3, 4序列来说,对于两种操作方案: 1 在1买入,4卖出 2 在1买入,2卖出同时买入,3卖出...
My code: publicclassSolution{publicintmaxProfit(int[]prices){if(prices==null||prices.length<2)return0;boolean isInMode=false;inti=0;intmax=0;inttempMax=0;while(i<prices.length){for(intj=i+1;j<prices.length;j++){if(prices[j]<=prices[i]&&!isInMode){i=j;tempMax+=max;max=0;if(i...
Can you solve this real interview question? Best Time to Buy and Sell Stock - You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choo