Skip to content

Commit 34bc75a

Browse files
committed
feat: add solutions to lc problems: No.3718,3719
1 parent ebeca22 commit 34bc75a

File tree

14 files changed

+471
-16
lines changed

14 files changed

+471
-16
lines changed

solution/3700-3799/3718.Smallest Missing Multiple of K/README.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,93 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Sm
6060

6161
<!-- solution:start -->
6262

63-
### 方法一
63+
### 方法一:哈希表 + 枚举
64+
65+
我们先用一个哈希表 $\textit{s}$ 存储数组 $\textit{nums}$ 中出现的数字。然后从 $k$ 的第一个正倍数 $k \times 1$ 开始,依次枚举每个正倍数,直到找到第一个不在哈希表 $\textit{s}$ 中出现的倍数,即为答案。
66+
67+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
6468

6569
<!-- tabs:start -->
6670

6771
#### Python3
6872

6973
```python
70-
74+
class Solution:
75+
def missingMultiple(self, nums: List[int], k: int) -> int:
76+
s = set(nums)
77+
for i in count(1):
78+
x = k * i
79+
if x not in s:
80+
return x
7181
```
7282

7383
#### Java
7484

7585
```java
76-
86+
class Solution {
87+
public int missingMultiple(int[] nums, int k) {
88+
boolean[] s = new boolean[101];
89+
for (int x : nums) {
90+
s[x] = true;
91+
}
92+
for (int i = 1;; ++i) {
93+
int x = k * i;
94+
if (x >= s.length || !s[x]) {
95+
return x;
96+
}
97+
}
98+
}
99+
}
77100
```
78101

79102
#### C++
80103

81104
```cpp
82-
105+
class Solution {
106+
public:
107+
int missingMultiple(vector<int>& nums, int k) {
108+
unordered_set<int> s;
109+
for (int x : nums) {
110+
s.insert(x);
111+
}
112+
for (int i = 1;; ++i) {
113+
int x = k * i;
114+
if (!s.contains(x)) {
115+
return x;
116+
}
117+
}
118+
}
119+
};
83120
```
84121
85122
#### Go
86123
87124
```go
125+
func missingMultiple(nums []int, k int) int {
126+
s := map[int]bool{}
127+
for _, x := range nums {
128+
s[x] = true
129+
}
130+
for i := 1; ; i++ {
131+
if x := k * i; !s[x] {
132+
return x
133+
}
134+
}
135+
}
136+
```
88137

138+
#### TypeScript
139+
140+
```ts
141+
function missingMultiple(nums: number[], k: number): number {
142+
const s = new Set<number>(nums);
143+
for (let i = 1; ; ++i) {
144+
const x = k * i;
145+
if (!s.has(x)) {
146+
return x;
147+
}
148+
}
149+
}
89150
```
90151

91152
<!-- tabs:end -->

solution/3700-3799/3718.Smallest Missing Multiple of K/README_EN.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,93 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3718.Sm
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Hash Table + Enumeration
62+
63+
We first use a hash table $\textit{s}$ to store the numbers that appear in the array $\textit{nums}$. Then, starting from the first positive multiple of $k$, which is $k \times 1$, we enumerate each positive multiple in sequence until we find the first multiple that does not appear in the hash table $\textit{s}$, which is the answer.
64+
65+
The time complexity is $O(n)$ and the space complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$.
6266

6367
<!-- tabs:start -->
6468

6569
#### Python3
6670

6771
```python
68-
72+
class Solution:
73+
def missingMultiple(self, nums: List[int], k: int) -> int:
74+
s = set(nums)
75+
for i in count(1):
76+
x = k * i
77+
if x not in s:
78+
return x
6979
```
7080

7181
#### Java
7282

7383
```java
74-
84+
class Solution {
85+
public int missingMultiple(int[] nums, int k) {
86+
boolean[] s = new boolean[101];
87+
for (int x : nums) {
88+
s[x] = true;
89+
}
90+
for (int i = 1;; ++i) {
91+
int x = k * i;
92+
if (x >= s.length || !s[x]) {
93+
return x;
94+
}
95+
}
96+
}
97+
}
7598
```
7699

77100
#### C++
78101

79102
```cpp
80-
103+
class Solution {
104+
public:
105+
int missingMultiple(vector<int>& nums, int k) {
106+
unordered_set<int> s;
107+
for (int x : nums) {
108+
s.insert(x);
109+
}
110+
for (int i = 1;; ++i) {
111+
int x = k * i;
112+
if (!s.contains(x)) {
113+
return x;
114+
}
115+
}
116+
}
117+
};
81118
```
82119
83120
#### Go
84121
85122
```go
123+
func missingMultiple(nums []int, k int) int {
124+
s := map[int]bool{}
125+
for _, x := range nums {
126+
s[x] = true
127+
}
128+
for i := 1; ; i++ {
129+
if x := k * i; !s[x] {
130+
return x
131+
}
132+
}
133+
}
134+
```
86135

136+
#### TypeScript
137+
138+
```ts
139+
function missingMultiple(nums: number[], k: number): number {
140+
const s = new Set<number>(nums);
141+
for (let i = 1; ; ++i) {
142+
const x = k * i;
143+
if (!s.has(x)) {
144+
return x;
145+
}
146+
}
147+
}
87148
```
88149

89150
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int missingMultiple(vector<int>& nums, int k) {
4+
unordered_set<int> s;
5+
for (int x : nums) {
6+
s.insert(x);
7+
}
8+
for (int i = 1;; ++i) {
9+
int x = k * i;
10+
if (!s.contains(x)) {
11+
return x;
12+
}
13+
}
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func missingMultiple(nums []int, k int) int {
2+
s := map[int]bool{}
3+
for _, x := range nums {
4+
s[x] = true
5+
}
6+
for i := 1; ; i++ {
7+
if x := k * i; !s[x] {
8+
return x
9+
}
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int missingMultiple(int[] nums, int k) {
3+
boolean[] s = new boolean[101];
4+
for (int x : nums) {
5+
s[x] = true;
6+
}
7+
for (int i = 1;; ++i) {
8+
int x = k * i;
9+
if (x >= s.length || !s[x]) {
10+
return x;
11+
}
12+
}
13+
}
14+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution:
2+
def missingMultiple(self, nums: List[int], k: int) -> int:
3+
s = set(nums)
4+
for i in count(1):
5+
x = k * i
6+
if x not in s:
7+
return x
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function missingMultiple(nums: number[], k: number): number {
2+
const s = new Set<number>(nums);
3+
for (let i = 1; ; ++i) {
4+
const x = k * i;
5+
if (!s.has(x)) {
6+
return x;
7+
}
8+
}
9+
}

solution/3700-3799/3719.Longest Balanced Subarray I/README.md

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,126 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3700-3799/3719.Lo
8585

8686
<!-- solution:start -->
8787

88-
### 方法一
88+
### 方法一:哈希表 + 枚举
89+
90+
我们可以枚举子数组的左端点 $i$,然后从左端点开始向右枚举右端点 $j$,在枚举的过程中使用一个哈希表 $\textit{vis}$ 来记录子数组中出现过的数字,同时使用一个长度为 $2$ 的数组 $\textit{cnt}$ 来分别记录子数组中不同偶数和不同奇数的数量。当 $\textit{cnt}[0] = \textit{cnt}[1]$ 时,更新答案 $\textit{ans} = \max(\textit{ans}, j - i + 1)$。
91+
92+
时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $\textit{nums}$ 的长度。
8993

9094
<!-- tabs:start -->
9195

9296
#### Python3
9397

9498
```python
95-
99+
class Solution:
100+
def longestBalanced(self, nums: List[int]) -> int:
101+
n = len(nums)
102+
ans = 0
103+
for i in range(n):
104+
cnt = [0, 0]
105+
vis = set()
106+
for j in range(i, n):
107+
if nums[j] not in vis:
108+
cnt[nums[j] & 1] += 1
109+
vis.add(nums[j])
110+
if cnt[0] == cnt[1]:
111+
ans = max(ans, j - i + 1)
112+
return ans
96113
```
97114

98115
#### Java
99116

100117
```java
101-
118+
class Solution {
119+
public int longestBalanced(int[] nums) {
120+
int n = nums.length;
121+
int ans = 0;
122+
for (int i = 0; i < n; ++i) {
123+
Set<Integer> vis = new HashSet<>();
124+
int[] cnt = new int[2];
125+
for (int j = i; j < n; ++j) {
126+
if (vis.add(nums[j])) {
127+
++cnt[nums[j] & 1];
128+
}
129+
if (cnt[0] == cnt[1]) {
130+
ans = Math.max(ans, j - i + 1);
131+
}
132+
}
133+
}
134+
return ans;
135+
}
136+
}
102137
```
103138

104139
#### C++
105140

106141
```cpp
107-
142+
class Solution {
143+
public:
144+
int longestBalanced(vector<int>& nums) {
145+
int n = nums.size();
146+
int ans = 0;
147+
for (int i = 0; i < n; ++i) {
148+
unordered_set<int> vis;
149+
int cnt[2]{};
150+
for (int j = i; j < n; ++j) {
151+
if (!vis.contains(nums[j])) {
152+
vis.insert(nums[j]);
153+
++cnt[nums[j] & 1];
154+
}
155+
if (cnt[0] == cnt[1]) {
156+
ans = max(ans, j - i + 1);
157+
}
158+
}
159+
}
160+
return ans;
161+
}
162+
};
108163
```
109164
110165
#### Go
111166
112167
```go
168+
func longestBalanced(nums []int) (ans int) {
169+
n := len(nums)
170+
for i := 0; i < n; i++ {
171+
vis := map[int]bool{}
172+
cnt := [2]int{}
173+
for j := i; j < n; j++ {
174+
if !vis[nums[j]] {
175+
vis[nums[j]] = true
176+
cnt[nums[j]&1]++
177+
}
178+
if cnt[0] == cnt[1] {
179+
ans = max(ans, j-i+1)
180+
}
181+
}
182+
}
183+
return
184+
}
185+
```
113186

187+
#### TypeScript
188+
189+
```ts
190+
function longestBalanced(nums: number[]): number {
191+
const n = nums.length;
192+
let ans = 0;
193+
for (let i = 0; i < n; ++i) {
194+
const vis = new Set<number>();
195+
const cnt: number[] = Array(2).fill(0);
196+
for (let j = i; j < n; ++j) {
197+
if (!vis.has(nums[j])) {
198+
vis.add(nums[j]);
199+
++cnt[nums[j] & 1];
200+
}
201+
if (cnt[0] === cnt[1]) {
202+
ans = Math.max(ans, j - i + 1);
203+
}
204+
}
205+
}
206+
return ans;
207+
}
114208
```
115209

116210
<!-- tabs:end -->

0 commit comments

Comments
 (0)