Monthly Archives: March 2023

C# || Daily Temperatures – How To Find The Number Of Days Until Warmer Temperature Using C#

The following is a module with functions which demonstrates how to find the number of days until warmer temperature using C#.


1. Daily Temperatures – Problem Statement

Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead.

Example 1:


Input: temperatures = [73,74,75,71,69,72,76,73]
Output: [1,1,4,2,1,1,0,0]

Example 2:


Input: temperatures = [30,40,50,60]
Output: [1,1,1,0]

Example 3:


Input: temperatures = [30,60,90]
Output: [1,1,0]


2. Daily Temperatures – Solution

The following is a solution which demonstrates how to find the number of days until warmer temperature.

This solution uses the monotonic stack approach.

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:


[1,1,4,2,1,1,0,0]
[1,1,1,0]
[1,1,0]