Skip to content

Latest commit

ย 

History

History
567 lines (405 loc) ยท 13.9 KB

algorithm.md

File metadata and controls

567 lines (405 loc) ยท 13.9 KB

์•Œ๊ณ ๋ฆฌ์ฆ˜



Dynamic Programming์ด๋ž€? ์žฅ์ ์€?

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

ํ”ผ๋ณด๋‚˜์น˜๋ฅผ ํ†ตํ•œ ์žฌ๊ท€์™€ DP ๋น„๊ต ์„ค๋ช…

N = int(input())
D = [0, 1]

for i in range(2, N + 1):
    D.append(D[i-2] + D[i-1])

print(D[N])



EASY, ํ™”์ดํŠธ๋ณด๋“œ ๋ฉด์ ‘

๋ฐฐ์—ด nums์— [0, n]๋ฒ”์œ„์˜ n๊ฐœ์˜ ์–‘์˜ ์ •์ˆ˜๊ฐ€ ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. [0, n]๋ฒ”์œ„ ์ˆ˜ ์ค‘์—์„œ ๋ฐฐ์—ด์— ๋น ์ ธ์žˆ๋Š” ์ˆ˜ ํ•˜๋‚˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

ํ’€์ด1. HashSet

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์†Œ๊ฐœ
    • HashSet์— ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋„ฃ์Šต๋‹ˆ๋‹ค.
    • ๋‹ค์‹œ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉฐ HashSet์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ฐพ์Šต๋‹ˆ๋‹ค.
    • Set์€ O(1)์œผ๋กœ ์ฐพ๊ธฐ์— .contatins์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(1)์ž…๋‹ˆ๋‹ค.
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„์„
    • ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
    • ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
class Solution {
    public int missingNumber(int[] nums) {
        Set<Integer> numSet = new HashSet<Integer>();
        for (int num : nums) {
            numSet.add(num);
        }

        int expectedNumCount = nums.length + 1;
        for (int number = 0; number < expectedNumCount; number++) {
            if (!numSet.contains(number)) {
                return number;
            }
        }
        return -1;
    }
}

ํ’€์ด2. ๋น„ํŠธ ์—ฐ์‚ฐ

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์†Œ๊ฐœ
    • ๊ฐ™์€ ์ˆซ์ž๋ฅผ O(1)์— ์ง€์›Œ๋ฒ„๋ฆฌ๋Š” ๊ฐ•๋ ฅํ•œ ๋น„ํŠธ ์—ฐ์‚ฐ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
    • XOR์—ฐ์‚ฐ์€ ๊ฐ™์€ ์ˆ˜์ด๋ฉด 0์œผ๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
    • ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ idx์™€ ๋ฐฐ์—ด์˜ ๊ฐ’๊ณผ XOR์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    • ๊ฐ™์€ ์ˆ˜๋Š” 0์œผ๋กœ ๋˜๋ฏ€๋กœ ์ตœ์ข…์ ์œผ๋กœ ๋ฐฐ์—ด์˜ ๊ฐ’์— ๋ˆ„๋ฝ๋œ ์ˆ˜๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
Index   0   1   2   3
Value   0   1   3   4
missing = 4^(0^0)^(1^1)^(2^3)^(3^4)  
        = (4^4)^(0^0)^(1^1)^(3^3)^2   # ๊ตํ™˜๋ฐฅ์น™์œผ๋กœ ๊ฐ™์€ ์ˆ˜๋ผ๋ฆฌ ๋ฌถ์–ด์ค€๋‹ค.
        = 0^0^0^0^2                   # ๊ฐ™์€ ์ˆ˜ ๋ผ๋ฆฌ ๋ฌถ์œผ๋ฉด ๋ฐฐ์—ด์— ๋น ์ง„ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ค๊ฒŒ๋œ๋‹ค.
        = 2
  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„์„
    • ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
    • ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
class Solution {
    public int missingNumber(int[] nums) {
        int missing = nums.length;
        for (int i = 0; i < nums.length; i++) {
            missing ^= i ^ nums[i];
        }
        return missing;
    }
}

ํ’€์ด3. ๊ฐ€์šฐ์Šค ๊ณต์‹

  • ์—ฐ์†๋œ ์–‘์˜์ •์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ณต์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. โˆ‘โ€‹ni=โ€‹n(n+1)/2
  • ์—ฐ์†๋œ ์ˆ˜ - ํ˜„์žฌ ๋ฐฐ์—ด์˜ ์ˆ˜๋ฅผ ๋นผ๋ฉด ๋ฐฐ์—ด์— ๋ˆ„๋ฝ๋œ ํ•œ ๊ฐœ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
  • ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
class Solution {
    public int missingNumber(int[] nums) {
        int expectedSum = nums.length * (nums.length + 1) / 2;
        int actualSum = 0;
        for (int num : nums) {
            actualSum += num;
        }
        return expectedSum - actualSum;
    }
}




EASY, ํ™”์ดํŠธ๋ณด๋“œ ๋ฉด์ ‘, Joyful Pythonic

๋ฐฐ์—ด์˜ ์ˆซ์ž๋ฅผ ํ™œ์šฉํ•ด์„œ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜์„ธ์š”.

์ž…๋ ฅ ์ถœ๋ ฅ ๊ฐ’ ํ™•์ธ (๐Ÿ‘ˆ Click)

1 <= nums.length <= 100, 0 <= nums[i] <= 109

์ž…๋ ฅ: nums = [10,2]
์ถœ๋ ฅ: "210"

์ž…๋ ฅ: nums = [3,30,34,5,9]
์ถœ๋ ฅ: "9534330"

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

๋ณธ ๋ฌธ์ œ๋Š” ์ „ํ˜•์ ์ธ ์ •๋ ฌ๋ฌธ์ œ๋ฅผ ์‚ด์ง ๋น„ํ‹€์–ด์„œ ์ƒˆ๋กœ์šด ์ •๋ ฌ ๊ธฐ์ค€์„ ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ํž˜์„ ์ฃผ์–ด์•ผํ•  ๊ฒƒ์€ '์ƒˆ๋กœ์šด ์ •๋ ฌ ๊ธฐ์ค€' ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ๋‚ด์žฅํ•จ์ˆ˜์˜ ์˜ค๋ฆ„์ฐจ์ˆœ, ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ์ด ์•„๋‹Œ Custom Sort ์ฆ‰ ์ƒˆ๋กœ์šด ์ •๋ ฌ ๊ธฐ์ค€์„ ๊ตฌํ˜„ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•์€ ๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ์กฐ๊ฑด์— ๋งž์ถ”์–ด swap์„ ํ•˜๋Š” ๋ฐฉ์‹์ด ์žˆ๊ฒ ์ง€๋งŒ ์ •๋ ฌ ๋ฌธ์ œ๋‹ต๊ฒŒ python์˜ sort ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


ํ’€์ด: Custom Sort(Customized comparator)

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„์„
    • ์‹œ๊ฐ„๋ณต์žก๋„: O(nlgn) - ์ •๋ ฌํ•˜๋Š”๋ฐ ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„๋ณต์žก๋„.
    • ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
class Solution(object):
    def largestNumber(self, nums):
        def numOrder(x, y):
            left = int(x + y)
            right = int(y + x)
            return left - right

        if len(nums) == 0: return ""

        numsStr = [str(n) for n in nums]
        numsStr.sort(reverse=True, cmp=numOrder)

        if numsStr[0] == '0': return "0"
        else: return "".join(numsStr)

๊ธด์žฅํ•œ ์ƒํƒœ + ๋ฉด์ ‘์ด๋ผ๋Š” ์••๋ฐ•์˜ ์ž๋ฆฌ์—์„œ ์‰ฝ๊ฒŒ ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ ํŒŒ์ด์ฌ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ€์žฅ ๋‚™ํ›„๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ตฌํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์—ฌ๋Ÿฌ๋ถ„๋“ค์ด ๋ฉด์ ‘๊ด€ ์•ž์—์„œ ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„์„ ํ–ˆ๋‹ค๋ฉด ๋ฉด์ ‘๊ด€์€ ์ญ?ํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ถ„์˜ ์ด๋ ฅ์„œ๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณผ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ณธ ์ฝ”๋“œ๊ฐ€ ์„ธ๋ จ๋˜์ง€ ๋ชปํ•œ ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • sort์— ์‚ฌ์šฉ๋œ cmp ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” python3.0 ์ดํ›„๋กœ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฐธ๊ณ  Python Doc
  • ํ•จ์ˆ˜์•ˆ์—์„œ int, ํ•จ์ˆ˜ ๋ฐ–์—์„œ strํ˜•์œผ๋กœ ๋ฐ์ดํ„ฐ ํ˜•์ด ํ˜ผ๋ž€์Šค๋Ÿฝ์Šต๋‹ˆ๋‹ค.
  • if๊ฐ€ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋‚จ๋ฐœ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ํ’€์ด๋ฅผ pythonicํ•œ ํ’€์ด๋กœ ๋ฐ”๊พธ์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


class Solution:
    def largestNumber(self, nums):
        class Predicate(str):
            def __lt__(self, other):
                return self + other < other + self

        res = ''.join(sorted(map(str, nums), key=Predicate, reverse=True))
        return '0' if res[0] == '0' else res


key๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฐฑ์ค€ ๋ฌธ์ œ๋ฅผ ์ข€ ํ’€์–ด๋ดค๋‹ค๋ฉด ๋‚ฏ์„ค์ง€ ์•Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

>>> tuple_list = [('Covenant', 9),
    	          ('Covenant', 1)]
                  
>>> tuple_list.sort(key=lambda x : (x[0], x[1])) 
>>> print(tuple_list)
[('Covenant', 1), ('Covenant', 9)]

์ •๋ ฌ ์กฐ๊ฑด์œผ๋กœ ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ๊ฐ–๋Š” ๊ฒฝ์šฐ ํŠœํ”Œ๋กœ ์‚ฌ์šฉํ•ด์„œ ์ƒˆ๋กœ์šด ์ •๋ ฌ ์กฐ๊ฑด์„ ์ค„๋•Œ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” ์—ฌ๊ธฐ์— Predicate class์˜ __lt__(less than)๋ผ๋Š” ๋งค์ง ๋ฉ”์†Œ๋“œ๋ฅผ ์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

a = ["10", "5"] ์ผ๋•Œ Predicate์˜ __lt__์˜ ์ •์˜๋œ ๊ฐ’์œผ๋กœ ๊ณ„์‚ฐํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ •๋ ฌ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

a[0] + a[1] = "105"
a[1] + a[0] = "510"

105๋ณด๋‹ค ํฐ 510์ด ๋‹ต์œผ๋กœ ๋ฐ˜ํ™˜ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.






map, hashmap, set์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜์„ธ์š”

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)



์„ ํƒ ์•Œ๊ณ ๋ฆฌ์ฆ˜, ๊ผฌ๋ฆฌ๋ฌผ๊ธฐ ์ตœ์ ํ™”

๋ฐฐ์—ด A์˜ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜์„ธ์š”.

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

1. ๋ฐฐ์—ด A์˜ ์ตœ๋Œ€๊ฐ’

์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)

import sys

def find_largest_number_in_array(A):
    ans = -sys.maxsize
    for number in A:
        if number > ans:
            ans = number
    return ans

2. ๋ฐฐ์—ด A์˜ ์ตœ๋Œ€๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜์‹œ์˜ค

์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)

def find_small_and_largest_number_in_array(A):
    _max, _min = -sys.maxsize, sys.maxsize
    for number in A:
        if number > _max:
            _max = number
        elif number < _min:
            _min = number
    return _max, _min

3. ์œ„์˜ ํ’€์ด๋ณด๋‹ค ๋น ๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ์„ธ์š”.

์‹œ๊ฐ„๋ณต์žก๋„: O(n), ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)

def optimization_find_small_and_largest_number_in_array(A):
    _max = _min= A[0]

    for idx in range(0, len(A), 2):
        first = A[idx]
        second = A[idx + 1]
        if first < second:
            if first < _min: _min = first
            if second > _max: _max = second
        else:
            if second < _min: _min = second
            if first > _max: _max = first
    return _max, _min

*๋ฐฐ์—ด์˜ ๊ฐฏ์ˆ˜๊ฐ€ ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ index out of range exception์ด ๋ฐœ์ƒํ•˜๋ฏ€๋กœ Padding ๊ฐ’์„ ํ•˜๋‚˜ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.




์™„์ „ํƒ์ƒ‰, ๊ผฌ๋ฆฌ๋ฌผ๊ธฐ ์ตœ์ ํ™”

๋ฐฐ์—ด A์—์„œ ์ค‘๋ณต๋˜๋Š” ์›์†Œ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ตœ์ ํ™”ํ•ด๋ณด์„ธ์š”.

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

ํ’€์ด 1. ๋ธŒ๋ฃจํŠธํฌ์Šค

์‹œ๊ฐ„๋ณต์žก๋„: O(n^2) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def bruteforce(A):
    for i in range(len(A)):
        for j in range(i + 1, len(A)):
            if A[i] == A[j]:
                print("Duplicates exist: " + str(A[i]))
                return
    print("No duplicates in given array")

ํ’€์ด 2. ์ •๋ ฌ

ํ’€์ด 1์„ ์ตœ์ ํ™”. ์ •๋ ฌ์„ ํ•˜๋ฉด ๋ฐ”๋กœ ์˜†์˜ ์›์†Œ์™€ ๋น„๊ตํ•˜๋ฉด ๋˜๊ธฐ์— ํƒ์ƒ‰ ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„: O(nlogn) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def sorting(A):
    A.sort()
    for i in range(len(A)-1):
        if A[i] == A[i+1]:
            print("Duplicates exist: " + str(A[i]))
            return
    print("No duplicates in given array")

ํ’€์ด 3. ํ•ด์‰ฌ

set()์— ์ €์žฅํ•˜๋ฉด ์ˆ˜๋ฅผ ๋„ฃ๊ธฐ ์ „์— set()์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•  ๋•Œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(1)์ž…๋‹ˆ๋‹ค. ์ •๋ ฌ๋ณด๋‹ค ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

def hash(A):
    tmp = set()
    for i in A:
        if i in tmp:
            print("Duplicates exist: " + str(i))
            return
        tmp.add(i)
    print("No duplicates in given array")

ํ’€์ด 4. negation ์ „๋žต

  • ํ’€์ด 3 ํ•ด์‰ฌ์—์„œ ์ตœ์•…์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ์›์†Œ๋ฅผ ์ €์žฅํ•ด์•ผํ•˜๊ธฐ์— ๊ณต๊ฐ„๋ณต์žก๋„ n์ž…๋‹ˆ๋‹ค.
  • ๊ณต๊ฐ„ ๋ณต์žก๋„๋ฅผ O(1)๋กœ ์ค„์ผ ์ˆ˜ ์žˆ๋Š” ์ƒ์†Œํ•˜์ง€๋งŒ ์–ด๋ ต์ง€ ์•Š์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์†Œ๊ฐœํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def negation(A):
    for i in range(len(A)):
        if A[abs(A[i])] < 0:
            print("Duplicates exist", abs(A[i]))
            return
        A[A[i]] = -A[A[i]]
    print("No duplicates in given array")

check dup elements

  • A = [3, 1, 0, 1, 4]๋ฅผ ์˜ˆ์‹œ๋กœ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
  • Step1. A[abs(A[i])]๊ฐ€ ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ A[A[i]]๋ฅผ ์Œ์ˆ˜๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • Step2. A[abs(A[i])]๊ฐ€ ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ A[A[i]]๋ฅผ ์Œ์ˆ˜๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • Step3. A[abs(A[i])]๊ฐ€ ์Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ A[A[i]]๋ฅผ ์Œ์ˆ˜๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
  • Step4. A[abs(A[i])]๊ฐ€ ์Œ์ˆ˜์ด๋ฏ€๋กœ ์ค‘๋ณต ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์ด ๋ฐฉ๋ฒ•์€ 0 ~ n-1 ๋ฒ”์œ„์ผ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. n ์ด์ƒ์˜ ์ˆซ์ž๊ฐ€ ๋ฐฐ์—ด์— ์žˆ์„ ๊ฒฝ์šฐ Out of range ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.





์™„์ „ํƒ์ƒ‰, ๊ผฌ๋ฆฌ๋ฌผ๊ธฐ ์ตœ์ ํ™”

๋ฐฐ์—ด์— ๋น ์ง„ ์ˆ˜๋ฅผ ์ฐพ์œผ์„ธ์š”.

์˜ˆ๋น„ ๋‹ต์•ˆ ๋ณด๊ธฐ (๐Ÿ‘ˆ Click)

๋ฌธ์ œ

์„œ๋กœ ๋‹ค๋ฅธ [1, n]๋ฒ”์œ„์˜ n-1๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์— ๋น ์ง„ ์ˆ˜๋ฅผ ์ฐพ์œผ์„ธ์š”.


๋ณธ ๋ฌธ์ œ์— ๋Œ€ํ•œ ์ƒ์„ธ ํ•ด์„ค์€ covenant.tistory.com/245์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


ํ’€์ด 1. ์™„์ „ํƒ์ƒ‰

์‹œ๊ฐ„๋ณต์žก๋„: O(n^2) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def find_missing_number_bruteforce(A):
    N = len(A)

    for cur in range(1, N+1):
        flag = False
        for a in A:
            if cur == a:
                flag = True; break

        if flag is False:
            print("Missing number is " + str(cur))
            break

ํ’€์ด 2. ์ •๋ ฌ

์‹œ๊ฐ„๋ณต์žก๋„: O(nlogn) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def find_missing_number_sort(A):
    A.sort()
    for cur in range(1, len(A)+1):
        if cur not in A:
            print("Missing number is " + str(cur))
            break

ํ’€์ด 3. ํ•ด์Š

์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

def find_missing_number_hashing(A):
    A = set(A)

    for cur in range(1, len(A)+1):
        if cur not in A:
            print("Missing number is " + str(cur))
            break

ํ’€์ด 4. ์ดํ•ฉ ๊ณต์‹(summation formula)

์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def find_missing_number_summation_formula(A):
    N = len(A)

    total_sum = (N + 1) * (N + 2) // 2
    curr_sum = sum(A)

    if total_sum - curr_sum != 0:
        print("Missing number is " + str(abs(total_sum - curr_sum)))

ํ’€์ด 5. XOR

์‹œ๊ฐ„๋ณต์žก๋„: O(n) ๊ณต๊ฐ„๋ณต์žก๋„: O(1)

def find_missing_number_xor(A):
    N = len(A)
    X1 = A[0]
    X2 = 0

    for i in range(1, N):
        X1 = X1 ^ A[i]
    for cur in range(1, N+2):
        X2 = X2 ^ cur

    print("Missing number is " + str(X1 ^ X2))





์œ ์ „ ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•˜์„ธ์š”.

๋‹ต์•ˆ ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.




pivotal(๋Œ€๊ฐ์„ ์ด ๊ณ ์ •์ธ ํ–‰๋ ฌ) 3x3, 4x4๋ฅผ ๋’ค์ง‘๋Š” ๋กœ์ง์„ ์งœ๋ณด์„ธ์š” ์žฌ๊ท€๋ฅผ ์จ์•ผํ•จ.

๋‹ต์•ˆ ์ค€๋น„์ค‘์ž…๋‹ˆ๋‹ค.





์šฉ๊ฐํ•œ ์นœ๊ตฌ๋“ค with ๋‚จ์†ก๋ฆฌ ์‚ผ๋ฒˆ์ง€