> 文章列表 > 【042】904. 水果成篮[滑动窗口]

【042】904. 水果成篮[滑动窗口]

【042】904. 水果成篮[滑动窗口]

你正在探访一家农场,农场从左到右种植了一排果树。这些树用一个整数数组 fruits 表示,其中 fruits[i] 是第 i 棵树上的水果 种类 。
你想要尽可能多地收集水果。然而,农场的主人设定了一些严格的规矩,你必须按照要求采摘水果:
你只有 两个 篮子,并且每个篮子只能装 单一类型 的水果。每个篮子能够装的水果总量没有限制。
你可以选择任意一棵树开始采摘,你必须从 每棵 树(包括开始采摘的树)上 恰好摘一个水果 。采摘的水果应当符合篮子中的水果类型。每采摘一次,你将会向右移动到下一棵树,并继续采摘。
一旦你走到某棵树前,但水果不符合篮子的水果类型,那么就必须停止采摘。
给你一个整数数组 fruits ,返回你可以收集的水果的 最大 数目。

https://leetcode.cn/problems/fruit-into-baskets/description/

/** Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.*/package com.huawei.prac;import java.util.HashMap;
import java.util.Map;public class Solution13th {public static void main(String[] args) {int[] fruits = {3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4};System.out.println(totalFruit(fruits)); // 5}/*** 904. 水果成篮[滑动窗口]* 找至多包含两种元素的最长子串,返回其长度* 1.map存放不同元素及其数量* 2.left = right - 1; left从前往后** @param fruits 整数数组* @return 最大数目*/public static int totalFruit(int[] fruits) {int maxTree = 0;int left = 0;int right = 0;Map<Integer, Integer> map = new HashMap<>();while (right < fruits.length) {map.merge(fruits[right], 1, (oldV, val) -> ++oldV);while (map.size() > 2) {map.computeIfPresent(fruits[left], (key, oldV) -> --oldV);if (map.get(fruits[left]) == 0) {map.remove(fruits[left]);}left++;}maxTree = Math.max(maxTree, right - left + 1);right++;}return maxTree;}
}