일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- Decorator
- t1
- Protocol
- 715. Range Module
- concurrency
- 109. Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- data science
- Python Code
- DWG
- 밴픽
- 43. Multiply Strings
- 컴퓨터의 구조
- 프로그래머스
- shiba
- attribute
- 시바견
- 파이썬
- 30. Substring with Concatenation of All Words
- Regular Expression
- Python
- Generator
- Python Implementation
- 운영체제
- iterator
- Class
- 315. Count of Smaller Numbers After Self
- LeetCode
- kaggle
- Today
- Total
목록분류 전체보기 (407)
Scribbling
My own code is always messy. The idea is to sort the list and calculate sums. Time complexity will be O(NlogN) due to sorting. class Solution: def numPairsDivisibleBy60(self, time: List[int]) -> int: time = [t % 60 for t in time] time.sort() ret = 0 l, r = 0, len(time) - 1 while l < r: if time[l] + time[r] == 60: if time[l] == 30: cnt = 0 while l 0 else c[0] c[t] += 1 return ans
My somewhat complicated code using queue. Both time and memory complexities are O(N). # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Codec: def serialize(self, root): """Encodes a tree to a single string. :type root: TreeNode :rtype: str """ if not root: return '' ret = str(root.val) + ' ' q = [r..
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return None odd_ptr = head even_ptr = even_head = ListNode() cur = head.next cnt = 2 while cur != None: if cnt % 2 != 0: odd_ptr.next = cur odd_ptr = cur else: even_pt..
To solve this problem in-place, we can think of using two more states(2, 3) rather than two states(1, 0). class Solution: def gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify board in-place instead. """ m, n = len(board), len(board[0]) for y in range(m): for x in range(n): cnt = self.countOnes(board, y, x) if board[y][x] == 0: if cnt == 3: board[y][x] = 3 else..
As a novice, it was not easy at all for me to solve this problem. I relied on other people's explanations and codes, and this post is to summarize their ideas. It is very tricky to solve this problem within O(N) time & O(1) memory. (* without modifying the array) The best way is "Floyd's Algorithm" as this problem can be seen as a linked list with a cycle. Check out the below video for further e..
O(N**2) Time Complexity, O(N) Memory Not an effective solution at all especially in terms of time complexity. class Solution: def numSquares(self, n: int) -> int: INT_MAX = int(1e5) dp = [INT_MAX] * (n+1) x = 1 while x * x int: psns = [] x = 1 while x * x int: INT_MAX = int(1e5) dp = [INT_MAX] * (n+1) dp[0] = 0 for i in range(1, n+1): for j in range(1, int(i**0.5)+1): dp[i] = min(dp[i], dp[i-j*j..
Several different solutions class Solution: def searchMatrix(self, matrix: List[List[int]], target: int) -> bool: j = bisect_right(matrix[0], target) - 1 i = bisect_right([mat[0] for mat in matrix], target) - 1 while i >= 0 and j >= 0: if matrix[i][j] < target: return False for k in range(0, i+1): if matrix[k][j] == target: return True for k in range(0, j+1): if matrix[i][k] == target: return Tr..
Using two pointers. class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ j = 0 for i, num in enumerate(nums): if num != 0: nums[j] = num j += 1 for k in range(j, len(nums)): nums[k] = 0
Solution for Q.234 within O(N) time complexity and O(1) memory. To check whether it's a palindrome, we have to check two elements simultaneously from the head and tail. But in singly linked list, we have no way to check the elements in reverse order from the tail. So, the solution is simple. Reverse the second half of the list and compare two lists. Below is the code. # Definition for singly-lin..
가장 먼저 떠오른 방법은 Priority Queue를 사용하는 것. Time Complexity: O(N*logN) import heapq class PriorityQueue: def __init__(self): self.pq = [] self.removals = [] def insert(self, elem): heapq.heappush(self.pq, -elem) def gettop(self): return -self.pq[0] def remove(self, elem): if elem == -self.pq[0]: heapq.heappop(self.pq) while self.removals and self.pq[0] == self.removals[0]: heapq.heappop(self.pq) heapq..