Doubt in space and Time Complexity

I am solving this question on leetcode…
739. Daily Temperatures
Although i have solved it… correctly…
This is my solution…


I have doubt in space and Time complexity of this code…
I am not able to determine confidently the space and time complexity.
Could you please explain the space and time complexity

Space complexity is clearly O(n) as you are just storing the elements.
Time complexity is also O(n) as every element is popped and pushed at most once into the stack.

Yes… But in for loop we are excuting one while loop… does that not affect the time complexity…??

Note that the time complexity is not affected by the number of loops.

The inner loop overall runs for ‘n’ instances, that is what matters.

If the inner while loop ran for O(n) for every i of outer loop, then it would’ve been O(n^2).