GeezerVison is showing github.com — Refine 668_Kth_Smallest_Number_in_Multiplication_Table · anandanthony/leetcode@4882b9d
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -175,6 +175,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal | 175 | 175 | | 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/628_Maximum_Product_of_Three_Numbers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/628_Maximum_Product_of_Three_Numbers.java) | Actually, we should only care about min1, min2 and max1-max3, to find these five elements, we can use 1. Brute force, O(n^3) and O(1)<br>2. Sort, O(nlogn) and O(1)<br>3. Single scan with 5 elements keep, O(n) and O(1) | | 176 | 176 | | 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) | | 177 | 177 | | 665 | [Non-decreasing Array](https://leetcode.com/problems/non-decreasing-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/665_Non-decreasing_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/665_Non-decreasing_Array.java) | 1. Find the broken index, then check this point, O(n) and O(1)<br>2. Replace broken point with correct value, then check if there are more than 1 broken point, O(n) and O(1) | | | 178 | +| 668 | [Kth Smallest Number in Multiplication Table](https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/668_Kth_Smallest_Number_in_Multiplication_Table.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/668_Kth_Smallest_Number_in_Multiplication_Table.java) [Cpp](https://github.com/qiyuangong/leetcode/blob/master/cpp/668_Kth_Smallest_Number_in_Multiplication_Table.cpp) | Binary search, O(mlog(mn) and O(1) | | 178 | 179 | | 671 | [Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/671_Second_Minimum_Node_In_a_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/671_Second_Minimum_Node_In_a_Binary_Tree.java) | Note that min value is root: 1. Get all values then find result, O(n) and O(n)<br>2. BFS or DFS traverse the tree, then find the reslut, O(n) and O(n)| | 179 | 180 | | 674 | [Longest Continuous Increasing Subsequence](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/674_Longest_Continuous_Increasing_Subsequence.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/674_Longest_Continuous_Increasing_Subsequence.java) | Scan nums once, check nums[i] < nums[i+1], if not reset count, O(n) and O(1) | | 180 | 181 | | 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. | | |
Collapse file Copy file name to clipboardExpand all lines: cpp/668_Kth_Smallest_Number_in_Multiplication_Table.cpp +19-18Lines changed: 19 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | class Solution { | 2 | 2 | public: | 3 | | - #define ll long long int | 4 | | - | 5 | | - bool valid(ll x, int m, int n, int k){ | 6 | | - int cnt=0; | 7 | | - for(int i=1;i<=m;i++){ | 8 | | - cnt+=n<x/i?n:x/i; | 9 | | - if(x/i==0)break; | | 3 | +#define ll long long int | | 4 | + bool valid(ll x, int m, int n, int k) { | | 5 | + int cnt = 0; | | 6 | + for (int i = 1; i <= m; i++) { | | 7 | + cnt += n < x / i ? n : x / i; | | 8 | + if (x / i == 0) | | 9 | + break; | 10 | 10 | } | 11 | | - return cnt>=k; | | 11 | + return cnt >= k; | 12 | 12 | } | 13 | | - | | 13 | + | 14 | 14 | int findKthNumber(int n1, int n2, int k) { | 15 | | - ll l=0, r=n1*n2,ans; | 16 | | - while(l<=r){ | 17 | | - ll m = l +(r-l)/2; | 18 | | - if(valid(m,n1,n2,k)){ | 19 | | - ans=m; | 20 | | - r=m-1; | 21 | | - } | 22 | | - else{ | 23 | | - l=m+1; | | 15 | + ll l = 0, r = n1 * n2, ans; | | 16 | + while (l <= r) { | | 17 | + // ith row [i, 2*i, 3*i, ..., n*i] | | 18 | + // for each column, k = x // i | | 19 | + ll m = l + (r - l) / 2; | | 20 | + if (valid(m, n1, n2, k)) { | | 21 | + ans = m; | | 22 | + r = m - 1; | | 23 | + } else { | | 24 | + l = m + 1; | 24 | 25 | } | 25 | 26 | } | 26 | 27 | return ans; | |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution { | | 2 | + // https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/solution/ | | 3 | + public boolean enough(int x, int m, int n, int k) { | | 4 | + int count = 0; | | 5 | + for (int i = 1; i <= m; i++) { | | 6 | + count += Math.min(x / i, n); | | 7 | + } | | 8 | + return count >= k; | | 9 | + } | | 10 | + | | 11 | + public int findKthNumber(int m, int n, int k) { | | 12 | + int lo = 1, hi = m * n; | | 13 | + while (lo < hi) { | | 14 | + // ith row [i, 2*i, 3*i, ..., n*i] | | 15 | + // for each column, k = x // i | | 16 | + int mi = lo + (hi - lo) / 2; | | 17 | + if (!enough(mi, m, n, k)) lo = mi + 1; | | 18 | + else hi = mi; | | 19 | + } | | 20 | + return lo; | | 21 | + } | | 22 | +} |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution: | | 2 | + def findKthNumber(self, m: int, n: int, k: int) -> int: | | 3 | + # https://leetcode.com/problems/kth-smallest-number-in-multiplication-table/solution/ | | 4 | + def enough(x): | | 5 | + count = 0 | | 6 | + # ith row [i, 2*i, 3*i, ..., n*i] | | 7 | + # for each column, k = x // i | | 8 | + for i in range(1, m+1): | | 9 | + count += min(x // i, n) | | 10 | + return count >= k | | 11 | + | | 12 | + lo, hi = 1, m * n | | 13 | + while lo < hi: | | 14 | + mi = (lo + hi) // 2 | | 15 | + if not enough(mi): | | 16 | + lo = mi + 1 | | 17 | + else: | | 18 | + hi = mi | | 19 | + return lo |
|