Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions Design/2296.Design-a-Text-Editor/2296.Design-a-Text-Editor_v1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ class TextEditor {
list<char>::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)
Expand All @@ -24,7 +21,7 @@ class TextEditor {
t.erase(iter2);
k--;
ret++;
}
}
return ret;
}

Expand All @@ -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; i<p; i++)
{
ret.push_back(*iter);
iter = next(iter);
}
return ret;
return ret;
}

string cursorRight(int k)
{
while (*iter!='#' && k>0)
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; i<p; i++)
{
ret.push_back(*iter);
iter = next(iter);
Expand Down
16 changes: 16 additions & 0 deletions Design/2296.Design-a-Text-Editor/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
### 2296.Design-a-Text-Editor

#### 解法1:链表
本题需要有一种线性的数据结构,能有一个灵活的指针,可以o(1)地删除和添加指向的内部元素,并且依然可以o(1)的时间左移右移。显然这就是链表。

C++里面自带链表结构:
```cpp
list<char>List;
```
该链表的迭代器就是指针
```cpp
list<char>::iterator iter;
```

#### 解法2:两个栈
我们以指针为界,左边的部分放入一个栈,右边的部分放入一个栈。删除就意味着将弹出左边栈的顶部元素即可。打印的话,因为不超过10个字符,所以从栈顶拿出10个字符暂存下来再放回去就可以了。
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class Solution {
public:
int maximumCost(int n, vector<vector<int>>& highways, int k)
{
vector<vector<pair<int,int>>>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<vector<int>>dp(1<<n, vector<int>(n, INT_MIN));
for (int i=0; i<n; i++)
dp[1<<i][i] = 0;

for (int state = 0; state < (1<<n); state++)
{
for (int last=0; last<n; last++)
{
if (((state>>last)&1)==0) continue;
for (auto nxt: next[last])
{
auto [j, t] = nxt;
if ((state>>j)&1) continue;
dp[state+(1<<j)][j] = max(dp[state+(1<<j)][j], dp[state][last]+t);
}

if (__builtin_popcount(state)==k+1)
ret = max(ret, dp[state][last]);
}
}

return ret;

}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
### 2247.Maximum-Cost-of-Trip-With-K-Highways

本题是说要找出一条最长的路径,恰能包含k+1个节点(起始点可以自由选择)。要求节点不能重复经过(自然边也不会重复经过)。

这是一个经典的旅行商问题(TSP)。解法和```943.Find-the-Shortest-Superstring```一样。考虑到节点总数不超过15个,我们可以用二进制数state表示经过的节点的集合。令dp[state][last]表示走过了state所代表的节点集合、并且最后一站是节点last的情况下,所能得到的最优解。假设last与j相邻,且j不在state中,则有状态转移方程
```cpp
dp[state + (1<<j)][j] = dp[state][last] + cost[last][j]
```
最终考察所有含有k+1个节点的state,取其dp值的最大。
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
int maximumProfit(vector<int>& present, vector<int>& future, int budget)
{
int n = present.size();

vector<int>dp(1001);
for (int i=0; i<n; i++)
for (int j=budget; j>=present[i]; j--)
{
dp[j] = max(dp[j], dp[j-present[i]]+future[i]-present[i]);
}

return dp[budget];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### 2291.Maximum-Profit-From-Trading-Stocks

非常直观的01背包问题。挨个遍历物品。考察对于给定某budget情况下,加入这个物品是否能带来更大的收益,即```dp[budget] = max(dp[budget], dp[budget-cost[i]] + profit[i])```
15 changes: 9 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+)
Expand Down Expand Up @@ -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)
Expand All @@ -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+)
Expand Down