-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
solution/2500-2599/2533.Number of Good Binary Strings/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
solution/2500-2599/2555.Maximize Win From Two Segments/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
solution/2500-2599/2558.Take Gifts From the Richest Pile/Solution.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实累加的话,那个初始参数有时候可以省略的,没有指定初始值的时候会将第一个元素作为累加器,这边是考虑更严谨一些么。

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那可以省略,有的语言需要自己赋初始值
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那后面如果我做什么题看到省略能ac的话 就顺带改了哈~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以改了,更简洁