[LeetCode] 178. Rank Scores
·
코딩테스트/MySQL
🔗 Problem Linkhttps://leetcode.com/problems/rank-scores/description/❔Thinkingid와 score가 적힌 테이블에서, score가 높은 순서대로 순위를 매겨 반환한다.동점자는 동일한 순위를 부여하고, 순위에 간격은 없도록 한다.💻Solution1. Dense_Rank 함수 사용SELECT score, DENSE_RANK() OVER (ORDER BY score DESC) AS 'rank'FROM Scores 2. 함수 없이 구현자신보다 높은 점수의 개수를 순위로 활용한다.DISTINCT는 동일한 점수를 한번만 세도록 한다.scores 테이블이 커질수록 성능이 저하된다.SELECT s1.score, (SELECT COUNT(DISTINCT ..
[Baekjoon] 1013. Contact
·
코딩테스트/Python
🔗 Problem Linkhttps://www.acmicpc.net/problem/1013❔Thinking주어진 패턴 "(100+1+ | 01)+"과 일치하는 문자열인 경우 "YES", 아니라면 "NO"를 반환한다.💻Solutionimport reimport sysinput = sys.stdin.readlineT = int(input().rstrip())r = re.compile("(100+1+|01)+")for _ in range(T): pattern = input().rstrip() if r.fullmatch(pattern): print('YES') else: print('NO')🗝️keypointre로 compile하면, r을 해당 패턴을 검색하는데 활..
[LeetCode] 643. Maximum Average Subarray I
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/maximum-average-subarray-i/description/❔Thinking숫자 배열 nums와 k가 주어질 때, k개의 연속된 부분 수열 중 가장 큰 평균을 반환한다.💻Solution1. 평균으로 비교하기def findMaxAverage(self, nums: List[int], k: int) -> float: max_avg = -(10**4 * 10**5 + 1) left, right = 0, 0 window_sum = 0 while right 0 else -(-window_sum / k)) window_sum -= nums[left] left = ..
[LeetCode] 283. Move Zeroes
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/move-zeroes/description/❔Thinking숫자 배열 nums가 주어지면, 0을 오른쪽으로 옮기면서 다른 숫자의 순서는 유지해야 한다.새로운 배열을 생성하지 않아야 하기 때문에, 값을 swap하는 방식을 택한다.💻Solution1. 0이 아닌 숫자의 인덱스를 찾는 방식def moveZeroes(self, nums: List[int]) -> None: for i in range(len(nums)): if nums[i] == 0: flag = False non_zero_idx = i while non_zero_idx   2. 투 포인터..
[Programmers] Level 2. 아날로그 시계
·
코딩테스트/Python
🔗 Problem Link 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr❔Thinking아날로그 시계에서, 초침이 주어진 시간 범위 내에서의 시침 또는 분침과 일치하는 횟수를 반환한다.두개의 바늘이 겹친다는 의미는, 하나의 바늘이 1초 후에 다른 바늘보다 앞선다는 뜻과 동일하다.00시와 12시는 모든 바늘이 겹치는 구간이기 때문에, 정확히 예외처리를 해야 한다.💻Solutiondef solution(h1, m1, s1, h2, m2, s2): cnt = 0 init_time = h1*60*60 + m1*60 + s1 end_time = h2*60*60 + m2*60 + s2 i..
[LeetCode] 443. String Compression
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/string-compression/❔Thinking주어진 문자 배열을 압축한다 (ex - ['a','a'] -> ['a','2'])주어진 문자 배열에 압축한 결과를 저장한다.💻Solutiondef compress(self, chars: List[str]) -> int: compressed = [] n = len(chars) cnt = 0 for i in range(1, len(chars)): if chars[i] == chars[i-1]: cnt += 1 else: cnt += 1 compressed.append(cha..