Suresh Gudibanda’s Post

LeetCode Problem Solved | #739 : Daily Temperatures Today I solved 739. Daily Temperatures - a classic problem that beautifully demonstrates the power of the Monotonic Stack pattern. 🧩 Problem Summary Given an array of daily temperatures, return an array such that for each day, you tell how many days you have to wait until a warmer temperature. If there is no future warmer day, return 0 for that day. Example: Input: [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] 💡 Solution approach : ✔ Store indices (not temperatures). ✔ Maintain decreasing order of temperatures in the stack. ✔ When a warmer temperature appears, pop elements and calculate the difference in indices. 💻 I’ve added my Java solution in the comments below. #LeetCode #DataStructures #Java #MonotonicStack #DSA #ProblemSolving #Stack

  • graphical user interface, text, application, chat or text message

class Solution {     public int[] dailyTemperatures(int[] temperatures) {         Stack<Integer> stack = new Stack<>();         int[] nums = new int[temperatures.length];         for (int i = 0; i < temperatures.length; i++) {             while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {                 int index = stack.pop();                 nums[index] = i - index;             }             stack.push(i);         }         return nums;     } }

Like
Reply

To view or add a comment, sign in

Explore content categories