Skip to content

Commit 366b0e0

Browse files
authoredMay 2, 2024
feat: update solutions to lcci problems: No.17.05~17.08 (doocs#2709)
1 parent 8e2a2e6 commit 366b0e0

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed
 

‎lcci/17.05.Find Longest Subarray/README_EN.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,20 @@
4040

4141
## Solutions
4242

43-
### Solution 1
43+
### Solution 1: Prefix Sum + Hash Table
44+
45+
The problem requires finding the longest subarray with an equal number of characters and digits. We can treat characters as $1$ and digits as $-1$, transforming the problem into finding the longest subarray with a sum of $0$.
46+
47+
We can use the idea of prefix sums and a hash table `vis` to record the first occurrence of each prefix sum. We use variables `mx` and `k` to record the length and the left endpoint of the longest subarray that meets the conditions, respectively.
48+
49+
Next, we iterate through the array, calculating the prefix sum `s` at the current position `i`:
50+
51+
- If the prefix sum `s` at the current position exists in the hash table `vis`, we denote the first occurrence of `s` as `j`, then the sum of the subarray in the interval $[j + 1,..,i]$ is $0$. If the length of the current subarray is greater than the length of the longest subarray found so far, i.e., $mx < i - j$, we update `mx = i - j` and `k = j + 1`.
52+
- Otherwise, we store the current prefix sum `s` as the key and the current position `i` as the value in the hash table `vis`.
53+
54+
After the iteration, we return the subarray with a left endpoint of `k` and a length of `mx`.
55+
56+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
4457

4558
<!-- tabs:start -->
4659

‎lcci/17.06.Number Of 2s In Range/README_EN.md

+32-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,38 @@
2121

2222
## Solutions
2323

24-
### Solution 1
24+
### Solution 1: Digit DP
25+
26+
This problem is essentially about finding the number of occurrences of the digit $2$ in the given interval $[l,..r]$. The count is related to the number of digits and the digit at each position. We can use the idea of Digit DP to solve this problem. In Digit DP, the size of the number has little impact on the complexity.
27+
28+
For the interval $[l,..r]$, we usually transform it into $[1,..r]$ and then subtract $[1,..l - 1]$, i.e.,
29+
30+
$$
31+
ans = \sum_{i=1}^{r} ans_i - \sum_{i=1}^{l-1} ans_i
32+
$$
33+
34+
However, for this problem, we only need to find the value in the interval $[1,..r]$.
35+
36+
Here, we use memoization to implement Digit DP. We start from the top and search down to the bottom to get the number of schemes, then return the answer layer by layer and accumulate it, finally getting the final answer from the starting point of the search.
37+
38+
The basic steps are as follows:
39+
40+
1. Convert the number $n$ into an int array $a$, where $a[1]$ is the least significant digit, and $a[len]$ is the most significant digit.
41+
2. Design the function $dfs()$ based on the problem information. For this problem, we define $dfs(pos, cnt, limit)$, and the answer is $dfs(len, 0, true)$.
42+
43+
Where:
44+
45+
- `pos` represents the number of digits, starting from the least significant digit or the first digit, usually depending on the digit construction property of the problem. For this problem, we choose to start from the most significant digit, so the initial value of `pos` is `len`.
46+
- `cnt` represents the number of $2$s in the current number.
47+
- `limit` represents the restriction on the digits that can be filled. If there is no restriction, you can choose $[0,1,..9]$, otherwise, you can only choose $[0,..a[pos]]$. If `limit` is `true` and the maximum value has been reached, then the next `limit` is also `true`. If `limit` is `true` but the maximum value has not been reached, or if `limit` is `false`, then the next `limit` is `false`.
48+
49+
For details on the implementation of the function, please refer to the code below.
50+
51+
The time complexity is $O(\log n)$.
52+
53+
Similar problems:
54+
55+
- [233. Number of Digit One](https://github.com/doocs/leetcode/blob/main/solution/0200-0299/0233.Number%20of%20Digit%20One/README_EN.md)
2556

2657
<!-- tabs:start -->
2758

‎lcci/17.07.Baby Names/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,15 @@
2222

2323
## Solutions
2424

25-
### Solution 1
25+
### Solution 1: Hash Table + DFS
26+
27+
For each pair of synonyms, we establish bidirectional edges between the two names and store them in the adjacency list $g$. Then, we traverse all names, store them in the set $s$, and store their frequencies in the hash table $cnt$.
28+
29+
Next, we traverse each name in the set $s$. If the name has not been visited, we perform a depth-first search to find all names in the connected component where the name is located. We use the name with the smallest lexicographic order as the real name, and the sum of their frequencies is the frequency of the real name. Then, we store this name and its frequency in the answer array.
30+
31+
After traversing all names, the answer array is what we seek.
32+
33+
The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Where $n$ and $m$ are the lengths of the name array and the synonym array, respectively.
2634

2735
<!-- tabs:start -->
2836

‎lcci/17.08.Circus Tower/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
### 方法一:排序 + 离散化 + 树状数组
2222

23-
我们现将所有人按照身高从小到大排序,若身高相同,则按照体重从大到小排序。这样我们可以将问题转换为求体重数组的最长递增子序列的问题。
23+
我们先将所有人按照身高从小到大排序,若身高相同,则按照体重从大到小排序。这样我们可以将问题转换为求体重数组的最长递增子序列的问题。
2424

2525
最长递增子序列的问题可以使用动态规划求解,时间复杂度 $O(n^2)$。但是我们可以使用树状数组来优化求解过程,时间复杂度 $O(n \log n)$。
2626

‎lcci/17.08.Circus Tower/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@
2121

2222
## Solutions
2323

24-
### Solution 1
24+
### Solution 1: Sorting + Discretization + Binary Indexed Tree
25+
26+
First, we sort all people in ascending order by height. If the heights are the same, we sort them in descending order by weight. This way, we can transform the problem into finding the longest increasing subsequence of the weight array.
27+
28+
The longest increasing subsequence problem can be solved using dynamic programming with a time complexity of $O(n^2)$. However, we can optimize the solution process using a Binary Indexed Tree, which reduces the time complexity to $O(n \log n)$.
29+
30+
The space complexity is $O(n)$, where $n$ is the number of people.
2531

2632
<!-- tabs:start -->
2733

0 commit comments

Comments
 (0)