Skip to content

Commit 7a554d7

Browse files
committed
feat: add solutions to lc problems: No.1796,2489
* No.1796.Second Largest Digit in a String * No.2489.Number of Substrings With Fixed Ratio
1 parent 9bceaec commit 7a554d7

File tree

26 files changed

+749
-109
lines changed

26 files changed

+749
-109
lines changed

solution/0800-0899/0895.Maximum Frequency Stack/README_EN.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ class FreqStack {
120120
public FreqStack() {
121121

122122
}
123-
123+
124124
public void push(int val) {
125125
cnt.put(val, cnt.getOrDefault(val, 0) + 1);
126126
q.offer(new int[] {cnt.get(val), ++ts, val});
127127
}
128-
128+
129129
public int pop() {
130130
int val = q.poll()[2];
131131
cnt.put(val, cnt.get(val) - 1);
@@ -150,14 +150,14 @@ class FreqStack {
150150
public FreqStack() {
151151

152152
}
153-
153+
154154
public void push(int val) {
155155
cnt.put(val, cnt.getOrDefault(val, 0) + 1);
156156
int t = cnt.get(val);
157157
d.computeIfAbsent(t, k -> new ArrayDeque<>()).push(val);
158158
mx = Math.max(mx, t);
159159
}
160-
160+
161161
public int pop() {
162162
int val = d.get(mx).pop();
163163
cnt.put(val, cnt.get(val) - 1);
@@ -184,12 +184,12 @@ public:
184184
FreqStack() {
185185

186186
}
187-
187+
188188
void push(int val) {
189189
++cnt[val];
190190
q.emplace(cnt[val], ++ts, val);
191191
}
192-
192+
193193
int pop() {
194194
auto [a, b, val] = q.top();
195195
q.pop();
@@ -217,13 +217,13 @@ public:
217217
FreqStack() {
218218
219219
}
220-
220+
221221
void push(int val) {
222222
++cnt[val];
223223
d[cnt[val]].push(val);
224224
mx = max(mx, cnt[val]);
225225
}
226-
226+
227227
int pop() {
228228
int val = d[mx].top();
229229
--cnt[val];

solution/0800-0899/0898.Bitwise ORs of Subarrays/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
## Description
66

7-
<p>We have an array <code>arr</code> of non-negative integers.</p>
7+
<p>Given an integer array <code>arr</code>, return <em>the number of distinct bitwise ORs of all the non-empty subarrays of</em> <code>arr</code>.</p>
88

9-
<p>For every (contiguous) subarray <code>sub = [arr[i], arr[i + 1], ..., arr[j]]</code> (with <code>i &lt;= j</code>), we take the bitwise OR of all the elements in <code>sub</code>, obtaining a result <code>arr[i] | arr[i + 1] | ... | arr[j]</code>.</p>
9+
<p>The bitwise OR of a subarray is the bitwise OR of each integer in the subarray. The bitwise OR of a subarray of one integer is that integer.</p>
1010

11-
<p>Return the number of possible results. Results that occur more than once are only counted once in the final answer</p>
11+
<p>A <strong>subarray</strong> is a contiguous non-empty sequence of elements within an array.</p>
1212

1313
<p>&nbsp;</p>
1414
<p><strong class="example">Example 1:</strong></p>
@@ -41,8 +41,8 @@ There are 3 unique values, so the answer is 3.
4141
<p><strong>Constraints:</strong></p>
4242

4343
<ul>
44-
<li><code>1 &lt;= nums.length &lt;= 5 * 10<sup>4</sup></code></li>
45-
<li><code>0 &lt;= nums[i]&nbsp;&lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= arr.length &lt;= 5 * 10<sup>4</sup></code></li>
45+
<li><code>0 &lt;= arr[i] &lt;= 10<sup>9</sup></code></li>
4646
</ul>
4747

4848
## Solutions

solution/0900-0999/0976.Largest Perimeter Triangle/README_EN.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@
1212
<pre>
1313
<strong>Input:</strong> nums = [2,1,2]
1414
<strong>Output:</strong> 5
15+
<strong>Explanation:</strong> You can form a triangle with three side lengths: 1, 2, and 2.
1516
</pre>
1617

1718
<p><strong class="example">Example 2:</strong></p>
1819

1920
<pre>
20-
<strong>Input:</strong> nums = [1,2,1]
21+
<strong>Input:</strong> nums = [1,2,1,10]
2122
<strong>Output:</strong> 0
23+
<strong>Explanation:</strong>
24+
You cannot use the side lengths 1, 1, and 2 to form a triangle.
25+
You cannot use the side lengths 1, 1, and 10 to form a triangle.
26+
You cannot use the side lengths 1, 2, and 10 to form a triangle.
27+
As we cannot use any three side lengths to form a triangle of non-zero area, we return 0.
2228
</pre>
2329

2430
<p>&nbsp;</p>

solution/1000-1099/1027.Longest Arithmetic Subsequence/README_EN.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,38 @@
44

55
## Description
66

7-
<p>Given an array <code>nums</code> of integers, return the <strong>length</strong> of the longest arithmetic subsequence in <code>nums</code>.</p>
7+
<p>Given an array <code>nums</code> of integers, return <em>the length of the longest arithmetic subsequence in</em> <code>nums</code>.</p>
88

9-
<p>Recall that a <em>subsequence</em> of an array <code>nums</code> is a list <code>nums[i<sub>1</sub>], nums[i<sub>2</sub>], ..., nums[i<sub>k</sub>]</code> with <code>0 &lt;= i<sub>1</sub> &lt; i<sub>2</sub> &lt; ... &lt; i<sub>k</sub> &lt;= nums.length - 1</code>, and that a sequence <code>seq</code> is <em>arithmetic</em> if <code>seq[i+1] - seq[i]</code> are all the same value (for <code>0 &lt;= i &lt; seq.length - 1</code>).</p>
9+
<p><strong>Note</strong> that:</p>
10+
11+
<ul>
12+
<li>A <strong>subsequence</strong> is an array that can be derived from another array by deleting some or no elements without changing the order of the remaining elements.</li>
13+
<li>A sequence <code>seq</code> is arithmetic if <code>seq[i + 1] - seq[i]</code> are all the same value (for <code>0 &lt;= i &lt; seq.length - 1</code>).</li>
14+
</ul>
1015

1116
<p>&nbsp;</p>
1217
<p><strong class="example">Example 1:</strong></p>
1318

1419
<pre>
1520
<strong>Input:</strong> nums = [3,6,9,12]
1621
<strong>Output:</strong> 4
17-
<strong>Explanation: </strong>
18-
The whole array is an arithmetic sequence with steps of length = 3.
22+
<strong>Explanation: </strong> The whole array is an arithmetic sequence with steps of length = 3.
1923
</pre>
2024

2125
<p><strong class="example">Example 2:</strong></p>
2226

2327
<pre>
2428
<strong>Input:</strong> nums = [9,4,7,2,10]
2529
<strong>Output:</strong> 3
26-
<strong>Explanation: </strong>
27-
The longest arithmetic subsequence is [4,7,10].
30+
<strong>Explanation: </strong> The longest arithmetic subsequence is [4,7,10].
2831
</pre>
2932

3033
<p><strong class="example">Example 3:</strong></p>
3134

3235
<pre>
3336
<strong>Input:</strong> nums = [20,1,15,3,10,5,8]
3437
<strong>Output:</strong> 4
35-
<strong>Explanation: </strong>
36-
The longest arithmetic subsequence is [20,15,10,5].
38+
<strong>Explanation: </strong> The longest arithmetic subsequence is [20,15,10,5].
3739
</pre>
3840

3941
<p>&nbsp;</p>

solution/1200-1299/1207.Unique Number of Occurrences/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Description
66

7-
<p>Given an array of integers <code>arr</code>, return <code>true</code> if the number of occurrences of each value in the array is <strong>unique</strong>, or <code>false</code> otherwise.</p>
7+
<p>Given an array of integers <code>arr</code>, return <code>true</code> <em>if the number of occurrences of each value in the array is <strong>unique</strong> or </em><code>false</code><em> otherwise</em>.</p>
88

99
<p>&nbsp;</p>
1010
<p><strong class="example">Example 1:</strong></p>
@@ -32,7 +32,7 @@
3232
<p><strong>Constraints:</strong></p>
3333

3434
<ul>
35-
<li><code>1 &lt;= arr.length&nbsp;&lt;= 1000</code></li>
35+
<li><code>1 &lt;= arr.length &lt;= 1000</code></li>
3636
<li><code>-1000 &lt;= arr[i] &lt;= 1000</code></li>
3737
</ul>
3838

solution/1700-1799/1729.Find Followers Count/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This table contains the IDs of a user and a follower in a social media app where
2020

2121
<p>Write an SQL query that will, for each user, return the number of followers.</p>
2222

23-
<p>Return the result table ordered by <code>user_id</code>.</p>
23+
<p>Return the result table ordered by <code>user_id</code> in ascending order.</p>
2424

2525
<p>The query result format is in the following example.</p>
2626

solution/1700-1799/1769.Minimum Number of Operations to Move All Balls to Each Box/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@
4747

4848
**方法一:预处理 + 枚举**
4949

50-
我们可以预处理出把每个位置 $i$ 左边的小球移动到 $i$ 的操作数,记为 $left[i]$;把每个位置 $i$ 右边的小球移动到 $i$ 的操作数,记为 $right[i]$。那么答案数组的第 $i$ 个元素就是 $left[i] + right[i]$。
50+
我们可以预处理出每个位置 $i$ 左边的小球移动到 $i$ 的操作数,记为 $left[i]$;每个位置 $i$ 右边的小球移动到 $i$ 的操作数,记为 $right[i]$。那么答案数组的第 $i$ 个元素就是 $left[i] + right[i]$。
5151

5252
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 `boxes` 的长度。
5353

5454
我们还可以进一步优化空间复杂度,只用一个答案数组 $ans$ 以及若干个变量即可。
5555

56-
时间复杂度 $O(n)$,忽略答案数组的空间复杂度,空间复杂度 $O(1)$。其中 $n$ 为 `boxes` 的长度。
56+
时间复杂度 $O(n)$,忽略答案数组的空间消耗,空间复杂度 $O(1)$。其中 $n$ 为 `boxes` 的长度。
5757

5858
<!-- tabs:start -->
5959

solution/1700-1799/1796.Second Largest Digit in a String/README.md

+138-22
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,25 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44-
假设字符串最大的数为 `largestDigit`,第二大的数为 `secondLargestDigit`,初始化均为 -1。
44+
**方法一:一次遍历**
4545

46-
遍历字符串,判断当前字符是否为数字型字符。若是,先转为数字 `num`。然后判断数字与 `largestDigit``secondLargestDigit` 的大小关系:
46+
我们定义 $a$ 和 $b$ 分别表示字符串中出现的最大数字和第二大数字,初始时 $a = b = -1$。
4747

48-
-`num > largestDigit`,将 `secondLargestDigit` 更新为 `largestDigit`,而 `largestDigit` 更新为 num;
49-
-`num > secondLargestDigit`,并且 `num < largestDigit`,将 `secondLargestDigit` 更新为 num;
50-
- 其他情况不做处理。
48+
遍历字符串 $s$,如果当前字符是数字,我们将其转换为数字 $v$,如果 $v \gt a$,说明 $v$ 是当前出现的最大数字,我们将 $b$ 更新为 $a$,并将 $a$ 更新为 $v$;如果 $v \lt a$,说明 $v$ 是当前出现的第二大数字,我们将 $b$ 更新为 $v$。
5149

52-
最后返回 `secondLargestDigit` 即可。
50+
遍历结束,返回 $b$ 即可。
51+
52+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
53+
54+
**方法二:位运算**
55+
56+
我们可以用一个整数 $mask$ 来标识字符串中出现的数字,其中 $mask$ 的第 $i$ 位表示数字 $i$ 是否出现过。
57+
58+
遍历字符串 $s$,如果当前字符是数字,我们将其转换为数字 $v$,将 $mask$ 的第 $v$ 个二进制位的值置为 $1$。
59+
60+
最后,我们从高位向低位遍历 $mask$,找到第二个为 $1$ 的二进制位,其对应的数字即为第二大数字。如果不存在第二大数字,返回 $-1$。
61+
62+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 的长度。
5363

5464
<!-- tabs:start -->
5565

@@ -60,15 +70,28 @@
6070
```python
6171
class Solution:
6272
def secondHighest(self, s: str) -> int:
63-
largest_digit = second_largest_digit = -1
73+
a = b = -1
6474
for c in s:
6575
if c.isdigit():
66-
num = int(c)
67-
if num > largest_digit:
68-
second_largest_digit, largest_digit = largest_digit, num
69-
elif num > second_largest_digit and num < largest_digit:
70-
second_largest_digit = num
71-
return second_largest_digit
76+
v = int(c)
77+
if v > a:
78+
a, b = v, a
79+
elif b < v < a:
80+
b = v
81+
return b
82+
```
83+
84+
```python
85+
class Solution:
86+
def secondHighest(self, s: str) -> int:
87+
mask = reduce(or_, (1 << int(c) for c in s if c.isdigit()), 0)
88+
cnt = 0
89+
for i in range(9, -1, -1):
90+
if (mask >> i) & 1:
91+
cnt += 1
92+
if cnt == 2:
93+
return i
94+
return -1
7295
```
7396

7497
### **Java**
@@ -78,21 +101,114 @@ class Solution:
78101
```java
79102
class Solution {
80103
public int secondHighest(String s) {
81-
int largestDigit = -1, secondLargestDigit = -1;
104+
int a = -1, b = -1;
105+
for (int i = 0; i < s.length(); ++i) {
106+
char c = s.charAt(i);
107+
if (Character.isDigit(c)) {
108+
int v = c - '0';
109+
if (v > a) {
110+
b = a;
111+
a = v;
112+
} else if (v > b && v < a) {
113+
b = v;
114+
}
115+
}
116+
}
117+
return b;
118+
}
119+
}
120+
```
121+
122+
```java
123+
class Solution {
124+
public int secondHighest(String s) {
125+
int mask = 0;
82126
for (int i = 0; i < s.length(); ++i) {
83127
char c = s.charAt(i);
84-
if (c >= '0' && c <= '9') {
85-
int num = c - '0';
86-
if (num > largestDigit) {
87-
secondLargestDigit = largestDigit;
88-
largestDigit = num;
89-
} else if (num > secondLargestDigit && num < largestDigit) {
90-
secondLargestDigit = num;
128+
if (Character.isDigit(c)) {
129+
mask |= 1 << (c - '0');
130+
}
131+
}
132+
for (int i = 9, cnt = 0; i >= 0; --i) {
133+
if (((mask >> i) & 1) == 1 && ++cnt == 2) {
134+
return i;
135+
}
136+
}
137+
return -1;
138+
}
139+
}
140+
```
141+
142+
### **C++**
143+
144+
```cpp
145+
class Solution {
146+
public:
147+
int secondHighest(string s) {
148+
int a = -1, b = -1;
149+
for (char& c : s) {
150+
if (isdigit(c)) {
151+
int v = c - '0';
152+
if (v > a) {
153+
b = a, a = v;
154+
} else if (v > b && v < a) {
155+
b = v;
91156
}
92157
}
93158
}
94-
return secondLargestDigit;
159+
return b;
160+
}
161+
};
162+
```
163+
164+
```cpp
165+
class Solution {
166+
public:
167+
int secondHighest(string s) {
168+
int mask = 0;
169+
for (char& c : s) if (isdigit(c)) mask |= 1 << c - '0';
170+
for (int i = 9, cnt = 0; ~i; --i) if (mask >> i & 1 && ++cnt == 2) return i;
171+
return -1;
95172
}
173+
};
174+
```
175+
176+
### **Go**
177+
178+
```go
179+
func secondHighest(s string) int {
180+
a, b := -1, -1
181+
for _, c := range s {
182+
if c >= '0' && c <= '9' {
183+
v := int(c - '0')
184+
if v > a {
185+
b, a = a, v
186+
} else if v > b && v < a {
187+
b = v
188+
}
189+
}
190+
}
191+
return b
192+
}
193+
```
194+
195+
```go
196+
func secondHighest(s string) int {
197+
mask := 0
198+
for _, c := range s {
199+
if c >= '0' && c <= '9' {
200+
mask |= 1 << int(c-'0')
201+
}
202+
}
203+
for i, cnt := 9, 0; i >= 0; i-- {
204+
if mask>>i&1 == 1 {
205+
cnt++
206+
if cnt == 2 {
207+
return i
208+
}
209+
}
210+
}
211+
return -1
96212
}
97213
```
98214

0 commit comments

Comments
 (0)