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. VScode中Go的相关插件的安装

    一.安装Go插件失败 使用VScode时,当我们安装完go语言扩展时,新建一个go的源码文件,进行保存时,会提示我们需要安装一些go的扩展插件,可别小看这些插件,这些插件都是非常有用的,比如说自动补全 ...

  2. 阿里云 Ubuntu16.04 部署 LAMP

    1.更新软件源 sudo apt-get update 2.安装Apache sudo apt-get install apache2 3.查看Apache是否安装成功 apache2 –v 如下所示 ...

  3. 通过zookeeper连接hive beeline

    beeline -u 'jdbc:hive2://zk01:2181,zk02:2181,zk03:2181/;serviceDiscoveryMode=zooKeeper;zooKeeperName ...

  4. 创建有关hbase数据库的项目时所遇到的问题

    1.在以前使用其他数据库时,经常会使用id自增来做主键,但是hbase数据库中不知道怎么来设置自增主键,所以我打算不要id自增主键.然后删除原来的表,重新创建表. 删除表语句: 用drop命令可以删除 ...

  5. vue-cli中webpack配置解析

    版本号 vue-cli 2.8.1 (终端通过vue -V 可查看) vue 2.2.2 webpack 2.2.1 目录结构 ├── README.md ├── build │ ├── build. ...

  6. vue-cli构建的vue项目打包后css引入的背景图路径不对的问题

    使用vue-cli构建vue项目后,再打包遇到一个css引入的背景图片路径的问题,就是css代码中背景图片是根据相对路径来写的,如下图: 当使用npm run dev命令本地访问的时候,背景图片是正常 ...

  7. 使用Koa.js,离不开这十个中间件

    随着ES6的普及,async/await的语法受到更多JS开发者的青睐,Koa.js作为比较早支持使用该语法的Node框架越来越受到大家的喜爱,虽然Koa.js本身支持的功能很有限,但官方和社区提供了 ...

  8. APP测试总结2

    一.App测试流程与web项目流程区别 1.对UI要求比较高,需要更加注重用户体验.对于一个小小的屏幕,如何让用户使用更加轻便.简介.易用. 2.App是调用服务端接口展示数据.我们测试需要可以判断问 ...

  9. Kafka monitoring监控

    一.Metrics kafka有两个metrics包,在看源码的时候很容易混淆 package kafka.metrics package org.apache.kafka.common.metric ...

  10. 案例52-crm练习新增客户中加入文件上传功能(struts2文件上传)

    1 jsp/customer/add.jsp 完整代码: <%@ page language="java" contentType="text/html; char ...