package leetcode;

/**
 * https://leetcode.com/problems/max-consecutive-ones-iii/
 */
public class Problem1004 {
    public int longestOnes(int[] A, int K) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1004 prob = new Problem1004();
        System.out.println(prob.longestOnes(new int[]{1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0}, 2)); // 6
        System.out.println(prob.longestOnes(new int[]{0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1}, 3)); // 10
    }
}

@@ -12,16 +12,68 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -215,230 +215,2414 @@
     
-// TODO%0A        return 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1004 prob = new Problem1004();%0A        System.out.println(prob.longestOnes(new int%5B%5D%7B1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0%7D, 2)); // 6%0A
+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;%0A            int j = i;%0A            while (j %3C list.size()) %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%0A%0A        @Override%0A        public String toString() %7B%0A            return number + %22: %22 + count;%0A        %7D%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1004 prob = new Problem1004();%0A        System.out.println(prob.longestOnes(new int%5B%5D%7B1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0%7D, 2)); // 6%0A//        System.out.println(prob.longestOnes(new int%5B%5D%7B1, 1, 1%7D, 2)); // 3%0A//        System.out.println(prob.longestOnes(new int%5B%5D%7B0, 0, 0%7D, 3)); // 3%0A//        System.out.println(prob.longestOnes(new int%5B%5D%7B1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0%7D, 3)); // 10%0A//
     

Update problem 1004