package leetcode;

import java.util.Arrays;
import java.util.List;

/**
 * https://leetcode.com/problems/minimum-time-to-collect-all-apples-in-a-tree/
 */
public class Problem1443 {
    public int minTime(int n, int[][] edges, List<Boolean> hasApple) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem1443 prob = new Problem1443();
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,true,false,true,true,false))); // 8
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,true,false,false,true,false))); // 6
        System.out.println(prob.minTime(
            7,
            new int[][]{{0,1},{0,2},{1,4},{1,5},{2,3},{2,6}},
            Arrays.asList(false,false,false,false,false,false,false))); // 0
    }
}

@@ -12,16 +12,44 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -286,191 +286,210 @@
     
-// TODO%0A        return 0
+List%3CInteger%3E%5B%5D tree = new List%5Bn%5D
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem1443 prob = new Problem1443();%0A        System.out.println(prob.minTime(%0A            7,%0A   
+for (int i = 0; i %3C tree.length; i++) %7B%0A            tree%5Bi%5D = new ArrayList%3C%3E();%0A        %7D%0A        for (int%5B%5D edge : edges) %7B%0A            int from = edge%5B0%5D;%0A
     
@@ -497,174 +497,331 @@
     
-new
+  
  int
-%5B%5D%5B%5D%7B%7B0,1%7D,%7B0,2%7D,%7B1,4%7D,%7B1,5%7D,%7B2,3%7D,%7B2,6%7D%7D,%0A            Arrays.asList(false,false,true,false,true,true,false))); // 8%0A        System.out.println(prob.minTime(%0A 
+ to = edge%5B1%5D;%0A            tree%5Bfrom%5D.add(to);%0A        %7D%0A        IntRef answer = new IntRef();%0A        minTime(tree, hasApple, 0, answer);%0A        return Math.max(0, answer.value - 1);%0A    %7D%0A%0A    private static class IntRef %7B%0A        private int value;%0A    %7D%0A%0A    private static boolean minTime(List%3CInteger%3E%5B%5D tree,%0A
     
@@ -818,36 +818,32 @@
 tree,%0A          
- 7,%0A
             new 
@@ -842,327 +842,473 @@
     
-new int%5B%5D%5B%5D%7B%7B0,1%7D,%7B0,2%7D,%7B1,4%7D,%7B1,5%7D,%7B2,3%7D,%7B2,6%7D%7D,%0A            Arrays.asList(false,false,true,false,false,true,false))); // 6%0A        System.out.println(prob.minTime(%0A            7,%0A            new int%5B%5D%5B%5D%7B%7B0,1%7D,%7B0,2%7D,%7B1,4%7D,%7B1,5%7D,%7B2,3%7D,%7B2,6%7D%7D,%0A            Arrays.asList(false,false,false,false,false,false,false))); // 0
+             List%3CBoolean%3E hasApple,%0A                                   int node,%0A                                   IntRef time) %7B%0A        boolean found = hasApple.get(node);%0A        for (int children : tree%5Bnode%5D) %7B%0A            time.value++;%0A            found %7C= minTime(tree, hasApple, children, time);%0A        %7D%0A        if (found) %7B%0A            time.value++;%0A        %7D else %7B%0A            time.value = Math.max(0, time.value - 1);%0A        %7D%0A        return found;
 %0A   

Solve problem 1443