Daily Temperatures LeetCode solution in Java
1class Solution {2public int[] dailyTemperatures(int[] temperatures) {3HashMap<Integer, Integer> hm = new HashMap<>();4Stack<Integer> st = new Stack<>();5for (int i = 0; i < temperatures.length; i++) {6while (st.size() > 0 && temperatures[i] > temperatures[st.peek()]) {7hm.put(st.pop(), i);8}9st.push(i);10}11int[] ans = new int[temperatures.length];12for (int i = 0; i < temperatures.length; i++) {13if (hm.containsKey(i)) {14ans[i] = hm.get(i) - i;15} else {16ans[i] = 0;17}18}19return ans;20}21}