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. Django rest_framework----序列化组件

    生成hypermedialink serializer.pclass BookModelSerializers(serializers.ModelSerializer): class Meta: mo ...

  2. 无法链接到windows服务

    1.先将鼠标移动到桌面右下角的显示桌面按钮处,选择右侧按钮列表中的搜索列表,输入cmd.exe,右击搜索结果,选择以管理员身份运行. 2.然后输入netsh winsock reset catalog ...

  3. Mr_matcher的细节2

    1.参数服务器 ROS参数服务器能保存数据类型包括:strings, integers, floats, booleans, lists, dictionaries, iso8601 dates, a ...

  4. jQuery对象与DOM对象及互相转化

    <p id=‘’hello”></p> 普通处理,通过标准JavaScript处理: var p = document.getElementById('hello'); p.i ...

  5. WCF把书读薄(2)——消息交换、服务实例、会话与并发

    上一篇:WCF把书读薄(1)——终结点与服务寄宿 八.消息交换模式 WCF服务的实现是基于消息交换的,消息交换模式一共有三种:请求回复模式.单向模式与双工模式. 请求回复模式很好理解,比如int Ad ...

  6. 基于HTML5 Ajax文件上传进度条如何实现(jquery版本)

    <!DOCTYPE html> <html> <head> <title>html5_2.html</title> <meta htt ...

  7. CRC-32 校验算法

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

  8. C# 给图片添加透明的文字、图片水印

    #region 添加水印 /// <summary> /// 添加文字水印 /// </summary> /// <param name="image" ...

  9. 移动端适配video适配

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Codeforces Round #551 (Div. 2)A. Serval and Bus

    A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard in ...