diff --git a/solution/2500-2599/2533.Number of Good Binary Strings/README.md b/solution/2500-2599/2533.Number of Good Binary Strings/README.md index fbdd0f5ac3eb0..ddfb225148699 100644 --- a/solution/2500-2599/2533.Number of Good Binary Strings/README.md +++ b/solution/2500-2599/2533.Number of Good Binary Strings/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md b/solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md index ab95e0f205935..02f3791a894a9 100644 --- a/solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md +++ b/solution/2500-2599/2533.Number of Good Binary Strings/README_EN.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2533.Number of Good Binary Strings/Solution.ts b/solution/2500-2599/2533.Number of Good Binary Strings/Solution.ts new file mode 100644 index 0000000000000..049fb7c241a47 --- /dev/null +++ b/solution/2500-2599/2533.Number of Good Binary Strings/Solution.ts @@ -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; +} diff --git a/solution/2500-2599/2555.Maximize Win From Two Segments/README.md b/solution/2500-2599/2555.Maximize Win From Two Segments/README.md index 18c22529680de..2446da49617bd 100644 --- a/solution/2500-2599/2555.Maximize Win From Two Segments/README.md +++ b/solution/2500-2599/2555.Maximize Win From Two Segments/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2555.Maximize Win From Two Segments/README_EN.md b/solution/2500-2599/2555.Maximize Win From Two Segments/README_EN.md index b9259b9c6d0e5..5bba2b6d407d8 100644 --- a/solution/2500-2599/2555.Maximize Win From Two Segments/README_EN.md +++ b/solution/2500-2599/2555.Maximize Win From Two Segments/README_EN.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2555.Maximize Win From Two Segments/Solution.ts b/solution/2500-2599/2555.Maximize Win From Two Segments/Solution.ts new file mode 100644 index 0000000000000..5a3ce89290229 --- /dev/null +++ b/solution/2500-2599/2555.Maximize Win From Two Segments/Solution.ts @@ -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; +} diff --git a/solution/2500-2599/2557.Maximum Number of Integers to Choose From a Range II/README.md b/solution/2500-2599/2557.Maximum Number of Integers to Choose From a Range II/README.md index 4128575c6cd60..64fe3f11a4036 100644 --- a/solution/2500-2599/2557.Maximum Number of Integers to Choose From a Range II/README.md +++ b/solution/2500-2599/2557.Maximum Number of Integers to Choose From a Range II/README.md @@ -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) - ### **Python3** diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md index ef9c5c0d46cc6..129834bebf29e 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README.md @@ -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 diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md index 9f5f55d20bf75..c4876191c99b4 100644 --- a/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/README_EN.md @@ -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 diff --git a/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts new file mode 100644 index 0000000000000..5179a74c4d843 --- /dev/null +++ b/solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts @@ -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; +} diff --git a/solution/2500-2599/2560.House Robber IV/README.md b/solution/2500-2599/2560.House Robber IV/README.md index 76a95f34b3b70..302ded47df593 100644 --- a/solution/2500-2599/2560.House Robber IV/README.md +++ b/solution/2500-2599/2560.House Robber IV/README.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2560.House Robber IV/README_EN.md b/solution/2500-2599/2560.House Robber IV/README_EN.md index f0fafec95eab8..1c61f182c93de 100644 --- a/solution/2500-2599/2560.House Robber IV/README_EN.md +++ b/solution/2500-2599/2560.House Robber IV/README_EN.md @@ -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; +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2560.House Robber IV/Solution.ts b/solution/2500-2599/2560.House Robber IV/Solution.ts new file mode 100644 index 0000000000000..7bf2b13fa1237 --- /dev/null +++ b/solution/2500-2599/2560.House Robber IV/Solution.ts @@ -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; +}