Imports System
Imports System.Collections.Generic
Module WarmerDays
Sub PrintList(values As List(Of Integer))
For Each n In values
Console.Write(n & " ")
Next
Console.WriteLine()
End Sub
Function NumberOfDaysToWait(temperatures As List(Of Integer)) As List(Of Integer)
Dim size As Integer = temperatures.Count
Dim result As New List(Of Integer)(New Integer(size - 1) {})
Dim stack As New Stack(Of Integer)()
For i As Integer = 0 To size - 1
While stack.Count > 0 AndAlso temperatures(i) > temperatures(stack.Peek())
Dim idx As Integer = stack.Pop()
result(idx) = i - idx
End While
stack.Push(i)
Next
While stack.Count > 0
result(stack.Pop()) = 0
End While
Return result
End Function
Sub Main()
Dim temperatures As New List(Of Integer) From {82, 84, 81, 58, 85, 89, 75, 71}
' 82 -> 84 = 1
' 84 -> 81 -> 58 -> 85 = 3
' 81 -> 58 -> 85 = 2
' 58 -> 85 = 1
' 85 -> 89 = 1
' 89 -> 75 -> 71 = 0
' 75 -> 71 = 0
Dim result = NumberOfDaysToWait(temperatures)
PrintList(result)
End Sub
End Module
' run:
'
' 1 3 2 1 1 0 0 0
'