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. hdu2553 N皇后问题(dfs+回溯)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  2. nginx安装及高可用

    nginx的安装: server1: tar zxf nginx-1.14.0.tar.gz cd nginx-1.14.0/src/core/ vim nginx.h cd /root/nginx- ...

  3. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  4. GM MDI Tech 3 VS GM tech 2

    Many customers ask for this question: what is the difference between GM tech 2 and GM MDI Tech 3 sca ...

  5. layui upload封装

    <link rel="stylesheet" href="layui/css/layui.css"> <script src="js ...

  6. Unity Animation动画倒播

  7. Oracle PL/SQL 实现excel PMT函数、PPMT函数

    PMT函数 1.每月本息金额  = (本金×月利率×(1+月利率)^还款月数)÷ ((1+月利率)^还款月数-1) ,in_financeAmount in number) return number ...

  8. jemeter接口测试基础

    前言: 本文主要针对http接口进行测试,使用Jmeter工具实现. Jmter工具设计之初是用于做性能测试的,它在实现对各种接口的调用方面已经做的比较成熟,因此,本次直接使用Jmeter工具来完成对 ...

  9. 揭秘企业级web负载均衡完美架构

    相信很多朋友对企业级的负载均衡高可用实例非常感兴趣,此篇文章根据成熟的线上环境而写,旨在帮助大家迅速架构一个企业级的负载均衡高可用的web环境. 此系统架构仅映射内网VIP的80及443端口于外网的J ...

  10. machine learning 线性回归实战

    matlab 线性回归实战 统一 输入时列向量 输出也是列向量 中间的过程可以出现行向量或者列向量,但是不能影响输入和输出为列向量 参数运算的输入都不会只是一个实数,要么是列向量,要么是一个矩阵 对于 ...