|
| 1 | +#include <bits/stdc++.h> |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +/* |
| 5 | +Interestingly, i haven't even thought about greedy approach here. Why? Don't |
| 6 | +know. 18 months ago, 1.5 years ago, i solved the problem version I using greedy |
| 7 | +approach. Did I get help from AI? No idea. I think back then I didn't get any AI |
| 8 | +help. |
| 9 | +
|
| 10 | +Today i was thinking about how tf to approach the problem using dynamic |
| 11 | +programming. I asked AI to give me a hint and it gave me the two recurrence |
| 12 | +relations below. That immediately helped me understand how to solve the problem. |
| 13 | +When trying to get dynamic programming problem done, i am looking for recurrence |
| 14 | +relation which is essentially explaining the steps done at each step. At first i |
| 15 | +was bamboozled on what steps to take. I thought ok... at each cell, a stock is |
| 16 | +bought and sold... how do we know? we need a flag... ok... parameter... And then |
| 17 | +if we make a choice... we want to take a max out of the options we select... |
| 18 | +ok... i didn't think about "what choice did i have yesterday" or "what choice |
| 19 | +did i have previously"... if i did, i might have had the solution before i got |
| 20 | +pissed the fuck off and asked AI for hint... ahhhhhh.... so annoyed.... |
| 21 | +never mind. |
| 22 | +
|
| 23 | +After seeing the relation, the whole problem revealed itself to me, making me |
| 24 | +see that i had the problem solved and if i had kept going i would have had the |
| 25 | +solution within the next 30 minutes or 1 hour or so... yeah.... i don't know... |
| 26 | +today was not my day... |
| 27 | +
|
| 28 | +I just saw comment in the file that contains first part of the problem... how i |
| 29 | +came up with the greedy algorithm... i said 18 months ago that i figured out |
| 30 | +it's greedy... lol today didn't seem greedy at all! and after all, i am focusing |
| 31 | +only on dynamic programming problems. I don't care much about other problem |
| 32 | +types... i just want to ramp up on all dynamic programming problems that i |
| 33 | +can... there are 600 dynamic programming problems on leetcode and i am going to |
| 34 | +smack them all. My goal is to solve all dynamic programming problems... on |
| 35 | +leetcode... and then... i don't know what... i don't know what do i do then... i |
| 36 | +will just go and cry in a corner... or something... i got my place to be now... |
| 37 | +these files... this repo... it has gone a long way... a long way... really... |
| 38 | +you know... |
| 39 | +*/ |
| 40 | + |
| 41 | +// Meh, quite garbage |
| 42 | +// Thanks to a hint from gpt, it didn't take me more than 30 minutes to figure |
| 43 | +// out the rest... jesus christ, that sucks, major spoiler, crap damn it ffs |
| 44 | +// |
| 45 | +// The hint: |
| 46 | +// dp[i][true] = max(dp[i-1][true], dp[i-1][false] - p[i]) |
| 47 | +// dp[i][false] = max(dp[i-1][false], dp[i-1][true] + p[i]) |
| 48 | +// |
| 49 | +// From there... The rest was easy as fuck... |
| 50 | +// Another hint... 2 variables... easy as fuck... jesus |
| 51 | +namespace bottomUp { |
| 52 | + |
| 53 | +class Solution { |
| 54 | + public: |
| 55 | + int maxProfit(vector<int>& p) { |
| 56 | + int m = p.size() - 1; |
| 57 | + constexpr int sz = 3e4; |
| 58 | + array<unordered_map<bool, int>, sz> dp; |
| 59 | + dp[0][true] = -p[0]; |
| 60 | + dp[1][false] = 0; |
| 61 | + for (int i = 1; i <= m; ++i) { |
| 62 | + dp[i][true] = max(dp[i - 1][true], dp[i - 1][false] - p[i]); |
| 63 | + dp[i][false] = max(dp[i - 1][false], dp[i - 1][true] + p[i]); |
| 64 | + } |
| 65 | + return max(dp[m][true], dp[m][false]); |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +} // namespace bottomUp |
| 70 | + |
| 71 | +namespace twoVariables { |
| 72 | + |
| 73 | +class Solution { |
| 74 | + public: |
| 75 | + int maxProfit(vector<int>& p) { |
| 76 | + int held_y = -p[0]; |
| 77 | + int held_n = 0; |
| 78 | + for (int i = 1; i < p.size(); ++i) { |
| 79 | + held_y = max(held_y, held_n - p[i]); |
| 80 | + held_n = max(held_n, held_y + p[i]); |
| 81 | + } |
| 82 | + return max(held_y, held_n); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +} // namespace twoVariables |
| 87 | + |
| 88 | +namespace topDown { |
| 89 | + |
| 90 | +class Solution { |
| 91 | + public: |
| 92 | + int maxProfit(vector<int>& p) { |
| 93 | + int m = p.size() - 1; |
| 94 | + vector<unordered_map<bool, int>> dp(m + 1); |
| 95 | + function<int(const int, const bool)> f = [&](const int i, const bool h) { |
| 96 | + if (dp[i].find(h) != dp[i].end()) return dp[i][h]; |
| 97 | + if (i == 0) return dp[i][h] = h ? -p[i] : 0; |
| 98 | + return dp[i][h] = max(f(i - 1, h), f(i - 1, !h) + (h ? -1 : 1) * p[i]); |
| 99 | + }; |
| 100 | + return max(f(m, true), f(m, false)); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace topDown |
0 commit comments