Personally, this is a relatively difficult DP problem. This link posts a typical DP solution to it. Need some time to get how it works.The code is rewritten as follows.1 classSolution {2 Public:3 intMaxprofit (vectorint>prices) {4 intn = prices.size (), num =2;5 if(N 1)return 0;6vectorint> > dp (num +1, vectorint> (n,0));7 for(intK =1; K ) {8 inttemp = dp[k-1][0]-prices[0];9 for(inti =1; I ) {TenDp[k][i] = max (Dp[k][i-1], Prices[i] +temp);
Algorithm:Scan through the array, keep finding1. Prices[i] with the condition:prices[i] 2. Prices[i] with the condition:prices[i] > prices[i+1], or at the end of a ascending trend, treat it as selling PO IntKeeping till reach the end of the array; Note:1. If a buying point is recorded and does reach the end, then must exist a selling point after it (must exist some point L Arger than buying point)2. If reach the end when finding the buying point, then stop and finish the whole programe.best tim
It is easy to know that you are seeking a continuous maximum number of sub-arrays. But note the boundary conditions, assuming the sum of the maximum subarray of 0, then do not trade, return 0.public class Solution {public int maxprofit (int[] prices) { if (Prices.length Leetcode-best time to Buy and Sell Stock
Traverse the price vector, add up the profit for each period of the ascent, and remember to calculate the last profit at the end. Class Solution { //Add up profit for each period of ascent //Minval: Trough value //maxval: Peak //Profit: Profit public: int Maxprofit ( Vector
profit + = Maxval-minval; Minval = Maxval = Prices[i]; } If up, change the peak else{ maxval = prices[i]; } } Finall
* * * * File:stock_price.cpp * Author:hongbin * gives a stock price sequence to find the best buy and sell point, that is, after the sequence of elements with the maximum value of the preceding elements. * #include
Given a vector, the vector represents the price of a stock in the first day, Y to make the stock yield the largest should be bought on the day I, the first i+n days to sell, to find out the value of the maximum profit is how muchIdea: Record the minimum value with a variable, a variable record the maximum benefit, scan the price from beginning to end, when scanning to the price of the day I, if the price is
/* * 121. best time to Buy and Sell Stock * This topic is simple, but I still do it wrong. This problem is more than you think. * First the order of Min and Max does not matter. In addition, you can start looping from 0 without waiting for 1 to start */ public Span style= "color: #0000ff;" >int maxprofit (int [] prices) { int min = integer.max_value, MAX = 0; for (int i = 0; i = Math.min (min, pr
Consider that the price of a stock is constantly changing, and selling can only be done after buying. and can only buy and sell once.I want to sweep it at the beginning and ask for the smallest, but certainly not so simple. you're 484 stupid .Because it can only be sold once, if I sell, the possible profit value is, Prices[i]-min, is today's price.Comparing this value to the previous possible maximum, you c
An extension of the best time to Buy and Sell Stock III. The idea was still to use dynamic programming (see here for detailed introduction). However, in this problem, some trick (the quickprofit function below) are required to pass the TLE.The code is rewritten below.1 classSolution {2 Public:3 intMaxprofit (intK, vectorint>prices) {4 intn =prices.size ();5 if(k >= N/2)returnQuickprofit
Original title address: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/The content is in the code and comments, it is not wordy.Class Solution {Public:int Maxprofit (vectorbest time to Buy and Sell Stock--leetcode
Say you have an array for whichITh element is the price of a given stock on dayI.
If you were only permitted to complete at most one transaction (ie, buy one and every one share of the stock), design an algorithm to find the maximum profit.
At the beginning, I thought it was the maximum value minus the minimum value. However, because the purchase occurred before
Title Link: click~/* Test instructions: An array, the element I represents the price of the stock of day I, allow up to two times to buy and sell, for maximum profit *//** * idea: Record the maximum profit of the day with the Currprofit array, scan the array from start to finish to get the * Currprofit = max (Currpor Fit[i], prices[i]-low) * * Record the maximum profit after the day with a futureprofit arra
The topics are as follows:
best time to buy and Sell the total accepted:43912 total submissions:135635 My submissions question Solution
Say you have an array for which the ith element are the price of a given the
If you are were only permitted to complete in most one transaction (ie, buy one and sell one share of the "stock", design an AL Gorithm to find the max
Problem description:
Say you have an array for whichITh element is the price of a given stock on dayI.
If you were only permitted to complete at most one transaction (ie, buy one and every one share of the stock), design an algorithm to find the maximum profit.
Solution:
1 Public int maxprofit (INT [] prices) {2 if (prices. length
: The profit earned at the current price (if you don't make money, you don't sell).①, if Curmax = 0, means that the current value is smaller than the value of the front;②, Curmax > 0, i.e., Curmax = (price[i-1]-price[i-2] + price[i]-price[i-1]) = Price[i]-price[i-1]. That is, the current price is the maximum value, and the lowest value is subtracted from the front.Max: The maximum profit in the total process. Public int maxProfit2 (int[] prices) { int curmax = 0, max = 0;
Class Solution {public: int Maxprofit (vectorint Maxprofit (vectorBest solution, feel good slag.As long as there is an increase can be added to the sum inside ... There is no need to calculate the rising range.Still have to analyze the problem carefully.[Leetcode] best time to Buy and Sell Stock II
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.