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 https://leetcode.com/problems/minimum-window-substring/description/ -- 76

.................

leetcode problem 76  minimum window substring

follow up of the previous problem: 1.can find duplicate elements  2. string instead of numbers

For string: create the hashmap: key: char, value: integer

For duplicate number: create the original hashmap for conditions.

//if(num==0) sum++;
//if(num<1) sum++; // first appears or <
if(num<Tmap.get(c)) sum++;
map.put(c,++num);

the evolving process from the last to the current one and it is interesting that num id always >=0

class Solution {
public String minWindow(String s, String t) {
int K = t.length();
int N = s.length();
int L = -1,R = -1;
int left = -1, right = -1;
int sum = 0;// size of subarray of T
int ans = N+1;
Map<Character, Integer> map = new HashMap<Character, Integer>();
Map<Character, Integer> Tmap = new HashMap<Character, Integer>();
for(Character c : t.toCharArray()){
map.put(c,0);
if(Tmap.containsKey(c)){
Tmap.put(c,Tmap.get(c)+1);
}else Tmap.put(c,1);
}
while(true){
if(sum==K){
if(ans > (right-left)){
//ans = Math.min(ans, right-left);
ans = right-left;
L = left;
R = right;
}
}
if(sum<K){ //
right++;
if(right>=N) break;
//find the elements
char c = s.charAt(right);
if(map.containsKey(c)){
int num = map.get(c);
//if(num==0) sum++;
//if(num<1) sum++; // first appears or <
if(num<Tmap.get(c)) sum++;
map.put(c,++num);
}
}else{
left++;
if(left>=N) break;
char c = s.charAt(left);
if(map.containsKey(c)){
int num = map.get(c);
num--;
map.put(c,num);
//if(num==0) sum--;
//if(num<1) sum--;
if(num<Tmap.get(c)) sum--;
}
} }
if((L==-1) && (R==-1) )
return "";
else return s.substring(L+1,R+1);
}
}

String: str.length(), str.toCharArray(), s.substring(), s.charAt();

Hashmap: map.containsKey(), map.put(key,value), map.get(key), map.entrySet, Map.entry<Key,Value>

Sliding Window - The Smallest Window II(AIZU) && Leetcode 76的更多相关文章

  1. window.onload和window.document.readystate的探究

    在编写前端页面的时候,我们时常需要对页面加载的状态进行判断,以便进行相应的操作. 比如在移动端,时常需要在页面完全加载完成之前,先显示一个loading的图标,等待页面完成加载完成后,才显示出真正要展 ...

  2. window.location.href = window.location.href 跳转无反应 a 超链接 onclick 点击跳转无反应

    错误写法 , 主要是在 href="#"这里 <a href="#" id="send" onclick="return b ...

  3. window.parent与window.openner区别介绍

    今天总结一下js中几个对象的区别和用法: 首先来说说 parent.window与top.window的用法 "window.location.href"."locati ...

  4. window.parent 与 window.opener

    window.parent针对iframe,window.opener针对window.open 父页面parent.jsp: <%@ page language="java" ...

  5. window.location和window.open

    window.location和window.open的区别 window.location = "http://www.baidu.com" 跳转后有后退功能 window.lo ...

  6. JavaScript(Iframe、window.open、window.showModalDialog)父窗口与子窗口之间的操作

    一.Iframe 篇 公共部分 //父对象得到子窗口的值 //ObjectID是窗口标识,ContentID是元素ID function GetValue(ObjectID,ContentID) { ...

  7. 关于window.onload,window.onbeforeload与window.onunload

    ★  window.onload  当页面加载完毕的时候执行,即在当前页面进行其他操作之前执行.如,刚进入某个网页的弹窗提示. (  与window.onload相近的可以参考我写的另外一篇记录&qu ...

  8. JavaScript中,window.opener是什么?window.parent和window.opener有啥区别?

    来自CSDN的问答: window.opener是什么啊? ++++++++++++++++++++++++++++++++++++++++++++++++++ 弹出本窗体的句柄 比如你想点一个按钮直 ...

  9. window.parent与window.openner 之前的总结

    今天总结一下js中几个对象的区别和用法: 1.首先来说说 parent.window与top.window的用法 "window.location.href","loca ...

随机推荐

  1. Educational Codeforces Round 13 B

    Description The girl Taylor has a beautiful calendar for the year y. In the calendar all days are gi ...

  2. JS 打乱数组顺序

    function rand(arr) { var len = arr.length //首先从最大的数开始遍历,之后递减 for(var i = arr.length - 1; i >= 0; ...

  3. java——数据结构

    底层数据结构: 数组 ArrayList 链表 LinkedList 应用数据结构: 二分搜索树 BST 最大堆/最小堆 MaxHeap/MinHeap 线段树 SegmentTree 字典树 Tri ...

  4. my.副本

    梦幻西游手游 天蓬下凡 副本 第三关:天蓬终于决定要告白了,主怪大唐,护卫狮驼岭,帮凶是龙宫,打过这关也很简单. 第四关:康太尉找来了天兵,怪仍然是有法有攻,但是打起来也是不难. ZC: 姚太尉是物理 ...

  5. spring中注解注入 context:component-scan 的使用说明

    通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描 ...

  6. redis的三种启动方式,个人常用第二种

    redis的启动方式1.直接启动  进入redis根目录,执行命令:  #加上‘&’号使redis以后台程序方式运行 1 ./redis-server & 2.通过指定配置文件启动  ...

  7. keil版本控制

    keil5.15才有对git svn的支持,之前的有其它的svcs;   http://www.keil.com/appnotes/files/apnt_279.pdf http://stdbit.c ...

  8. Hadoop Intro - Configure 01

    hadoop配置文件详解.安装及相关操作   一.      Hadoop伪分布配置 1. 在conf/hadoop-env.sh文件中增加:export JAVA_HOME=/home/Java/j ...

  9. Vertex And Fragment Shader(顶点和片段着色器)

    Vertex And Fragment Shader(顶点和片段着色器) Shader "Unlit/ Vertex­_And_Fragment_Shader " { Proper ...

  10. (转)DNS原理及其解析过程

    DNS原理及其解析过程原文:http://blog.51cto.com/369369/812889 网络通讯大部分是基于TCP/IP的,而TCP/IP是基于IP地址的,所以计算机在网络上进行通讯时只能 ...