Skip to content

feat: add solutions to lc problems: No.2533,2555,2558,2560 #1506

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions solution/2500-2599/2533.Number of Good Binary Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,31 @@ func goodBinaryStrings(minLength int, maxLength int, oneGroup int, zeroGroup int
}
```

### **TypeScript**

```ts
function goodBinaryStrings(
minLength: number,
maxLength: number,
oneGroup: number,
zeroGroup: number,
): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(maxLength + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= maxLength; ++i) {
if (i >= oneGroup) {
f[i] += f[i - oneGroup];
}
if (i >= zeroGroup) {
f[i] += f[i - zeroGroup];
}
f[i] %= mod;
}
return f.slice(minLength).reduce((a, b) => a + b, 0) % mod;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实累加的话,那个初始参数有时候可以省略的,没有指定初始值的时候会将第一个元素作为累加器,这边是考虑更严谨一些么。
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那可以省略,有的语言需要自己赋初始值

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那后面如果我做什么题看到省略能ac的话 就顺带改了哈~

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以改了,更简洁

}
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,31 @@ func goodBinaryStrings(minLength int, maxLength int, oneGroup int, zeroGroup int
}
```

### **TypeScript**

```ts
function goodBinaryStrings(
minLength: number,
maxLength: number,
oneGroup: number,
zeroGroup: number,
): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(maxLength + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= maxLength; ++i) {
if (i >= oneGroup) {
f[i] += f[i - oneGroup];
}
if (i >= zeroGroup) {
f[i] += f[i - zeroGroup];
}
f[i] %= mod;
}
return f.slice(minLength).reduce((a, b) => a + b, 0) % mod;
}
```

### **...**

```
Expand Down
20 changes: 20 additions & 0 deletions solution/2500-2599/2533.Number of Good Binary Strings/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function goodBinaryStrings(
minLength: number,
maxLength: number,
oneGroup: number,
zeroGroup: number,
): number {
const mod = 10 ** 9 + 7;
const f: number[] = Array(maxLength + 1).fill(0);
f[0] = 1;
for (let i = 1; i <= maxLength; ++i) {
if (i >= oneGroup) {
f[i] += f[i - oneGroup];
}
if (i >= zeroGroup) {
f[i] += f[i - zeroGroup];
}
f[i] %= mod;
}
return f.slice(minLength).reduce((a, b) => a + b, 0) % mod;
}
30 changes: 30 additions & 0 deletions solution/2500-2599/2555.Maximize Win From Two Segments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,36 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
function maximizeWin(prizePositions: number[], k: number): number {
const n = prizePositions.length;
const f: number[] = Array(n + 1).fill(0);
let ans = 0;
const search = (x: number): number => {
let left = 0;
let right = n;
while (left < right) {
const mid = (left + right) >> 1;
if (prizePositions[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
for (let i = 1; i <= n; ++i) {
const x = prizePositions[i - 1];
const j = search(x - k);
ans = Math.max(ans, f[j] + i - j);
f[i] = Math.max(f[i - 1], i - j);
}
return ans;
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,36 @@ func max(a, b int) int {
}
```

### **TypeScript**

```ts
function maximizeWin(prizePositions: number[], k: number): number {
const n = prizePositions.length;
const f: number[] = Array(n + 1).fill(0);
let ans = 0;
const search = (x: number): number => {
let left = 0;
let right = n;
while (left < right) {
const mid = (left + right) >> 1;
if (prizePositions[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
for (let i = 1; i <= n; ++i) {
const x = prizePositions[i - 1];
const j = search(x - k);
ans = Math.max(ans, f[j] + i - j);
f[i] = Math.max(f[i - 1], i - j);
}
return ans;
}
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions solution/2500-2599/2555.Maximize Win From Two Segments/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function maximizeWin(prizePositions: number[], k: number): number {
const n = prizePositions.length;
const f: number[] = Array(n + 1).fill(0);
let ans = 0;
const search = (x: number): number => {
let left = 0;
let right = n;
while (left < right) {
const mid = (left + right) >> 1;
if (prizePositions[mid] >= x) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
};
for (let i = 1; i <= n; ++i) {
const x = prizePositions[i - 1];
const j = search(x - k);
ans = Math.max(ans, f[j] + i - j);
f[i] = Math.max(f[i - 1], i - j);
}
return ans;
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `banned` 的长度。

相似题目:[2557. 从一个范围内选择最多整数 II](/solution/2500-2599/2557.Maximum%20Number%20of%20Integers%20to%20Choose%20From%20a%20Range%20II/README.md)

相似题目:[2556. 从一个范围内选择最多整数 I](/solution/2500-2599/2554.Maximum%20Number%20of%20Integers%20to%20Choose%20From%20a%20Range%20I/README.md)

<!-- tabs:start -->

### **Python3**
Expand Down
21 changes: 21 additions & 0 deletions solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,27 @@ func (hp) Pop() (_ interface{}) { return }
func (hp) Push(interface{}) {}
```

### **TypeScript**

```ts
function pickGifts(gifts: number[], k: number): number {
const pq = new MaxPriorityQueue();
for (const v of gifts) {
pq.enqueue(v, v);
}
while (k--) {
let v = pq.dequeue().element;
v = Math.floor(Math.sqrt(v));
pq.enqueue(v, v);
}
let ans = 0;
while (!pq.isEmpty()) {
ans += pq.dequeue().element;
}
return ans;
}
```

### **Rust**

```rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,27 @@ func (hp) Pop() (_ interface{}) { return }
func (hp) Push(interface{}) {}
```

### **TypeScript**

```ts
function pickGifts(gifts: number[], k: number): number {
const pq = new MaxPriorityQueue();
for (const v of gifts) {
pq.enqueue(v, v);
}
while (k--) {
let v = pq.dequeue().element;
v = Math.floor(Math.sqrt(v));
pq.enqueue(v, v);
}
let ans = 0;
while (!pq.isEmpty()) {
ans += pq.dequeue().element;
}
return ans;
}
```

### **Rust**

```rust
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function pickGifts(gifts: number[], k: number): number {
const pq = new MaxPriorityQueue();
for (const v of gifts) {
pq.enqueue(v, v);
}
while (k--) {
let v = pq.dequeue().element;
v = Math.floor(Math.sqrt(v));
pq.enqueue(v, v);
}
let ans = 0;
while (!pq.isEmpty()) {
ans += pq.dequeue().element;
}
return ans;
}
30 changes: 30 additions & 0 deletions solution/2500-2599/2560.House Robber IV/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ func minCapability(nums []int, k int) int {
}
```

### **TypeScript**

```ts
function minCapability(nums: number[], k: number): number {
const f = (mx: number): boolean => {
let cnt = 0;
let j = -2;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] <= mx && i - j > 1) {
++cnt;
j = i;
}
}
return cnt >= k;
};

let left = 1;
let right = Math.max(...nums);
while (left < right) {
const mid = (left + right) >> 1;
if (f(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
```

### **...**

```
Expand Down
30 changes: 30 additions & 0 deletions solution/2500-2599/2560.House Robber IV/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ func minCapability(nums []int, k int) int {
}
```

### **TypeScript**

```ts
function minCapability(nums: number[], k: number): number {
const f = (mx: number): boolean => {
let cnt = 0;
let j = -2;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] <= mx && i - j > 1) {
++cnt;
j = i;
}
}
return cnt >= k;
};

let left = 1;
let right = Math.max(...nums);
while (left < right) {
const mid = (left + right) >> 1;
if (f(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}
```

### **...**

```
Expand Down
25 changes: 25 additions & 0 deletions solution/2500-2599/2560.House Robber IV/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function minCapability(nums: number[], k: number): number {
const f = (mx: number): boolean => {
let cnt = 0;
let j = -2;
for (let i = 0; i < nums.length; ++i) {
if (nums[i] <= mx && i - j > 1) {
++cnt;
j = i;
}
}
return cnt >= k;
};

let left = 1;
let right = Math.max(...nums);
while (left < right) {
const mid = (left + right) >> 1;
if (f(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}