diff --git a/1048-longest-string-chain/1048-longest-string-chain.cpp b/1048-longest-string-chain/1048-longest-string-chain.cpp new file mode 100644 index 00000000..238b6231 --- /dev/null +++ b/1048-longest-string-chain/1048-longest-string-chain.cpp @@ -0,0 +1,22 @@ +bool cmp(string &a, string &b){ + return a.length() < b.length(); +} +class Solution { +public: + int longestStrChain(vector& words) { + sort(words.begin(), words.end(), cmp); + map mp; + int ans = 0; + for(auto word: words){ + int cur = 0; + for(int i=0;i& prices) { + + int n = prices.size(); + vector profit_after_buying(k + 1, -1e9), profit_after_selling(k + 1, 0); + + for (int i = 0; i < prices.size(); ++i) { + int cur_price = prices[i]; + for (int j = k; j >= 1; --j) { + profit_after_buying[j] = max(profit_after_buying[j], profit_after_selling[j-1] - cur_price); + profit_after_selling[j] = max(profit_after_selling[j], profit_after_buying[j] + cur_price); + } + } + return profit_after_selling[k]; + } + +}; \ No newline at end of file