bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 904. Fruit Into Baskets

904. Fruit Into Baskets

Leetcode Solutions

Sliding Window Approach

  1. Initialize max_picked to 0, left to 0, and an empty hash map basket.
  2. Iterate over the array with the right pointer.
  3. Add fruits[right] to the basket and increment its count.
  4. While the basket contains more than two types of fruits: a. Decrease the count of fruits[left] in the basket. b. If the count becomes 0, remove fruits[left] from the basket. c. Increment left.
  5. Update max_picked with the maximum of its current value and the size of the window (right - left + 1).
  6. Return max_picked after the loop ends.
UML Thumbnail

Brute Force Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...