코딩테스트/코딩테스트 Python

[CodeSignal] singleNumber (Python)

sseozytank 2022. 9. 5.

https://app.codesignal.com/interview-practice/task/APDXraJZYfPSYqQMJ/description

 

singleNumber | CodeSignal

Press space bar to start a drag. When dragging you can use the arrow keys to move the item around and escape to cancel. Some screen readers may require you to be in focus mode or to use your pass through key

app.codesignal.com

 

✔️문제 요약 

1.한개를 제외하고 정수 2개씩 들어가있는 배열이 주어짐
2.그 나머지 한개를 찾기
3.시간복잡도 O(n)

 

✔️풀이 방법 

1.리스트에서 카운트함수를 통해 한개만 있는 것들을 res에 담아준다.

def solution(nums):
    res = 0
    for num in nums:
        element = nums.count(num)
        if element==1:
            res=num
        else:
            pass
    return res

 

2.정답 - 비트 연산

def solution(nums):
    res = 0
    for num in nums:
         res ^= num;
    return res

 

✔️고찰

-1차원 for문이 O(n)이라해서 이 부분에 대해서 오래 고민하진 않고 풀었다. 

-코린이라 시간복잡도에 대한 이해가 매우 낮았는데, 이번 기회를 통해 알 수 있게 되었다. 

참고자료 : https://chancoding.tistory.com/43  (설명이 정말정말 잘되어있다) 

 

[Python] 파이썬 자료형 및 연산자의 시간 복잡도(Big-O) 총 정리

시간 복잡도를 알아야 하는 이유 백준에서 알고리즘을 풀다 보니 '시간 초과'되는 경우를 자주 겪었습니다. 문제를 풀고 나서도 결과 시간이 다른 사람들보다 상당히 높게 나오는 경우가 있었는

chancoding.tistory.com

-지금은 문제에서 이렇게 풀라해서 풀지만 나중에는 내 스스로 이런걸 생각하면서 풀 수 있도록 차근차근 열심히 해야겠다! 

댓글