You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1400-1499/1472.Design Browser History/README_EN.md
+11-1
Original file line number
Diff line number
Diff line change
@@ -75,7 +75,17 @@ browserHistory.back(7); // You are in "google.com",
75
75
76
76
<!-- solution:start -->
77
77
78
-
### Solution 1
78
+
### Solution 1: Two Stacks
79
+
80
+
We can use two stacks, $\textit{stk1}$ and $\textit{stk2}$, to store the back and forward pages, respectively. Initially, $\textit{stk1}$ contains the $\textit{homepage}$, and $\textit{stk2}$ is empty.
81
+
82
+
When calling $\text{visit}(url)$, we add $\textit{url}$ to $\textit{stk1}$ and clear $\textit{stk2}$. The time complexity is $O(1)$.
83
+
84
+
When calling $\text{back}(steps)$, we pop the top element from $\textit{stk1}$ and push it to $\textit{stk2}$. We repeat this operation $steps$ times until the length of $\textit{stk1}$ is $1$ or $steps$ is $0$. Finally, we return the top element of $\textit{stk1}$. The time complexity is $O(\textit{steps})$.
85
+
86
+
When calling $\text{forward}(steps)$, we pop the top element from $\textit{stk2}$ and push it to $\textit{stk1}$. We repeat this operation $steps$ times until $\textit{stk2}$ is empty or $steps$ is $0$. Finally, we return the top element of $\textit{stk1}$. The time complexity is $O(\textit{steps})$.
87
+
88
+
The space complexity is $O(n)$, where $n$ is the length of the browsing history.
Copy file name to clipboardexpand all lines: solution/1400-1499/1473.Paint House III/README_EN.md
+22-2
Original file line number
Diff line number
Diff line change
@@ -53,7 +53,7 @@ Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.
53
53
<strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
54
54
<strong>Output:</strong> 11
55
55
<strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2]
56
-
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
56
+
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
57
57
Cost of paint the first and last house (10 + 1) = 11.
58
58
</pre>
59
59
@@ -84,7 +84,27 @@ Cost of paint the first and last house (10 + 1) = 11.
84
84
85
85
<!-- solution:start -->
86
86
87
-
### Solution 1
87
+
### Solution 1: Dynamic Programming
88
+
89
+
We define $f[i][j][k]$ to represent the minimum cost to paint houses from index $0$ to $i$, with the last house painted in color $j$, and exactly forming $k$ blocks. The answer is $f[m-1][j][\textit{target}]$, where $j$ ranges from $1$ to $n$. Initially, we check if the house at index $0$ is already painted. If it is not painted, then $f[0][j][1] = \textit{cost}[0][j - 1]$, where $j \in [1,..n]$. If it is already painted, then $f[0][\textit{houses}[0]][1] = 0$. All other values of $f[i][j][k]$ are initialized to $\infty$.
90
+
91
+
Next, we start iterating from index $i=1$. For each $i$, we check if the house at index $i$ is already painted:
92
+
93
+
If it is not painted, we can paint the house at index $i$ with color $j$. We enumerate the number of blocks $k$, where $k \in [1,..\min(\textit{target}, i + 1)]$, and enumerate the color of the previous house $j_0$, where $j_0 \in [1,..n]$. Then we can derive the state transition equation:
If it is already painted, we can paint the house at index $i$ with color $j$. We enumerate the number of blocks $k$, where $k \in [1,..\min(\textit{target}, i + 1)]$, and enumerate the color of the previous house $j_0$, where $j_0 \in [1,..n]$. Then we can derive the state transition equation:
Finally, we return $f[m - 1][j][\textit{target}]$, where $j \in [1,..n]$. If all values of $f[m - 1][j][\textit{target}]$ are $\infty$, then return $-1$.
106
+
107
+
The time complexity is $O(m \times n^2 \times \textit{target})$, and the space complexity is $O(m \times n \times \textit{target})$. Here, $m$, $n$, and $\textit{target}$ represent the number of houses, the number of colors, and the number of blocks, respectively.
Copy file name to clipboardexpand all lines: solution/1400-1499/1474.Delete N Nodes After M Nodes of a Linked List/README_EN.md
+40-1
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,11 @@ Head of the linked list after removing nodes is returned.
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: Simulation
71
+
72
+
We can simulate the entire deletion process. First, use a pointer $\textit{pre}$ to point to the head of the linked list, then traverse the linked list, moving $m - 1$ steps. If $\textit{pre}$ is null, it means the number of nodes from the current node is less than $m$, so we directly return the head. Otherwise, use a pointer $\textit{cur}$ to point to $\textit{pre}$, then move $n$ steps. If $\textit{cur}$ is null, it means the number of nodes from $\textit{pre}$ is less than $m + n$, so we directly set the $\textit{next}$ of $\textit{pre}$ to null. Otherwise, set the $\textit{next}$ of $\textit{pre}$ to the $\textit{next}$ of $\textit{cur}$, then move $\textit{pre}$ to its $\textit{next}$. Continue traversing the linked list until $\textit{pre}$ is null, then return the head.
73
+
74
+
The time complexity is $O(n)$, where $n$ is the number of nodes in the linked list. The space complexity is $O(1)$.
71
75
72
76
<!-- tabs:start -->
73
77
@@ -201,6 +205,41 @@ func deleteNodes(head *ListNode, m int, n int) *ListNode {
0 commit comments