Open sync-by-unito[bot] opened 4 years ago
➤ Nelson 3513 commented:
Q283, MoveZeros, Classic question to present thinking process:
//283. Move Zeroes [Easy] Classic question to present the thinking process.
class Solution_q283 {
public:
void moveZeroes(std::vector
//step2. move all non-zero val to left.
fast = slow + 1;
while (fast < n) {
if (nums[fast] != 0) {
nums[slow++] = nums[fast];
}
++fast;
}
//step3. fill zero to the tail.
while (slow < n) nums[slow++] = 0;
}
};
➤ Nelson 3513 commented:
//121. Best Time to Buy and Sell Stock [Easy] //why this one may be a dynamic programming: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/39112/Why-is-this-problem-tagged-with-%22Dynamic-programming%22
class Solution_q121 {
public:
int maxProfit(std::vector
for(int p: prices){
maxProfit = std::max(maxProfit, p-buy);
buy = std::min(buy, p);
}
return maxProfit;
/*int maxProfit = 0, buy = INT32_MAX, n = prices.size();
for(int i=0; i<n; ++i){
//Traverse the prices, preserves the min price we've seen.
if(buy >= prices[i]){ //if smaller price is found, record it.
buy = prices[i];
}else{
//calculate profit.
maxProfit = std::max(maxProfit, prices[i] - buy);
}
}
return maxProfit;*/
}
};
➤ Nelson 3513 commented:
//122. Best Time to Buy and Sell Stock II d - a = (b - a) + (c - b) + (d - c) 1, buy and then sell on the same day means doing nothing on the that day, before the day start, you have nothing, when the day ends, you have nothing 2, same goes for "sell and then buy on the same day", before the day start, you have an amount of money, when the day ends, you still have the same amount of money 3, so you can actually add the diff up
class Solution_q122 {
public:
int maxProfit(std::vector
➤ Nelson 3513 commented:
//123 or 188. Best Time to Buy and Sell Stock III/IV [Hard]
int maxProfit(int k_trans, std::vector
class Solution_q123 {
public:
int maxProfit(std::vector
for(int p: prices){
//buy2 try to use current price - profit1, for example if we earn $100 so far
//today it's price is $300, we only need to "pay" additional $200 to buy it.
maxProfit2 = std::max(maxProfit2, p - buy2);
buy2 = std::min(buy2, p - maxProfit1);
//buy1 always records the minimun buy point.
//maxProfit1 is BY FAR the maximun profit.
maxProfit1 = std::max(maxProfit1, p - buy1);
buy1 = std::min(buy1, p);
}
return maxProfit2;
}
};
➤ Nelson 3513 commented:
Classic: This follows the same recurrence relations: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/most-consistent-ways-of-dealing-with-the-series-of-stock-problems
//121. Best Time to Buy and Sell Stock [Easy]
class Solution_q121 {
public:
int maxProfit(std::vector
//122. Best Time to Buy and Sell Stock II [Easy]
class Solution_q122 {
public:
int maxProfit(std::vector
for (const int& p : prices) {
T_i0 = std::max(T_i0, T_i1 + p);
T_i1 = std::max(T_i1, T_i0 - p);
}
return T_i0;
}
};
//123. Best Time to Buy and Sell Stock III [Hard], DP
class Solution_q123 {
public:
int maxProfit(std::vector
//188. Best Time to Buy and Sell Stock IV [Hard]
class Solution_q188 {
public:
int maxProfitForLargeK(std::vector
//714. Best Time to Buy and Sell Stock with Transaction Fee [Med]
class Solution_q714 {
public:
int maxProfit(std::vector
//309. Best Time to Buy and Sell Stock with Cooldown [Med]
class Solution_q309 {
public:
int maxProfit(std::vector
DP for Stock Questions, Good explanations:
DP Patterns: https://leetcode.com/discuss/general-discussion/458695/Dynamic-Programming-Patterns https://leetcode.com/discuss/interview-question/778035/dynamic-programming-patterns
┆Issue is synchronized with this Trello card by Unito