zoukankan      html  css  js  c++  java
  • LeetCode 904. Fruit Into Baskets

    原题链接在这里:

    题目:

    In a row of trees, the i-th tree produces fruit with type tree[i].

    You start at any tree of your choice, then repeatedly perform the following steps:

    1. Add one piece of fruit from this tree to your baskets.  If you cannot, stop.
    2. Move to the next tree to the right of the current tree.  If there is no tree to the right, stop.

    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.

    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.

    What is the total amount of fruit you can collect with this procedure?

    Example 1:

    Input: [1,2,1]
    Output: 3
    Explanation: We can collect [1,2,1].
    

    Example 2:

    Input: [0,1,2,2]
    Output: 3
    Explanation: We can collect [1,2,2].
    If we started at the first tree, we would only collect [0, 1].
    

    Example 3:

    Input: [1,2,3,2,2]
    Output: 4
    Explanation: We can collect [2,3,2,2].
    If we started at the first tree, we would only collect [1, 2].
    

    Example 4:

    Input: [3,3,3,1,2,1,1,2,3,3,4]
    Output: 5
    Explanation: We can collect [1,2,1,1,2].
    If we started at the first tree or the eighth tree, we would only collect 4 fruits.

    Note:

    1. 1 <= tree.length <= 40000
    2. 0 <= tree[i] < tree.length

    题解:

    The question is asking for longest subarray that contains at most 2 distinct interger.

    It is similar to Longest Substring with At Most Two Distinct Characters.

    Have two pointers. When runner gets to index i, if its count in map is 0, then it is a new distinct integer.

    if the count of distinct integers is larger than 2, it is invalid, move walker to make it valid.

    Then maintain the longest.

    Time Complexity: O(n). n = tree.length.

    Space: O(n).

    AC Java: 

     1 class Solution {
     2     public int totalFruit(int[] tree) {
     3         if(tree == null || tree.length == 0){
     4             return 0;
     5         }
     6         
     7         int res = 0;
     8         int [] map = new int[tree.length];
     9         int walker = 0;
    10         int runner = 0;
    11         int sum = 0;
    12         
    13         while(runner < tree.length){
    14             if(map[tree[runner++]]++ == 0){
    15                 sum++;
    16             }
    17             
    18             while(sum > 2){
    19                 if(map[tree[walker++]]-- == 1){
    20                     sum--;
    21                 }
    22             }
    23             
    24             res = Math.max(res, runner-walker);
    25         }
    26         
    27         return res;
    28     }
    29 }
  • 相关阅读:
    Delphi的类和对象(九)- 类运算符as、is
    delphi中as,is关键字是如何工作的?
    delphi 中 as 和 is 的使用
    甘超波:NLP发问技巧
    甘超波:NLP如何挖掘信念
    甘超波:NLP自我价值感
    甘超波:NLP次感元
    甘超波:NLP前提假设之每个人都有足够资源,能达成成功的资源
    甘超波:NLP十二条前提假设之重复旧的行为,只会得到旧的结果
    甘超波:NLP十二条前提假设之诺要求知、必须行动
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11397676.html
Copyright © 2011-2022 走看看