일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
- codeup
- gcp
- 자연어처리
- 슬라이딩윈도우
- slidingwindow
- Python
- 니트코드
- nlp
- 파이썬알고리즘
- medium
- GenerativeAI
- SQL
- 파이썬기초100제
- stratascratch
- Stack
- GenAI
- 투포인터
- sql코테
- 코드업
- 생성형AI
- dfs
- Greedy
- two-pointer
- heap
- 리트코드
- 알고리즘
- Python3
- LeetCode
- 파이썬
- 릿코드
- Today
- Total
목록Greedy (6)
Tech for good

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)를 사용하여 두 리스트를 같은 인..

class Solution: def isPrefixOfWord(self, sentence: str, searchWord: str) -> int: words = sentence.split() for i, word in enumerate(words): if word.startwith(searchword): return i+1 return -1 Explanation:Split the Sentence: The input sentence is split into a list of words using .split(), which automatically handles single spaces.Iterate Over Words..