forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
67 lines (51 loc) · 1.66 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// Source : https://leetcode.com/problems/daily-temperatures/description/
/// Author : liuyubobobo
/// Time : 2018-08-28
#include <iostream>
#include <vector>
using namespace std;
/// Binary Search the higher temperature
/// Time Complexity: O(n*t*logn) where n is the number of data and t is the max temperature
/// Space Complexity: O(n)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<vector<int>> records;
for(int i = 0 ; i <= 100 ; i ++){
vector<int> empty_vec;
records.push_back(empty_vec);
}
for(int i = 0 ; i < temperatures.size() ; i ++)
records[temperatures[i]].push_back(i);
vector<int> res;
for(int i = 0 ; i < temperatures.size() ; i ++){
int future = getWarmerDay(temperatures[i], i, records);
if(future == -1)
res.push_back(0);
else
res.push_back(future - i);
}
return res;
}
private:
int getWarmerDay(int t, int curDay, const vector<vector<int>>& records){
int res = INT_MAX;
for(int i = t + 1 ; i <= 100 ; i ++){
vector<int>::const_iterator iter = upper_bound(records[i].begin(), records[i].end(), curDay);
if(iter != records[i].end())
res = min(res, *iter);
}
return res == INT_MAX ? -1 : res;
}
};
void printVec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> vec = {73, 74, 75, 71, 69, 72, 76, 73};
printVec(Solution().dailyTemperatures(vec));
// 1 1 4 2 1 1 0 0
return 0;
}