406. Minimum Size Subarray Sum

 public class Solution {
/**
* @param nums: an array of integers
* @param s: An integer
* @return: an integer representing the minimum size of subarray
*/
public int minimumSize(int[] nums, int s) {
// write your code here
if (nums == null || nums.length == 0) {
return -1;
}
int sum = 0;
int ans = Integer.MAX_VALUE;
int i = 0;
int j = 0;
while (i < nums.length && j < nums.length) {
while (sum < s && j < nums.length) {
sum += nums[j];
j++;
}
while (sum >= s) {
ans = Math.min(ans, j - i);
sum -= nums[i];
i++;
}
}
return ans == Integer.MAX_VALUE ? -1 : ans;
}
}

384. Longest Substring Without Repeating Characters

 public class Solution {
/**
* @param s: a string
* @return: an integer
*/
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() == 0) {
return 0;
}
int[] map = new int[256];
int i = 0;
int j = 0;
int ans = Integer.MIN_VALUE;
while (i < s.length() && j < s.length()) {
while (j < s.length() && map[s.charAt(j)] == 0) {
map[s.charAt(j)] = 1;
ans = Math.max(ans, j - i + 1);
j++;
}
map[s.charAt(i)] = 0;
i++;
}
return ans == Integer.MIN_VALUE ? -1 : ans;
}
}

386. Longest Substring with At Most K Distinct Characters

 public class Solution {
/**
* @param s: A string
* @param k: An integer
* @return: An integer
*/
public int lengthOfLongestSubstringKDistinct(String s, int k) {
// write your code here
if (s == null || s.length() == 0 || k == 0) {
return 0;
}
int left = 0;
int r = 0;
int ans = 0;
int sum = 0;
int[] cnt = new int[256];
for (r = 0; r < s.length(); r++) {
cnt[s.charAt(r)]++;
if (cnt[s.charAt(r)] == 1) {
sum++;
}
while (sum > k) {
cnt[s.charAt(left)]--;
if (cnt[s.charAt(left)] == 0) {
sum--;
}
left++;
}
ans = Math.max(ans, r - left + 1);
}
return ans;
}
}

465. Kth Smallest Sum In Two Sorted Arrays

 class Pair {
int x;
int y;
int sum; public Pair(int x, int y, int sum) {
this.x = x;
this.y = y;
this.sum = sum;
}
} public class Solution {
/**
* @param A: an integer arrays sorted in ascending order
* @param B: an integer arrays sorted in ascending order
* @param k: An integer
* @return: An integer
*/
public int kthSmallestSum(int[] A, int[] B, int k) {
// write your code here
if (A == null || A.length == 0) {
return 0;
}
if (B == null || B.length == 0) {
return 0;
}
if (k == 0) {
return 0;
} Comparator<Pair> pairComparator = new Comparator<Pair>() {
@Override
public int compare(Pair o1, Pair o2) {
return o1.sum - o2.sum;
}
}; PriorityQueue<Pair> minHeap = new PriorityQueue<>(pairComparator);
int[] dx = new int[]{1, 0};
int[] dy = new int[]{0, 1};
boolean[][] visit = new boolean[A.length][B.length];
minHeap.add(new Pair(0, 0, A[0] + B[0]));
visit[0][0] = true; for (int i = 1; i < k; i++) {
Pair center = minHeap.poll();
for (int j = 0; j < 2; j++) {
if (center.x + dx[j] > A.length - 1 || center.y + dy[j] > B.length - 1 || visit[center.x + dx[j]][center.y + dy[j]]) {
continue;
}
minHeap.add(new Pair(center.x + dx[j], center.y + dy[j], A[center.x + dx[j]] + B[center.y + dy[j]]));
visit[center.x + dx[j]][center.y + dy[j]] = true;
}
}
return minHeap.peek().sum;
}
}

follow Up — 20181101的更多相关文章

  1. jQuery Scroll Follow

    Overview Scroll Follow is a simple jQuery plugin that enables a DOM object to follow the page as the ...

  2. as follows ,as follow && following

    在现在牛津英语上,as follow 和 as follows 用法差不多的,但后者更常用,不是说谁指一个谁指好几个.牵强附会! 为了保证正确性,你应该用as follows,单数的最好少用.意义差不 ...

  3. [转]Installing python 2.7 on centos 6.3. Follow this sequence exactly for centos machine only

    Okay for centos 6.4 also On apu.0xdata.loc, after this install was done $ which python /usr/local/bi ...

  4. 编译原理LL1文法Follow集算法实现

    import hjzgg.first.First; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set ...

  5. Follow me to learn what is Unit of Work pattern

    Introduction A Unit of Work is a combination of several actions that will be grouped into a transact ...

  6. Follow me to learn what is repository pattern

    Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...

  7. Follow me to learn how to use mvc template

    Introduction After having gone through many project: Project A Project B Project C I start to write ...

  8. 【转】Github轻松上手6-推荐follow的牛人和值得watch的repo

    转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzk5.html Github作为一个social coding 网站,其作用远远超过了一个简单的VCS( ...

  9. To follow the path

    look to the master,    follow the master,    walk with the master,    see through the master,    bec ...

随机推荐

  1. 使用 Sentry集中处理错误

    Sentry的简介 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将程序的所有 exception 自动记录下来,处理 exception 是每个程 ...

  2. sql 存储过程返回多个值

    ALTER PROCEDURE your_sp_name    ASBEGIN    DECLARE @a INT, @b INT, @c INT    SELECT @a= COUNT(1) FRO ...

  3. PHP中循环结构之foreach循环语句

    在PHP中foreach循环语句,常用于遍历数组,一般有两种使用方式:不取下标.取下标. (1)只取值,不取下标 <?php foreach (数组 as 值){ //执行的任务 } ?> ...

  4. MongoDB整理笔记のGUI操作

    值得幸运的是,其实MongoDB也有像类似于PL/SQL一样的界面操作工具操作MongoDB. 下面就来介绍几款不同的界面工具,大家各取所需! MongoVUE 主页:http://www.mongo ...

  5. CRC-32 校验算法

      crc32的头文件 ===========================分割线=========================== //crc32.h #ifndef _CRC32_H #de ...

  6. WinForm中的焦点

    窗口打开后默认的焦点在TabIndex为0的元素上,即使代码中在其他元素上设置了Focus(),也没用,所以初始状态最好通过TabIndex来控制. WebForm中点其他如空白地方,之前的控件就会失 ...

  7. 「HEOI2016/TJOI2016」序列

    题目链接 戳这 Solution 首先考虑最暴力的dp 我们设: \(f[i]\)表示选择\(i\)以后所能形成的满足条件的子序列的最大值 \(minx[i]\)表示\(i\)能转换为的最小值 \(m ...

  8. ASP Session 对象

    http://www.w3school.com.cn/asp/asp_sessions.asp

  9. 洛谷P1251 餐巾计划问题(费用流)

    传送门 不得不说这题真是思路清奇,真是网络流的一道好题,完全没想到网络流的建图还可以这么建 我们把每一个点拆成两个点,分别表示白天和晚上,白天可以得到干净的餐巾(购买的,慢洗的,快洗的),晚上可以得到 ...

  10. mongoengine使用示例

    #!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2017年10月19日 @author: zzy ''' from mongo ...