๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜/๋ฌธ์ œ

[์ž๋ฐ”] Leetcode 268 - Missing Number

by applemango2021 2021. 3. 4.
๋ฌธ์ œ

๋ฐฐ์—ด nums๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, ์ด๋•Œ ๋ฐฐ์—ด์—๋Š” ๋ฒ”์œ„ [0,n]์— ์†ํ•˜๋Š” n๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋‹ด๊ฒจ์žˆ๋‹ค. ๋ฒ”์œ„๊ฐ€ [0,n]์ด๋ผ๋ฉด ์ด n-1๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์žˆ์–ด์•ผํ•˜๋Š”๋ฐ, n๊ฐœ๋งŒ ๋‹ด๊ฒจ์žˆ๋‹ค๋Š” ๊ฑด ์ˆซ์ž ํ•œ๊ฐœ๊ฐ€ ๋น ์ ธ์žˆ๋‹ค๋Š” ๋œป. ๋น ์ง„ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฌธ์ œ๋‹ค.

leetcode.com/problems/missing-number/

 

Missing Number - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

์ฒซ ๋ฒˆ์งธ Solution

์‰ฝ๊ฒŒ ์ƒ๊ฐํ–ˆ๋‹ค. ๊ธธ์ด n์„ ๊ฐ€์ง€๋Š” boolean ๋ฐฐ์—ด์„ ๋งŒ๋“ค๊ณ , ๋ฐฐ์—ด nums๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ boolean ๋ฐฐ์—ด์— visited(=๋ฐฐ์—ด์— ์ˆซ์ž๊ฐ€ ๋‚˜์™”๋Š”์ง€) ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜์„œ boolean ๋ฐฐ์—ด์„ ๋‹ค์‹œ ํƒ์ƒ‰ํ•˜๋ฉด์„œ false๋กœ ๋‚จ์•„์žˆ๋Š” ์ˆซ์ž๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด int answer ๋ณ€์ˆ˜์— ๋‹ด๊ณ  ๋ฐ”๋กœ return ํ•ด๋ฒ„๋ฆฐ๋‹ค.

๊ธฐ์กด ๋ฐฐ์—ด nums๋ฅผ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ O(n) + boolean ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ ์ตœ๋Œ€ O(n) ์ด๊ธฐ ๋•Œ๋ฌธ์— ์ตœ์ข… ์‹œ๊ฐ„ ๋ณต์žก๋„๋„  O(n)๋ฐ–์— ์•ˆ ๊ฑธ๋ฆฐ๋‹ค. ํ•˜์ง€๋งŒ, ๋ฐฐ์—ด์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ณต๊ฐ„๋ณต์žก๋„๊ฐ€ O(n)์ด๋‹ค. 

class Solution {
    public int missingNumber(int[] nums) {
        int answer = 0;
        
        int len = nums.length;
        
        boolean[] contained = new boolean[len+1];
        
        for(int i=0;i<len;i++){
            contained[nums[i]] = true;
        }
        
        for(int i=0;i<=len;i++){
            if(!contained[i]) {
                answer = i;
                break;
            }
        }
        
        return answer;
    }
}

 

๋‘ ๋ฒˆ์งธ Solution

์•„๋ž˜ ๋งํฌ์—์„œ ์ฐธ๊ณ ํ–ˆ๋‹ค.

XOR ์—ฐ์‚ฐ์ž์˜ ์„ฑ๊ฒฉ์„ ํ™œ์šฉํ•œ ํ’€์ด๋ฒ•์ธ๋ฐ, ์‹œ๊ฐ„๋ณต์žก๋„ O(n)์— ๊ณต๊ฐ„๋ณต์žก๋„๋„ O(1)์ด๋‹ค.

๊ทผ๋ฐ ๋‚ด๊ฐ€ ์•„์ง ์ดํ•ด๋ฅผ ์ž˜ ๋ชปํ•˜๊ฒ ๋‹ค. A^B^B=A์ธ ์ ์„ ์ด์šฉํ•ด์„œ ์ง  ์ฝ”๋“œ๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ๊ณ ๋ฏผ์ด ๋” ํ•„์š”ํ•˜๋‹ค.

leetcode.com/problems/missing-number/discuss/69786/3-different-ideas%3A-XOR-SUM-Binary-Search.-Java-code

 

3 different ideas: XOR, SUM, Binary Search. Java code - LeetCode Discuss

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

์„ธ ๋ฒˆ์งธ Solution

๊ฐ„๋‹จํ•œ ์ˆ˜ํ•™์‹์„ ์ด์šฉํ•œ ํ’€์ด๋ฒ•์ด๋‹ค!

n์ด ๊ต‰์žฅํžˆ ํด ๋•Œ์—๋Š” overflow๊ฐ€ ๋‚  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•˜์ง€๋งŒ, ์ผ๋‹จ ๋ฌธ์ œ ๋‚ด์—์„œ๋Š” 1<=n<=10000๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ์œผ๋‹ˆ๊นŒ ์ด ํ’€์ด๋ฒ•๋„ ์ ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค. Overflow ๊ฑฑ์ • ์—†์ด ํ•ด๊ฒฐํ•˜๋ ค๋ฉด ๋‘ ๋ฒˆ์งธ Solution์„ ์ ์šฉํ•˜๋Š” ๊ฒŒ ๋งž์„ ๊ฒƒ ๊ฐ™๋‹ค.

 

0๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ์ดํ•ฉ์„ ๊ตฌํ•œ๋‹ค๊ณ  ํ–ˆ์„ ๋•Œ, ์‹ค์ œ๋กœ ๋‚˜์™€์•ผ ํ•˜๋Š” ๊ฐ’์„ ๊ณ„์‚ฐํ•ด ๋†“๊ณ  (๋ณ€์ˆ˜ sum), ๋ฐฐ์—ด nums๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ sum์—์„œ num[i]์˜ ๊ฐ’์„ ๋นผ๊ฐ„๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฐฐ์—ด์„ ๋‹ค ๋Œ์•˜์„ ๋•Œ ์ตœ์ข…์ ์œผ๋กœ ์ •ํ•ด์ง€๋Š” sum์˜ ๊ฐ’์ด ๋น ์ง„ ์ˆซ์ž๊ฐ€ ๋˜๋Š” ๊ฒƒ!

 

๋‚˜๋Š” ์ด ํ’€์ด๋ฒ•์ด ์ฒซ ๋ฒˆ์งธ ํ’€์ด๋ฒ•๋ณด๋‹ค ํ›จ์”ฌ ๋‚ซ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ, Runtime์ด๋‚˜ Memory ์ธก๋ฉด์—์„œ ๋ชจ๋‘ ์ฒซ๋ฒˆ์งธ ํ’€์ด๋ฒ•๋ณด๋‹ค ์•ˆ ์ข‹๊ฒŒ ๋‚˜์™”๋‹ค. ์ด์œ ๊ฐ€ ๋ญ˜๊นŒ?

class Solution {
    public int missingNumber(int[] nums) {
        
        int len = nums.length;
        int sum = ((len+1)*len)/2;
        
        for(int i=0;i<len;i++){
            sum -= nums[i];
        }
        return sum;
    }
}