-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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,59 @@ | ||
// 题目:https://leetcode-cn.com/problems/koko-eating-bananas/ | ||
// 二分法:https://labuladong.gitbook.io/algo/di-ling-zhang-bi-du-xi-lie/er-fen-cha-zhao-xiang-jie | ||
|
||
#include <algorithm> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <list> | ||
#include <unordered_map> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
// 普通硬核搜索 超时 | ||
int minEatingSpeed_nomal(vector<int>& piles, int H) { | ||
int max = getMax(piles); | ||
// 连续的空间线性搜索,这是二分查找可以发挥作用的标志 | ||
for (int speed = 1; speed < max; ++speed) { | ||
if (canFinish(piles, speed, H)) return speed; | ||
} | ||
return max; | ||
} | ||
|
||
// 由于我们要求的是最小速度, 所以可以用一个搜索左侧边界的二分查找来代替线性搜索 | ||
int minEatingSpeed(vector<int>& piles, int H) { | ||
// 套用搜索左侧边界的算法框架 | ||
int left = 1, right = getMax(piles); | ||
// 搜索区间为 [left, right] | ||
while (left <= right) { | ||
// 防止溢出 | ||
int mid = left + (right - left) / 2; | ||
if (canFinish(piles, mid, H)) { | ||
// 遇到符合条件的 收缩右侧边界 | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return left; | ||
} | ||
|
||
// 辅助函数 | ||
bool canFinish(vector<int>& piles, int speed, int H) { | ||
int time = 0; | ||
for (int n : piles) { | ||
time += timeOf(n, speed); | ||
if (time > H) return false; | ||
} | ||
return true; | ||
} | ||
|
||
int timeOf(int n, int speed) { return (n / speed) + ((n % speed > 0) ? 1 : 0); } | ||
|
||
int getMax(vector<int>& piles) { | ||
int max = 0; | ||
for (int n : piles) max = n > max ? n : max; | ||
return max; | ||
} | ||
}; |
51 changes: 51 additions & 0 deletions
51
DataStructure/BinarySearch/1011-capacity-to-ship-packages-within-d-days.cpp
This file contains 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,51 @@ | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <iostream> | ||
#include <list> | ||
#include <unordered_map> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int shipWithinDays(vector<int>& weights, int D) { | ||
int left = getMax(weights); | ||
int right = getSum(weights); | ||
// 求最小值:左侧边界二分搜索 | ||
while (left <= right) { | ||
int mid = left + (right - left) / 2; | ||
if (canFinish(weights, D, mid)) { | ||
right = mid - 1; | ||
} else { | ||
left = mid + 1; | ||
} | ||
} | ||
return left; | ||
} | ||
int canFinish(vector<int>& weights, int D, int cap) { | ||
int i = 0; | ||
for (int day = 0; day < D; ++day) { | ||
// 每天的载重量都是cap个。 | ||
int cur_cap = cap; | ||
// 按顺序i, 当天能装多少装多少 装不下了就下一天继续 | ||
while (cur_cap - weights[i] >= 0) { | ||
cur_cap -= weights[i]; | ||
i++; | ||
if (i == weights.size()) return true; | ||
} | ||
} | ||
return false; | ||
} | ||
int getMax(vector<int>& weights) { | ||
int max = 0; | ||
for (int n : weights) max = n > max ? n : max; | ||
return max; | ||
} | ||
int getSum(vector<int>& weights) { | ||
int sum = 0; | ||
for (auto& weight : weights) { | ||
sum += weight; | ||
} | ||
return sum; | ||
} | ||
}; |