classSolution{ funcfindKthLargest(_ nums: [Int], _ k: Int) -> Int { funcquickSort(_ nums: inout [Int], _ l: Int, _ r: Int) -> Int { var l = l, r = r let key = nums[l] while l < r { while l < r, key >= nums[r] { r -= 1 } nums[l] = nums[r] while l < r, key <= nums[l] { l += 1 } nums[r] = nums[l] } nums[l] = key return l } var l = 0, r = nums.count-1, nums = nums while l < r { let index = quickSort(&nums, l, r) switch index { case k - 1: return nums[index] case0..<k-1: l = index + 1 break default: r = index - 1 break } } return nums[k-1] } }
publicclassTreeNode{ publicvar val: Int publicvarleft: TreeNode? publicvarright: TreeNode? publicinit(_ val: Int) { self.val = val self.left = nil self.right = nil } } classSolution{ funckthSmallest(_ root: TreeNode?, _ k: Int) -> Int { var stack = [TreeNode]() var cur = root var k = k while cur != nil || !stack.isEmpty { iflet temp = cur { stack.append(temp) cur = temp.left } else { let last = stack.removeLast() k -= 1 if k == 0 { return last.val } cur = last.right } } return0 } }
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.
Example 1:
1 2 3 4
Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3 Output: [[1,2],[1,4],[1,6]] Explanation: The first 3 pairs are returned from the sequence: [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:
1 2 3 4
Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2 Output: [1,1],[1,1] Explanation: The first 2 pairs are returned from the sequence: [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
Example 3:
1 2 3
Input: nums1 = [1,2], nums2 = [3], k = 3 Output: [1,3],[2,3] Explanation: All possible pairs are returned from the sequence: [1,3],[2,3]
classSolution{ funckthSmallest(_ matrix: [[Int]], _ k: Int) -> Int { guard !matrix.isEmpty else { return0 } let n = matrix.count-1 var lo = matrix[0][0], hi = matrix[n][n] while lo < hi { let mid = (lo + hi) >> 1 varcount = 0, j = n for i in0...n { while j >= 0, matrix[i][j] > mid { j -= 1 } count += j + 1 } ifcount < k { lo = mid + 1 } else { hi = mid } } return lo } }
Nearly every one have used the Multiplication Table. But could you find out the k-th smallest number quickly from the multiplication table?
Given the height m and the length n of a m * n Multiplication Table, and a positive integer k, you need to return the k-th smallest number in this table.
Example 1:
1 2 3 4 5 6 7 8 9
Input: m = 3, n = 3, k = 5 Output: Explanation: The Multiplication Table: 1 2 3 2 4 6 3 6 9
The 5-th smallest number is 3 (1, 2, 2, 3, 3).
Example 2:
1 2 3 4 5 6 7 8
Input: m = 2, n = 3, k = 6 Output: Explanation: The Multiplication Table: 1 2 3 2 4 6
classSolution{ funcfindKthNumber(_ m: Int, _ n: Int, _ k: Int) -> Int { guard m > 0, n > 0, k > 0else { return0 } var lo = 1, hi = m * n while lo < hi { let mid = (lo + hi) >> 1 varcount = 0, nN = n for i in1...m { while nN > 0, i * nN > mid { nN -= 1 } count += nN } ifcount < k { lo = mid + 1 } else { hi = mid } } return lo } }
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair (A, B) is defined as the absolute difference between A and B.
Example 1:
1 2 3 4 5 6 7 8 9 10
Input: nums = [1,3,1] k = 1 Output: 0 Explanation: Here are all the pairs: (1,3) -> 2 (1,1) -> 0 (3,1) -> 2 Then the 1st smallest distance pair is (1,1), and its distance is 0.
classSolution{ funcsmallestDistancePair(_ nums: [Int], _ k: Int) -> Int { guard nums.count > 1else { return0 } let sortedNums = nums.sorted() var lo = 0, hi = sortedNums.last! - sortedNums.first! while lo < hi { let mid = (lo + hi) >> 1 varcount = 0, i = 0 for j in0..<sortedNums.count { while i < sortedNums.count, sortedNums[j] - sortedNums[i] > mid { i += 1 } count += j-i } ifcount < k { lo = mid + 1 } else { hi = mid } } return lo } }
A sorted list A contains 1, plus some number of primes. Then, for every p < q in the list, we consider the fraction p/q.
What is the K-th smallest fraction considered? Return your answer as an array of ints, where answer[0] = p and answer[1] = q.
Example:
1 2 3 4 5 6 7 8 9
Input: A = [1, 2, 3, 5], K = 3 Output: [2, 5] Explanation: The fractions to be considered in sorted order are: 1/5, 1/3, 2/5, 1/2, 3/5, 2/3. The third fraction is 2/5.
Input: A = [1, 7], K = 1 Output: [1, 7]
Note:
A will have length between 2 and 2000.
Each A[i] will be between 1 and 30000.
K will be between 1 and A.length * (A.length - 1) / 2.
classSolution{ funckthSmallestPrimeFraction(_ A: [Int], _ K: Int) -> [Int] { let n = A.count guard n > 1, K > 0else { return [] } var lo = 0.0, hi = 1.0 while lo < hi { let mid = (lo + hi) / 2 var j = 1, count = 0, maxVal = 0.0, res = (0, 0) for i in0..<n-1 { while j < n, Double(A[i]) > mid * Double(A[j]) { j += 1 } if n == j { break } count += n-j let val = Double(A[i]) / Double(A[j]) if val > maxVal { res = (i, j) maxVal = val } } ifcount < K { lo = mid } elseifcount == K { return [A[res.0], A[res.1]] } else { hi = mid } } return [] } }