목표없이 풀다보니 의지가 약해지는 것 같아 'data structure'로 묶인 문제를 풀기로 결정.
https://leetcode.com/study-plan/data-structure/?progress=x0f8sqb1
Data Structure - Study Plan - LeetCode
In computer science, a data structure is a way to store and organize data. During the computer programming process, identifying and using the appropriate data structure is an important task as it can improve the overall efficiency of the algorithm. In larg
leetcode.com
그 중 1일차 Array 문제
https://leetcode.com/problems/contains-duplicate/?envType=study-plan&id=data-structure-i
Contains Duplicate - 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
중복 숫자가 있으면 true, 없으면 false 아주 쉬운 문제다.
이런건 역시 hashtable로 푸는게 최적일 것 같다.
파이썬 풀이1
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
table = {nums[0]}
for n in nums[1:]:
if n in table:
return True
table.add(n)
return False
시간복잡도는 O(N)에 iteration도 중간에 끝나는 괜찮은 코드인 것 같다.
파이썬 풀이2
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
return len(nums) != len(set(nums))
진짜 naive하게 짠 코드지만 의외로 이 코드는 메모리는 많이 먹어도 속도가 빠르다.
아무래도 매번 이터레이션을 도는 것 보다 set을 하나 새로 만들고
2개의 object의 길이를 built-in property에서 가져오는 것이 더 빠른 것 같다.
의외로 이게 pythonic한 코드일지도...?
코드 인터뷰에서는 1번처럼 작성하고
실무에서는 2번처럼 작성할 것 같다.
'Computer Science > CODINGTEST_PRACTICE' 카테고리의 다른 글
[LeetCode] 27. Remove Element (0) | 2023.01.06 |
---|---|
[LeeCode] 53. Maximum Subarray (0) | 2022.12.18 |
[LeetCode] 26. Remove Duplicates from Sorted Array 기록 (0) | 2022.11.21 |
[LeetCode] 21. Merge Two Sorted Lists 기록 (0) | 2022.11.16 |
[LeetCode] 151. Reverse Words in a String 기록 (0) | 2022.11.13 |