본문 바로가기

leetcode42

[LeetCode] Two City Scheduling https://leetcode.com/problems/two-city-scheduling/ Two City Scheduling - 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 쉬운줄 알았는데 은근히 까다로운 문제다. 처음에는 DP인가? 했다가 완전탐색인가? 했는데 최대 연산이 200! / 100! * 100! 인걸 보고 아닌 것을 깨닫고 그리디로 풀어봤다. 일단 최소한의 비용이 드는 승객들을 더하고 균형을 맞출 때에는 A와 B의 차이가 적은 순서대로 더해줘서 .. 2022. 3. 25.
[LeetCode] Valid Parentheses https://leetcode.com/problems/valid-parentheses/ Valid Parentheses - 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 Solution: def isValid(self, s: str) -> bool: types = { ')' : '(', ']' : '[', '}': '{' } stack = [] for par in s: if par in types: if not.. 2022. 3. 25.
[LeetCode] Swap Nodes in Pairs https://leetcode.com/problems/swap-nodes-in-pairs/ Swap Nodes in Pairs - 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 짝을 이뤄서 노드를 변경하는 것 value 값을 변경하면 안된다. 1 2 까지는 괜찮은데 3 4 를 변경할때 1의 next가 3이 아닌 4가 되어야 해서 이 부분에서 신경을 써줘야 함. 반복문을 이용한 풀이 class Solution: def swapPairs(self, head: Opt.. 2022. 3. 24.
[LeetCode] Boats to Save People https://leetcode.com/problems/boats-to-save-people/ Boats to Save People - 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 보트에 사람을 실을 수 있고(최대 두 명) 무게는 limit 까지만 최소한으로 움직이는 횟수를 구하는 문제 내 풀이 정렬 + 투 포인터를 활용해서 풀었다. class Solution: def numRescueBoats(self, people: List[int], limit: int) .. 2022. 3. 24.
[LeetCode] Add Two Numbers https://leetcode.com/problems/add-two-numbers/ Add Two Numbers - 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 연결 리스트를 뒤집어서 더한 값을 다시 뒤집어서 반환 내 풀이 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next cla.. 2022. 3. 23.
[LeetCode] Reverse Linked List https://leetcode.com/problems/reverse-linked-list/ Reverse Linked List - 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 연결 리스트를 뒤집으면 된다. 단순한 문젠데 뭔가 실제로 해보려니까 한동안 뇌 정지 왔다. 역시 아직 부족하다 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # se.. 2022. 3. 23.
[LeetCode] Broken Calculator https://leetcode.com/problems/broken-calculator/ Broken Calculator - 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 Solution: def brokenCalc(self, startValue: int, target: int) -> int: q : Deque = collections.deque() q.append((startValue, 0)) whil.. 2022. 3. 23.
[LeetCode] Merge Two Sorted Lists https://leetcode.com/problems/merge-two-sorted-lists/ Merge Two 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 정렬된 두 연결 리스트를 크기 순서대로 정렬하여 병합하는 문제 1. list1과 list2를 비교해 주면서 merge_list에 추가 2. 원소가 남은 친구들을 추가 # Definition for singly-linked list. # class ListNode: # def _.. 2022. 3. 23.
[LeetCode] Palindrome Linked List https://leetcode.com/problems/palindrome-linked-list/ Palindrome Linked List - 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 Deque를 활용하면 popleft도 O(1) 이므로 성능 향상 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self... 2022. 3. 23.
[LeetCode] Reverse String https://leetcode.com/problems/reverse-string/submissions/ Reverse String - 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 Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify s in-place instead. """ left, right = 0,.. 2022. 2. 28.
[LeetCode] Valid Palindrome https://leetcode.com/problems/valid-palindrome/ Valid Palindrome - 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 유효한 펠린드롬인지 확인하는 문제 Deque를 활용해서 풀이 가능 class Solution: def isPalindrome(self, s: str) -> bool: strs: Deque = collections.deque() for char in s: if char.isalnum(): strs.a.. 2022. 2. 25.
[LeetCode] Two Sum https://leetcode.com/problems/two-sum/ Two Sum - 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 Leet Code의 문제들을 풀어 볼 예정이다. 처음 문제인 Two Sum 문제를 풀어봤다. 예전 화이트보드 코딩 테스트때 이 문제를 접한 적이 있었는데, 이렇게 유명한 문제인지 몰랐다. 그 때는 잘 못 풀었는데.. 이 문제는 dict 를 사용하면 O(n) 으로 풀이할 수 있다. 나의 풀이 class Solution: def tw.. 2022. 2. 9.