LeetCode/NeetCode 98

[Sliding Window Fixed Size] 1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold

1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold ✅ 문제 요약 (LeetCode 1343)정수 배열 arr, 정수 k, threshold가 주어짐크기가 k인 연속된 부분 배열(subarray) 중,평균이 threshold 이상인 경우의 개수를 리턴✅ 핵심 아이디어슬라이딩 윈도우 크기 = k평균 ≥ threshold → sum ≥ threshold * k 로 변환해서 비교하면 정수만 다룰 수 있음슬라이딩 윈도우 방식으로 한 칸씩 이동하면서 합을 업데이트✅ 최적 풀이 (Python, O(n) 시간)class Solution: def numOfSubarrays(self, arr: List[int], k:..

LeetCode/NeetCode 2025.04.04

[카데인 Kadane] 978. Longest Turbulent Subarray ★★★★★

978. Longest Turbulent Subarray 이 문제는 **“인접한 숫자의 크고 작음이 번갈아가며 나타나는 subarray”**의 최대 길이를 구하는 문제예요.✅ 핵심 개념🔁 Turbulent Subarray란?인접한 숫자들이 번갈아 가며 크거나 작아지는 구조예요.예:[9, 4, 2, 10, 7, 8, 8, 1, 9]→ [4, 2, 10, 7, 8]는 turbulent→ 4 > 2 7 즉, 비교 부호 (>, 매번 반대여야 해요. ✅ 접근법: 슬라이딩 윈도우 + 비교class Solution: def maxTurbulenceSize(self, arr: List[int]) -> int: len_arr = len(arr) # 길이가 1이면 비교할 쌍이 없으므로 그..

LeetCode/NeetCode 2025.04.04

Graph 그래프 이론

Graph -> 링크드리스트, 트리 둘다 포함 -> vertex (노드) 와 edge (연결방향)으로 구성 -> E    그래서,Tree와 linked list 는 방향성 있는 그래프(directed Graph) 라고 할 수 있고 방향성 없는 그래프(Undirected Graph) 라는 의미는,= 즉, 양방향 모두 갈수 있다는 의미= 양방향 에지(간선)=> matrix 행렬 또는 adjacency list 인접 리스트 를 사용해서 문제 품!! 1. 행렬(Matrix) 문제는?# Matrix (2D Grid)grid = [[0, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0]] 0: Free1: Blocked 위에서 아래로 행..

LeetCode/NeetCode 2025.04.01

1472. Design Browser History

1472. Design Browser History https://leetcode.com/problems/design-browser-history/  https://youtu.be/i1G-kKnBu8k 이 문제는 브라우저의 방문 기록(뒤로가기, 앞으로가기) 기능을 구현하는 시뮬레이션 문제예요.직접 클래스를 설계하고, 함수들을 정의해야 하죠.🧠 문제 요약브라우저에는 다음과 같은 기능이 있어요:처음 접속하는 홈페이지로 시작visit(url): 새로운 페이지 방문 → 현재 위치에서 앞으로 갈 수 있는 기록은 모두 사라짐back(steps): 뒤로 steps만큼 감 (더 못 가면 멈춤)forward(steps): 앞으로 steps만큼 감 (더 못 가면 멈춤)각 함수는 현재 페이지의 URL을 리턴함📦 예시 흐..

LeetCode/NeetCode 2025.03.30

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