본문 바로가기

전체 글269

[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.
[MySQL] COUNT(1), COUNT(*), COUNT(column) 차이 MySQL에서 COUNT(1)과 COUNT(*)의 차이에 대해서 알아봤다. 결론은 COUNT(1)과 COUNT(*) 차이가 없다. COUNT(*)는 NULL 값을 포함하는지 여부에 관계없이 검색된 행 수의 카운트를 반환 COUNT(1), COUNT(0), COUNT(-2312312313) 모두 똑같이 동작함 https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_count MySQL :: MySQL 8.0 Reference Manual :: 12.20.1 Aggregate Function Descriptions MySQL 8.0 Reference Manual / ... / Functions and Operators / Aggre.. 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.
[MySQL] 사칙연산 + 형 변환 https://leetcode.com/problems/not-boring-movies/submissions/ Not Boring Movies - 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 위 문제를 풀면서 사칙연산하는 것 정리 위 문제는 나머지를 구하는 문제다. MOD(X, Y) X를 Y로 나눈 나머지 SELECT id, movie, description, rating FROM Cinema WHERE MOD(id, 2) = 1 AND description '.. 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.
[MySQL] DATE 타입으로 변경 DATE_FORMAT 코테에서 DATE_FORMAT이 생각이 안났다. DATE따로 FORMAT 따로 생각났다. 공부가 많이 부족하다고 느꼈다 정리를 해보자. DATETIME은 YYYY-MM-DD hh:mm:ss의 형식을 가진다 이를 수정하려면 DATE_FORMAT이 필요하다. DATE 타입은 YYYY-MM-DD의 형식을 가진다. 아마 가장 많이 사용되리라 생각하는 DATETIME에서 DATE로 변환하는 방법은 아래와 같다. -- 현재시간 SELECT NOW(); -- 2022-04-02 11:11:37 -- 변환 SELECT DATE_FORMAT(NOW(), '%Y-%m-%d'); -- 2022-04-02 -- YY 형식 SELECT DATE_FORMAT(NOW(), '%y-%m-%d'); -- 22-04-02 Specifi.. 2022. 4. 2.
[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.
Python이 느린 이유? Python이 느린 이유. 지금까지 공부했을 때는 그냥 인터프리터 언어라서~ GIL 때문에~ 이러면서 적당히 넘어갔었는데, 찾아보며 하나하나 공부하다 보니 심오한(?) 내용들이 많았다 일단 내가 이해한 Python이 느린 이유를 간단하게 적고 조금씩 자세하게 추가 해 나가도록 해야겠다. Python은 인터프리터 언어인가요? 나의 생각은 반만 인터프리터 사실 인터프리터, 컴파일러 언어라는게 상당히 애매모호한 개념이다. 1. 사실 Python이 느린 게 아니라 Python의 표준 구현체인 Cpython이 느린 것. - Cpython 과 pypy의 차이 + 구현체가 어떻게 동작하길래? 2. 인터프리터, 컴파일러의 차이 (컴파일이 의미하는 게 뭐길래?) - 사실 Python도 어떻게 보면 컴파일 언어고 어떻게 .. 2022. 4. 2.
[MySQL] 데이터 삭제 https://leetcode.com/problems/delete-duplicate-emails/ Delete 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 위 문제를 풀고 DELETE 구문에 대해 정리하고자 함. DELETE FROM 테이블 이름 WHERE 조건 DELETE p1 FROM PERSON p1, PERSON p2 WHERE p1.email = p2.email and p1.id > p2.id ; 위와 같이 뷰가 두 .. 2022. 3. 31.
[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.