Sliding Window

Seungyun.Lee·2023년 2월 16일
0

Java 코딩테스트

목록 보기
5/8

Sliding Window 알고리즘은 윈도우 즉 일정한 범위를 가지고 있는 것을 유지하면서 이것을 이동하는 것이다.

Sliding Window 알고리즘 문제인지 판별하는 조건 3가지

  1. First thing is, we have given something like an "Array" | OR | "String"
  2. Second thing is, they are talking about either "subsequence" | OR | "substring"
  3. And third most thing is, either we have given a "window size i.e. k" | OR | we have to "manually find out window size"

위의 3가지 조건중 2개이상 충족된다면 100% Sliding Window 알고리즘이다.
Sliding Window는 기본적으로 2가지 유형이있다

  1. Fix size sliding window {means K is given}
  2. Variable size sliding window {means K is not given}
while(j < size()){

    // Calculation's happen's here
-----------------------------------------------
    if(condition < k){
        j++;
    }
-----------------------------------------------

-----------------------------------------------
    else if(condition == k){
        // ans <-- calculation
        j++;
    }
----------------------------------------------

----------------------------------------------
    else if(condition > k){
        while(condition > k){
            // remove calculation for i
            i++;
        }
        j++;
    }
----------------------------------------------
}
return ans;

위의 코드는 2번 유형의 템플릿이다.

0개의 댓글