Skip to content

904. Fruit Into Baskets #2010

Answered by mah-shamim
mah-shamim asked this question in Q&A
Aug 4, 2025 · 1 comments · 2 replies
Discussion options

You must be logged in to vote

We need to find the maximum number of fruits we can collect from a row of fruit trees while adhering to specific constraints. The constraints are that we can only collect two types of fruits, and we must pick fruits from every tree starting from a chosen tree moving to the right until we encounter a tree that produces a fruit type not in our baskets.

Approach

  1. Sliding Window Technique: We use a sliding window approach to maintain a window of trees that contains at most two distinct fruit types. The window is defined by two pointers, left and right, which represent the current range of trees under consideration.
  2. Hash Map for Tracking Fruit Types: We use a hash map (associative array in PHP…

Replies: 1 comment 2 replies

Comment options

mah-shamim
Aug 4, 2025
Maintainer Author

You must be logged in to vote
2 replies
@topugit
Comment options

topugit Aug 4, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim Aug 4, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested medium Difficulty
2 participants