2. Sliding Window 중요

FrugalBoy
2025.01.23조회수 4회

FrugalBoy
구독자 5명구독중 12명
Wanna live like a frugal boy
For the genetically superior, success is easier to attain.
But it is by no means guaranteed.
After all, there is no gene for fate
-Gattaca-


i, j 움직여가면서 조건에 맞는 놈 생길때마다 window size가 바뀜
Template:
크게 2가지 step으로 이뤄짐
Expand Window: for문 안에서 첫부분에 j++하는 부분
Shrink Winow: while( invalid) 문에서 i++하는 부분
invalid 발상 떠올리는법: i=0, j=0에서 시작해서 j가 한칸씩 오른쪽으로 간다. 이렇게 j를 한칸씩 움직이면서 어떨때 invalid할지 생각하기
int i = 0, j = 0, ans = 0;
for (; j < N; ++j) {
// CODE: use A[j] to update state which might make the window invalid
for (; invalid(); ++i) { // when invalid, keep shrinking the left edge until it's valid again
// CODE: update state using A[i]
}
ans = max(ans, j - i + 1); // the window [i, j] is the maximum window we've found thus far
}
return ans;
가장 정석적인 문제:
https://leetcode.com/problems/longest-substring-without-r...