diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md index 2773842cd50b6..e0ccd81563a30 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md @@ -82,21 +82,6 @@ tags: #### Python3 -```python -class Solution: - def smallestDivisor(self, nums: List[int], threshold: int) -> int: - l, r = 1, max(nums) - while l < r: - mid = (l + r) >> 1 - if sum((x + mid - 1) // mid for x in nums) <= threshold: - r = mid - else: - l = mid + 1 - return l -``` - -#### Python3 - ```python class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: @@ -159,7 +144,7 @@ public: ```go func smallestDivisor(nums []int, threshold int) int { - return sort.Search(1000000, func(v int) bool { + return sort.Search(slices.Max(nums), func(v int) bool { v++ s := 0 for _, x := range nums { @@ -178,10 +163,7 @@ function smallestDivisor(nums: number[], threshold: number): number { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { @@ -192,6 +174,27 @@ function smallestDivisor(nums: number[], threshold: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_divisor(nums: Vec, threshold: i32) -> i32 { + let mut l = 1; + let mut r = *nums.iter().max().unwrap(); + while l < r { + let mid = (l + r) / 2; + let s: i32 = nums.iter().map(|&x| (x + mid - 1) / mid).sum(); + if s <= threshold { + r = mid; + } else { + l = mid + 1; + } + } + l + } +} +``` + #### JavaScript ```js @@ -205,10 +208,7 @@ var smallestDivisor = function (nums, threshold) { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md index 8699548f95875..75df80db43d59 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md @@ -71,21 +71,6 @@ The time complexity is $O(n \times \log M)$, where $n$ is the length of the arra #### Python3 -```python -class Solution: - def smallestDivisor(self, nums: List[int], threshold: int) -> int: - l, r = 1, max(nums) - while l < r: - mid = (l + r) >> 1 - if sum((x + mid - 1) // mid for x in nums) <= threshold: - r = mid - else: - l = mid + 1 - return l -``` - -#### Python3 - ```python class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: @@ -148,7 +133,7 @@ public: ```go func smallestDivisor(nums []int, threshold int) int { - return sort.Search(1000000, func(v int) bool { + return sort.Search(slices.Max(nums), func(v int) bool { v++ s := 0 for _, x := range nums { @@ -167,10 +152,7 @@ function smallestDivisor(nums: number[], threshold: number): number { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { @@ -181,6 +163,27 @@ function smallestDivisor(nums: number[], threshold: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn smallest_divisor(nums: Vec, threshold: i32) -> i32 { + let mut l = 1; + let mut r = *nums.iter().max().unwrap(); + while l < r { + let mid = (l + r) / 2; + let s: i32 = nums.iter().map(|&x| (x + mid - 1) / mid).sum(); + if s <= threshold { + r = mid; + } else { + l = mid + 1; + } + } + l + } +} +``` + #### JavaScript ```js @@ -194,10 +197,7 @@ var smallestDivisor = function (nums, threshold) { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.go b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.go index 4083ffbff34d7..e32a73b754134 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.go +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.go @@ -1,5 +1,5 @@ func smallestDivisor(nums []int, threshold int) int { - return sort.Search(1000000, func(v int) bool { + return sort.Search(slices.Max(nums), func(v int) bool { v++ s := 0 for _, x := range nums { diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.js b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.js index 68c7d7da08a48..7f95726d9453f 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.js +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.js @@ -8,10 +8,7 @@ var smallestDivisor = function (nums, threshold) { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.py b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.py index 8f9a9fe607214..d93ecd44d43ab 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.py +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.py @@ -1,10 +1,7 @@ class Solution: def smallestDivisor(self, nums: List[int], threshold: int) -> int: - l, r = 1, max(nums) - while l < r: - mid = (l + r) >> 1 - if sum((x + mid - 1) // mid for x in nums) <= threshold: - r = mid - else: - l = mid + 1 - return l + def f(v: int) -> bool: + v += 1 + return sum((x + v - 1) // v for x in nums) <= threshold + + return bisect_left(range(max(nums)), True, key=f) + 1 diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.rs b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.rs new file mode 100644 index 0000000000000..d7a4b2ff1940d --- /dev/null +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn smallest_divisor(nums: Vec, threshold: i32) -> i32 { + let mut l = 1; + let mut r = *nums.iter().max().unwrap(); + while l < r { + let mid = (l + r) / 2; + let s: i32 = nums.iter().map(|&x| (x + mid - 1) / mid).sum(); + if s <= threshold { + r = mid; + } else { + l = mid + 1; + } + } + l + } +} diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.ts b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.ts index aa20aa28f590f..8309c7cf54088 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.ts +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.ts @@ -3,10 +3,7 @@ function smallestDivisor(nums: number[], threshold: number): number { let r = Math.max(...nums); while (l < r) { const mid = (l + r) >> 1; - let s = 0; - for (const x of nums) { - s += Math.ceil(x / mid); - } + const s = nums.reduce((acc, x) => acc + Math.ceil(x / mid), 0); if (s <= threshold) { r = mid; } else { diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution2.py b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution2.py deleted file mode 100644 index d93ecd44d43ab..0000000000000 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution2.py +++ /dev/null @@ -1,7 +0,0 @@ -class Solution: - def smallestDivisor(self, nums: List[int], threshold: int) -> int: - def f(v: int) -> bool: - v += 1 - return sum((x + v - 1) // v for x in nums) <= threshold - - return bisect_left(range(max(nums)), True, key=f) + 1