LeetCode 37

27. Remove Element

27. Remove Element https://leetcode.com/problems/remove-element/description/ class Solution: def removeElement(self, nums: List[int], val: int) -> int: l, r = 0, len(nums)-1 while(l 좋아요! 전체적으로는 나쁘지 않은 접근이에요 — 양쪽 포인터(l, r)를 사용해서 val 값을 뒤로 보내는 방식인데, 이 코드에는 두 가지 문제점이 있어요:❌ 문제 1: l l == r일 때 마지막 값을 체크하지 못하고 루프가 끝나요.예: [2,2,3], val = 3이면 마지막 3을 못 체크함.❌ 문제 2: return l + 1은 정확한 개수를 보장하..

LeetCode/NeetCode 2025.03.30

[Two Pointers] 26. Remove Duplicates from Sorted Array

26. Remove Duplicates from Sorted Array https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ class Solution: def removeDuplicates(self, nums: List[int]) -> int: left, right = 0, 0 while right  주의 할 점 바로 return left 대신 return left + 1을 해줘야 해요.이유:left는 마지막 고유한 원소의 인덱스이고, 문제는 고유한 원소의 개수 k를 반환하라고 했죠.예를 들어 고유한 값이 5개면, 인덱스는 0부터 4까지니까 left = 4가 되는데, 실제 고유한 개수는..

LeetCode/NeetCode 2025.03.30

[Two Pointers] 42. Trapping Rain Water ★★★

42. Trapping Rain Water https://youtu.be/ZI2z5pq0TqA?si=vMNp4_SRqkju2a6b  O(n) 공간 사용from typing import Listclass Solution: def trap(self, height: List[int]) -> int: # 높이 배열의 길이를 가져옵니다. n = len(height) # 배열이 비어있는 경우 0을 반환 (예외 처리) if not n: return 0 # maxLeft: 각 위치에서 왼쪽으로 가장 높은 기둥의 높이를 저장할 리스트 # maxRight: 각 위치에서 오른쪽으로 가장 높은 기둥의 높이를 저장할 리스트 ..

LeetCode/NeetCode 2024.11.28

[LeetCode 75] Medium - 2300. Successful Pairs of Spells and Potions

2300. Successful Pairs of Spells and Potions https://youtu.be/OKnm5oyAhWg?si=k_d-0f6Nt10xOCD6 class Solution: def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]: # spell * potion >= success 를 만족하는 쌍을 찾아야 함 # 이를 변형하면: potion >= success / spell 이 됨 # 즉, potion의 값이 특정 기준값 이상인 경우를 찾아야 함. # 1. potions 리스트를 정렬하여 이진 탐색을 수행 가능하도록 준비..

LeetCode/LeetCode75 2024.11.22