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/1600-1699/1603.Design Parking System/README_EN.md
+19-15
Original file line number
Diff line number
Diff line change
@@ -62,7 +62,15 @@ parkingSystem.addCar(1); // return false because there is no available slot for
62
62
63
63
<!-- solution:start -->
64
64
65
-
### Solution 1
65
+
### Solution 1: Simulation
66
+
67
+
We use an array $\textit{cnt}$ of length 4 to represent the number of parking spaces for each type of car, where $\textit{cnt}[1]$, $\textit{cnt}[2]$, and $\textit{cnt}[3]$ represent the number of large, medium, and small parking spaces, respectively.
68
+
69
+
During initialization, we set $\textit{cnt}[1]$, $\textit{cnt}[2]$, and $\textit{cnt}[3]$ to the number of large, medium, and small parking spaces, respectively.
70
+
71
+
Each time a car parks, we check if there is a corresponding parking space in the parking lot. If not, we return $\textit{false}$; otherwise, we decrement the number of corresponding parking spaces by one and return $\textit{true}$.
72
+
73
+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
We can use two pointers, where one pointer $i$ starts from the beginning of string $a$, and the other pointer $j$ starts from the end of string $b$. If the characters pointed to by the two pointers are equal, then both pointers move towards the center until they encounter different characters or the two pointers cross.
78
+
79
+
If the two pointers cross, i.e., $i \geq j$, it means that $prefix$ and $suffix$ can already form a palindrome, and we return `true`. Otherwise, we need to check if $a[i,...j]$ or $b[i,...j]$ is a palindrome. If so, return `true`.
80
+
81
+
Otherwise, we try swapping the two strings $a$ and $b$ and repeat the same process.
82
+
83
+
The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of string $a$ or $b$.
Copy file name to clipboardexpand all lines: solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/README_EN.md
+5-1
Original file line number
Diff line number
Diff line change
@@ -66,7 +66,11 @@ tags:
66
66
67
67
<!-- solution:start -->
68
68
69
-
### Solution 1
69
+
### Solution 1: Hash Table + DFS
70
+
71
+
We use a hash table $\textit{s}$ to record the values of all nodes in the array $\textit{nodes}$, and then use depth-first search. When the node being traversed is null or its value is in the hash table $\textit{s}$, we return the current node. Otherwise, we recursively traverse the left and right subtrees. If the return values of both the left and right subtrees are not null, it means the current node is the lowest common ancestor. Otherwise, we return the non-null subtree's return value.
72
+
73
+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the number of nodes in the binary tree and the length of the array $\textit{nodes}$, respectively.
0 commit comments