본문 바로가기

Algorithm

(130)
[해시 테이블] 31 상위 K 빈도 요소(Top K Frequent Elements) https://leetcode.com/problems/top-k-frequent-elements/ Top K Frequent Elements - 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 collections.Counter를 이용하여 풀이한 첫번째 풀이는 다음과 같습니다. class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: count_dict = collections.Co..
[해시 테이블] 30 중복 문자 없는 가장 긴 부분 문자열(Longest Substring Without Repeating Characters) https://leetcode.com/problems/longest-substring-without-repeating-characters/ Longest Substring Without Repeating Characters - 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 중복 문자가 없는 가장 긴 부분 문자열(substring)의 길이를 리턴하는 문제입니다. 모든 substring을 탐색하여 시간복잡도 O(n2)으로 time out이 날것으로 예상했지만, 통과..
[해시 테이블] 29 보석과 돌(Jewels and Stones) https://leetcode.com/problems/jewels-and-stones/ Jewels and Stones - 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 jewels는 보석이고, stones는 갖고 있는 돌일때, stones에는 보석이 몇 개나 있을지 구하는 문제입니다. 이때 대소문자는 구분합니다. 파이썬의 collections.Counter를 이용하여 한 첫번째 풀이는 다음과 같습니다. class Solution: def numJewelsInS..
[해시 테이블] 28 해시맵 디자인(Design HashMap) https://leetcode.com/problems/design-hashmap/ Design HashMap - 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 다음 기능을 제공하는 해시맵을 디자인하는 문제입니다. put(key, value): 키,값을 해시맵에 삽입하고, 만약 이미 존재하는 키라면 업데이트한다. get(key): 키에 해당하는 값을 조회하고, 만약 키가 존재하지 않는다면 -1을 리턴한다. remove(key): 키에 해당하는 키, 값을 해시맵에서..
[데크, 우선순위 큐] 27 k개 정렬 리스트 병합(Merge k Sorted Lists) https://leetcode.com/problems/merge-k-sorted-lists/ Merge k Sorted Lists - 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 k개의 정렬된 리스트를 1개의 정렬된 리스트로 병합하는 문제입니다. 먼저 연결리스트의 모든 값들을 리스트에 넣은 후 풀이하는 방법은 다음과 같습니다. # Definition for singly-linked list. # class ListNode: # def __init__(self,..
[데크, 우선순위 큐] 26 원형 데크 디자인(Design Circular Deque) https://leetcode.com/problems/design-circular-deque/ Design Circular Deque - 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 배열을 통해 구현한 풀이는 다음과 같습니다. 크기가 k인 리스트를 배열로 사용합니다. class MyCircularDeque: def __init__(self, k): """ Initialize your data structure here. Set the size of the de..
[스택, 큐] 25 원형 큐 디자인(Design Circular Queue) - 작성중 https://leetcode.com/problems/design-circular-queue/ Design Circular Queue - 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 class MyCircularQueue: def __init__(self, k: int): self.q = [None]*k #배열로 원형큐 정의 self.maxlen = k #최대길이 저장 self.p1 = 0 #front 포인터 self.p2 = 0 #rear 포인터 #rear ..
[스택, 큐] 24 스택을 이용한 큐 구현(Implement Queue using Stacks) - 작성중 https://leetcode.com/problems/implement-queue-using-stacks/ Implement Queue using Stacks - 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 input과 output 두 개의 스택을 이용해서 queue를 구현합니다. output 스택에 아무것도 없는 상태에서 pop을 수행하려고 할 때 input 스택에 쌓여있는 값들을 전부 output 스택으로 이관시키는 것이 포인트입니다! class MyQue..