LeetCode – Refresh – Best Time to Buy and Sell Stock ii

This is simple, use greedy algorithm will solve the problem.

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         int len = prices.size(), result = 0;
 5         if (len < 2) return 0;
 6         for (int i = 0; i < len-1; i++) {
 7             if (prices[i+1] - prices[i] > 0) {
 8                 result += prices[i+1] - prices[i];
 9             }
10         }
11         return result;
12     }
13 };
时间: 2024-08-05 06:49:38

LeetCode – Refresh – Best Time to Buy and Sell Stock ii的相关文章

【LeetCode】Best Time to Buy and Sell Stock II

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 sha

Leetcode problem-122 Best Time to Buy and Sell Stock II 题解

Leetcode Problem-122  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, bu

[LeetCode][JavaScript]Best Time to Buy and Sell Stock II

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 sha

[C++]LeetCode: 77 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)

leetcode 【 Best Time to Buy and Sell Stock II 】python 实现

题目: 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 – Refresh – Best Time to Buy and Sell Stock iv

This is combination of II and III. 1. when k >= length, it is totally II. 2. when k < length, it is another III. A localMaximum and globalMaximum subarray need to be maintained. But we can reduce the two dimensional DP to one dimensional DP localMax

LeetCode – Refresh – Best Time to Buy and Sell Stock iii

III is a kind of I. But it require 2 maximum value. So scan from begin and scan from end to record the maximum value for currrent index. Then scan the maximum array to obtain the global maximum. 1 class Solution { 2 public: 3 int maxProfit(vector<int

LeetCode – Refresh – Best Time to Buy and Sell Stock

1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 if (prices.size() < 2) return 0; 5 int len = prices.size(), result = 0, rec = prices[0]; 6 for (int i = 1; i < len; i++) { 7 result = max(result, prices[i] - rec); 8 rec

Leetcode 122. Best Time to Buy and Sell Stock II JAVA语言

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