๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“š Study Note/LeetCode

904. Fruit Into Baskets

by Jellll_y 2023. 2. 7.

 

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:

  • You only have two baskets, and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold.
  • Starting from any tree of your choice, you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of your baskets.
  • Once you reach a tree with fruit that cannot fit in your baskets, you must stop.

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:

  • 1 <= fruits.length <= 105
  • 0 <= fruits[i] < fruits.length

 

์ด ๋ฌธ์ œ๋Š” 2๊ฐœ์˜ ๋ฐ”๊ตฌ๋‹ˆ์— ๊ณผ์ผ์„ ์ตœ๋Œ€ ๋ช‡ ๊ฐœ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ด๋•Œ ๊ฐ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” ํ•œ ๊ฐ€์ง€ ํƒ€์ž…์˜ ๊ณผ์ผ์„ ๋‹ด์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ

๊ฐ ๊ณผ์ผ์˜ ํƒ€์ž…์€ fruits[i] ์ž…๋‹ˆ๋‹ค.

ex) [1,2,3,4,1]์˜ fruits๊ฐ€ ์žˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ํƒ€์ž…์˜ ๊ฐœ์ˆ˜๋Š” 4๊ฐœ์ž…๋‹ˆ๋‹ค. 

 

์ฒ˜์Œ์—๋Š” brute force ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ์œผ๋‚˜ ๋งค๋ฒˆ left์—์„œ ๋ฐ”๊ตฌ๋‹ˆ ๊ฐœ์ˆ˜๊ฐ€

์ดˆ๊ณผํ•  ๋•Œ๊นŒ์ง€ ๋”ํ•˜๋‹ค ๋ณด๋‹ˆ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.

var totalFruit = function(fruits) {
     let ans = 0
    
     for(let left=0; left<fruits.length; left++){
         let right = left
         const basket = new Set()
        
         while(right < fruits.length){
             if(!basket.has(fruits[right]) && basket.size === 2) break
            
             basket.add(fruits[right])
             right++ 
         }

         ans = Math.max(ans, right - left)
     }
     return ans
}

 

๊ทธ๋ž˜์„œ ์กฐ๊ธˆ ๋” ํšจ์œจ์ ์ธ sliding window ๋ฐฉ๋ฒ•์œผ๋กœ ๋‹ค์‹œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

 

์˜์‚ฌ ์ฝ”๋“œ

  1. ๊ณผ์ผ์˜ ํƒ€์ž…๊ณผ ๊ฐœ์ˆ˜๋ฅผ ๊ธฐ๋กํ•  basket, window์˜ ์‹œ์ž‘๊ณผ ๋์„ ๋‚˜ํƒ€๋‚ด๋Š” left, right, ์ตœ๋Œ“๊ฐ’ max๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด ์ค๋‹ˆ๋‹ค.
  2. fruits.length๊นŒ์ง€ fruits ๋ฐฐ์—ด์„ ์ˆœํšŒํ•œ๋‹ค.
    1.  basket์— ๊ณผ์ผ์˜ ํƒ€์ž…๊ณผ ๊ฐœ์ˆ˜๋ฅผ ํ• ๋‹นํ•ด ์ค€๋‹ค.
    2. basket์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ 2๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ ์ฆ‰ ๊ณผ์ผ ํƒ€์ž…์˜ ๊ฐœ์ˆ˜๊ฐ€ 2๊ฐœ ์ด์ƒ์ธ ๊ฒฝ์šฐ while๋ฌธ์„ ํ†ตํ•ด ํ•˜๋‚˜๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ์™ผ์ชฝ(sliding window์˜ ์‹œ์ž‘ ๋ถ€๋ถ„)์—์„œ ์ง€์›Œ์ค€๋‹ค.
    3. max ์—…๋ฐ์ดํŠธ
  3. return max

 

/**
 * @param {number[]} fruits
 * @return {number}
 */
var totalFruit = function(fruits) {
    const basket = new Map()
    let left = 0, right;
    let max = 0 
    
    for(right=0; right < fruits.length; right++){
        basket.set(fruits[right], (basket.get(fruits[right])||0)+1)
        
        // type์ด 2๊ฐœ ์ด์ƒ์ด๋ฉด 2๊ฐœ๊ฐ€ ๋ ๋•Œ๊นŒ์ง€ ์ง€์›Œ์ค€๋‹ค.
        while(basket.size > 2){
            basket.set(fruits[left], basket.get(fruits[left]) - 1)
            
            if(basket.get(fruits[left]) === 0){
                basket.delete(fruits[left])
            }
            left++
        }
        
        max = Math.max(max, right - left + 1)
    }
    
    return max
};

'๐Ÿ“š Study Note > LeetCode' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

1162. As Far from Land as Possible  (0) 2023.02.10
2306. Naming a Company  (0) 2023.02.09
1091. Shortest Path in Binary Matrix  (0) 2023.02.06
2091. Removing Minimum and Maximum From Array  (0) 2023.02.01
209. Minimum Size Subarray Sum  (0) 2022.12.28