Skip to content

Commit

Permalink
2020.12.9
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoxuyan committed Dec 9, 2020
1 parent 92cf10b commit c7d2d3b
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DataStructure/0146-lru-cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LRUCache {
void put(int key, int value) {
// 若key不存在
if (hashtable.count(key) == 0) {
// 如果容量到了, 删除尾部元素, 再加上新节点
// 如果容量满了, 删除尾部元素, 再加上新节点
if (cache.size() == cap) {
hashtable.erase(cache.back().first);
cache.pop_back();
Expand All @@ -51,7 +51,7 @@ class LRUCache {
}
unordered_map<int, list<pair<int, int>>::iterator> hashtable;
list<pair<int, int>> cache; // key value
int cap = 0;
int cap = 0; // cache的容量
};

/**
Expand Down
59 changes: 59 additions & 0 deletions DataStructure/BinarySearch/0875-koko-eating-bananas.cpp
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;
}
};
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;
}
};

0 comments on commit c7d2d3b

Please sign in to comment.