http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_3_B
For a given array a1,a2,a3,...,aNa1,a2,a3,...,aN of NN elements and an integer KK, find the smallest sub-array size (smallest window length) where the elements in the sub-array contains all integers in range [1,2,...,K1,2,...,K]. If there is no such sub-array, report 0.
Idea: two pointer. 1.one is left and another is right, start from 0
2.move right pointer first until finding all the elements satisfying the requirements
3.move left (narrowing the subarray) until break the(sum<k)
4.then move right (repeat 2,3)
5.end with two pointers move to end
import java.util.Scanner; public class SmallestWindow2 { //http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_3_A //http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2692179#1 -- reference public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int K = in.nextInt();//target int a[] = new int[n+1]; for(int i = 1; i<=n ;i++){ a[i] = in.nextInt(); } int tag[] = new int[K+1]; //use two pointer int p1 = 0, p2 = 0; int min = n+1;//smallest size int sum = 0; //moving the pointer and check the sum < k while(p1<=n){ //p2 = p1; if(sum==K) { min = Math.min(min, p2-p1); //System.out.println(min); } //main body //move right pointer if(sum<K){ p2++; if(p2>n) break; if(a[p2] <= K){ if(tag[a[p2]]==0) sum++; tag[a[p2]]++; } }else { p1++; if(p1>n) break; if(a[p1]<=K){ tag[a[p1]]--; if(tag[a[p1]]==0) sum--; } } } if(min == n+1) System.out.println(0); else System.out.println(min); } }
folder: smallest window with two pointers
similar problems: the folder in AIZU(1-4)
leetcode problem
.................