当前位置:网站首页>904. fruit baskets
904. fruit baskets
2022-06-26 10:09:00 【later_ rql】
904. Fruit baskets
Title Description
You are visiting a farm , The farm planted a row of fruit trees from left to right . These trees use an array of integers fruits Express , among fruits[i] It's No i The fruit on the tree species .
You want to collect as much fruit as possible . However , The owner of the farm set some strict rules , You must pick the fruit as required :
1, You only have Two Basket , And each basket can only hold Single type The fruit of . There is no limit to the amount of fruit each basket can hold .
2, You can choose any tree to start picking , You have to go from Each tree Trees ( Including the trees that start picking ) On Just pick a fruit . The fruit picked should match the type of fruit in the basket . Every time you pick , You will move right to the next tree , And continue to pick .
3, Once you get to a tree , But the fruit doesn't match the type of fruit in the basket , Then you must stop picking .
Give you an array of integers fruits , Return the fruit you can collect Maximum number .
The content of the topic is too misleading , Read the pen pal's explanation : fruits The numbers in represent the type of fruit , Each basket can hold only one kind of Type of fruit , The essence of this problem is to find the longest subsequence that contains only two elements .
Test data
Input :fruits = [1,2,1]
Output :3
explain : You can pick all 3 tree .
Input :fruits = [1,2,3,2,2]
Output :4
explain : You can pick [2,3,2,2] These four trees .
If you start picking from the first tree , You can only pick [1,2] These two trees .
Input :fruits = [3,3,3,1,2,1,1,2,3,3,4]
Output :5
explain : You can pick [1,2,1,1,2] These five trees .
Their thinking
Method : The sliding window
Their thinking : A little confused , Especially when dealing with shrinking windows , I don't know how to deal with :
utilize map aggregate , Record the number of times each element , Added a judgment condition :
//** Because considering map The size in , According to the title requirements , Put in one data at a time , Have to consider map Size , if map.size()>2, Will left Subtract one from the corresponding number ,left++, Continue to judge , until map.size()=2 when , Continue to expand the window , Put elements , Continue to judge .**
while (map.size() > 2) {
map.put(fruits[left], map.get(fruits[left]) - 1);
if (map.get(fruits[left]) == 0) map.remove(fruits[left]);
left++;
}
Simple illustration :
Time complexity : O(N) N It's an array fruits The length of
Spatial complexity : O(N)
Code
Method : The sliding window
public int totalFruit(int[] fruits) {
if (fruits == null || fruits.length == 0) return 0;
int n = fruits.length;
Map<Integer, Integer> map = new HashMap<>();
int maxLen = 0, left = 0;
// Expand the window (right Move all the way to the right )
for (int i = 0; i < n; i++) {
map.put(fruits[i], map.getOrDefault(fruits[i], 0) + 1);
// contract the window , Every time according to map.size() Size to shrink to move left
while (map.size() > 2) {
map.put(fruits[left], map.get(fruits[left]) - 1);
if (map.get(fruits[left]) == 0) map.remove(fruits[left]);
left++;
}
maxLen = Math.max(maxLen, i - left + 1);
}
return maxLen;
}
source : Power button (LeetCode)
link :link-904. Fruit baskets
边栏推荐
- libgstreamer-1.0. so. 0: cannot open shared object file: No such file or directory
- SQL function
- Leetcode connected to rainwater series 42 (one dimension) 407 (2D)
- Meaning of go runtime
- Deep learning (tentsorflow2. version) three good student performance problems (1)
- 2021 national vocational college skills competition (secondary vocational group) network security competition questions (1) detailed analysis tutorial
- logback
- 字符串常量池、class常量池和运行时常量池
- Redis notes (12) - single thread architecture (non blocking IO, multiplexing) and multiple asynchronous threads
- Tensorflow dynamically allocates video memory
猜你喜欢
![Logical English structure [key points]](/img/4b/52a666ed01087adbc5fa4f9e1db393.png)
Logical English structure [key points]

Full introduction to flexboxlayout (Google official flexible implementation of flow layout control)

What you need to know to test -- URL, weak network, interface, automation

Redis master-slave replication in win10 system

Internationalization configuration

Test instructions - common interface protocol analysis

Go learning notes (83) - code specification and common development skills

软件测试---如何选择合适的正交表

SSM项目小例子,SSM整合图文详细教程

Install new version cmake & swig & tinyspline
随机推荐
全渠道、多场景、跨平台,App如何借助数据分析渠道流量
Code statistics tools cloc and SCC
Cloud native essay using Hana expression database service on Google kubernetes cluster
Dialog centered
Glide's most common instructions
【深度优先搜索】312.戳气球
Redis notes (15) - Pipeline (the client packages and sends batch commands to save network overhead)
什么是僵尸网络
Battery historian analyzes battery consumption
libmagic 介绍
Does the go compiled executable have dynamic library links?
Redis master-slave replication in win10 system
String类intern()方法和字符串常量池
c语言语法基础之——函数 小程序 求阶乘
Jupyter Notebook遇到的问题
自动化测试——关于unitest与pytest初始化共存问题
c语言语法基础之——局部变量及存储类别、全局变量及存储类别、宏定义 学习
Openxcap usage
LeetCode 958. 二叉树的完全性校验
Introduction to stored procedure testing