Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock II

方法:记录便利过程中的best profit

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        if(prices.size() < 2)
            return 0;

        int profit = 0, minValue = prices[0];

        for(int i=0; i<prices.size(); ++i)
        {
            profit = max(profit, prices[i] - minValue);

            if(prices[i] < minValue)
                minValue = prices[i];
        }

        return profit;
    }
};

Best Time to Buy and Sell Stock II

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int sum = 0;

        for(int i=1; i<prices.size(); ++i)
        {
            if(prices[i] - prices[i-1] > 0)
                sum += prices[i] - prices[i-1];
        }

        return sum;
    }
};
时间: 2024-10-14 00:54:26

Best Time to Buy and Sell Stock & Best Time to Buy and Sell Stock II的相关文章

Buy Nevada Fake ID the ideas and Buy Nevada Fakes

A brief summary is a snippet made programmatically from a site. What on earth is several by using a displayed Buy Missouri Fake ID snippet is definitely that it is elevated so that you can lure customer particular attention on the success website. Wh

27. Best Time to Buy and Sell Stock &amp;&amp; Best Time to Buy and Sell Stock II &amp;&amp; Best Time to Buy and Sell Stock III

Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/) Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most on

309. Best Time to Buy and Sell Stock with Cooldown

/* * 309. Best Time to Buy and Sell Stock with Cooldown * 2016-7-4 by Mingyang * http://buttercola.blogspot.com/2016/01/leetcode-best-time-to-buy-and-sell.html * *1. Define States * *To represent the decision at index i: *buy[i]: Max profit till inde

Java for LeetCode 188 Best Time to Buy and Sell Stock IV【HARD】

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 at most k transactions. 解题思路: 本题是Best Time to Buy and Sell Stock系列最难的一道,需要用到dp,JAVA实现如下: public i

Best Time to Buy and Sell Stock with Cooldown -- LeetCode

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) wit

Java for LeetCode 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, buy one and sell one share of the stock multiple times). Ho

[leetcode] 714. Best Time to Buy and Sell Stock with Transaction Fee

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee. You may complete as many transactions as you like, but you need to pay the t

LeetCode Best Time to Buy and Sell Stock with Transaction Fee

原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/description/ 题目: Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee 

解题报告Best Time to Buy and Sell Stock with Cooldown

题目 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)