|
| 1 | +package array; |
| 2 | + |
| 3 | +import java.util.Stack; |
| 4 | + |
| 5 | +/** |
| 6 | + * Created by gouthamvidyapradhan on 02/03/2019 |
| 7 | + * In a row of trees, the i-th tree produces fruit with type tree[i]. |
| 8 | + * |
| 9 | + * You start at any tree of your choice, then repeatedly perform the following steps: |
| 10 | + * |
| 11 | + * Add one piece of fruit from this tree to your baskets. If you cannot, stop. |
| 12 | + * Move to the next tree to the right of the current tree. If there is no tree to the right, stop. |
| 13 | + * Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop. |
| 14 | + * |
| 15 | + * You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each. |
| 16 | + * |
| 17 | + * What is the total amount of fruit you can collect with this procedure? |
| 18 | + * |
| 19 | + * |
| 20 | + * |
| 21 | + * Example 1: |
| 22 | + * |
| 23 | + * Input: [1,2,1] |
| 24 | + * Output: 3 |
| 25 | + * Explanation: We can collect [1,2,1]. |
| 26 | + * Example 2: |
| 27 | + * |
| 28 | + * Input: [0,1,2,2] |
| 29 | + * Output: 3 |
| 30 | + * Explanation: We can collect [1,2,2]. |
| 31 | + * If we started at the first tree, we would only collect [0, 1]. |
| 32 | + * Example 3: |
| 33 | + * |
| 34 | + * Input: [1,2,3,2,2] |
| 35 | + * Output: 4 |
| 36 | + * Explanation: We can collect [2,3,2,2]. |
| 37 | + * If we started at the first tree, we would only collect [1, 2]. |
| 38 | + * Example 4: |
| 39 | + * |
| 40 | + * Input: [3,3,3,1,2,1,1,2,3,3,4] |
| 41 | + * Output: 5 |
| 42 | + * Explanation: We can collect [1,2,1,1,2]. |
| 43 | + * If we started at the first tree or the eighth tree, we would only collect 4 fruits. |
| 44 | + * |
| 45 | + * |
| 46 | + * Note: |
| 47 | + * |
| 48 | + * 1 <= tree.length <= 40000 |
| 49 | + * 0 <= tree[i] < tree.length |
| 50 | + * |
| 51 | + */ |
| 52 | +public class FruitIntoBaskets { |
| 53 | + |
| 54 | + private int count = 0; |
| 55 | + private int max = 0; |
| 56 | + /** |
| 57 | + * Main method |
| 58 | + * @param args |
| 59 | + */ |
| 60 | + public static void main(String[] args) { |
| 61 | + int[] trees = {1, 0, 3, 4, 3}; |
| 62 | + System.out.println(new FruitIntoBaskets().totalFruit(trees)); |
| 63 | + } |
| 64 | + |
| 65 | + public int totalFruit(int[] tree) { |
| 66 | + int t1 = -1, t2 = -1; |
| 67 | + Stack<Integer> stack = new Stack<>(); |
| 68 | + for(int i : tree){ |
| 69 | + if(i == t1 || i == t2){ |
| 70 | + countAndMax(stack, i); |
| 71 | + } else { |
| 72 | + if(t1 == -1){ |
| 73 | + t1 = i; |
| 74 | + countAndMax(stack, i); |
| 75 | + } else if(t2 == -1){ |
| 76 | + t2 = i; |
| 77 | + countAndMax(stack, i); |
| 78 | + } else{ |
| 79 | + Stack<Integer> temp = new Stack<>(); |
| 80 | + count = 0; |
| 81 | + t1 = stack.pop(); |
| 82 | + countAndMax(temp, t1); |
| 83 | + while(!stack.isEmpty() && stack.peek() == t1){ |
| 84 | + countAndMax(temp, stack.pop()); |
| 85 | + } |
| 86 | + t2 = i; |
| 87 | + stack = temp; |
| 88 | + countAndMax(stack, i); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + return max; |
| 93 | + } |
| 94 | + |
| 95 | + private void countAndMax(Stack<Integer> stack, int i){ |
| 96 | + count++; |
| 97 | + stack.push(i); |
| 98 | + max = Math.max(max, count); |
| 99 | + } |
| 100 | +} |
0 commit comments