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 ...
随机推荐
- groupie
def add_group(group): c = group.c.astype('float') group['d'] = c/c.sum() return group df = pd ...
- Luogu 2573 [SCOI2012]滑雪
BZOJ 2753 首先可以按照题目要求的把所有的有向边建出来,然后进去广搜就可以求出第一问的解,然后考虑如何求解第二问,我们把所有搜到的边按照到达的点的高度位第一关键字,边的长度为第二关键字排序之后 ...
- Git安装和常用命令
Git是目前世界上最先进的分布式版本控制系统!!! Git能自动帮我们记录每次文件的改动,还可以让同事协作编辑. 接下来,简单的介绍下Git的安装和常用命令: Git安装: 1.Windows系统,进 ...
- eclipse——执行Maven命令
右键pom.xml文件 点击 m2 Maven build... 输入要执行的命令,点击Run 控制台会打印maven运行过程
- HDU 3723 Delta Wave (高精度+calelan数)
题意:给定一个图,问你只能向上向下,或者平着走,有多少种方法可以走到最后一个格. 析:首先先考虑,如果没有平的情况就是calelan数了,现在有平的情况,那么就枚举呗,因为数很大,所以要用高精度. 答 ...
- wpf使用truetype字体ttf
查了半天都是语焉不详,这篇算是稍微详细点的:http://www.cnblogs.com/junhengml/p/6878933.html 要先查找到字体的字库名称,才能使用: <Window. ...
- The Suspects——Asia Kaohsiung 2003
原创 The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 48698 Accepted: 23286 Des ...
- OnExit事件 OnChange事件
procedure TSetParkForm.edtPrePosExit(Sender: TObject); // 焦点移开 或已操作 begin if (G2.RowCount > 0) an ...
- Vue 编程式导航,路由history模式
import Vue from 'vue' import App from './App.vue' import Home from './components/Home.vue' import Ne ...
- kali linux之BurpSuite
web安全工具中的瑞士军刀,统一的集成工具发现web安全漏洞 所有的工具共享一个能处理并显示http消息的可扩展框架, 模块之间无缝交换信息. 有free版和professional版,java开发, ...