본문 바로가기

IT/코딩테스트

(111)
[Leetcode] 143. Reorder List https://leetcode.com/problems/reorder-list # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reorderList(self, head: Optional[ListNode]) -> None: """ Do not return anything, modify head in-place instead. """ slow = fast = head while fast != None and fast.next != None: slow = slow.next fast = fast..
[Leetcode] 234. Palindrome Linked List https://leetcode.com/problems/palindrome-linked-list # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPalindrome(self, head: Optional[ListNode]) -> bool: vals = [] node = head while node != None: vals.append(node.val) node = node.next for i in range(len(vals)): if vals[i] != vals[len(vals) - i..
[Leetcode] 206. Reverse Linked List https://leetcode.com/problems/reverse-linked-list # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: if head is None: return head nodes = [] node = head while node != None: nodes.append(node.val) node = node.next nodes.reverse() he..
[Leetcode] 1669. Merge In Between Linked Lists https://leetcode.com/problems/merge-in-between-linked-lists # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode: a_pre_node = None b_next_node = None pre_node = None node = list1 index = 0 while node != None: if inde..
[Leetcode] 621. Task Scheduler https://leetcode.com/problems/task-scheduler class Solution: def leastInterval(self, tasks: List[str], n: int) -> int: frequency = [0] * 26 for t in tasks: frequency[ord(t) - ord('A')] += 1 max_frequency_count = frequency.count(max(frequency)) shortest = len(tasks) longest = (max(frequency) - 1) * (n + 1) + max_frequency_count return max(shortest, longest)
[Leetcode] 452. Minimum Number of Arrows to Burst Balloons https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons class Solution: def findMinArrowShots(self, points: List[List[int]]) -> int: if len(points) == 0: return 0 points.sort(key = lambda x: x[1]) count = 1 end = points[0][1] for i in range(1, len(points)): if points[i][0] > end: count += 1 end = points[i][1] return count
[Leetcode] 57. Insert Interval https://leetcode.com/problems/insert-interval/ class Solution: def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]: new = [] for i in range(len(intervals)): if newInterval[1]intervals[i][1]: new.append(intervals[i]) else: newInterval=[min(newInterval[0],intervals[i][0]),max(intervals[i][1],newInterval[1])] new.append(newInterval) return new
[Leetcode] 3083. Existence of a Substring in a String and Its Reverse https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse class Solution: def isSubstringPresent(self, s: str) -> bool: r = s[::-1] substrs = [] for i in range(0, len(s)-1): substrs.append(s[i:i+2]) for s in substrs: if s in r: return True return False

728x90