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

LeetCode - Weekly Contest 296 ํ’€์ด ๋ฐ ํ›„๊ธฐ

by Jellll_y 2022. 6. 5.

 

2293. Min Max Game

๋‚œ์ด๋„: Easy


You are given a 0-indexed integer array nums whose length is a power of 2.

Apply the following algorithm on nums:

  1. Let n be the length of nums. If n == 1, end the process. Otherwise, create a new 0-indexed integer array newNums of length n / 2.
  2. For every even index i where 0 <= i < n / 2, assign the value of newNums[i] as min(nums[2 * i], nums[2 * i + 1]).
  3. For every odd index i where 0 <= i < n / 2, assign the value of newNums[i] as max(nums[2 * i], nums[2 * i + 1]).
  4. Replace the array nums with newNums.
  5. Repeat the entire process starting from step 1.

Return the last number that remains in nums after applying the algorithm.

 

Example 1:

Input: nums = [1,3,5,2,4,8,2,2]
Output: 1
Explanation: The following arrays are the results of applying the algorithm repeatedly.
First: nums = [1,5,4,2]
Second: nums = [1,4]
Third: nums = [1]
1 is the last remaining number, so we return 1.

Example 2:

Input: nums = [3]
Output: 3
Explanation: 3 is already the last remaining number, so we return 3.

 

Constraints:

  • 1 <= nums.length <= 1024
  • 1 <= nums[i] <= 109
  • nums.length is a power of 2.

๋ฌธ์ œ ์„ค๋ช… 

 ๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ nums์˜ ๊ธธ์ด๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋‹ค์Œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐ˜๋ณตํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

 

๋ฐฐ์—ด์„ ๋‘๊ฐœ ์”ฉ ๋ฌถ์–ด์„œ ์ฐจ๋ก€๋Œ€๋กœ ํ™€์ˆ˜ ์ฐจ๋ก€์ผ ๋•Œ ์ตœ์†Ÿ๊ฐ’์„ ์ง์ˆ˜ ์ฐจ๋ก€์ผ ๋•Œ ์ตœ๋Œ“๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค. 

 

์ ‘๊ทผ๋ฒ•

 

flag ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜์—ฌ 2๊ฐœ์”ฉ ๋ฌถ์€ ๋ฐฐ์—ด์ด ํ™€์ˆ˜๋ฒˆ์งธ ์ธ์ง€ ๋˜๋Š” ์ง์ˆ˜ ๋ฒˆ์งธ์ธ์ง€ ์ฒดํฌํ•œ๋‹ค

๊ทธ๋ฆฌ๊ณ  ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด nums์˜ ๊ธธ์ด๊ฐ€ 1์ด ๋  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•ด์ค€๋‹ค. 

 

์ฝ”๋“œ

/**
 * @param {number[]} nums
 * @return {number}
 */
var minMaxGame = function(nums) {
    if(nums.length === 1) return nums[0]
    let arr = [] 
    let flag = true
    for(let i=2; i<=nums.length; i+=2){
        const num = flag ? Math.min(...nums.slice(i-2,i)) : Math.max(...nums.slice(i-2,i))
        flag = !flag
        arr.push(num)
    }
    return minMaxGame(arr)
};

 

2294. Partition Array Such That Maximum Difference Is K

๋‚œ์ด๋„: Medium


You are given an integer array nums and an integer k. You may partition nums into one or more subsequences such that each element in nums appears in exactly one of the subsequences.

Return the minimum number of subsequences needed such that the difference between the maximum and minimum values in each subsequence is at most k.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

 

Example 1:

Input: nums = [3,6,1,2,5], k = 2
Output: 2
Explanation:
We can partition nums into the two subsequences [3,1,2] and [6,5].
The difference between the maximum and minimum value in the first subsequence is 3 - 1 = 2.
The difference between the maximum and minimum value in the second subsequence is 6 - 5 = 1.
Since two subsequences were created, we return 2. It can be shown that 2 is the minimum number of subsequences needed.

Example 2:

Input: nums = [1,2,3], k = 1
Output: 2
Explanation:
We can partition nums into the two subsequences [1,2] and [3].
The difference between the maximum and minimum value in the first subsequence is 2 - 1 = 1.
The difference between the maximum and minimum value in the second subsequence is 3 - 3 = 0.
Since two subsequences were created, we return 2. Note that another optimal solution is to partition nums into the two subsequences [1] and [2,3].

Example 3:

Input: nums = [2,2,4,5], k = 0
Output: 3
Explanation:
We can partition nums into the three subsequences [2,2], [4], and [5].
The difference between the maximum and minimum value in the first subsequences is 2 - 2 = 0.
The difference between the maximum and minimum value in the second subsequences is 4 - 4 = 0.
The difference between the maximum and minimum value in the third subsequences is 5 - 5 = 0.
Since three subsequences were created, we return 3. It can be shown that 3 is the minimum number of subsequences needed.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105
  • 0 <= k <= 105

๋ฌธ์ œ ์„ค๋ช… 

๋ฐฐ์—ด nums ์™€ ์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’ ์ฐจ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” k ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ,

์กฐ๊ฑด(์ตœ๋Œ“๊ฐ’ - ์ตœ์†Ÿ๊ฐ’ <= k)์„ ๋งŒ์กฑํ•˜๋Š” nums๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์ ‘๊ทผ๋ฒ•

nums๋ฅผ ๋‚ด๋ฆผ ์ฐจ์ˆœ ์ •๋ ฌํ•˜๊ณ  for๋ฌธ์„ ํ†ตํ•ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด(์ตœ๋Œ€๊ฐ’ - ์ตœ์†Œ๊ฐ’ <= k)์„ ๋งŒ์กฑํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.

์ฝ”๋“œ

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var partitionArray = function(nums, k) {
    nums.sort((a,b)=>b-a)
    let maxNum =  nums[0]
    let count = 1 
    //๊ฐ€์žฅ ์ž‘์€๊ฐ’
    for(let i=1; i<nums.length; i++){
        if(maxNum - nums[i] > k){
            count++
            maxNum  = nums[i]
        }
    }
    return count
};

 

2295. Replace Elements in an Array

๋‚œ์ด๋„: Medium


You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1].

It is guaranteed that in the ith operation:

  • operations[i][0] exists in nums.
  • operations[i][1] does not exist in nums.

Return the array obtained after applying all the operations.

 

Example 1:

Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
Output: [3,2,7,1]
Explanation: We perform the following operations on nums:
- Replace the number 1 with 3. nums becomes [3,2,4,6].
- Replace the number 4 with 7. nums becomes [3,2,7,6].
- Replace the number 6 with 1. nums becomes [3,2,7,1].
We return the final array [3,2,7,1].

Example 2:

Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
Output: [2,1]
Explanation: We perform the following operations to nums:
- Replace the number 1 with 3. nums becomes [3,2].
- Replace the number 2 with 1. nums becomes [3,1].
- Replace the number 3 with 2. nums becomes [2,1].
We return the array [2,1].

 

Constraints:

  • n == nums.length
  • m == operations.length
  • 1 <= n, m <= 105
  • All the values of nums are distinct.
  • operations[i].length == 2
  • 1 <= nums[i], operations[i][0], operations[i][1] <= 106
  • operations[i][0] will exist in nums when applying the ith operation.
  • operations[i][1] will not exist in nums when applying the ith operation.

๋ฌธ์ œ ์„ค๋ช…

nums์™€ operations์ด ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. 

์ด๋•Œ, operations[i][0] ์ด nums์— ์กด์žฌํ•˜๋ฉด operations [i][1]๋กœ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

์ ‘๊ทผ๋ฒ•

๋‹จ์ˆœํ•˜๊ฒŒ 2์ค‘ for๋ฌธ์„ ํ†ตํ•ด ๋ฌธ์ œ๋ฅผ ํ’€๊ฒŒ ๋˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์—

 

nums์— operataions[i][0] ์ด ์กด์žฌํ•  ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฐ’์„ ์‰ฝ๊ฒŒ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ ์œ„ํ•ด

list obj์— key, value๊ฐ’์œผ๋กœ nums์˜ ๊ฐ’๊ณผ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ์ €์žฅ ํ•ด์ค€ ๋’ค for๋ฌธ์„ ์ˆœํšŒํ•œ๋‹ค.!

 

์ฝ”๋“œ

/**
 * @param {number[]} nums
 * @param {number[][]} operations
 * @return {number[]}
 */
var arrayChange = function(nums, operations) {
    const list = {}
    for(let i=0; i<nums.length;i++){
        list[nums[i]] = i
    }
   for(let i=0; i<operations.length; i++){

       const idx = list[operations[i][0]] === undefined ?   -1 : list[operations[i][0]]
       // list์— operations[i][0]์ด ์กด์žฌํ•˜๋ฉด
       if(idx >= 0 ){
           nums[idx] = operations[i][1]
           delete list[operations[i][0]]
           list[operations[i][1]] = idx
       }
   }
   
    return nums
};

 

 

1,2,3๋ฒˆ ๋ชจ๋‘ ํ’€๊ณ  30๋ถ„์ด ๋‚จ์•˜์ง€๋งŒ ์‹œ๊ฐ„์ด ๋ถ€์กฑํ•ด์„œ 3 ์†”๋กœ ๋งˆ๋ฌด๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.!

4๋ฒˆ์€... ๋””์ž์ธ ๋ฌธ์ œ์˜€๋Š”๋ฐ ์•„์ง์€ ์–ด๋ ต๋„ค์š” ใ…Žใ…Žใ…Ž 

๋” ์ข‹์€ ๋ฐฉ๋ฒ• ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋กœ ๋‚จ๊ฒจ ์ฃผ์„ธ์š”~