본문 바로가기

leetcode42

[MySQL] 그룹별 상위 N 개 https://leetcode.com/problems/department-top-three-salaries/ Department Top Three Salaries - 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 위의 문제를 풀다 RANK에서 그룹별로 처리하는 방법을 알게 되어 정리. 결론은 그룹별로 해당 Row의 Rank를 매기고 싶을 때 PARTITION BY를 사용하면 된다. SELECT Department, Employee, salary FROM ( SE.. 2022. 3. 31.
[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.
[LeetCode] Employees Earning More Than Their Managers https://leetcode.com/problems/employees-earning-more-than-their-managers/ Employees Earning More Than Their Managers - 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 E.name AS Employee FROM Employee E JOIN Employee M ON E.managerId = M.id WHE.. 2022. 3. 31.
[LeetCode] Consecutive Numbers (MySQL) https://leetcode.com/problems/consecutive-numbers/ Consecutive 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 row 3개 연속해서 등장하는 num을 찾는 문제. 1. Where IN 활용 select distinct Num as ConsecutiveNums from Logs where (Id+1,Num) in (Select * from Logs) and (ID+2, Num) in (Select .. 2022. 3. 31.
[MySQL] rank함수 - 랭킹 https://leetcode.com/problems/rank-scores/ Rank Scores - 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 위 문제를 풀면서 알게 된 사실을 정리. MySQL 8 버전 이상부터는 rank, dense_rank 함수 사용 가능 MySQL 버전 확인 SELECT VERSION() rank 함수와, dense_rank 함수 모두 같은 순위는 같은 랭크로 처리됨 rank함수는 순위가 밀려남 EX) 1, 1, 3, 4, 5 den.. 2022. 3. 31.
[LeetCode] Combine Two Tables (MySQL) https://leetcode.com/problems/combine-two-tables/ Combine Two Tables - 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 personId로 조인하는 문제 outer join을 하면 된다. SELECT firstName, lastName, city, state FROM Person p LEFT JOIN Address a ON p.personId = a.personId ; 2022. 3. 31.
[LeetCode] Search a 2D Matrix https://leetcode.com/problems/search-a-2d-matrix/ Search a 2D Matrix - 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 searchMatrix(self, matrix: List[List[int]], target: int) ->.. 2022. 3. 30.
[LeetCode] 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.head = 0 self.tail = 0 self.size = k def enQueue(self, value: int.. 2022. 3. 29.
[LeetCode] 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 스택을 두 개 사용해서 큐를 구현하는 문제 나의 풀이 class MyQueue: def __init__(self): self.s1 = list() self.s2 = list() def push(self, x: int) -> None: self.s1.appe.. 2022. 3. 29.
[LeetCode] Implement Stack using Queues(큐로 스택 구현) https://leetcode.com/problems/implement-stack-using-queues/ Implement Stack using Queues - 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 큐로 스택을 구현하는 근본 문제 push 할 때 append 해주고 기존에 queue에 있던 원소는 popleft 이후 다시 큐의 맨 뒤로 줄줄이 붙여주면 된다. 요소 삽입 시 시간복잡도가 O(N)이 되어 다소 비효율적이긴 하지만, 큐로 스택을 구현할 수 .. 2022. 3. 29.
[LeetCode] Daily Temperatures https://leetcode.com/problems/daily-temperatures/ Daily Temperatures - 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 dailyTemperatures(self, temperatures: List[int]) -> List[int]: stack = [] answer = [0] * len(temp.. 2022. 3. 29.
[LeetCode] Remove Duplicate Letters https://leetcode.com/problems/remove-duplicate-letters/ Remove Duplicate Letters - 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 removeDuplicateLetters(self, s: str) -> str: last_occ = {} visited = set() stack = [] for i in range(len(s)): last_occ[s[i]] = i fo.. 2022. 3. 29.
[LeetCode] Number of Islands https://leetcode.com/problems/number-of-islands/ Number of Islands - 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 매우 유명한 섬 문제 dfs로 풀이했다 class Solution: def numIslands(self, grid: List[List[str]]) -> int: move = [(0,1),(0,-1),(1,0),(-1,0)] def OOB(x, y): return x=len(grid) or y=l.. 2022. 3. 25.