일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- 코드업
- LeetCode
- slidingwindow
- gcp
- 자연어처리
- stratascratch
- 파이썬기초100제
- 니트코드
- Python3
- heap
- GenAI
- SQL
- sql코테
- Greedy
- Stack
- array
- codeup
- two-pointer
- 리트코드
- Python
- nlp
- 파이썬
- 생성형AI
- 파이썬알고리즘
- GenerativeAI
- 투포인터
- 릿코드
- dfs
- 알고리즘
- 슬라이딩윈도우
- Today
- Total
목록Greedy (11)
Tech for good

https://leetcode.com/problems/jump-game/description/class Solution: def canJump(self, nums: List[int]) -> bool: max_reach = 0 for i in range(len(nums)): if i > max_reach: return False # Current index is unreachable max_reach = max(max_reach, i + nums[i]) # Update the farthest reachable index return True✅ Explanation:max_reach keeps t..

class Solution: def minSubsequence(self, nums: List[int]) -> List[int]: nums.sort(reverse=True) totalSum = sum(nums) result = [] current = 0 for num in nums: result.append(num) current += num if current > totalSum - current: break return resultclass Solution: def minSubsequence(self, nums: List[int]) ..

class Solution: def minMovesToSeat(self, seats: List[int], students: List[int]) -> int: students.sort(reverse=True) seats.sort(reverse=True) minMove = 0 for i in range(len(students)): minMove += abs(students[i] - seats[i]) return minMove✅ Why Use abs() (Absolute Value)?The problem asks for the total number of moves to align each student with a sea..

class Solution: def largestOddNumber(self, num: str) -> str: for i in range(len(num)-1, -1, -1): if int(num[i]) % 2 == 1: return ''.join(num[:i+1]) return "" # 64278# i = 3# num = [6, 4, 2, 7, 8]# num[:i] -> [6, 4, 2]# num[:i+1] -> [6, 4, 2, 7] Backward iteration in Pythonrange function is provided with three arguments(N, -1, -1). Use th..

class Solution: def maximumUnits(self, boxTypes: List[List[int]], truckSize: int) -> int: # Sort boxTypes by units per box in descending order boxTypes.sort(key=lambda x: x[1], reverse=True) total_units = 0 for boxes, units_per_box in boxTypes: if truckSize == 0: break # Take as many boxes as possible, up to the truckSiz..

class Solution: def largestPerimeter(self, nums: List[int]) -> int: nums.sort(reverse=True) # Sort descending for greedy check for i in range(len(nums)-2): a, b, c = nums[i], nums[i+1], nums[i+2] if b + c > a: # Triangle inequality return a + b + c return 0 for i in range(len(nums) - 2):is used because we are checking triplets of side ..

class Solution: def lemonadeChange(self, bills: List[int]) -> bool: five, ten = 0, 0 for bill in bills: if bill == 5: five += 1 elif bill == 10: if five == 0: return False five -= 1 ten += 1 else: if ten > 0 and five > 0: ten -= 1 ..

Look through Example g = child A want 1 number of cookie, child B want 2 number of cookies, child C want 3 number of cookiess = cookie A is 1 number of cookie, cookie B is 2 number of cookiesclass Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() child = 0 # pointer to children cookie = 0 # pointer to cookies whi..

class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort() return sum(nums[::2])🧠 Greedy Strategy:To maximize the sum of the minimums of all n pairs, you should:Sort the arrayAlways pair adjacent elements: (nums[0], nums[1]), (nums[2], nums[3]), ...This ensures that the smaller number in each pair is as large as possible — which directly contributes to the fina..

첫번째 방법 (Two-Pointer + Queue = Greedy)(1) Two-Pointer 두 개의 리스트 (positives, negatives)를 따로 저장한 후, 두 개의 포인터를 이용하여 한 번에 두 리스트를 합치는 방식 차용.여기서 zip(positives, negatives)를 사용하여 두 리스트를 같은 인덱스에서 번갈아가며 가져오는 방식을 적용.(2) Queue Python의 리스트 (list)를 Queue처럼 사용하여 양수와 음수를 저장했다가 순차적으로 가져와서 최종 리스트를 생성하는 방식.positives.append(num) 및 negatives.append(num) 연산은 O(1) 이므로 효율적임.여기서 zip(positives, negatives)를 사용하여 두 리스트를 같은 인..