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 }
  • 相关阅读:
    postfix 邮件中继配置
    shell脚本网络流量实时查看
    zabbix配置邮件报警(第四篇)
    pptp服务故障
    Centos6.7 ELK日志系统部署
    rrdtool 实践
    Centos6.7安装Cacti教程
    Nagios事件机制实践
    Nrpe 插件安装教程
    如何查找一个命令由哪个rpm安装&&rpm 的相关查询方法
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11397676.html
Copyright © 2011-2022 走看看