标签:就是 自己 while ring The solution each pass https
刚开始写得时候感觉像贪心,但没分析好不敢写,自己写了个记忆化搜索,后来看讲解把贪心法给补上
记忆化搜索
const int MAXN = 1e5+50;
class Solution {
public:
int dp[MAXN];
int dfs(int now, vector<int>& heights, int bricks, int ladders){
if (dp[now] != -1) return dp[now];
int n = heights.size();
if (now == n-1) return n-1;
while (now+1 < n && heights[now] > heights[now+1]) now++;
if (now == n-1) return n-1;
int a = now,b = now;
if (ladders > 0) a = dfs(now+1, heights, bricks, ladders-1);
if (bricks >= heights[now+1]-heights[now]) b = dfs(now+1, heights, bricks-(heights[now+1]-heights[now]), ladders);
dp[now] = max(a,b);
return dp[now];
}
int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
int n = heights.size();
for (int i = 0; i <= n; i++) dp[i] = -1;
return dfs(0,heights, bricks, ladders);
}
};
贪心:
/*贪心法就是把梯子先存着,将梯子用在高度差比较大的地方*/
class Solution {
public:
priority_queue<int> que;
int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
while (!que.empty()) que.pop();
int ans, sum = 0;
for (int i = 0; i < heights.size(); i++){
ans = i;
if (i == heights.size()-1) break;
int a = heights[i], b = heights[i+1];
if (a >= b) {
//pass
}else{
que.push(a-b);
if (que.size() > ladders){
sum -= que.top();que.pop();
}
if (sum > bricks) break;
}
}
return ans;
}
};
在答案的每个位置只有两种选择,不是H就是V,按字典序排序后,H肯定在前面而且一字排开,范围是1-C[h+v-1] [h-1],如果超过这个范围,那肯定就是V打头.(通过这题再一次复习了一下组合数及其写法..)
const int MAXN = 32;
class Solution {
public:
int C[MAXN][MAXN];
void init(){
C[0][0] = 1;
for (int i = 1; i < MAXN; i++){
C[i][0] = 1;
for (int j = 1; j <= i; j++){
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
}
}
string kthSmallestPath(vector<int>& d, int k) {
init();
int h = d[1], v = d[0], n = h+v;
string ans;
for (int i = 0; i < n; i++){
if (h > 0 && k <= C[h+v-1][h-1]){
h--;
ans += "H";
}else{
if (h > 0) k -= C[h+v-1][h-1];
else k -= C[h+v-1][0];
v--;
ans += "V";
}
}
return ans;
}
};
总结:组合数及其写法???♂?
void init(){
C[0][0] = 1;
for (int i = 1; i < MAXN; i++){
C[i][0] = 1;
for (int j = 1; j <= i; j++){
C[i][j] = C[i-1][j] + C[i-1][j-1];
}
}
}
标签:就是 自己 while ring The solution each pass https
原文地址:https://www.cnblogs.com/Beic233/p/13910484.html