package leetcode;

import java.util.Arrays;
import java.util.Stack;

/**
 * https://leetcode.com/problems/daily-temperatures/
 */
public class Problem739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] result = new int[temperatures.length];
        Stack<Integer> stack = new Stack<>();
        int j = 1;
        for (int i = 0; i < temperatures.length - 1; i++) {
            if (temperatures[i] < temperatures[j]) {
                result[i] = j - i;
            } else {
                stack.push(i);
            }
        }

//        int i = 0;
//        int j = 1;
//        int count = 1;
//        while (i < temperatures.length) {
//            if (j == temperatures.length) {
//                count = 1;
//                i++;
//                j = i + 1;
//                continue;
//            }
//            if (temperatures[i] < temperatures[j++]) {
//                result[i++] = count;
//                count = 1;
//                j = i + 1;
//            } else {
//                count++;
//            }
//        }
        return result;
    }

    public static void main(String[] args) {
        Problem739 prob = new Problem739();
        System.out.println(Arrays.toString(prob.dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73}))); // [1, 1, 4, 2, 1, 1, 0, 0]
//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int[]{73, 73}))); // [0, 0]
//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int[]{73, 72, 71, 70}))); // [0, 0, 0, 0]
//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int[]{55, 38, 53, 81, 61, 93, 97, 32, 43, 78}))); // [3,1,1,2,1,1,0,1,1,0]
    }
}

@@ -16,33 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -280,35 +280,16 @@
 ck%3C%3E();%0A
-        int j = 1;%0A
         
@@ -331,12 +331,8 @@
 ngth
- - 1
 ; i+
@@ -356,41 +356,23 @@
 if (
-temperatures%5Bi%5D %3C temperatures%5Bj%5D
+stack.isEmpty()
 ) %7B%0A
@@ -391,25 +391,20 @@
     
-result%5Bi%5D = j - i
+stack.add(i)
 ;%0A  
@@ -441,16 +441,24 @@
     
+ int j =
  stack.p
 ush(
@@ -457,13 +457,12 @@
 ck.p
-ush(i
+eek(
 );%0A 
@@ -472,94 +472,8 @@
     
-    %7D%0A        %7D%0A%0A//        int i = 0;%0A//        int j = 1;%0A//        int count = 1;%0A//
     
@@ -483,20 +483,16 @@
  while (
-i %3C 
 temperat
@@ -499,41 +499,13 @@
 ures
-.length) %7B%0A//            if (j ==
+%5Bj%5D %3C
  tem
@@ -517,21 +517,15 @@
 ures
-.length
+%5Bi%5D
 ) %7B%0A
-//
     
@@ -540,102 +540,64 @@
     
-count = 1;%0A//                i++;%0A//                j = i + 1;%0A//                continue;%0A// 
+    result%5Bj%5D = i - j;%0A                    stack.pop();%0A
     
@@ -595,36 +595,32 @@
 p();%0A           
-%7D%0A//
             if (
@@ -616,56 +616,31 @@
     
-   
 if (
-temperatures%5Bi%5D %3C temperatures%5Bj++%5D
+stack.isEmpty()
 ) %7B%0A
-//
     
@@ -655,784 +655,177 @@
     
-result%5Bi++%5D = count;%0A//                count = 1;%0A//                j = i + 1;%0A//            %7D else %7B%0A//                count++;%0A//            %7D%0A//        %7D%0A        return result;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem739 prob = new Problem739();%0A        System.out.println(Arrays.toString(prob.dailyTemperatures(new int%5B%5D%7B73, 74, 75, 71, 69, 72, 76, 73%7D))); // %5B1, 1, 4, 2, 1, 1, 0, 0%5D%0A//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int%5B%5D%7B73, 73%7D))); // %5B0, 0%5D%0A//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int%5B%5D%7B73, 72, 71, 70%7D))); // %5B0, 0, 0, 0%5D%0A//        System.out.println(Arrays.toString(prob.dailyTemperatures(new int%5B%5D%7B55, 38, 53, 81, 61, 93, 97, 32, 43, 78%7D))); // %5B3,1,1,2,1,1,0,1,1,0%5D
+        break;%0A                    %7D%0A                    j = stack.peek();%0A                %7D%0A                stack.add(i);%0A            %7D%0A        %7D%0A        return result;
 %0A   

Solve problem 739