Skip to content

Commit b7d2658

Browse files
authored
feat: add solutions to lc problems: No.1385+ (doocs#2159)
1 parent 201b8e9 commit b7d2658

File tree

27 files changed

+284
-71
lines changed

27 files changed

+284
-71
lines changed

solution/1300-1399/1385.Find the Distance Value Between Two Arrays/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ For arr1[2]=8 we have:
5757

5858
## Solutions
5959

60+
**Solution 1: Sorting + Binary Search**
61+
62+
We can first sort the array $arr2$, then for each element $a$ in array $arr1$, use binary search to find the first element in array $arr2$ that is greater than or equal to $a-d$. If such an element exists and is less than or equal to $a+d$, it means that it does not meet the distance requirement. Otherwise, it meets the distance requirement. We accumulate the number of elements that meet the distance requirement, which is the answer.
63+
64+
The time complexity is $O((m + n) \times \log n)$, and the space complexity is $O(\log n)$. Where $m$ and $n$ are the lengths of arrays $arr1$ and $arr2$, respectively.
65+
6066
<!-- tabs:start -->
6167

6268
### **Python3**

solution/1300-1399/1386.Cinema Seat Allocation/README_EN.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@
5151

5252
## Solutions
5353

54+
**Solution 1: Hash Table + Bit Manipulation**
55+
56+
We use a hash table $d$ to store all the reserved seats, where the key is the row number, and the value is the state of the reserved seats in that row, i.e., a binary number. The $j$-th bit being $1$ means the $j$-th seat is reserved, and $0$ means the $j$-th seat is not reserved.
57+
58+
We traverse $reservedSeats$, for each seat $(i, j)$, we add the state of the $j$-th seat (corresponding to the $10-j$ bit in the lower bits) to $d[i]$.
59+
60+
For rows that do not appear in the hash table $d$, we can arrange $2$ families arbitrarily, so the initial answer is $(n - len(d)) \times 2$.
61+
62+
Next, we traverse the state of each row in the hash table. For each row, we try to arrange the situations $1234, 5678, 3456$ in turn. If a situation can be arranged, we add $1$ to the answer.
63+
64+
After the traversal, we get the final answer.
65+
66+
The time complexity is $O(m)$, and the space complexity is $O(m)$. Where $m$ is the length of $reservedSeats$.
67+
5468
<!-- tabs:start -->
5569

5670
### **Python3**

solution/1300-1399/1387.Sort Integers by The Power Value/README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ The fourth number in the sorted array is 7.
5353

5454
## Solutions
5555

56+
**Solution 1: Custom Sorting**
57+
58+
First, we define a function $f(x)$, which represents the number of steps required to change the number $x$ to $1$, i.e., the weight of the number $x$.
59+
60+
Then, we sort all the numbers in the interval $[lo, hi]$ in ascending order of weight. If the weights are the same, we sort them in ascending order of the numbers themselves.
61+
62+
Finally, we return the $k$-th number after sorting.
63+
64+
The time complexity is $O(n \times \log n \times M)$, and the space complexity is $O(n)$. Where $n$ is the number of numbers in the interval $[lo, hi]$, and $M$ is the maximum value of $f(x)$. In this problem, the maximum value of $M$ is $178$.
65+
5666
<!-- tabs:start -->
5767

5868
### **Python3**

solution/1300-1399/1388.Pizza With 3n Slices/README_EN.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,35 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Dynamic Programming**
47+
48+
We can transform this problem into: In a circular array of length $3n$, select $n$ non-adjacent numbers so that the sum of these $n$ numbers is maximized.
49+
50+
The proof is as follows:
51+
52+
- When $n = 1$, we can choose any number in the array.
53+
- When $n > 1$, there must exist a number such that there are two consecutive numbers on one side of it that have not been selected, and at least one number on the other side has not been selected. Therefore, we can remove this number and the numbers on both sides of it from the array, and then the remaining $3(n - 1)$ numbers form a new circular array. The problem scale is reduced to selecting $n - 1$ non-adjacent numbers in a circular array of length $3(n - 1)$, so that the sum of these $n - 1$ numbers is maximized.
54+
55+
Therefore, the problem we need to solve can be transformed into: In a circular array of length $3n$, select $n$ non-adjacent numbers so that the sum of these $n$ numbers is maximized.
56+
57+
In a circular array, if the first number is selected, the last number cannot be selected. If the last number is selected, the first number cannot be selected. Therefore, we can split the circular array into two arrays, one is without the first number, and the other is without the last number. Then solve the maximum value of these two arrays separately, and finally take the larger of the two maximum values.
58+
59+
We use a function $g(nums)$, which represents the maximum sum of selecting $n$ non-adjacent numbers in the array $nums$. Then our goal is to find the larger value between $g(slices)$ and $g(slices[1:])$.
60+
61+
The solution method of function $g(nums)$ is as follows:
62+
63+
We denote the length of array $nums$ as $m$, and define $f[i][j]$ as the maximum sum of selecting $j$ non-adjacent numbers in the first $i$ numbers of array $nums$.
64+
65+
Consider $f[i][j]$, if we do not select the $i$-th number, then $f[i][j] = f[i - 1][j]$. If we select the $i$-th number, then $f[i][j] = f[i - 2][j - 1] + nums[i - 1]$. Therefore, we can get the state transition equation:
66+
67+
$$
68+
f[i][j] = \max(f[i - 1][j], f[i - 2][j - 1] + nums[i - 1])
69+
$$
70+
71+
Finally, return $f[m][n]$.
72+
73+
The time complexity is $O(n^2)$, and the space complexity is $O(n^2)$. Where $n$ is the length of the array $slices$.
74+
4675
<!-- tabs:start -->
4776

4877
### **Python3**

solution/1300-1399/1389.Create Target Array in the Given Order/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ nums index target
6464

6565
## Solutions
6666

67+
**Solution 1: Simulation**
68+
69+
We create a list $target$ to store the target array. Since the problem guarantees that the insertion position always exists, we can directly insert in the given order into the corresponding position.
70+
71+
The time complexity is $O(n^2)$, and the space complexity is $O(n)$. Where $n$ is the length of the array.
72+
6773
<!-- tabs:start -->
6874

6975
### **Python3**

solution/1300-1399/1390.Four Divisors/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ The answer is the sum of divisors of 21 only.
4343

4444
## Solutions
4545

46+
**Solution 1: Factor Decomposition**
47+
48+
We can perform factor decomposition on each number. If the number of factors is $4$, then this number meets the requirements of the problem, and we can add its factors to the answer.
49+
50+
The time complexity is $O(n \times \sqrt{n})$, where $n$ is the length of the array. The space complexity is $O(1)$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/1300-1399/1392.Longest Happy Prefix/README_EN.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@
3535

3636
## Solutions
3737

38+
**Solution 1: String Hashing**
39+
40+
**String Hashing** is a method to map a string of any length to a non-negative integer, with the probability of collision being almost zero. String hashing is used to calculate the hash value of a string, which allows for quick determination of whether two strings are equal.
41+
42+
We choose a fixed value BASE, and consider the string as a number in BASE radix, assigning a value greater than 0 to represent each character. Generally, the values we assign are much smaller than BASE. For example, for strings composed of lowercase letters, we can assign a=1, b=2, ..., z=26. We choose a fixed value MOD, and calculate the remainder of the BASE radix number divided by MOD, which is used as the hash value of the string.
43+
44+
Generally, we choose BASE=131 or BASE=13331, at which point the probability of hash value collision is extremely low. As long as the hash values of two strings are the same, we consider the two strings to be equal. Usually, MOD is chosen as $2^{64}$. In C++, we can directly use the unsigned long long type to store this hash value. When calculating, we do not handle arithmetic overflow. When overflow occurs, it is equivalent to automatically taking the modulus of $2^{64}$, which can avoid inefficient modulus operations.
45+
46+
Except for extremely specially constructed data, the above hash algorithm is unlikely to produce collisions. In general, the above hash algorithm can appear in the standard answers of the problem. We can also choose some appropriate values of BASE and MOD (such as large prime numbers), and perform several groups of hash operations. Only when the results are all the same do we consider the original strings to be equal, making it even more difficult to construct data that causes this hash to produce errors.
47+
3848
<!-- tabs:start -->
3949

4050
### **Python3**

solution/1300-1399/1393.Capital GainLoss/README_EN.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ Corona Masks stock was bought at day 1 for 10$ and was sold at day 3 for 1010$.
6767

6868
## Solutions
6969

70+
**Solution 1: GROUP BY + SUM(IF())**
71+
72+
We use `GROUP BY` to group the buy and sell operations of the same stock, and then use `SUM(IF())` to calculate the capital gains and losses of each stock.
73+
7074
<!-- tabs:start -->
7175

7276
### **SQL**

solution/1300-1399/1394.Find Lucky Integer in an Array/README_EN.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343

4444
## Solutions
4545

46+
**Solution 1: Counting**
47+
48+
We can use a hash table or array $cnt$ to count the occurrences of each number in $arr$, then traverse $cnt$ to find the largest $x$ that satisfies $cnt[x] = x$. If there is no such $x$, return $-1$.
49+
50+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of $arr$.
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/1300-1399/1395.Count Number of Teams/README_EN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@
5151

5252
## Solutions
5353

54+
**Solution 1: Enumerate Middle Element**
55+
56+
We can enumerate each element $rating[i]$ in the array $rating$ as the middle element, then count the number of elements $l$ that are smaller than it on the left, and the number of elements $r$ that are larger than it on the right. The number of combat units with this element as the middle element is $l \times r + (i - l) \times (n - i - 1 - r)$. We can add this to the answer.
57+
58+
The time complexity is $O(n^2)$, and the space complexity is $O(1)$. Where $n$ is the length of the array $rating$.
59+
60+
**Solution 2: Binary Indexed Tree**
61+
62+
We can use two binary indexed trees to maintain the number of elements $l$ that are smaller than each element on the left in the array $rating$, and the number of elements $r$ that are larger than it on the right. Then count the number of combat units with this element as the middle element as $l \times r + (i - l) \times (n - i - 1 - r)$, and add this to the answer.
63+
64+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $rating$.
65+
5466
<!-- tabs:start -->
5567

5668
### **Python3**

0 commit comments

Comments
 (0)