Description
Given a list of daily temperatures, produce a list that, for each day in the input, tells you how many days you would have to wait until a warmer temperature. If there is no future day for which this is possible, put 0 instead.
For example, given the list temperatures = [73, 74, 75, 71, 69, 72, 76, 73], your output should be [1, 1, 4, 2, 1, 1, 0, 0].
Note
The length of temperatures will be in the range [1, 30000]. Each temperature will be an integer in the range [30, 100].
Solution 1(C++)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int len=temperatures.size();
vector<int> res(len, 0);
stack<int> s;
for(int i=0; i<len; i++){
while(!s.empty() && temperatures[i]>temperatures[s.top()]){
int index = s.top();
s.pop();
res[index] = i - index;
}
s.push(i);
}
return res;
}
};
Solution 2(C++)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
vector<int> res(temperatures.size());
for (int i = temperatures.size() - 1; i >= 0; --i) {
int j = i+1;
while (j < temperatures.size() && temperatures[j] <= temperatures[i]) {
if (res[j] > 0) j = res[j] + j;
else j = temperatures.size();
}
if (j < temperatures.size()) res[i] = j - i;
}
return res;
}
};
Solution 3(C++)
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int len=temperatures.size();
vector<int> res(len, INT_MAX);
unordered_map<int, int> m;
for(int i=len-1; i>-1; i--){
m[temperatures[i]] = i;
int n=0;
for(n=temperatures[i]+1; n<101; n++){
if(m.find(n)!=m.end()) res[i]=min(res[i], m[n]-i);
}
if(res[i]==INT_MAX) res[i]=0;
}
return res;
}
};
算法分析
其实题目就是要找到比当前temperatures[i]最近的大于它的数的坐标。
解法一: 如果一个子序列是递减的:i0,i1,i2,…,in,那么会一直往后查找,直到出现一个增加的数j,(这里的i与j都是索引)。然而这个变大的数也仅仅比之前的数in大。in对应的res[in]=1。如果j大于in-1,那么res[in-1]=2。依次类推。直到ik停止。那么ik~in的数对应res的值都确定了。剩下没有确定的自然就是i0~ik与新来的j。
注意: 这里j < ik。说明新加入的j还是满足递减的子序列的。所以没有确定的元素可以组成新的子序列,然后在temperatures继续往后遍历。
遇到这种需要储存“发生过”的信息,然后经过某些条件对储存信息进行更新,再加入新的信息,这种结构可以考虑stack与queue。由于这里“对储存信息进行更新”的那些信息优先选择是后进来的信息,所以符合stack的“先进先出”的特点,故而用stack比较好。
构建一个stack,用来储存递减元素的索引,当有新的元素要判断时,新元素与stack.top()比较,如果大于top,让res[stack.top()]=i-stack.top(),然后stack.pop()。重复以上过程,直到新元素小于stack.top()。将新元素压入栈。遍历完整个数组即可。
解法二: 参考:LeetCode-55. Jump Game 两者有一些地方相似。这个相似的地方比较抽象,我这里就不多说,可以多体会体会。这个解法是参考别人的答案:[C++] Clean code with explanation: O(n) time and O(1) space (beats 99.13%)。 别人答案写的很详细,我这里做一个备份:
We compute the result in reverse order. Given temperatures = [73, 74, 75, 71, 69, 72, 76, 73], suppose that we already obtain number of days for i >= 3: res = [?, ?, ?, 2, 1, 1, 0, 0] where ? denotes values to be determined. In order to find the number of days for i = 2, we proceed as follows:
- Let j = i + 1, i.e., j = 3. If temperatures[i] < temperatures[j], res[i] = 1;
- Otherwise, temperatures[i] >= temperatures[j]. we examine the value of res[j], which is equal to 2, which means that the most recent day with higher temperature than temperatures[j] in the future is 2 days away. Since temperatures[i] >= temperatures[j], the most recent day with higher temperature than temperatures[i] should be at least 2 days away. Therefore, we can skip some days and arrive directly at day j + res[j]. This process continues until we find higher temperature or we know we should stop as descibed in the third point.
- If temperatures[j] == 0, we shoud directly set res[i] to 0 since we know we cannot find higher temperature in the future.
解法三: 考虑到温度的变化范围仅仅为:[30,100],利用map储存temperatures中出现过温度的索引,当出现新的温度,可以在map中查找比该温度大的温度的索引。为了避免重复元素出现,可以从后往前遍历temperatures,并向map中添加新的索引。
程序分析
略。
本文介绍了一种用于预测未来温暖天气的算法。通过分析每日气温数据,该算法能够为每一天计算出直至下一次气温升高的天数。文章提供了三种不同的C++实现方案,并详细解释了每种方法的工作原理。
2257

被折叠的 条评论
为什么被折叠?



