-
Notifications
You must be signed in to change notification settings - Fork 0
Koko Eating Bananas #137
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
875.Koko-Eating-Bananas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Koko Eating Bananas #137
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # 875. Koko Eating Bananas | ||
|
|
||
| ## step1 | ||
| 二分探索を思いつく。時間計算量はO(nlog max(piles))。17mほど。最小値と最大値を何度か間違えた(ガチャを引いてしまった)。 | ||
|
|
||
| とりあえず書いた答えを残す。 | ||
|
|
||
| 改善する。 | ||
|
|
||
| 上限と下限を単純に 1, max(piles)とした方が可読性が上がるが、速度は落ちる。 | ||
|
|
||
| ## step2 | ||
| 二分探索を手で書く | ||
|
|
||
| https://leetcode.com/problems/koko-eating-bananas/solutions/7047251/simple-solution-by-harshita_114-3c0a/ | ||
|
|
||
| can_eat_all の判定はfor文を使えば早期終了できる場合があるのか | ||
|
|
||
| 実際の実行時間はジェネレータ表記の方が早かった | ||
|
|
||
| https://github.com/yamashita-ki/codingTest/pull/15 | ||
|
|
||
| https://github.com/Exzrgs/LeetCode/pull/45/changes | ||
|
|
||
| https://github.com/TaisukeFujise/leetcode_tafujise/pull/16 | ||
|
|
||
| ## step3 | ||
| 今回は省略で良いだろう |
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 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def minEatingSpeed(self, piles: List[int], h: int) -> int: | ||
| def can_eat_all(k): | ||
| return sum(((pile + k - 1) // k for pile in piles)) <= h | ||
|
|
||
| candidate = range( | ||
| max(min(piles) // h, 1), | ||
| max((max(piles) * 2 - 1) // (h // len(piles)), 1) + 1, | ||
| ) | ||
| index = bisect.bisect_left( | ||
| range(len(candidate)), True, key=lambda index: can_eat_all(candidate[index]) | ||
| ) | ||
| return candidate[index] |
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,18 @@ | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def minEatingSpeed(self, piles: list[int], h: int) -> int: | ||
| if not piles: | ||
| return 0 | ||
|
|
||
| def can_eat_all(k): | ||
| return sum(((pile + k - 1) // k for pile in piles)) <= h | ||
|
|
||
| max_candidate = max((max(piles) * 2 - 1) // (h // len(piles)), 1) | ||
| min_candidate = max(min(piles) // h, 1) | ||
| candidate = range(min_candidate, max_candidate + 1) | ||
| index = bisect.bisect_left( | ||
| range(len(candidate)), True, key=lambda index: can_eat_all(candidate[index]) | ||
| ) | ||
| return candidate[index] |
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,18 @@ | ||
| class Solution: | ||
| def minEatingSpeed(self, piles: list[int], h: int) -> int: | ||
| if not piles: | ||
| return 0 | ||
|
|
||
| def can_eat_all(k): | ||
| return sum(((pile + k - 1) // k for pile in piles)) <= h | ||
|
|
||
| left = max(min(piles) // h, 1) | ||
| right = max((max(piles) * 2 - 1) // (h // len(piles)), 1) + 1 | ||
| while left < right: | ||
| mid = left + (right - left) // 2 | ||
| if not can_eat_all(mid): | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
|
|
||
| return left | ||
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,23 @@ | ||
| class Solution: | ||
| def minEatingSpeed(self, piles: list[int], h: int) -> int: | ||
| if not piles: | ||
| return 0 | ||
|
|
||
| def can_eat_all(k): | ||
| hours = 0 | ||
| for i, pile in enumerate(piles): | ||
| hours += (pile + k - 1) // k | ||
| if hours + (len(piles) - i - 1) > h: | ||
| return False | ||
| return True | ||
|
|
||
| left = max(min(piles) // h, 1) | ||
| right = max((max(piles) * 2 - 1) // (h // len(piles)), 1) + 1 | ||
| while left < right: | ||
| mid = left + (right - left) // 2 | ||
| if not can_eat_all(mid): | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
|
|
||
| return left |
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,18 @@ | ||
| class Solution: | ||
| def minEatingSpeed(self, piles: list[int], h: int) -> int: | ||
| if not piles: | ||
| return 0 | ||
|
|
||
| def can_eat_all(k): | ||
| return sum(((pile + k - 1) // k for pile in piles)) <= h | ||
|
|
||
| left = max(min(piles) // h, 1) | ||
| right = max((max(piles) * 2 - 1) // (h // len(piles)), 1) + 1 | ||
| while left < right: | ||
| mid = (left + right) // 2 | ||
| if not can_eat_all(mid): | ||
| left = mid + 1 | ||
| else: | ||
| right = mid | ||
|
|
||
| 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.
Python の int は格納できる数値の大きさに実質上限がないため、オーバーフローを回避する書き方はしなくてよいと思います。
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.
今後はそのように書こうと思います。
他の言語や numpy を使用する場合を想定していましたが、この場合は不要ですね。