diff --git a/Design/2296.Design-a-Text-Editor/2296.Design-a-Text-Editor_v1.cpp b/Design/2296.Design-a-Text-Editor/2296.Design-a-Text-Editor_v1.cpp index cf6ae12be..84e0a47db 100644 --- a/Design/2296.Design-a-Text-Editor/2296.Design-a-Text-Editor_v1.cpp +++ b/Design/2296.Design-a-Text-Editor/2296.Design-a-Text-Editor_v1.cpp @@ -3,16 +3,13 @@ class TextEditor { list::iterator iter; public: TextEditor() { - t.push_back('#'); iter = t.begin(); } void addText(string text) { for (auto ch: text) - { t.insert(iter, ch); - } } int deleteText(int k) @@ -24,7 +21,7 @@ class TextEditor { t.erase(iter2); k--; ret++; - } + } return ret; } @@ -35,36 +32,36 @@ class TextEditor { iter = prev(iter); k--; } - int p = 10; - while (iter!=t.begin() && p>0) + int p = 0; + while (iter!=t.begin() && p<10) { iter = prev(iter); - p--; + p++; } string ret; - for (int i=0; i<10-p; i++) + for (int i=0; i0) + while (iter!=t.end() && k>0) { iter = next(iter); k--; } - int p = 10; - while (iter!=t.begin() && p>0) + int p = 0; + while (iter!=t.begin() && p<10) { iter = prev(iter); - p--; + p++; } string ret; - for (int i=0; i<10-p; i++) + for (int i=0; iList; +``` +该链表的迭代器就是指针 +```cpp +list::iterator iter; +``` + +#### 解法2:两个栈 +我们以指针为界,左边的部分放入一个栈,右边的部分放入一个栈。删除就意味着将弹出左边栈的顶部元素即可。打印的话,因为不超过10个字符,所以从栈顶拿出10个字符暂存下来再放回去就可以了。 diff --git a/Dynamic_Programming/2247.Maximum-Cost-of-Trip-With-K-Highways/2247.Maximum-Cost-of-Trip-With-K-Highways.cpp b/Dynamic_Programming/2247.Maximum-Cost-of-Trip-With-K-Highways/2247.Maximum-Cost-of-Trip-With-K-Highways.cpp new file mode 100644 index 000000000..64a81808c --- /dev/null +++ b/Dynamic_Programming/2247.Maximum-Cost-of-Trip-With-K-Highways/2247.Maximum-Cost-of-Trip-With-K-Highways.cpp @@ -0,0 +1,38 @@ +class Solution { +public: + int maximumCost(int n, vector>& highways, int k) + { + vector>>next(n); + for (auto highway: highways) + { + int a = highway[0], b = highway[1], t = highway[2]; + next[a].push_back({b,t}); + next[b].push_back({a,t}); + } + + int ret = -1; + vector>dp(1<(n, INT_MIN)); + for (int i=0; i>last)&1)==0) continue; + for (auto nxt: next[last]) + { + auto [j, t] = nxt; + if ((state>>j)&1) continue; + dp[state+(1<& present, vector& future, int budget) + { + int n = present.size(); + + vectordp(1001); + for (int i=0; i=present[i]; j--) + { + dp[j] = max(dp[j], dp[j-present[i]]+future[i]-present[i]); + } + + return dp[budget]; + } +}; diff --git a/Dynamic_Programming/2291.Maximum-Profit-From-Trading-Stocks/Readme.md b/Dynamic_Programming/2291.Maximum-Profit-From-Trading-Stocks/Readme.md new file mode 100644 index 000000000..e5a744797 --- /dev/null +++ b/Dynamic_Programming/2291.Maximum-Profit-From-Trading-Stocks/Readme.md @@ -0,0 +1,3 @@ +### 2291.Maximum-Profit-From-Trading-Stocks + +非常直观的01背包问题。挨个遍历物品。考察对于给定某budget情况下,加入这个物品是否能带来更大的收益,即```dp[budget] = max(dp[budget], dp[budget-cost[i]] + profit[i])``` diff --git a/Readme.md b/Readme.md index 8af49f275..cf54fcceb 100644 --- a/Readme.md +++ b/Readme.md @@ -680,6 +680,7 @@ [1049.Last-Stone-Weight-II](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1049.Last-Stone-Weight-II) (H-) [1449.Form-Largest-Integer-With-Digits-That-Add-up-to-Target](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1449.Form-Largest-Integer-With-Digits-That-Add-up-to-Target) (H-) [1981.Minimize-the-Difference-Between-Target-and-Chosen-Elements](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1981.Minimize-the-Difference-Between-Target-and-Chosen-Elements) (M+) +[2291.Maximum-Profit-From-Trading-Stocks](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2291.Maximum-Profit-From-Trading-Stocks) (M) * ``键盘型`` [650.2-Keys-Keyboard](https://github.com/wisdompeak/LeetCode/blob/master/Dynamic_Programming/650.2-Keys-Keyboard) (M+) [651.4-Keys-Keyboard](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/651.4-Keys-Keyboard) (M+) @@ -736,7 +737,6 @@ * ``状态压缩DP`` [465.Optimal-Account-Balancing](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/465.Optimal-Account-Balancing) (H) [691.Stickers-to-Spell-Word](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/691.Stickers-to-Spell-Word) (H) -[943.Find-the-Shortest-Superstring](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/943.Find-the-Shortest-Superstring) (H+) [1125.Smallest-Sufficient-Team](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1125.Smallest-Sufficient-Team) (H) [1349.Maximum-Students-Taking-Exam](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1349.Maximum-Students-Taking-Exam) (H) [1411.Number-of-Ways-to-Paint-N×3-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1411.Number-of-Ways-to-Paint-N%C3%973-Grid) (M) @@ -754,11 +754,14 @@ [1986.Minimum-Number-of-Work-Sessions-to-Finish-the-Tasks](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1986.Minimum-Number-of-Work-Sessions-to-Finish-the-Tasks) (M+) [2152.Minimum-Number-of-Lines-to-Cover-Points](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2152.Minimum-Number-of-Lines-to-Cover-Points) (H-) * ``带权二分图`` -[1066.Campus-Bikes-II](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1066.Campus-Bikes-II) (H+) -[1595.Minimum-Cost-to-Connect-Two-Groups-of-Points](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1595.Minimum-Cost-to-Connect-Two-Groups-of-Points) (H) -[1879.Minimum-XOR-Sum-of-Two-Arrays](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1879.Minimum-XOR-Sum-of-Two-Arrays) (H) -[1947.Maximum-Compatibility-Score-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1947.Maximum-Compatibility-Score-Sum) (H) -[2172.Maximum-AND-Sum-of-Array](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2172.Maximum-AND-Sum-of-Array) (H) + [1066.Campus-Bikes-II](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1066.Campus-Bikes-II) (H+) + [1595.Minimum-Cost-to-Connect-Two-Groups-of-Points](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1595.Minimum-Cost-to-Connect-Two-Groups-of-Points) (H) + [1879.Minimum-XOR-Sum-of-Two-Arrays](https://github.com/wisdompeak/LeetCode/tree/master/BFS/1879.Minimum-XOR-Sum-of-Two-Arrays) (H) + [1947.Maximum-Compatibility-Score-Sum](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1947.Maximum-Compatibility-Score-Sum) (H) + [2172.Maximum-AND-Sum-of-Array](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2172.Maximum-AND-Sum-of-Array) (H) + * ``TSP`` + [943.Find-the-Shortest-Superstring](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/943.Find-the-Shortest-Superstring) (H+) + [2247.Maximum-Cost-of-Trip-With-K-Highways](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/2247.Maximum-Cost-of-Trip-With-K-Highways) (H) * ``Catalan`` [096.Unique-Binary-Search-Trees](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/096.Unique-Binary-Search-Trees) (M+) [1259.Handshakes-That-Don't-Cross](https://github.com/wisdompeak/LeetCode/tree/master/Dynamic_Programming/1259.Handshakes-That-Don't-Cross) (M+)