[Python] 448. Find All Numbers Disappeared in an Array 1부터 n까지 있는 리스트 nums에 빠진 리스트를 return하는 문제 class Solution: def findDisappearedNumbers(self, nums: List[int]) -> List[int]: s = set(nums) return [i for i in range(1, len(nums)+1) if i not in s] Runtime : 19ms (92.77%)Memory : 31.50MB (41.04%) 코딩 공부/Leetcode 2025.02.02
[Python] 414. Third Maximum Number nums의 숫자가 3개 이상이면, 세 번째로 큰 수를 return2개 이하면, 가장 큰 수를 return하는 문제 class Solution: def thirdMax(self, nums: List[int]) -> int: n = sorted(list(set(nums))) if len(n) set 후 list를 취해준 것을 sorted까지 진행했다(음수까지 정렬하려면 필요했음) Runtime : 0msMemory : 19.16MB (21.26%) 코딩 공부/Leetcode 2025.02.01
[Python] 1051. Height Checker 정렬한 리스트와 기존 리스트가 다른 숫자를 count하는 문제 class Solution: def heightChecker(self, heights: List[int]) -> int: heig = sorted(heights) a = 0 for i in range(len(heig)): if heights[i] != heig[i]: a += 1 return asorted한 리스트와 비교해서 a를 1씩 올려줌 Runtime : 0msMemory : 17.94MB (6.58%) 코딩 공부/Leetcode 2025.01.31
[Python] 905. Sort Array By Parity 난이도 : easy nums 내부의 홀수를 리스트 뒷쪽으로 미는 문제 class Solution: def sortArrayByParity(self, nums: List[int]) -> List[int]: left = 0 for i in range(len(nums)): if nums[i] % 2 == 0: nums[left], nums[i] = nums[i], nums[left] left += 1 return nums Runtime : 2ms (57.60%)Memory : 18.38MB (52.70%) 코딩 공부/Leetcode 2025.01.30
[Python] 283. Move Zeroes 난이도 : easy nums에서 0을 뒤로 전부 미는 문제 class Solution: def moveZeroes(self, nums: List[int]) -> None: """ Do not return anything, modify nums in-place instead. """ a = [x for x, val in enumerate(nums) if val == 0] for i in range(len(a)-1, -1, -1): del nums[a[i]] nums.append(0) Runtime : 11ms (25.72%)Memory : 18.6.MB (60.92%) 코딩 공부/Leetcode 2025.01.29
[Python] 1299. Replace Elements with Greatest Element on Right Side 난이도 : easy index가 i일 때, i+1부터 끝까지 수 중 가장 큰 값을 arr[i]로 바꿔주는 문제return은 arr로만 진행 class Solution: def replaceElements(self, arr: List[int]) -> List[int]: for i in range(len(arr)-1): arr[i] = max(arr[i+1:]) arr[-1] = -1 return arr처음 풀었던 방법말 그대로 max값으로 바꿔주고, 마지막 줄을 -1로 바꾸는 형식하지만 time limit에 걸렸다.. class Solution: def replaceElements(self, arr: List[int]) -> List[in.. 코딩 공부/Leetcode 2025.01.28
[Python] 1462. Course Schedule IV 난이도 : medium 총 길이가 numCourses인 코스에서 prerequisites라는 path 대로 갈 수 있을 때, queries의 요소들이 각각 이동할 수 있는지 True와 False을 return하는 문제. from collections import defaultdictclass Solution: def checkIfPrerequisite(self, numCourses: int, prerequisites: List[List[int]], queries: List[List[int]]) -> List[bool]: graph = defaultdict(list) for pre, post in prerequisites: graph[pre].. 코딩 공부/Leetcode 2025.01.28
[Python] 941. Valid Mountain Array 난이도 : easy 위의 사진과 같이, mountaiin array가 성립하면 True를 return하는 문제 class Solution: def validMountainArray(self, arr: List[int]) -> bool: peak = max(arr) top = arr.index(peak) left = 0 right = len(arr) - 1 if top == left or top == right: return False while left = arr[left + 1]: return False left += 1 whil.. 코딩 공부/Leetcode 2025.01.27
[Python] 1346. Check If N and Its Double Exist 난이도 : easy arr에 있는 요소 중 2배인 원소가 있으면 true를 returnclass Solution: def checkIfExist(self, arr: List[int]) -> bool: ans = [] for i in arr: if i * 2 in ans or i / 2 in ans: return True ans.append(i) return Falseans라는 리스트를 설정하고, ans 리스트 내에 2배나 1/2인 원소가 있으면 true를 return.중복되지 않도록 일부러 if문 뒤에 append를 추가 Runtime : 3ms (42.22%)Memory : 18.05MB (6... 코딩 공부/Leetcode 2025.01.26
[Python] 2425. Bitwise XOR of All Pairings 난이도 : Medium 양수로만 구성된 num1와 num2라는 배열이 있는데, 두 배열의 요소들을 하나씩 XOR 취한 것을 num3로 취한다.이 num3의 요소를 또, 전부 XOR 취한 값을 계산하는 문제 from functools import reduceclass Solution: def xorAllNums(self, nums1: List[int], nums2: List[int]) -> int: ans = [x ^ y for x in nums1 for y in nums2] return reduce(lambda x, y : x ^ y, ans)이렇게 정석적으로 풀었더니, Memory limit에 걸렸다.. 하하 이 문제를 풀기 위해서는 먼저 XOR의 특성이 중요한데, XO.. 코딩 공부/Leetcode 2025.01.25