LeetCode: Best Time to Buy and Sell Stock II 解题报告

Best Time to Buy and Sell Stock II
Question Solution
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).

解:
既然要最大化利润,我们应该拿下股票所有的升值部分。从开始到最后股票可能有升有降,那么,我们把每一个升值的区间的利润加在一起就是最大值。

也就是每次比较当天和前一天的股票值,如果是上升,就加上这个差值即可。

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         if (prices == null) {
 4             return 0;
 5         }
 6
 7         int maxProfit = 0;
 8
 9         int len = prices.length;
10         for (int i = 1; i < len; i++) {
11             int dif = prices[i] - prices[i - 1];
12
13             if (dif > 0) {
14                 maxProfit += dif;
15             }
16         }
17
18         return maxProfit;
19     }
20 }

GitHub代码链接

时间: 2024-10-22 01:19:23

LeetCode: Best Time to Buy and Sell Stock II 解题报告的相关文章

LeetCode: Best Time to Buy and Sell Stock III 解题报告

Best Time to Buy and Sell Stock IIIQuestion SolutionSay 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 at most two transactions. Note:You may not en

LeetCode: Best Time to Buy and Sell Stock II [122]

[题目] 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

LeetCode Best Time to Buy and Sell Stock II

Best Time to Buy and Sell Stock II Total Accepted: 41127 Total Submissions: 108434 My Submissions Question Solution 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

LeetCode&mdash;&mdash;Best Time to Buy and Sell Stock II (股票买卖时机问题2)

问题: 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)

[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 stock multiple times). Ho

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 stock multiple times). Ho

[leetcode]Best Time to Buy and Sell Stock II @ Python

原题地址:https://oj.leetcode.com/problems/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 transaction

[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 stock multiple times). Ho

LeetCode Best Time to Buy and Sell Stock II (简单题)

题意: 股票买卖第2题.给出每天的股票价格,每次最多买一股,可以多次操作,但是每次在买之前必须保证身上无股票.问最大的利润? 思路: 每天的股票价格可以看成是一条曲线,能卖掉就卖掉,那么肯定是在上升的时候就可以卖掉,但是在不卖的时候要保证自己身上的那只股票的价格是最低价买进的. 1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 int pre=2147483647, ans=0; 6 for(