Programming/LeetCode37 [LeetCode] Balanced Binary Tree (균형 이진 트리 확인 Python) https://leetcode.com/problems/balanced-binary-tree/ Balanced Binary Tree - 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 이하 깔끔한 풀이 class Solution: def isBalanced(self, root): self.bal = True def dfs(node): if not node.. 2022. 4. 20. [LeetCode] Trim a Binary Search Tree (트리 순환 재귀) https://leetcode.com/problems/trim-a-binary-search-tree/ Trim a Binary Search Tree - 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 a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.va.. 2022. 4. 15. [LeetCode] Spiral Matrix II (나선 매트릭스 순환) https://leetcode.com/problems/spiral-matrix-ii/ Spiral Matrix II - 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 generateMatrix(self, n: int) -> List[List[int]]: matrix = [[0] * n for _ in range(n)] x, y, d.. 2022. 4. 13. [LeetCode] Game of Life https://leetcode.com/problems/game-of-life/ Game of Life - 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 gameOfLife(self, board: List[List[int]]) -> None: """ Do not return anything, modify .. 2022. 4. 12. [LeetCode] Diameter of Binary Tree (이진트리 가장 긴 경로) https://leetcode.com/problems/diameter-of-binary-tree/ Diameter of Binary Tree - 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: longest: int = 0 def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int: def dfs(node): if not node: return -1 left = dfs(node.l.. 2022. 4. 8. [LeetCode] Maximum Depth of Binary Tree (이진트리 최대 깊이 구하기) https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - 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 트리에서 깊이(Depth)는 루트에서부터 현재 노드까지의 거리이다. 최대 깊이는 리프 노드까지 거리 중 가장 긴 거리를 구하면 된다. 재귀 방식 # Definition for a binary tree node. # class TreeNode: # def _.. 2022. 4. 8. [LeetCode] Sales Analysis III (최소 최대 GROUP BY MINMAX 활용) https://leetcode.com/problems/sales-analysis-iii/ 그렇게 어려운 문제는 아니지만 여러 풀이 방법이 있었다. 처음 생각한 단순한 풀이 SELECT product_id, product_name FROM Product WHERE product_id IN( SELECT product_id FROM Sales WHERE sale_date BETWEEN '2019-01-01' AND '2019-03-31' ) AND product_id NOT IN( SELECT product_id FROM Sales WHERE sale_date NOT BETWEEN '2019-01-01' AND '2019-03-31' ) ; 딱 봐도 뭔가 비효율적이다. GROUP BY + HAVING SEL.. 2022. 4. 5. [LeetCode] Swapping Nodes in a Linked List https://leetcode.com/problems/swapping-nodes-in-a-linked-list/ Swapping Nodes in a 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 문제 풀이 접근이 어려웠다. 왼쪽까지 움직이고 왼쪽부터 next가 null일 때까지 움직이면 right가 나오는 것을 캐치하면 풀 수 있는 문제 단순 val 바꿔주는 코드 class Solution: def swapNodes(self, head:.. 2022. 4. 4. [LeetCode] 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 중복없는 가장 긴 증가하는 부분수열 문제 투 포인터와 dict 자료구조를 활용하면 된다. 예전 풀이 class Solution: def lengthOfLongestSubstring(self, .. 2022. 4. 3. [LeetCode] Tree Node https://leetcode.com/problems/tree-node/ Tree인지 Inner 인지 Leaf인지 확인하는 문제 SQL 문에서 CASE WHEN 사용하고 조건문을 작성시 서브쿼리를 사용하면 깔끔하게 풀이 가능 SELECT id, CASE WHEN p_id is NULL THEN 'Root' WHEN id IN (SELECT p_id FROM tree) THEN 'Inner' ELSE 'Leaf' END AS type FROM Tree order by id ; 2022. 4. 3. [LeetCode] Next Permutation https://leetcode.com/problems/next-permutation/ Next Permutation - 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. 리스트 오른쪽부터 왼쪽으로 탐색하며 오른쪽 원소와 왼쪽 원소를 비교해서 값이 작아진 경우를 찾는다. - 만약 끝까지 찾지 못하였으면 리스트 전체가 역순으로 정렬되어 있으므로.. 2022. 4. 3. [LeetCode] Valid Palindrome II https://leetcode.com/problems/valid-palindrome-ii/submissions/ Valid Palindrome II - 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(object): def validPalindrome(self, s): """ :type s: str :rtype: bool """ # Time: O(n) # Space: O(n) left, right = 0,.. 2022. 4. 2. [LeetCode] Department Highest Salary https://leetcode.com/problems/department-highest-salary/ Department Highest Salary - 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 부서별 봉급이 가장 높은 사람의 아이디, 봉급, 부서이름을 출력 나의 풀이 SELECT D.name AS Department, Employee, salary FROM ( SELECT name AS Employee, salary, E.departmentId FROM E.. 2022. 3. 31. [LeetCode] Customers Who Never Order https://leetcode.com/problems/customers-who-never-order/ Customers Who Never Order - 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 상품을 주문한 적 없는 회원을 찾는 문제 나의 풀이 - Left Join 활용 SELECT Customers FROM (SELECT name AS Customers, customerId FROM Customers AS c LEFT JOIN Orders AS o ON .. 2022. 3. 31. [LeetCode] Duplicate Emails https://leetcode.com/problems/duplicate-emails/ Duplicate Emails - 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 email이 중복된 id를 출력하면 되는 문제 SELECT email AS Email FROM Person GROUP BY email HAVING COUNT(email) > 1 ; GROUP BY + HAVING을 활용하여 간단하게 풀이할 수 있다. 2022. 3. 31. 이전 1 2 3 다음