This is the 26th day of my participation in Gwen Challenge
Liver for many days – dynamic planning ten even – super delicate analysis | brush title punch card
What problem can I choose to do dynamic programming?
Counting 1.
- How many ways can I get to the bottom right corner
- How many ways can I pick the sum of k numbers yes is sum
2. Find the maximum and minimum
- The maximum numeric sum of the path from the upper left corner to the lower right corner
- Maximum ascending subsequence length
3. Seek existence
- Stone game, whether the first hand will win
- Can we pick k numbers such that the sum is sum
Leecode 121. The best time to buy and sell stocks
Given an array prices, its ith element prices[I] represents the price of a given stock on day I.
You can only buy the stock on one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make.
Return the maximum profit you can make from the transaction. If you can’t make any profit, return 0.
Example 1:
Input:,1,5,3,6,4 [7]
Output: 5
Explanation: Buy on day 2 (stock price = 1), sell on day 5 (stock price = 6), maximum profit = 6-1 = 5. Note that the profit cannot be 7-1 = 6, because the selling price needs to be higher than the buying price; Also, you can’t sell stocks before you buy them.
—
Four steps for dynamic planning ~~~ ❤️❤️❤️❤️
This problem, too easy
2.1. Dynamic planning component 1: State determination
To put it simply, when solving dynamic programming, you need to open an array. What does each element of the array f[I] or f[I][j] represent, similar to what x, y, and z represent in mathematical problems
The last step
We define a minimum buying point, minPoint
So the yield on day I is going to be prices[I] -minpoint
Isn’t the last step the payoff of the last element in the array, probably not the biggest payoff.
subproblems
The last step of the previous step for maximum profit is saved by Maxprofit.
Nice \color{yellow}{very nice ~} very nice ❤️❤️❤️
2.2. Dynamic programming component 2: Transfer equation
maxprofit = min{prices[i] – minPoint}
2.3. Dynamic programming component 3: Initial conditions and boundary cases
2.4. Dynamic programming component 4: Order of calculation
The top-down
Reference code
The language version
func maxProfit(prices []int) int {
minValue := math.MaxInt64
maxValue := 0
for i := 0; i < len(prices); i++ {
if prices[i] < minValue {
minValue = prices[i]
} else if prices[i]-minValue > maxValue {
maxValue = prices[i] - minValue
}
}
return maxValue
}
Copy the code
Java version
public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if(prices[i] - minprice > maxprofit) { maxprofit = prices[i] - minprice; }}returnmaxprofit; }}Copy the code
❤ ️ ❤ ️ ❤ ️ ❤ ️
Thank you very much talent can see here, if this article is written well, feel that there is something to ask for praise 👍 for attention ❤️ for share 👥 for handsome Oba I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much!
At the end of this article, I recently compiled an interview material “Java Interview Process Manual”, covering Java core technology, JVM, Java concurrency, SSM, microservices, databases, data structures and so on. How to obtain: GitHub github.com/Tingyu-Note… , more attention to the public number: Ting rain notes, one after another.