category-wise-problems

contains category wise problems(data structures, competitive) of popular platforms.

View the Project on GitHub mayankdutta/category-wise-problems

Best time to sell the stocks I

Best time to sell the stocks II

Greedy - if we encountered a lower price. - why not have it and sell on the high price day. - since no transaction limit, we can implement this. ```cpp class Solution { public: int maxProfit(vector& prices) { int ans = 0; for (int i = 0; i < prices.size() - 1; i++) { if (prices[i + 1] > prices[i]) { ans += (prices[i + 1] - prices[i]); } } return ans; } }; ``` </details>
DP (memo) ```cpp class Solution { public: vector prices; vector<vector> memo; int find(int i, bool buy) { if (i >= prices.size()) return 0; int &ans = memo[i][buy]; if (ans != -1) return ans; ans = find(i + 1, buy); ans = buy ? max(-prices[i] + find(i + 1, !buy), ans) : max(prices[i] + find(i + 1, !buy), ans); return ans; } int maxProfit(vector &prices) { int n = prices.size(); if (n < 2) return 0; this->prices = prices; memo = vector<vector>(n + 1, vector(2, -1)); return find(0, 1); } }; ``` </details>
DP (iterative) ```cpp ```
### Best time to sell the stocks III