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. AnnotationConfigApplicationContext

    package com.test; import java.io.IOException; import java.io.InputStream; import java.net.URL; impor ...

  2. sqlserver 索引进阶(上)

    参考原文:http://www.cnblogs.com/tjy9999/p/4494662.html 2. 非聚集索引 SET STATISTICS io ON SET STATISTICS time ...

  3. Oracle 12c DG备库Alert报错ORA-01110

    环境是12.2.0.1 version, Oracle Data Guard备库近段时间一直报错,但是备库主库同步一致,数据一致. 2019-03-06T23:42:22.184048+08:00 E ...

  4. Robot Framework常用库简介

    标准库 Robot Framework可以直接导入使用的库,包括: • Builtin:包含经常需要的关键字.自动导入无需import,因此总是可用的 • Dialogs:提供了暂停测试执行和从用户的 ...

  5. SQL Server Reporting Service(SSRS) 第六篇 SSRS 部署总结

    前段时间完成了第一批次SSRS报表的开发,本来以为大功已经告成,结果没有想到在整个发布与部署过程中还是遇到了很多的问题,现将这些问题一一列举出来,希望对以后能够有所启发! 1. 关于数据源与数据集的发 ...

  6. 性能测试工具LoadRunner11-LR之Virtual User Generator 移动app录制

    准备条件: 1.安装插件LR_03105_Patch4.EXE,安装完成之后就会有Mobile App(HTTP/HTML),如下截图所示 2.安装热点wifi,160wifi(注:有可能有的热点软件 ...

  7. Android微信开放平台,申请移动应用的 应用签名 如何获取

    在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate?t=manage/createMobile&type=app&a ...

  8. java多线程异常捕获

    java多线程中出现了异常,如何捕获.利用UncaughtExceptionHandler这个接口就可以了. 代码如下: package com.ming.thread.six.threadcreat ...

  9. 13、Selenium+python+API分类总结

    Selenium+python+API分类总结 http://selenium-python.readthedocs.org/index.html 分类 方法 方法描述 客户端操作 __init__( ...

  10. pat06-图6. 公路村村通(30)

    06-图6. 公路村村通(30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 现有村落间道路的统计数据表中,列出了有可能建设成标准公路的 ...