Daily Temperatures Solutions in C++
Number 739
Difficulty Medium
Acceptance 63.4%
Link LeetCode
Other languages Go
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// 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);elseres.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 0return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/daily-temperatures/description//// Author : liuyubobobo/// Time : 2018-08-28#include <iostream>#include <vector>using namespace std;/// Reverse Process/// Time Complexity: O(n*t) where n is the number of data and t is the max temperature/// Space Complexity: O(t)class Solution {public:vector<int> dailyTemperatures(vector<int>& temperatures) {vector<int> next(101, -1);vector<int> res(temperatures.size(), -1);for(int i = temperatures.size() - 1; i >= 0 ; i --){int ret = INT_MAX;for(int j = temperatures[i] + 1; j <= 100; j ++)if(next[j] != -1 && next[j] < ret)ret = next[j];res[i] = ret == INT_MAX ? 0 : ret - i;next[temperatures[i]] = i;}return 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 0return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/daily-temperatures/description//// Author : liuyubobobo/// Time : 2018-08-28#include <iostream>#include <vector>#include <stack>using namespace std;/// Using Stack/// Time Complexity: O(n)/// Space Complexity: O(n)class Solution {public:vector<int> dailyTemperatures(vector<int>& temperatures) {vector<int> res(temperatures.size(), 0);stack<int> stack;for(int i = temperatures.size() - 1; i >= 0 ; i --){while(!stack.empty() && temperatures[stack.top()] <= temperatures[i])stack.pop();if(!stack.empty())res[i] = stack.top() - i;stack.push(i);}return 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 0return 0;}