follow Up — 20181101
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的更多相关文章
- jQuery Scroll Follow
Overview Scroll Follow is a simple jQuery plugin that enables a DOM object to follow the page as the ...
- as follows ,as follow && following
在现在牛津英语上,as follow 和 as follows 用法差不多的,但后者更常用,不是说谁指一个谁指好几个.牵强附会! 为了保证正确性,你应该用as follows,单数的最好少用.意义差不 ...
- [转]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 ...
- 编译原理LL1文法Follow集算法实现
import hjzgg.first.First; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set ...
- 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 ...
- Follow me to learn what is repository pattern
Introduction Creating a generic repository pattern in an mvc application with entity framework is th ...
- 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 ...
- 【转】Github轻松上手6-推荐follow的牛人和值得watch的repo
转自:http://blog.sina.com.cn/s/blog_4b55f6860100zzk5.html Github作为一个social coding 网站,其作用远远超过了一个简单的VCS( ...
- To follow the path
look to the master, follow the master, walk with the master, see through the master, bec ...
随机推荐
- Python基础入门-元祖
其实,元组合列表的特性和使用几乎差不太多,今天我们重点来看下元组的一些操作和使用. 1.元祖的定义和特点 定义:元组是以小括号包围,元素以逗号分隔,不可变的序列之一. 特点: 1)元祖内的元素不可以增 ...
- ios PNG Crush error (PNG图片错误)
我是这么解决的: I had the same problem. How to fix : Open up image with Preview -> File > Export > ...
- GlobalAlloc()和malloc()、HeapAlloc()
两者都是在堆上分配内存区. malloc()是C运行库中的动态内存分配函数,WINDOWS程序基本不使用了,因为它比WINDOWS内存分配函数少了一些特性,如,整理内存. GlobalAlloc( ...
- Eclipse中连接数据库错误:com.microsoft.sqlserver.jdbc.SQLServerException: 之类的错误
原创 错误:org.apache.jasper.JasperException: Unable to compile class for JSP 原因是页面指令中 import="java. ...
- SignalR 跨域解决方案全面
SignalR 分:PersistentConnection和Hub 2种模式. 跨域又分:UseCors和JsonP 2种方法 所以例子写了4种. 核心代码: UseCors //Persiste ...
- 【连载】redis库存操作,分布式锁的四种实现方式[四]--基于Redis lua脚本机制实现分布式锁
一.redis lua介绍 Redis 提供了非常丰富的指令集,但是用户依然不满足,希望可以自定义扩充若干指令来完成一些特定领域的问题.Redis 为这样的用户场景提供了 lua 脚本支持,用户可以向 ...
- C# LINQ(8)
回顾之前的代码都是LINQ自行推断类型.其实LINQ在查询的结束是可以动态创建类型. , , , , , , , , , , }; var list = from num in intArray &a ...
- App Store提交审核报错 ERROR ITMS-90087解决办法
1.原因说明 app对Wifi进行配网, 使用了GizWifiSDK.framework提交App Store时候报错了 App Store Connect Operation Error ERROR ...
- time,datetime,calendar模块
Python中,与时间有关的模块有time,datetime和calendar. 1.时钟时间:time 在Python中,用三种方式来表示时间:时间戳,格式化时间字符串和结构化时间. 1)时间戳,就 ...
- Spark JavaRDD、JavaPairRDD、Dataset之间的相互转换
主要内容: 1. JavaRDD to JavaPairRDD 2. Dataset to JavaPairRDD 3. JavaPairRDD to JavaRDD 4. JavaRDD to Da ...