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. nodelet的应用

    1.创建一个包,如example_pkg catkin_create_pkg example_pkg 2.创建MyNodeletClass.h文件 cd ~/catkin_ws/src/example ...

  2. 7.linux安全基线加固

    本文大多截图出自于:http://c.biancheng.net/cpp/shell/ 现在大多数企业都是使用linux作为服务器,不仅是linux是开源系统,更是因为linux比windows更安全 ...

  3. Laravel Gate 授权方式的使用指南

    参考链接:An Introduction to Laravel Authorization Gates 本文使用 Laravel 的 Gate 授权方式 实现一个基于用户角色的博客发布系统. 在系统包 ...

  4. PC/APP/H5三端测试的相同与不同

    随着手机应用的不断状态,同一款产品的移动端应用市场占相较PC端也越来越大,那么app与PC端针对这些产品的测试有什么相同与不同之处呢?总结如下: 首先谈一谈相同之处: 一,针对同一个系统功能的测试,三 ...

  5. (转)ASP.NET基础之HttpHandler学习

    原文地址:http://www.cnblogs.com/wujy/archive/2013/08/18/3266009.html 经过前两篇[ASP.NET基础之HttpModule学习]和[ASP. ...

  6. 软件工程:Java实现WC.exe基本功能

    项目相关要求 GitHub地址:https://github.com/3216004716/WC 实现一个统计程序,它能正确统计程序文件中的字符数.单词数.行数,以及还具备其他扩展功能,并能够快速地处 ...

  7. C#7.0连接MySQL8.0数据库的小笔记

    1.要连接MySql数据库必须首先下载MySql官方的连接.net的文件,文件下载地址为https://dev.mysql.com/downloads/connector/net/6.6.html#d ...

  8. DFT到FFT的理解

    DFT简化计算理解(FFT)   DFT: WN=e^(-j*2*pi/N) DFT复杂度o(N^2) 降低与N^2的依赖 使N = LM  (L^2+m^2 <= N^2) N点DFT分解为M ...

  9. 【连载】redis库存操作,分布式锁的四种实现方式[二]--基于Redisson实现分布式锁

    一.redisson介绍 redisson实现了分布式和可扩展的java数据结构,支持的数据结构有:List, Set, Map, Queue, SortedSet, ConcureentMap, L ...

  10. 【BZOJ3589】动态树 树链剖分+线段树

    Description 别忘了这是一棵动态树, 每时每刻都是动态的. 小明要求你在这棵树上维护两种事件 事件0: 这棵树长出了一些果子, 即某个子树中的每个节点都会长出K个果子. 事件1: 小明希望你 ...