LeetCode [JS] > 3192. Minimum Operations to Make Binary Array Elements Equal to One II

2025. 3. 12. 21:48ยท๐Ÿ”’Algorithm

๐Ÿ’ก๋ฌธ์ œ๋งํฌ

https://leetcode.com/problems/minimum-operations-to-make-binary-array-elements-equal-to-one-ii/

๐Ÿ”จ๋ฌธ์ œํ’€์ด

ํฐ ์–ด๋ ค์›€ ์—†์ด ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์›๋ž˜๋Š” flipped์ด๋ผ๋Š” ๊ฐ’์„ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  cnt๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๋ฉด์„œ for of ๋ฌธ ๋‚ด๋ถ€์—์„œ cnt%2===0์ผ๋•Œ 1์ผ๋•Œ ๊ฐ๊ฐ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌํ–ˆ์ง€๋งŒ ๊ทธ๊ฑธ flipped ์ด๋ผ๋Š” boolean ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•ด์„œ ์—ฐ์‚ฐ๊ณผ์ •์„ ๋นผ๋‹ˆ run-time ์†๋„๊ฐ€ ํ–ฅ์ƒ ๋˜์—ˆ๋‹ค. 

/**
 * @param {number[]} nums
 * @return {number}
 */
var minOperations = function(nums) {
    
    let flipped = false
    let cnt = 0
    
    for(const num of nums){
        if((num===0 && !flipped)||(num===1 && flipped)){
            ++cnt
            flipped=!flipped
        }
    }
    return cnt

};

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

HackerRank > big-sorting  (0) 2025.03.22
LeetCode[JS] > 2718. Sum of Matrix After Queries  (0) 2025.03.15
LeetCode[JS] 53๋ฒˆ Maximum Subarray  (0) 2025.03.11
๋ฐฑ์ค€[JS] > 1697๋ฒˆ ์ˆจ๋ฐ”๊ผญ์งˆ  (0) 2025.03.09
10์ง„์ˆ˜ <-> 16์ง„์ˆ˜ ๋ณ€ํ™˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜  (0) 2025.03.08
'๐Ÿ”’Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • HackerRank > big-sorting
  • LeetCode[JS] > 2718. Sum of Matrix After Queries
  • LeetCode[JS] 53๋ฒˆ Maximum Subarray
  • ๋ฐฑ์ค€[JS] > 1697๋ฒˆ ์ˆจ๋ฐ”๊ผญ์งˆ
devWarrior
devWarrior
  • devWarrior
    devWarrior
    devWarrior
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๐ŸงฉDev (263)
      • โญFE (34)
      • ๐Ÿ”’Algorithm (155)
      • โž•Etc. (11)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

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

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

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

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

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.3
devWarrior
LeetCode [JS] > 3192. Minimum Operations to Make Binary Array Elements Equal to One II
์ƒ๋‹จ์œผ๋กœ

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