Skip to content

Commit

Permalink
219th Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 12, 2024
1 parent a14d27f commit 36fd0a1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,12 @@ Ace Coding Interview with 75 Qs
| 374. Guess Number Higher or Lower | [Solution][374] | Easy |
| 2300. Successful Pairs of Spells and Potions | [Solution][2300] | Medium |
| 162. Find Peak Element | [Solution][162] | Medium |
| 875. Koko Eating Bananas | Solution | Medium |
| 875. Koko Eating Bananas | [Solution][875] | Medium |

[374]: ./src/page-4/374.%20Guess%20Number%20Higher%20or%20Lower/guessNumber.ts
[2300]: ./src/page-21/2300.%20Successful%20Pairs%20of%20Spells%20and%20Potions/successfulPairs.ts
[162]: ./src/page-2/162.%20Find%20Peak%20Element/findPeakElement.ts
[875]: ./src/page-9/875.%20Koko%20Eating%20Bananas/minEatingSpeed.ts

| Backtracking | | |
| ----------------------------------------- | -------- | ------ |
Expand Down
9 changes: 9 additions & 0 deletions src/page-9/875. Koko Eating Bananas/minEatingSpeed.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { minEatingSpeed } from './minEatingSpeed';

describe('875. Koko Eating Bananas', () => {
test('minEatingSpeed', () => {
expect(minEatingSpeed([3, 6, 7, 11], 8)).toBe(4);
expect(minEatingSpeed([30, 11, 23, 4, 20], 5)).toBe(30);
expect(minEatingSpeed([30, 11, 23, 4, 20], 6)).toBe(23);
});
});
31 changes: 31 additions & 0 deletions src/page-9/875. Koko Eating Bananas/minEatingSpeed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
type MinEatingSpeed = (piles: number[], h: number) => number;

/**
* Accepted
*/
export const minEatingSpeed: MinEatingSpeed = (piles, h) => {
let left = 1;
let right = Math.max(...piles);

const canEatAll = (k: number): boolean => {
let hours = 0;

for (const pile of piles) {
hours += Math.ceil(pile / k);
}

return hours <= h;
};

while (left < right) {
const mid = Math.floor((left + right) / 2);

if (canEatAll(mid)) {
right = mid; // Try smaller k
} else {
left = mid + 1; // Try larger k
}
}

return left;
};

0 comments on commit 36fd0a1

Please sign in to comment.