package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/max-consecutive-ones-iii/
 */
public class Problem1004 {
    public int longestOnes(int[] A, int K) {
        List<NumberCount> list = new ArrayList<>();
        for (int i = 0; i < A.length; i++) {
            if (A[i] == 0) {
                if (list.isEmpty()) {
                    list.add(new NumberCount(0, 1));
                    continue;
                }
                NumberCount nc = list.get(list.size() - 1);
                if (nc.number == A[i]) {
                    nc.count++;
                } else {
                    list.add(new NumberCount(0, 1));
                }
            } else {
                if (list.isEmpty()) {
                    list.add(new NumberCount(1, 1));
                    continue;
                }
                NumberCount nc = list.get(list.size() - 1);
                if (nc.number == A[i]) {
                    nc.count++;
                } else {
                    list.add(new NumberCount(1, 1));
                }
            }
        }
        int answer = 0;
        for (int i = 0; i < list.size(); i++) {
            int max = 0;
            int count = K;
            int j = i;
            while (j < list.size()) {
                NumberCount nc = list.get(j);
                if (nc.number == 0) {
                    if (count - nc.count >= 0) {
                        count -= nc.count;
                        max += nc.count;
                    } else {
                        max += count;
                        break;
                    }
                } else {
                    max += nc.count;
                }
                j++;
            }
            answer = Math.max(answer, max);
        }
        for (int i = list.size() - 1; i >= 0; i--) {
            int max = 0;
            int count = K;
            int j = i;
            while (j >= 0) {
                NumberCount nc = list.get(j);
                if (nc.number == 0) {
                    if (count - nc.count >= 0) {
                        count -= nc.count;
                        max += nc.count;
                    } else {
                        max += count;
                        break;
                    }
                } else {
                    max += nc.count;
                }
                j--;
            }
            answer = Math.max(answer, max);
        }
        return answer;
    }

    private static class NumberCount {
        private final int number;
        private int count;

        public NumberCount(int number, int count) {
            this.number = number;
            this.count = count;
        }
    }
}

@@ -16,60 +16,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -163,1029 +163,156 @@
     
-List%3CNumberCount%3E list = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C A.length; i++) %7B%0A            if (A%5Bi%5D == 0) %7B%0A                if (list.isEmpty()) %7B%0A                    list.add(new NumberCount(0, 1));%0A                    continue;%0A                %7D%0A                NumberCount nc = list.get(list.size() - 1);%0A                if (nc.number == A%5Bi%5D) %7B%0A                    nc.count++;%0A                %7D else %7B%0A                    list.add(new NumberCount(0, 1));%0A                %7D%0A            %7D else %7B%0A                if (list.isEmpty()) %7B%0A                    list.add(new NumberCount(1, 1));%0A                    continue;%0A                %7D%0A                NumberCount nc = list.get(list.size() - 1);%0A                if (nc.number == A%5Bi%5D) %7B%0A                    nc.count++;%0A                %7D else %7B%0A                    list.add(new NumberCount(1, 1));%0A                %7D%0A            %7D%0A        %7D%0A        int answer = 0;%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            int max = 0;%0A            int count = K
+int answer = 0;%0A        int left = 0;%0A        int right = 0;%0A        while (right %3C A.length) %7B%0A            if (A%5Bright%5D == 0) %7B%0A                K--
 ;%0A  
@@ -317,34 +317,25 @@
             
-int j = i;
+%7D
 %0A           
@@ -346,115 +346,34 @@
 le (
-j %3C list.size()) %7B%0A                NumberCount nc = list.get(j);%0A                if (nc.number == 0) %7B%0A    
+K %3C 0 && left %3C= right) %7B%0A
     
@@ -388,34 +388,25 @@
     if (
-count - nc.count %3E
+A%5Bleft%5D =
 = 0) %7B%0A 
@@ -428,1282 +428,175 @@
     
-    count -= nc.count;%0A                        max += nc.count;%0A                    %7D else %7B%0A                        max += count;%0A                        break;%0A                    %7D%0A                %7D else %7B%0A                    max += nc.count;%0A                %7D%0A                j++;%0A            %7D%0A            answer = Math.max(answer, max);%0A        %7D%0A        for (int i = list.size() - 1; i %3E= 0; i--) %7B%0A            int max = 0;%0A            int count = K;%0A            int j = i;%0A            while (j %3E= 0) %7B%0A                NumberCount nc = list.get(j);%0A                if (nc.number == 0) %7B%0A                    if (count - nc.count %3E= 0) %7B%0A                        count -= nc.count;%0A                        max += nc.count;%0A                    %7D else %7B%0A                        max += count;%0A                        break;%0A                    %7D%0A                %7D else %7B%0A                    max += nc.count;%0A                %7D%0A                j--;%0A            %7D%0A            answer = Math.max(answer, max);%0A        %7D%0A        return answer;%0A    %7D%0A%0A    private static class NumberCount %7B%0A        private final int number;%0A        private int count;%0A%0A        public NumberCount(int number, int count) %7B%0A            this.number = number;%0A            this.count = count;%0A        %7D
+K++;%0A                %7D%0A                left++;%0A            %7D%0A            right++;%0A            answer = Math.max(answer, right - left);%0A        %7D%0A        return answer;
 %0A   

Update problem 1004