All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0701_0800.s0739_daily_temperatures.Solution.cpp Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #Top_100_Liked_Questions #Array #Stack #Monotonic_Stack #Programming_Skills_II_Day_6
// #Big_O_Time_O(n)_Space_O(n) #2024_05_21_Time_103_ms_(91.31%)_Space_100.9_MB_(86.96%)

#include 

class Solution {
public:
    std::vector dailyTemperatures(const std::vector& temperatures) {
        int n = temperatures.size();
        std::vector sol(n, 0);
        for (int i = n - 2; i >= 0; --i) {
            int j = i + 1;
            while (j < n) {
                if (temperatures[i] < temperatures[j]) {
                    sol[i] = j - i;
                    break;
                } else {
                    if (sol[j] == 0) {
                        break;
                    }
                    j += sol[j];
                }
            }
        }
        return sol;
    }
};




© 2015 - 2025 Weber Informatics LLC | Privacy Policy