• 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 }
  • 相关阅读:
    在VMware中为Red Hat配置静态ip并可访问网络-Windows下的VMware
    03-nginx虚拟主机配置
    解决nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
    02-nginx信号量
    RedHat Linux设置yum软件源为本地ISO
    01-nginx介绍及编译安装
    Linux.负载均衡
    01-MySQL优化大的思路
    10 华电内部文档搜索系统 search02
    10 华电内部文档搜索系统 search03
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11397676.html
Copyright © 2020-2023  润新知