# 2020-7-15

#### 407. 接雨水 II

class Solution {
public:
struct cell{
int x, y, h;
bool operator < (const cell & rs)const{
return h > rs.h;
}
};
bool vis[125][125];
int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1};
int trapRainWater(vector<vector<int>>& heightMap) {
int n = heightMap.size() , m = heightMap[0].size();
priority_queue <cell> q;
for(int i=0;i<n;i++) { q.push({i, 0 , heightMap[i][0]}); vis[i][0] = 1;}
for(int i=0;i<n;i++) { q.push({i, m-1 , heightMap[i][m-1]}); vis[i][m-1] = 1;}
for(int i=0;i<m;i++) { q.push({0, i , heightMap[0][i]}); vis[0][i] = 1;}
for(int i=0;i<m;i++) { q.push({n-1, i , heightMap[n-1][i]}); vis[n-1][i] = 1;}

int ans = 0;
while(q.size()){
cell now = q.top(); q.pop();
for(int i=0;i<4;i++){
int xx = now.x + dx[i], yy = now.y+dy[i];
if(xx<0 || yy<0 || xx>=n || yy>=m || vis[xx][yy]) continue;
ans += max(now.h - heightMap[xx][yy], 0);
vis[xx][yy] = 1;
q.push({xx, yy, max(heightMap[xx][yy], now.h)});
}
}
return ans;
}
};
View Code

#### 402. 移掉K位数字

class Solution {
public:
string removeKdigits(string num, int k) {
stack <int> stk;
for(int i=0;i<num.size();i++){
while(k && stk.size() && stk.top() > num[i]) {
stk.pop();
k-=1;
}
stk.push(num[i]);
}
while(k && stk.size()){ stk.pop();k--;}
string ans;
while(stk.size()) {ans += stk.top(); stk.pop();}
if(ans.size()==0) return "0";
reverse(ans.begin(), ans.end());
int i=0;
while(ans[i]=='0') i++;
if(ans.substr(i).size()==0) return "0";
return ans.substr(i);
}
};
View Code

posted @ 2020-07-20 21:38  樱花庄的龙之介大人  阅读(101)  评论(0编辑  收藏