Created
April 24, 2021 19:35
-
-
Save SamirPaulb/18f2ce373b756e1d62c8d0043afaa3c3 to your computer and use it in GitHub Desktop.
determine how many days required to wait for the next warmer day by O(n) from a list
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
# Function to determine how many days required to wait for the next warmer day | |
def dailyTemperatures(T): | |
n = len(T) | |
# To store the answer | |
daysOfWait = [-1] * n | |
s = [] | |
# Traverse all the temperatures | |
for i in range(n): | |
# Check if current index is the | |
# next warmer temperature of | |
# any previous indexes | |
while(len(s) != 0 and | |
T[s[-1]] < T[i]): | |
daysOfWait[s[-1]] = i - s[-1] | |
# Pop the element | |
s.pop(-1) | |
# Push the current index | |
s.append(i) | |
# Print waiting days | |
for i in range(n): | |
print(daysOfWait[i], end = " ") | |
# Driver Code | |
# Given temperatures | |
arr = [ 73, 74, 75, 71, 69, 72, 76, 73 ] | |
# Function call | |
dailyTemperatures(arr) | |
# This code is contributed by Shivam Singh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Function to determine how many days required to wait for the next warmer day but time complexity must be O(n) from a list