|
| 1 | +/** |
| 2 | + * [1985] Find the Kth Largest Integer in the Array |
| 3 | + * |
| 4 | + * You are given an array of strings nums and an integer k. Each string in nums represents an integer without leading zeros. |
| 5 | + * Return the string that represents the k^th largest integer in nums. |
| 6 | + * Note: Duplicate numbers should be counted distinctly. For example, if nums is ["1","2","2"], "2" is the first largest integer, "2" is the second-largest integer, and "1" is the third-largest integer. |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: nums = ["3","6","7","10"], k = 4 |
| 11 | + * Output: "3" |
| 12 | + * Explanation: |
| 13 | + * The numbers in nums sorted in non-decreasing order are ["3","6","7","10"]. |
| 14 | + * The 4^th largest integer in nums is "3". |
| 15 | + * |
| 16 | + * Example 2: |
| 17 | +]* |
| 18 | + * Input: nums = ["2","21","12","1"], k = 3 |
| 19 | + * Output: "2" |
| 20 | + * Explanation: |
| 21 | + * The numbers in nums sorted in non-decreasing order are ["1","2","12","21"]. |
| 22 | + * The 3^rd largest integer in nums is "2". |
| 23 | + * |
| 24 | + * Example 3: |
| 25 | + * |
| 26 | + * Input: nums = ["0","0"], k = 2 |
| 27 | + * Output: "0" |
| 28 | + * Explanation: |
| 29 | + * The numbers in nums sorted in non-decreasing order are ["0","0"]. |
| 30 | + * The 2^nd largest integer in nums is "0". |
| 31 | + * |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * |
| 35 | + * 1 <= k <= nums.length <= 10^4 |
| 36 | + * 1 <= nums[i].length <= 100 |
| 37 | + * nums[i] consists of only digits. |
| 38 | + * nums[i] will not have any leading zeros. |
| 39 | + * |
| 40 | + */ |
| 41 | +pub struct Solution {} |
| 42 | + |
| 43 | +// problem: https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/ |
| 44 | +// discuss: https://leetcode.com/problems/find-the-kth-largest-integer-in-the-array/discuss/?currentPage=1&orderBy=most_votes&query= |
| 45 | + |
| 46 | +// submission codes start here |
| 47 | + |
| 48 | +impl Solution { |
| 49 | + pub fn kth_largest_number(nums: Vec<String>, k: i32) -> String { |
| 50 | + let mut nums = nums; |
| 51 | + let k = (k - 1) as usize; |
| 52 | + |
| 53 | + nums.select_nth_unstable_by(k, |a, b| (b.len()).cmp(&a.len()).then_with(|| b.cmp(&a))); |
| 54 | + |
| 55 | + nums[k].clone() |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// submission codes end |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::*; |
| 64 | + |
| 65 | + #[test] |
| 66 | + fn test_1985_example_1() { |
| 67 | + let nums = vec_string!["3", "6", "7", "10"]; |
| 68 | + let k = 4; |
| 69 | + |
| 70 | + let result = "3".to_string(); |
| 71 | + |
| 72 | + assert_eq!(Solution::kth_largest_number(nums, k), result); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_1985_example_2() { |
| 77 | + let nums = vec_string!["2", "21", "12", "1"]; |
| 78 | + let k = 3; |
| 79 | + |
| 80 | + let result = "2".to_string(); |
| 81 | + |
| 82 | + assert_eq!(Solution::kth_largest_number(nums, k), result); |
| 83 | + } |
| 84 | + |
| 85 | + #[test] |
| 86 | + fn test_1985_example_3() { |
| 87 | + let nums = vec_string!["0", "0"]; |
| 88 | + let k = 2; |
| 89 | + |
| 90 | + let result = "0".to_string(); |
| 91 | + |
| 92 | + assert_eq!(Solution::kth_largest_number(nums, k), result); |
| 93 | + } |
| 94 | +} |
0 commit comments