Skip to content

Commit 652e4a7

Browse files
authored
feat: add solutions for lc No.1545,3666 (#5054)
1 parent d881de6 commit 652e4a7

11 files changed

Lines changed: 302 additions & 33 deletions

File tree

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,55 @@ function findKthBit(n: number, k: number): string {
211211
}
212212
```
213213

214-
<!-- tabs:end -->
215-
216-
<!-- solution:end -->
217-
218-
<!-- solution:start -->
219-
220-
### 方法二:位运算
221-
222-
<!-- tabs:start -->
214+
#### Rust
223215

224-
#### TypeScript
216+
```rust
217+
impl Solution {
218+
pub fn find_kth_bit(n: i32, k: i32) -> char {
219+
fn dfs(n: i32, k: i32) -> i32 {
220+
if k == 1 {
221+
return 0;
222+
}
223+
if (k & (k - 1)) == 0 {
224+
return 1;
225+
}
226+
let m: i32 = 1 << n;
227+
if k * 2 < m - 1 {
228+
dfs(n - 1, k)
229+
} else {
230+
dfs(n - 1, m - k) ^ 1
231+
}
232+
}
225233

226-
```ts
227-
const findKthBit = (n: number, k: number): string =>
228-
String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
234+
if dfs(n, k) == 0 { '0' } else { '1' }
235+
}
236+
}
229237
```
230238

231239
#### JavaScript
232240

233241
```js
234-
const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
242+
/**
243+
* @param {number} n
244+
* @param {number} k
245+
* @return {character}
246+
*/
247+
var findKthBit = function (n, k) {
248+
const dfs = function (n, k) {
249+
if (k === 1) {
250+
return 0;
251+
}
252+
if ((k & (k - 1)) === 0) {
253+
return 1;
254+
}
255+
const m = 1 << n;
256+
if (k * 2 < m - 1) {
257+
return dfs(n - 1, k);
258+
}
259+
return dfs(n - 1, m - k) ^ 1;
260+
};
261+
return dfs(n, k).toString();
262+
};
235263
```
236264

237265
<!-- tabs:end -->

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/README_EN.md

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -197,27 +197,55 @@ function findKthBit(n: number, k: number): string {
197197
}
198198
```
199199

200-
<!-- tabs:end -->
201-
202-
<!-- solution:end -->
203-
204-
<!-- solution:start -->
205-
206-
### Solution 2: Bit Manipulation
207-
208-
<!-- tabs:start -->
200+
#### Rust
209201

210-
#### TypeScript
202+
```rust
203+
impl Solution {
204+
pub fn find_kth_bit(n: i32, k: i32) -> char {
205+
fn dfs(n: i32, k: i32) -> i32 {
206+
if k == 1 {
207+
return 0;
208+
}
209+
if (k & (k - 1)) == 0 {
210+
return 1;
211+
}
212+
let m: i32 = 1 << n;
213+
if k * 2 < m - 1 {
214+
dfs(n - 1, k)
215+
} else {
216+
dfs(n - 1, m - k) ^ 1
217+
}
218+
}
211219

212-
```ts
213-
const findKthBit = (n: number, k: number): string =>
214-
String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
220+
if dfs(n, k) == 0 { '0' } else { '1' }
221+
}
222+
}
215223
```
216224

217225
#### JavaScript
218226

219227
```js
220-
const findKthBit = (n, k) => String((((k / (k & -k)) >> 1) & 1) ^ (k & 1) ^ 1);
228+
/**
229+
* @param {number} n
230+
* @param {number} k
231+
* @return {character}
232+
*/
233+
var findKthBit = function (n, k) {
234+
const dfs = function (n, k) {
235+
if (k === 1) {
236+
return 0;
237+
}
238+
if ((k & (k - 1)) === 0) {
239+
return 1;
240+
}
241+
const m = 1 << n;
242+
if (k * 2 < m - 1) {
243+
return dfs(n - 1, k);
244+
}
245+
return dfs(n - 1, m - k) ^ 1;
246+
};
247+
return dfs(n, k).toString();
248+
};
221249
```
222250

223251
<!-- tabs:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} k
4+
* @return {character}
5+
*/
6+
var findKthBit = function (n, k) {
7+
const dfs = function (n, k) {
8+
if (k === 1) {
9+
return 0;
10+
}
11+
if ((k & (k - 1)) === 0) {
12+
return 1;
13+
}
14+
const m = 1 << n;
15+
if (k * 2 < m - 1) {
16+
return dfs(n - 1, k);
17+
}
18+
return dfs(n - 1, m - k) ^ 1;
19+
};
20+
return dfs(n, k).toString();
21+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
pub fn find_kth_bit(n: i32, k: i32) -> char {
3+
fn dfs(n: i32, k: i32) -> i32 {
4+
if k == 1 {
5+
return 0;
6+
}
7+
if (k & (k - 1)) == 0 {
8+
return 1;
9+
}
10+
let m: i32 = 1 << n;
11+
if k * 2 < m - 1 {
12+
dfs(n - 1, k)
13+
} else {
14+
dfs(n - 1, m - k) ^ 1
15+
}
16+
}
17+
18+
if dfs(n, k) == 0 { '0' } else { '1' }
19+
}
20+
}

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

solution/1500-1599/1545.Find Kth Bit in Nth Binary String/Solution2.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

solution/3200-3299/3296.Minimum Number of Seconds to Make Mountain Height Zero/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ $$
132132

133133
接下来,我们确定二分查找的左边界 $l = 1$,由于最少有一个工作,且每个工人的工作时间不超过 $10^6$,要想使山的高度降低到 $0$,最少需要 $(1 + \textit{mountainHeight}) \cdot \textit{mountainHeight} / 2 \cdot \textit{workerTimes}[i] \leq 10^{16}$ 秒,因此我们可以确定二分查找的右边界 $r = 10^{16}$。然后我们不断地将区间 $[l, r]$ 二分,直到 $l = r$,此时 $l$ 即为答案。
134134

135-
时间复杂度 $O(n \times \log M)$,其中 $n$ 为工人的数量,而 $M$ 为二分查找的右边界,本题中 $M = 10^{16}$。
135+
时间复杂度 $O(n \times \log M)$,其中 $n$ 为工人的数量,而 $M$ 为二分查找的右边界,本题中 $M = 10^{16}$。空间复杂度 $O(1)$。
136136

137137
<!-- tabs:start -->
138138

solution/3200-3299/3296.Minimum Number of Seconds to Make Mountain Height Zero/README_EN.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ We can sum up all the $h'$ values for the workers to get the total reduced heigh
128128

129129
Next, we determine the left boundary of the binary search $l = 1$. Since there is at least one worker, and each worker's working time does not exceed $10^6$, to reduce the mountain height to $0$, it takes at least $(1 + \textit{mountainHeight}) \cdot \textit{mountainHeight} / 2 \cdot \textit{workerTimes}[i] \leq 10^{16}$ seconds. Therefore, we can set the right boundary of the binary search to $r = 10^{16}$. Then, we continuously halve the interval $[l, r]$ until $l = r$. At this point, $l$ is the answer.
130130

131-
The time complexity is $O(n \times \log M)$, where $n$ is the number of workers, and $M$ is the right boundary of the binary search, which is $10^{16}$ in this problem.
131+
The time complexity is $O(n \times \log M)$, where $n$ is the number of workers, and $M$ is the right boundary of the binary search, which is $10^{16}$ in this problem. The space complexity is $O(1)$.
132132

133133
<!-- tabs:start -->
134134

solution/3600-3699/3666.Minimum Operations to Equalize Binary String/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,66 @@ impl Solution {
402402
}
403403
```
404404

405+
#### C#
406+
407+
```cs
408+
public class Solution {
409+
public int MinOperations(string s, int k) {
410+
int n = s.Length;
411+
412+
var ts = new SortedSet<int>[2];
413+
ts[0] = new SortedSet<int>();
414+
ts[1] = new SortedSet<int>();
415+
416+
for (int i = 0; i <= n; i++) {
417+
ts[i % 2].Add(i);
418+
}
419+
420+
int cnt0 = 0;
421+
foreach (char c in s) {
422+
if (c == '0') {
423+
cnt0++;
424+
}
425+
}
426+
427+
ts[cnt0 % 2].Remove(cnt0);
428+
429+
var q = new Queue<int>();
430+
q.Enqueue(cnt0);
431+
432+
int ans = 0;
433+
434+
while (q.Count > 0) {
435+
int size = q.Count;
436+
for (int i = 0; i < size; i++) {
437+
int cur = q.Dequeue();
438+
if (cur == 0) {
439+
return ans;
440+
}
441+
442+
int l = cur + k - 2 * Math.Min(cur, k);
443+
int r = cur + k - 2 * Math.Max(k - n + cur, 0);
444+
445+
var t = ts[l % 2];
446+
447+
var toRemove = new List<int>();
448+
foreach (int next in t.GetViewBetween(l, r)) {
449+
q.Enqueue(next);
450+
toRemove.Add(next);
451+
}
452+
453+
foreach (int next in toRemove) {
454+
t.Remove(next);
455+
}
456+
}
457+
ans++;
458+
}
459+
460+
return -1;
461+
}
462+
}
463+
```
464+
405465
<!-- tabs:end -->
406466

407467
<!-- solution:end -->

solution/3600-3699/3666.Minimum Operations to Equalize Binary String/README_EN.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,66 @@ impl Solution {
399399
}
400400
```
401401

402+
#### C#
403+
404+
```cs
405+
public class Solution {
406+
public int MinOperations(string s, int k) {
407+
int n = s.Length;
408+
409+
var ts = new SortedSet<int>[2];
410+
ts[0] = new SortedSet<int>();
411+
ts[1] = new SortedSet<int>();
412+
413+
for (int i = 0; i <= n; i++) {
414+
ts[i % 2].Add(i);
415+
}
416+
417+
int cnt0 = 0;
418+
foreach (char c in s) {
419+
if (c == '0') {
420+
cnt0++;
421+
}
422+
}
423+
424+
ts[cnt0 % 2].Remove(cnt0);
425+
426+
var q = new Queue<int>();
427+
q.Enqueue(cnt0);
428+
429+
int ans = 0;
430+
431+
while (q.Count > 0) {
432+
int size = q.Count;
433+
for (int i = 0; i < size; i++) {
434+
int cur = q.Dequeue();
435+
if (cur == 0) {
436+
return ans;
437+
}
438+
439+
int l = cur + k - 2 * Math.Min(cur, k);
440+
int r = cur + k - 2 * Math.Max(k - n + cur, 0);
441+
442+
var t = ts[l % 2];
443+
444+
var toRemove = new List<int>();
445+
foreach (int next in t.GetViewBetween(l, r)) {
446+
q.Enqueue(next);
447+
toRemove.Add(next);
448+
}
449+
450+
foreach (int next in toRemove) {
451+
t.Remove(next);
452+
}
453+
}
454+
ans++;
455+
}
456+
457+
return -1;
458+
}
459+
}
460+
```
461+
402462
<!-- tabs:end -->
403463

404464
<!-- solution:end -->

0 commit comments

Comments
 (0)