[LeetCode] 177. Nth Highest Salary
·
코딩테스트/MySQL
🔗 Problem Linkhttps://leetcode.com/problems/nth-highest-salary/description/❔Thinkingsalary가 담긴 Employee 테이블에서, 상위 n번째의 급여 값을 반환하는 함수를 작성한다.n번째는 중복을 포함하지 않는, 고유한 salary 값에서 계산한다.💻Solution1. DENSE_RANK() 함수를 활용한 풀이CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INTBEGIN RETURN ( SELECT salary FROM ( SELECT DISTINCT(salary), DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FRO..
[LeetCode] 1456. Maximum Number of Vowels in a Substring of Given Length
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/❔Thinking문자열 s와 정수 k가 주어질 때, k개의 문자를 가지는 s의 부분 문자열 가운데에서 모음(a,e,i,o,u)이 최대 몇 개인지 반환한다.s = 'abciiidef', k = 3 인 경우 output은 3이다. (부분 문자열 'iii')s = 'aeiou', k = 2 인 경우 output은 2이다 (어떤 부분 문자열이라도 2) 💻Solution1. two-pointer를 활용한 풀이def maxVowels(self, s: str, k: int) -> int: left = 0 ..
[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 ..
[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. 투 포인터..
[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..
[LeetCode] 238. Product of Array Except Self
·
코딩테스트/Python
🔗 Problem Linkhttps://leetcode.com/problems/product-of-array-except-self/❔Thinking배열이 주어지면, 자신의 원소를 제외한 모든 원소의 곱을 배열에 담아 반환한다.나누기 연산 없이, O(n)의 시간복잡도를 가지는 알고리즘을 작성해야 한다.💻Solutiondef productExceptSelf(self, nums: List[int]) -> List[int]: left, right = 1, 1 n = len(nums) answer = [1] * n for i in range(n): answer[i] *= left left *= nums[i] for i in range(n-1, -1, -1):..