https://app.codesignal.com/interview-practice/task/APDXraJZYfPSYqQMJ/description
✔️문제 요약
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' 카테고리의 다른 글
[CodeSignal] rotateImage (Python) (0) | 2022.09.14 |
---|---|
[CodeSiganl] missingNumber (Python) (0) | 2022.09.07 |
[CodeSignal] amendTheSentence (Python) (0) | 2022.09.05 |
[CodeSignal] List Beautifier (Python) (0) | 2022.09.05 |
[CodeSignal] Mex Function (Python) (0) | 2022.09.05 |
댓글