-
Notifications
You must be signed in to change notification settings - Fork 4
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
9 changed files
with
144 additions
and
0 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
Empty file.
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,27 @@ | ||
# 42. 接雨水 | ||
|
||
需要考虑前两个 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int trap(vector<int>& v) { | ||
int res = 0; | ||
vector<int>s; | ||
for(int i=0;i<v.size();i++){ | ||
while(!s.empty()&&v[s.back()]<=v[i]){ | ||
int idx = s.back(); | ||
s.pop_back(); | ||
if(!s.empty()){ | ||
int left = s.back(); | ||
int width = i - left - 1; | ||
int height = min(v[left],v[i]) - v[idx]; | ||
res += width * height; | ||
} | ||
} | ||
s.push_back(i); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
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,28 @@ | ||
# 496. 下一个更大元素 I | ||
|
||
从代码来看,v1的作用就是用来映射一次,增加了一层映射 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> nextGreaterElement(vector<int>&v1,vector<int>&v2) { | ||
vector<int>res(v1.size(),-1); | ||
unordered_map<int, int> m; // key:下标元素,value:下标 | ||
for (int i = 0; i < v1.size(); i++) { | ||
m[v1[i]] = i; | ||
} | ||
vector<int>s; | ||
for(int i=0;i<v2.size();i++){ | ||
while(!s.empty()&&v2[s.back()]<v2[i]){ | ||
int idx = s.back(); | ||
s.pop_back(); | ||
if (m.find(v2[idx])!=m.end()){ | ||
res[m[v2[idx]]]=v2[i]; | ||
} | ||
} | ||
s.push_back(i); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
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,21 @@ | ||
# 503.下一个更大元素II | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> nextGreaterElements(vector<int>& v) { | ||
vector<int>res(v.size(),-1); | ||
vector<int>s; | ||
int n = v.size(); | ||
for(int i=0;i<n*2;i++){ | ||
while(!s.empty()&&v[s.back()]<v[i%n]){ | ||
int idx = s.back(); | ||
s.pop_back(); | ||
res[idx]=v[i%n]; | ||
} | ||
s.push_back(i%n); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
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,24 @@ | ||
# 739. 每日温度 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
vector<int> dailyTemperatures(vector<int>& v) { | ||
vector<int>res(v.size(),0); | ||
// 1 | ||
// 1 1 | ||
// 1 1 1 | ||
vector<int>s; | ||
for(int i=0;i<v.size();i++){ | ||
while(!s.empty()&&v[s.back()]<v[i]) | ||
{ | ||
int idx=s.back(); | ||
s.pop_back(); | ||
res[idx]=i-idx; | ||
} | ||
s.push_back(i); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
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,36 @@ | ||
# 84.柱状图中最大的矩形 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int largestRectangleArea(vector<int>& v) { | ||
int res = 0; | ||
vector<int>rightfirstsmallerthanmine(v.size(),v.size()); | ||
vector<int>leftffirstsmallerthanmine(v.size(),-1); | ||
vector<int>s; | ||
for(int i=0;i<v.size();i++){ | ||
while(!s.empty()&&v[s.back()]>v[i]){ | ||
int idx = s.back(); | ||
s.pop_back(); | ||
rightfirstsmallerthanmine[idx]=i; | ||
} | ||
s.push_back(i); | ||
} | ||
s.clear(); | ||
for(int i=v.size()-1;i>=0;i--){ | ||
while(!s.empty()&&v[s.back()]>v[i]){ | ||
int idx = s.back(); | ||
s.pop_back(); | ||
leftffirstsmallerthanmine[idx]=i; | ||
} | ||
s.push_back(i); | ||
} | ||
for(int i=0;i<v.size();i++){ | ||
int width = rightfirstsmallerthanmine[i] - leftffirstsmallerthanmine[i]-1; | ||
int height = v[i]; | ||
res = max(res, width*height); | ||
} | ||
return res; | ||
} | ||
}; | ||
``` |
Empty file.
Empty file.