406. Minimum Size Subarray
同向双指针
https://www.lintcode.com/problem/minimum-size-subarray-sum/description?_from=ladder&&fromId=4
1 public class Solution { 2 /** 3 * @param nums: an array of integers 4 * @param s: An integer 5 * @return: an integer representing the minimum size of subarray 6 */ 7 public int minimumSize(int[] nums, int s) { 8 // write your code here 9 if(nums==null || nums.length ==0){ 10 return -1; 11 } 12 13 int sum = 0; 14 int res = Integer.MAX_VALUE; 15 for(int l =0,r=0;r<nums.length;r++){ 16 sum +=nums[r]; 17 18 while(sum>=s){ 19 res = Math.min(res,r-l+1); 20 sum = sum -nums[l]; 21 l++; 22 } 23 } 24 25 return res==Integer.MAX_VALUE?-1:res; 26 } 27 }
384. Longest Substring Without Repeating Characters
同向双指针
1 public class Solution { 2 /** 3 * @param s: a string 4 * @return: an integer 5 */ 6 public int lengthOfLongestSubstring(String s) { 7 // write your code here 8 if(s==null || s.length()==0){ 9 return 0; 10 } 11 12 Set<Character> set = new HashSet<>(); 13 int left =0; 14 int right =0; 15 int ans = Integer.MIN_VALUE; 16 17 while(left<s.length() && right<s.length()){ 18 while(right<s.length() && !set.contains(s.charAt(right))){ 19 set.add(s.charAt(right)); 20 ans = Math.max(ans,right-left+1); 21 right++; 22 } 23 24 set.remove(s.charAt(left)); 25 left++; 26 } 27 28 return ans == Integer.MIN_VALUE ? -1 : ans; 29 } 30 }
32. Minimum Window Substring
同向双指针
https://www.lintcode.com/problem/minimum-size-subarray-sum/?_from=ladder&&fromId=4
1 public class Solution { 2 /** 3 * @param source : A string 4 * @param target: A string 5 * @return: A string denote the minimum window, return "" if there is no such a string 6 */ 7 public String minWindow(String source , String target) { 8 // write your code here 9 if(source==null || source.length()==0){ 10 return source; 11 } 12 13 Map<Character,Integer> map = new HashMap<>(); 14 for(int i=0;i<target.length();i++){ 15 char c = target.charAt(i); 16 if(map.containsKey(c)){ 17 map.put(c,map.get(c)+1); 18 }else{ 19 map.put(c,1); 20 } 21 } 22 23 int i=0; 24 int j =0; 25 String res= ""; 26 int min = Integer.MAX_VALUE; 27 int countT = target.length(); 28 int countS = 0; 29 30 //while 循环不要加 j<source.length 的条件 31 while(i<source.length()){ 32 while(j<source.length() && countS<countT){ 33 char c = source.charAt(j); 34 if(map.containsKey(c)){ 35 if(map.get(c)>0) countS++; 36 map.put(c,map.get(c)-1); 37 } 38 39 j++; 40 } 41 42 if(countS>=countT){ 43 if(j-i<min){ 44 min = j-i; 45 res = source.substring(i,j); 46 } 47 } 48 49 char cc = source.charAt(i); 50 if(map.containsKey(cc)){ 51 if(map.get(cc)>=0) countS--; 52 map.put(cc,map.get(cc)+1); 53 } 54 i++; 55 } 56 57 return res; 58 } 59 }
386. Longest Substring with At Most K Distinct Characters
同向双指针
1 public class Solution { 2 /** 3 * @param s: A string 4 * @param k: An integer 5 * @return: An integer 6 */ 7 public int lengthOfLongestSubstringKDistinct(String s, int k) { 8 // write your code here 9 if(s==null || s.length()==0 || k==0){ 10 return 0; 11 } 12 13 int l = 0; 14 int r = 0; 15 int ans = 0; 16 int sum = 0; 17 18 int[] cnt = new int[256]; 19 for(r=0;r<s.length();r++){ 20 cnt[s.charAt(r)]++; 21 if(cnt[s.charAt(r)]==1){ 22 sum++; 23 } 24 25 while(sum>k){ 26 cnt[s.charAt(l)]--; 27 if(cnt[s.charAt(l)]==0){ 28 sum--; 29 } 30 l++; 31 } 32 33 ans = Math.max(ans,r-l+1); 34 } 35 36 return ans; 37 38 } 39 }
465. Kth Smallest Sum In Two Sorted Arrays
heap
class Pair{ int x; int y; int sum; 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> comparator = new Comparator<Pair>(){ @Override public int compare(Pair o1,Pair o2){ return o1.sum-o2.sum; } }; PriorityQueue<Pair> minHeap = new PriorityQueue<Pair>(comparator); boolean[][] visit = new boolean[A.length][B.length]; minHeap.add(new Pair(0,0,A[0]+B[0])); visit[0][0] = true; int[] dx = {0,1}; int[] dy = {1,0}; 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; } }
401. Kth Smallest Number in Sorted Matrix
heap
1 class Pair { 2 private int x; 3 private int y; 4 private int val; 5 6 public Pair(int x, int y, int val) { 7 this.x = x; 8 this.y = y; 9 this.val = val; 10 } 11 12 public int getX() { 13 return x; 14 } 15 16 public void setX(int x) { 17 this.x = x; 18 } 19 20 public int getY() { 21 return y; 22 } 23 24 public void setY(int y) { 25 this.y = y; 26 } 27 28 public int getVal() { 29 return val; 30 } 31 32 public void setVal(int val) { 33 this.val = val; 34 } 35 } 36 37 public class Solution { 38 /** 39 * @param matrix: a matrix of integers 40 * @param k: An integer 41 * @return: the kth smallest number in the matrix 42 */ 43 public int kthSmallest(int[][] matrix, int k) { 44 // write your code here 45 if (matrix == null || matrix.length == 0 || matrix.length * matrix[0].length < k) { 46 return -1; 47 } 48 Comparator<Pair> comparator = new Comparator<Pair>() { 49 @Override 50 public int compare(Pair o1, Pair o2) { 51 return o1.getVal() - o2.getVal(); 52 } 53 }; 54 int r = matrix.length; 55 int c = matrix[0].length; 56 PriorityQueue<Pair> minHeap = new PriorityQueue<>(comparator); 57 boolean[][] visited = new boolean[r][c]; 58 59 minHeap.add(new Pair(0, 0, matrix[0][0])); 60 visited[0][0] = true; 61 62 for (int i = 1; i <= k - 1; i++) { 63 Pair cur = minHeap.poll(); 64 if (cur.getX() + 1 < r && !visited[cur.getX() + 1][cur.getY()]) { 65 minHeap.add(new Pair(cur.getX() + 1, cur.getY(), matrix[cur.getX() + 1][cur.getY()])); 66 visited[cur.getX() + 1][cur.getY()] = true; 67 } 68 if (cur.getY() + 1 < c && !visited[cur.getX()][cur.getY() + 1]) { 69 minHeap.add(new Pair(cur.getX(), cur.getY() + 1, matrix[cur.getX()][cur.getY() + 1])); 70 visited[cur.getX()][cur.getY() + 1] = true; 71 } 72 } 73 74 return minHeap.peek().getVal(); 75 } 76 }
543. Kth Largest in N Arrays
heap
https://www.lintcode.com/problem/kth-largest-in-n-arrays/description?_from=ladder&&fromId=4
1 class Node{ 2 int value; 3 int fromId; 4 int index; 5 public Node(int value,int fromId,int index){ 6 this.value = value; 7 this.fromId = fromId; 8 this.index = index; 9 } 10 } 11 public class Solution { 12 /** 13 * @param arrays: a list of array 14 * @param k: An integer 15 * @return: an integer, K-th largest element in N arrays 16 */ 17 public int KthInArrays(int[][] arrays, int k) { 18 // write your code here 19 if(arrays==null || arrays.length==0 ||k<=0){ 20 return 0; 21 } 22 23 Comparator<Node> comparator = new Comparator<Node>(){ 24 @Override 25 public int compare(Node o1,Node o2){ 26 return o2.value - o1.value; 27 } 28 }; 29 30 PriorityQueue<Node> maxHeap = new PriorityQueue<Node>(comparator); 31 32 int n = arrays.length; 33 34 //sort and put first column into heap 35 for(int i =0;i<n;i++){ 36 Arrays.sort(arrays[i]); 37 38 if(arrays[i].length>0){ 39 int fromId = i; 40 int index = arrays[i].length-1; 41 int value = arrays[i][index]; 42 maxHeap.add(new Node(value,fromId,index)); 43 } 44 } 45 46 for(int i=1;i<k;i++){ 47 Node curr = maxHeap.poll(); 48 49 if(curr.index>0){ 50 curr.index--; 51 maxHeap.add(new Node(arrays[curr.fromId][curr.index],curr.fromId,curr.index)); 52 } 53 } 54 55 return maxHeap.peek().value; 56 57 58 } 59 }