From 66d49c88c9a642ecc1adeea2ce2ebe984c21b2d8 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 25 Jun 2025 06:53:48 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.2040 No.2040.Kth Smallest Product of Two Sorted Arrays --- .../README.md | 59 +++++++++++++++++++ .../README_EN.md | 59 +++++++++++++++++++ .../Solution.rs | 54 +++++++++++++++++ 3 files changed, 172 insertions(+) create mode 100644 solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/Solution.rs diff --git a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md index ac5d851b018b6..cddb788d2dafe 100644 --- a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md +++ b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README.md @@ -356,6 +356,65 @@ function kthSmallestProduct(nums1: number[], nums2: number[], k: number): number } ``` +#### Rust + +```rust +impl Solution { + pub fn kth_smallest_product(nums1: Vec, nums2: Vec, k: i64) -> i64 { + let m = nums1.len(); + let n = nums2.len(); + let a = nums1[0].abs().max(nums1[m - 1].abs()) as i64; + let b = nums2[0].abs().max(nums2[n - 1].abs()) as i64; + let mut l = -a * b; + let mut r = a * b; + + let count = |p: i64| -> i64 { + let mut cnt = 0i64; + for &x in &nums1 { + if x > 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) > p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += left as i64; + } else if x < 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) <= p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += (n - left) as i64; + } else if p >= 0 { + cnt += n as i64; + } + } + cnt + }; + + while l < r { + let mid = l + (r - l) / 2; + if count(mid) >= k { + r = mid; + } else { + l = mid + 1; + } + } + l + } +} +``` + diff --git a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md index 97650014c80a7..37e3102aa44e6 100644 --- a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md +++ b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/README_EN.md @@ -357,6 +357,65 @@ function kthSmallestProduct(nums1: number[], nums2: number[], k: number): number } ``` +#### Rust + +```rust +impl Solution { + pub fn kth_smallest_product(nums1: Vec, nums2: Vec, k: i64) -> i64 { + let m = nums1.len(); + let n = nums2.len(); + let a = nums1[0].abs().max(nums1[m - 1].abs()) as i64; + let b = nums2[0].abs().max(nums2[n - 1].abs()) as i64; + let mut l = -a * b; + let mut r = a * b; + + let count = |p: i64| -> i64 { + let mut cnt = 0i64; + for &x in &nums1 { + if x > 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) > p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += left as i64; + } else if x < 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) <= p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += (n - left) as i64; + } else if p >= 0 { + cnt += n as i64; + } + } + cnt + }; + + while l < r { + let mid = l + (r - l) / 2; + if count(mid) >= k { + r = mid; + } else { + l = mid + 1; + } + } + l + } +} +``` + diff --git a/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/Solution.rs b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/Solution.rs new file mode 100644 index 0000000000000..0d312303d1ef8 --- /dev/null +++ b/solution/2000-2099/2040.Kth Smallest Product of Two Sorted Arrays/Solution.rs @@ -0,0 +1,54 @@ +impl Solution { + pub fn kth_smallest_product(nums1: Vec, nums2: Vec, k: i64) -> i64 { + let m = nums1.len(); + let n = nums2.len(); + let a = nums1[0].abs().max(nums1[m - 1].abs()) as i64; + let b = nums2[0].abs().max(nums2[n - 1].abs()) as i64; + let mut l = -a * b; + let mut r = a * b; + + let count = |p: i64| -> i64 { + let mut cnt = 0i64; + for &x in &nums1 { + if x > 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) > p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += left as i64; + } else if x < 0 { + let mut left = 0; + let mut right = n; + while left < right { + let mid = (left + right) / 2; + if (x as i64) * (nums2[mid] as i64) <= p { + right = mid; + } else { + left = mid + 1; + } + } + cnt += (n - left) as i64; + } else if p >= 0 { + cnt += n as i64; + } + } + cnt + }; + + while l < r { + let mid = l + (r - l) / 2; + if count(mid) >= k { + r = mid; + } else { + l = mid + 1; + } + } + l + } +}