package leetcode;

/**
 * https://leetcode.com/problems/longest-well-performing-interval/
 */
public class Problem1124 {
    public int longestWPI(int[] hours) {
        int answer = 0;
        for (int i = 0; i < hours.length; i++) {
            int tiring = 0;
            int nonTiring = 0;
            for (int j = i; j < hours.length; j++) {
                if (hours[j] <= 8) {
                    nonTiring++;
                } else {
                    tiring++;
                }
                if (tiring > nonTiring) {
                    answer = Math.max(answer, j - i + 1);
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1124 prob = new Problem1124();
        System.out.println(prob.longestWPI(new int[]{9,9,6,0,6,6,9})); // 3
        System.out.println(prob.longestWPI(new int[]{8,6,9,9,10,9,0,6,6,9})); // 9
    }
}

@@ -12,16 +12,66 @@
 tcode;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Stack;%0A%0A
 /**%0A * h
@@ -220,19 +220,43 @@
  int
- answer = 0
+%5B%5D sums = new int%5Bhours.length + 1%5D
 ;%0A  
@@ -278,14 +278,15 @@
 i = 
-0
+1
 ; i %3C
+=
  hou
@@ -319,59 +319,207 @@
     
-int tiring = 0
+sums%5Bi%5D = sums%5Bi - 1%5D + (hours%5Bi - 1%5D %3C= 8 ? -1 : 1)
 ;%0A     
+   %7D%0A
        
-int nonTiring = 0;%0A    
+ System.out.println(Arrays.toString(sums));%0A        // Monotone decreasing stack.%0A        Stack%3CInteger%3E stack = new Stack%3C%3E();%0A
     
@@ -535,18 +535,19 @@
 int 
-j = i; j
+i = 0; i
  %3C
+=
  hou
@@ -561,19 +561,15 @@
 th; 
-j
+i
 ++) %7B%0A
-    
     
@@ -584,21 +584,55 @@
 if (
-hours%5Bj%5D %3C= 8
+stack.isEmpty() %7C%7C sums%5Bstack.peek()%5D %3E sums%5Bi%5D
 ) %7B%0A
@@ -651,23 +651,21 @@
     
-    nonTiring++
+stack.push(i)
 ;%0A  
@@ -678,115 +678,198 @@
     
+%7D%0A
     
-%7D else %7B%0A                    tiring++;%0A                %7D%0A                if (tiring %3E nonTiring) %7B%0A    
+    %7D%0A        System.out.println(stack);%0A        int answer = 0;%0A        for (int i = hours.length; i %3E= 0; i--) %7B%0A            while (!stack.empty() && sums%5Bstack.peek()%5D %3C sums%5Bi%5D) %7B%0A
     
@@ -910,17 +910,24 @@
 er, 
-j - i + 1
+i - stack.peek()
 );%0A 
@@ -933,33 +933,44 @@
                 
-%7D
+stack.pop();
 %0A            %7D%0A 
@@ -1095,24 +1095,26 @@
 blem1124();%0A
+//
         Syst

Update problem 1124