From d42b19f40757dbbac64e69f102fd10b3a6c71f83 Mon Sep 17 00:00:00 2001 From: JAYNISH MEHTA <112923627+jaynishmehta@users.noreply.github.com> Date: Sun, 19 May 2024 23:20:00 +0530 Subject: [PATCH 1/3] Create README - LeetHub --- 0904-fruit-into-baskets/README.md | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 0904-fruit-into-baskets/README.md diff --git a/0904-fruit-into-baskets/README.md b/0904-fruit-into-baskets/README.md new file mode 100644 index 0000000..d8e708d --- /dev/null +++ b/0904-fruit-into-baskets/README.md @@ -0,0 +1,44 @@ +

904. Fruit Into Baskets

Medium


You are visiting a farm that has a single row of fruit trees arranged from left to right. The trees are represented by an integer array fruits where fruits[i] is the type of fruit the ith tree produces.

+ +

You want to collect as much fruit as possible. However, the owner has some strict rules that you must follow:

+ + + +

Given the integer array fruits, return the maximum number of fruits you can pick.

+ +

 

+

Example 1:

+ +
Input: fruits = [1,2,1]
+Output: 3
+Explanation: We can pick from all 3 trees.
+
+ +

Example 2:

+ +
Input: fruits = [0,1,2,2]
+Output: 3
+Explanation: We can pick from trees [1,2,2].
+If we had started at the first tree, we would only pick from trees [0,1].
+
+ +

Example 3:

+ +
Input: fruits = [1,2,3,2,2]
+Output: 4
+Explanation: We can pick from trees [2,3,2,2].
+If we had started at the first tree, we would only pick from trees [1,2].
+
+ +

 

+

Constraints:

+ + +
\ No newline at end of file From 1a4ff5e2737ed920fbbf9039b85e26173d4fd102 Mon Sep 17 00:00:00 2001 From: JAYNISH MEHTA <112923627+jaynishmehta@users.noreply.github.com> Date: Sun, 19 May 2024 23:20:01 +0530 Subject: [PATCH 2/3] Attach NOTES - LeetHub --- 0904-fruit-into-baskets/NOTES.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 0904-fruit-into-baskets/NOTES.md diff --git a/0904-fruit-into-baskets/NOTES.md b/0904-fruit-into-baskets/NOTES.md new file mode 100644 index 0000000..38c1374 --- /dev/null +++ b/0904-fruit-into-baskets/NOTES.md @@ -0,0 +1 @@ +​ \ No newline at end of file From c273bcab62c1efed51aa51e7f379ecb4e024ab3c Mon Sep 17 00:00:00 2001 From: JAYNISH MEHTA <112923627+jaynishmehta@users.noreply.github.com> Date: Sun, 19 May 2024 23:20:02 +0530 Subject: [PATCH 3/3] Time: 128 ms (39.50%), Space: 77.7 MB (8.94%) - LeetHub --- .../0904-fruit-into-baskets.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0904-fruit-into-baskets/0904-fruit-into-baskets.cpp diff --git a/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp b/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp new file mode 100644 index 0000000..1f89d63 --- /dev/null +++ b/0904-fruit-into-baskets/0904-fruit-into-baskets.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int totalFruit(vector& fruits) { + int j=0; + int k=0; + mapm; + int maxi=0; + for(k=0;k2){ + maxi=max(maxi,(k-j)); + while(m.size()>2){ + m[fruits[j]]--; + if(m[fruits[j]]==0)m.erase(fruits[j]); + j++; + } + } + } + maxi= max(maxi,(k-j)); + return maxi; + } +}; \ No newline at end of file