九章算法强化班ladder题目梳理
Posted 一个后端狗
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了九章算法强化班ladder题目梳理相关的知识,希望对你有一定的参考价值。
1 - Follow up in Code Interview
kth-smallest-number-in-sorted-matrix
Find the kth smallest number in at row and column sorted matrix.
[
[1 ,5 ,7],
[3 ,7 ,8],
[4 ,8 ,9],
]
注意点:
-
Comparator<Pair>的实现
-
PriorityQueue poll add
1 import java.util.*; 2 3 class Pair{ 4 public int x, y, val; 5 public Pair(int x, int y, int val){ 6 this.x = x; 7 this.y = y; 8 this.val = val; 9 } 10 } 11 12 class PairComparator implements Comparator<Pair> { 13 public int compare(Pair a, Pair b){ 14 return a.val - b.val; 15 } 16 } 17 18 public class Solution { 19 /** 20 * @param matrix: a matrix of integers 21 * @param k: an integer 22 * @return: the kth smallest number in the matrix 23 */ 24 public int kthSmallest(int[][] matrix, int k) { 25 // write your code here 26 int[] dx = {0, 1}; 27 int[] dy = {1, 0}; 28 int m = matrix.length; 29 int n = matrix[0].length; 30 boolean[][] visited = new boolean[m][n]; 31 PriorityQueue<Pair> pq = new PriorityQueue<>(k, new PairComparator()); 32 pq.add(new Pair(0, 0, matrix[0][0])); 33 34 for (int i = 0; i < k - 1; i++){ 35 Pair cur = pq.poll(); 36 for (int j = 0; j < 2; j++){ 37 int x = cur.x + dx[j]; 38 int y = cur.y + dy[j]; 39 if (x < m && y < n && !visited[x][y]){ 40 pq.add(new Pair(x, y, matrix[x][y])); 41 visited[x][y] = true; 42 43 } 44 } 45 } 46 return pq.peek().val; 47 } 48 }
minimum-size-subarray-sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn‘t one, return -1 instead.
注意点:
- 对于当前i,找到j使得i~j>=s,对于下一个i,j不需要回退到i(题目性质),所以可以在O(n)时间复杂度内得到解
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) return -1; 10 int result = -1; 11 int j = 0; 12 int sum = 0; 13 for (int i = 0; i < nums.length; i++){ 14 boolean flag = false; 15 if (i > 0){ 16 sum -= nums[i - 1]; 17 flag = true; 18 } 19 while (j < nums.length){ 20 if (!flag) { 21 sum += nums[j]; 22 } 23 else { 24 flag = false; 25 } 26 if (sum >= s){ 27 if (result == -1 || j - i + 1 < result){ 28 result = j - i + 1; 29 } 30 break; 31 } 32 j++; 33 } 34 } 35 return result; 36 } 37 }
longest-substring-without-repeating-characters
Given a string, find the length of the longest substring without repeating characters.
注意点:
- 使用一个map记录各个字母出现次数
1 import java.util.*; 2 public class Solution { 3 /** 4 * @param s: a string 5 * @return: an integer 6 */ 7 public int lengthOfLongestSubstring(String s) { 8 // write your code here 9 int result = 0; 10 if (s == null || s.length() == 0){ 11 return result; 12 } 13 Map<Character, Integer> map = new HashMap<>(); 14 int j = 0; 15 for (int i = 0; i < s.length(); i++){ 16 if (i > 0){ 17 map.put(s.charAt(i - 1), map.get(s.charAt(i - 1)) - 1); 18 if (map.get(s.charAt(i - 1)) == 0){ 19 map.remove(s.charAt(i - 1)); 20 } 21 } 22 while(j < s.length()){ 23 if (map.containsKey(s.charAt(j))){ 24 break; 25 } 26 else { 27 map.put(s.charAt(j), 1); 28 if (map.entrySet().size() > result){ 29 result = map.entrySet().size(); 30 } 31 j++; 32 } 33 } 34 } 35 return result; 36 } 37 }
longest-substring-with-at-most-k-distinct-characters
Given a string s, find the length of the longest substring T that contains at most k distinct characters.
注意:
-
map.entrySet().size() > k 判断键的个数也可以用keySet
1 public class Solution { 2 /** 3 * @param s : A string 4 * @return : The length of the longest substring 5 * that contains at most k distinct characters. 6 */ 7 public int lengthOfLongestSubstringKDistinct(String s, int k) { 8 // write your code here 9 int result = 0; 10 if (s == null || s.length() == 0){ 11 return result; 12 } 13 Map<Character, Integer> map = new HashMap<>();a 14 int j = 0; 15 for (int i = 0; i < s.length(); i++){ 16 boolean flag = false; 17 if (i > 0){ 18 map.put(s.charAt(i - 1), map.get(s.charAt(i - 1)) - 1); 19 if (map.get(s.charAt(i - 1)) == 0){ 20 map.remove(s.charAt(i - 1)); 21 } 22 flag = true; 23 } 24 while(j < s.length()){ 25 if(!flag){ 26 if (map.containsKey(s.charAt(j))){ 27 map.put(s.charAt(j), map.get(s.charAt(j)) + 1); 28 } 29 else{ 30 map.put(s.charAt(j), 1); 31 } 32 } 33 else { 34 flag = false; 35 } 36 if (map.entrySet().size() > k){ 37 break; 38 } 39 else { 40 if (j - i + 1 > result){ 41 result = j - i + 1; 42 } 43 j++; 44 } 45 } 46 } 47 return result; 48 } 49 }
kth-smallest-sum-in-two-sorted-arrays
Given two integer arrays sorted in ascending order and an integer k. Define sum = a + b, where a is an element from the first array and b is an element from the second one. Find the kth smallest sum out of all possible sums.
注意点:
- 把这两个数组的和,看成一个矩阵,就变成kth-smallest-number-in-sorted-matrix了
1 import java.util.*; 2 3 class Pair{ 4 public int x, y, val; 5 public Pair(int x, int y, int val){ 6 this.x = x; 7 this.y = y; 8 this.val = val; 9 } 10 } 11 12 class PairComparator implements Comparator<Pair>{ 13 public int compare(Pair a, Pair b){ 14 return a.val - b.val; 15 } 16 } 17 18 19 public class Solution { 20 /** 21 * @param A an integer arrays sorted in ascending order 22 * @param B an integer arrays sorted in ascending order 23 * @param k an integer 24 * @return an integer 25 */ 26 public int kthSmallestSum(int[] A, int[] B, int k) { 27 // Write your code here 28 int[] dx = {0, 1}; 29 int[] dy = {1, 0}; 30 int m = A.length; 31 int n = B.length; 32 boolean[][] visited = new boolean[m][n]; 33 PriorityQueue<Pair> pq = new PriorityQueue<>(k, new PairComparator()); 34 pq.add(new Pair(0, 0, A[0] + B[0])); 35 for (int i = 0; i < k - 1; i++){ 36 Pair cur = pq.poll(); 37 for(int j = 0; j < 2; j++){ 38 int x = cur.x + dx[j]; 39 int y = cur.y + dy[j]; 40 if (x < m && y < n && !visited[x][y]){ 41 visited[x][y] = true; 42 pq.add(new Pair(x, y, A[x] + B[y])); 43 } 44 } 45 } 46 return pq.peek().val; 47 } 48 }
kth-largest-in-n-arrays
Find K-th largest element in N arrays.
N个数组找第K大
1 import java.util.*; 2 3 class Item{ 4 public int val, index, array; 5 public Item(int val, int index, int array){ 6 this.val = val; 7 this.index = index; 8 this.array = array; 9 } 10 } 11 12 class ItemComparator implements Comparator<Item>{ 13 public int compare(Item a, Item b){ 14 return b.val - a.val; 15 } 16 } 17 18 public class Solution { 19 /** 20 * @param arrays a list of array 21 * @param k an integer 22 * @return an integer, K-th largest element in N arrays 23 */ 24 public int KthInArrays(int[][] arrays, int k) { 25 // Write your code here 26 PriorityQueue<Item> pq = new PriorityQueue<>(k, new ItemComparator()); 27 for (int i = 0; i < arrays.length; i++){ 28 Arrays.sort(arrays[i]); 29 if (arrays[i].length > 0){ 30 pq.add(new Item(arrays[i][arrays[i].length - 1], arrays[i].length - 1, i)); 31 } 32 } 33 for (int i = 0; i < k - 1; i++){ 34 Item item = pq.poll(); 35 if (item.index > 0){ 36 pq.offer(new Item(arrays[item.array][item.index - 1], item.index - 1, item.array)); 37 } 38 } 39 return pq.peek().val; 40 } 41 }
two-sum-less-than-or-equal-to-target
Given an array of integers, find how many pairs in the array such that their sum is less than or equal to
a specific target number. Please return the number of pairs.
找小于等于target的pairs数
注意点:
- 先排序
- 两根指针移动时,可以根据两根指针的和,得到一个范围的解
1 import java.util.*; 2 public class Solution { 3 /** 4 * @param nums an array of integer 5 * @param target an integer 6 * @return an integer 7 */ 8 public int twoSum5(int[] nums, int target) { 9 // Write your code here 10 int result = 0; 11 if (nums == null || nums.length < 2){ 12 return result; 13 } 14 Arrays.sort(nums); 15 int left = 0; 16 int right = nums.length - 1; 17 while (left < right){ 18 if (nums[left] + nums[right] <= target){ 19 result += right - left; 20 left++; 21 } 22 else{ 23 right--; 24 } 25 } 26 return result; 27 } 28 }
triangle-count
Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find?
上一题的变形题,先排序,固定最后一个元素,找前面大于target的pairs数
1 import java.util.*; 2 public class Solution { 3 /** 4 * @param S: A list of integers 5 * @return: An integer 6 */ 7 public int triangleCount(int S[]) { 8 // write your code here 9 Arrays.sort(S); 10 int result = 0; 11 for(int i = S.length - 1; i > 1; i--){ 12 result += twoSum(S, 0, i - 1, S[i]); 13 } 14 return result; 15 } 16 17 private int twoSum(int S[], int start, int end, int target){ 18 int result = 0; 19 while (start < end){ 20 if (S[start] + S[end] > target){ 21 result += end - start; 22 end--; 23 } 24 else{ 25 start++; 26 } 27 } 28 return result; 29 } 30 }
以上是关于九章算法强化班ladder题目梳理的主要内容,如果未能解决你的问题,请参考以下文章
九章算法 基础算法 强化算法 系统设计 大数据 安卓 leetcode 高清视频
九章算法 基础算法 强化算法 系统设计 大数据 安卓 leetcode 高清视频