publicclassSolution { publicintMinEatingSpeed(int[] piles, int h) { int low = 1; int high = 0; foreach (int pile in piles) { high = Math.Max(high, pile); } int k = high; while (low < high) { int speed = (high - low) / 2 + low; long time = GetTime(piles, speed); if (time <= h) { k = speed; high = speed; } else { low = speed + 1; } } return k; }
publiclongGetTime(int[] piles, int speed) { long time = 0; foreach (int pile in piles) { int curTime = (pile + speed - 1) / speed; time += curTime; } return time; } }
classSolution { public: intminEatingSpeed(vector<int>& piles, int h){ int low = 1; int high = 0; for (int pile : piles) { high = max(high, pile); } int k = high; while (low < high) { int speed = (high - low) / 2 + low; long time = getTime(piles, speed); if (time <= h) { k = speed; high = speed; } else { low = speed + 1; } } return k; }
longgetTime(const vector<int>& piles, int speed){ long time = 0; for (int pile : piles) { int curTime = (pile + speed - 1) / speed; time += curTime; } return time; } };
longgetTime(constint* piles, int pilesSize, int speed) { long time = 0; for (int i = 0; i < pilesSize; i++) { int curTime = (piles[i] + speed - 1) / speed; time += curTime; } return time; } intminEatingSpeed(int* piles, int pilesSize, int h) { int low = 1; int high = 0; for (int i = 0; i < pilesSize; i++) { high = MAX(high, piles[i]); } int k = high; while (low < high) { int speed = (high - low) / 2 + low; long time = getTime(piles, pilesSize, speed); if (time <= h) { k = speed; high = speed; } else { low = speed + 1; } } return k; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funcminEatingSpeed(piles []int, h int)int { max := 0 for _, pile := range piles { if pile > max { max = pile } } return1 + sort.Search(max-1, func(speed int)bool { speed++ time := 0 for _, pile := range piles { time += (pile + speed - 1) / speed } return time <= h }) }