LeetCode [JS] > 2780. Minimum Index of a Valid Split

2025. 4. 5. 17:14ยท๐Ÿ”’Algorithm

๋ฌธ์ œ๋งํฌ

https://leetcode.com/problems/minimum-index-of-a-valid-split/description/

๋ฌธ์ œํ’€์ด

dominantํ•œ ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ ์ •ํ™•ํžˆ 1๊ฐœ๋ผ๊ณ  ์ „์ œ๊ฐ€ ๋˜์–ด ์žˆ๋‹ค. ๋ฐฐ์—ด์—์„œ ํŠน์ • ์ˆ˜๊ฐ€ ๋ฐ˜๋ณด๋‹ค ๋งŽ์ด ๋“ฑ์žฅํ•œ๋‹ค๋Š” ์• ๊ธฐ์ด๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด์„œ [1,2,3,2] ์ด๋Ÿฐ ๋ฐฐ์—ด์€ dominant์ˆ˜๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ์–ด์งˆ ์ˆ˜ ์—†๋‹ค๋Š” ์• ๊ธฐ์ด๋‹ค. 

 

์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ split ํ•˜์—ฌ ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ 2๊ฐœ์˜ ๋ฐฐ์—ด๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ ๊ฐ ๋ฐฐ์—ด์˜ dominantํ•œ ์ˆ˜๊ฐ€ ๊ฐ™์€ split point๋ฅผ ์•Œ์•„์•ผ ๋œ๋‹ค.

์ฃผ์–ด์ง„ ๋ฐฐ์—ด์—์„œ dominantํ•œ ์ˆ˜๋Š” ํ•œ๊ฐœ์ด๋ฏ€๋กœ  ์• ์ดˆ์— ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ 2๊ฐœ๋กœ ๋‚˜๋ˆ„๊ณ  ๊ฐ ๋ฐฐ์—ด์˜ dominantํ•œ ์ˆ˜๊ฐ€ ๊ฐ™์„ ๋•Œ ๊ทธ ์ˆ˜๋Š” ์›๋ž˜ ๋ฐฐ์—ด์˜ dominant ์ˆ˜์ผ ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.

 

์œ„ ์‚ฌ์‹ค์„ ์•Œ๋ฉด ์šฐ๋ฆฌ๋Š” ์ „์ฒด๋ฐฐ์—ด์˜ dominantํ•œ ์ˆ˜๋ฅผ ๊ตฌํ•œ ๋’ค ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ์ˆœํšŒํ•˜๋ฉด์„œ ์™ผ์ชฝ,์˜ค๋ฅธ์ชฝ ๋ฐฐ์—ด์˜ dominant ์ˆ˜์˜ ๊ฐฏ์ˆ˜๋งŒ ํŒŒ์•…ํ•˜๋ฉด ์œ ํšจํ•œ split point๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

์ด ๋ฌธ์ œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(n)์œผ๋กœ ํŒŒ์•…๋œ๋‹ค.

/**
 * @param {number[]} nums
 * @return {number}
 */
var minimumIndex = function(nums) {

  const l = nums.length

  // Step 1: ์ „์ฒด ์นด์šดํŠธ ๋งต ๋งŒ๋“ค๊ธฐ
  const totalMap = new Map()
  for(const n of nums){
    totalMap.set(n,(totalMap.get(n)||0)+1)
  }

  // Step 2: dominant element ์ฐพ๊ธฐ (์กฐ๊ฑด: ๋“ฑ์žฅ ํšŸ์ˆ˜๊ฐ€ ์ ˆ๋ฐ˜ ์ดˆ๊ณผ)
  let dominant = -1
  for(const [n,cnt] of totalMap.entries()){
        if(cnt*2>l){
            dominant = n
            break;
        }
  }
    
  let splitIdx = undefined
  let leftCnt = 0
  let rightCnt = totalMap.get(dominant)

  // Step 3: ์™ผ์ชฝ ์นด์šดํŠธ ๋ˆ„์ ํ•˜๋ฉด์„œ valid split ์ฐพ๊ธฐ
  for(let i=0; i<=l-1; ++i){
    
    if(nums[i]===dominant){
        ++leftCnt 
        --rightCnt
    }
    
    if(leftCnt*2>i+1&&rightCnt*2>l-(i+1)){
        splitIdx = i
        break
    }    
  }

  return splitIdx === undefined ? -1 : splitIdx

}

 

'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

LeetCode [JS] > 1128. Number of Equivalent Domino Pairs  (0) 2025.04.07
LeetCode [JS] > 859. Buddy strings  (0) 2025.04.05
LeetCode > 2208 Minimum Operations to Halve Array Sum  (0) 2025.04.05
LeetCode > 1346. Check If N and Its Double Exist  (0) 2025.04.03
LeetCode > 806. Number of Lines To Write String  (0) 2025.04.02
'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • LeetCode [JS] > 1128. Number of Equivalent Domino Pairs
  • LeetCode [JS] > 859. Buddy strings
  • LeetCode > 2208 Minimum Operations to Halve Array Sum
  • LeetCode > 1346. Check If N and Its Double Exist
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐ŸงฉDev (263)
      • โญFE (34)
      • ๐Ÿ”’Algorithm (155)
      • โž•Etc. (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
    • ๊ธ€์“ฐ๊ธฐ
    • ๊ด€๋ฆฌ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    dp
    Algorithm
    ๊ตฌํ˜„
    javascript
    react
    js
    Easy
    ์‹ค๋ฒ„4
    ๊ทธ๋ฆฌ๋””
    ์•Œ๊ณ ๋ฆฌ์ฆ˜
    ์˜ค๋ธ”์™„
    ๊ณจ๋“œ5
    ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ
    Lv2
    BFS
    leetcode
    ์‹ค๋ฒ„1
    DFS
    ์ž์Šค
    FE
    ํ”„๋ก ํŠธ์—”๋“œ
    ํ‹ฐ์Šคํ† ๋ฆฌ์ฑŒ๋ฆฐ์ง€
    ์ฝ”๋”ฉํ…Œ์ŠคํŠธ
    ์‹ค๋ฒ„2
    node.js
    ์‹ค๋ฒ„3
    ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
    ๋ฐฑ์ค€
    ์ฝ”ํ…Œ
    nodejs
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
devWarrior
LeetCode [JS] > 2780. Minimum Index of a Valid Split
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”