[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). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

Hide Tags

Array Greedy


#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int maxProfit(vector<int> &prices) {
        int n=prices.size();
        if(n<2) return 0;
        int sum=0;
        for(int i =1;i<n;i++){
            if(prices[i]>prices[i-1]){
            sum += prices[i] - prices[i-1];
            }
        }
        return sum;
    }
};

int main()
{
    vector<int > prices{1,3,2,4,7};
    Solution sol;
    cout<<sol.maxProfit(prices)<<endl;
    return 0;
}
时间: 2024-12-25 00:21:54

[LeetCode] Best Time to Buy and Sell Stock II 贪心算法的相关文章

[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 [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: Best Time to Buy and Sell Stock II 解题报告

Best Time to Buy and Sell Stock IIQuestion 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 as many transactions as you like (ie, buy one

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 (简单题)

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