Skip to content

Commit e4ccdff

Browse files
committed
feat: add solutions to lc problem: No.1283. Find the Smallest Divisor Given a Threshold
1 parent b368be2 commit e4ccdff

File tree

6 files changed

+218
-2
lines changed

6 files changed

+218
-2
lines changed

solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README.md

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,22 +54,98 @@
5454

5555
<!-- 这里可写通用的实现逻辑 -->
5656

57+
二分查找。
58+
5759
<!-- tabs:start -->
5860

5961
### **Python3**
6062

6163
<!-- 这里可写当前语言的特殊实现逻辑 -->
6264

6365
```python
64-
66+
class Solution:
67+
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
68+
left, right = 1, 1000000
69+
while left < right:
70+
mid = (left + right) >> 1
71+
s = 0
72+
for num in nums:
73+
s += (num + mid - 1) // mid
74+
if s <= threshold:
75+
right = mid
76+
else:
77+
left = mid + 1
78+
return left
6579
```
6680

6781
### **Java**
6882

6983
<!-- 这里可写当前语言的特殊实现逻辑 -->
7084

7185
```java
86+
class Solution {
87+
public int smallestDivisor(int[] nums, int threshold) {
88+
int left = 1, right = 1000000;
89+
while (left < right) {
90+
int mid = (left + right) >> 1;
91+
int s = 0;
92+
for (int num : nums) {
93+
s += (num + mid - 1) / mid;
94+
}
95+
if (s <= threshold) {
96+
right = mid;
97+
} else {
98+
left = mid + 1;
99+
}
100+
}
101+
return left;
102+
}
103+
}
104+
```
105+
106+
### **C++**
107+
108+
```cpp
109+
class Solution {
110+
public:
111+
int smallestDivisor(vector<int>& nums, int threshold) {
112+
int left = 1, right = 1000000;
113+
while (left < right) {
114+
int mid = left + right >> 1;
115+
int s = 0;
116+
for (int& num : nums) {
117+
s += (num + mid - 1) / mid;
118+
}
119+
if (s <= threshold) {
120+
right = mid;
121+
} else {
122+
left = mid + 1;
123+
}
124+
}
125+
return left;
126+
}
127+
};
128+
```
72129
130+
### **Go**
131+
132+
```go
133+
func smallestDivisor(nums []int, threshold int) int {
134+
left, right := 1, 1000000
135+
for left < right {
136+
mid := (left + right) >> 1
137+
s := 0
138+
for _, num := range nums {
139+
s += (num + mid - 1) / mid
140+
}
141+
if s <= threshold {
142+
right = mid
143+
} else {
144+
left = mid + 1
145+
}
146+
}
147+
return left
148+
}
73149
```
74150

75151
### **...**

solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/README_EN.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,87 @@ If the divisor is 4 we can get a sum of 7 (1+1+2+3) and if the divisor is 5 the
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
63+
left, right = 1, 1000000
64+
while left < right:
65+
mid = (left + right) >> 1
66+
s = 0
67+
for num in nums:
68+
s += (num + mid - 1) // mid
69+
if s <= threshold:
70+
right = mid
71+
else:
72+
left = mid + 1
73+
return left
6274
```
6375

6476
### **Java**
6577

6678
```java
79+
class Solution {
80+
public int smallestDivisor(int[] nums, int threshold) {
81+
int left = 1, right = 1000000;
82+
while (left < right) {
83+
int mid = (left + right) >> 1;
84+
int s = 0;
85+
for (int num : nums) {
86+
s += (num + mid - 1) / mid;
87+
}
88+
if (s <= threshold) {
89+
right = mid;
90+
} else {
91+
left = mid + 1;
92+
}
93+
}
94+
return left;
95+
}
96+
}
97+
```
98+
99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int smallestDivisor(vector<int>& nums, int threshold) {
105+
int left = 1, right = 1000000;
106+
while (left < right) {
107+
int mid = left + right >> 1;
108+
int s = 0;
109+
for (int& num : nums) {
110+
s += (num + mid - 1) / mid;
111+
}
112+
if (s <= threshold) {
113+
right = mid;
114+
} else {
115+
left = mid + 1;
116+
}
117+
}
118+
return left;
119+
}
120+
};
121+
```
67122
123+
### **Go**
124+
125+
```go
126+
func smallestDivisor(nums []int, threshold int) int {
127+
left, right := 1, 1000000
128+
for left < right {
129+
mid := (left + right) >> 1
130+
s := 0
131+
for _, num := range nums {
132+
s += (num + mid - 1) / mid
133+
}
134+
if s <= threshold {
135+
right = mid
136+
} else {
137+
left = mid + 1
138+
}
139+
}
140+
return left
141+
}
68142
```
69143

70144
### **...**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int smallestDivisor(vector<int>& nums, int threshold) {
4+
int left = 1, right = 1000000;
5+
while (left < right) {
6+
int mid = left + right >> 1;
7+
int s = 0;
8+
for (int& num : nums) {
9+
s += (num + mid - 1) / mid;
10+
}
11+
if (s <= threshold) {
12+
right = mid;
13+
} else {
14+
left = mid + 1;
15+
}
16+
}
17+
return left;
18+
}
19+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func smallestDivisor(nums []int, threshold int) int {
2+
left, right := 1, 1000000
3+
for left < right {
4+
mid := (left + right) >> 1
5+
s := 0
6+
for _, num := range nums {
7+
s += (num + mid - 1) / mid
8+
}
9+
if s <= threshold {
10+
right = mid
11+
} else {
12+
left = mid + 1
13+
}
14+
}
15+
return left
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int smallestDivisor(int[] nums, int threshold) {
3+
int left = 1, right = 1000000;
4+
while (left < right) {
5+
int mid = (left + right) >> 1;
6+
int s = 0;
7+
for (int num : nums) {
8+
s += (num + mid - 1) / mid;
9+
}
10+
if (s <= threshold) {
11+
right = mid;
12+
} else {
13+
left = mid + 1;
14+
}
15+
}
16+
return left;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def smallestDivisor(self, nums: List[int], threshold: int) -> int:
3+
left, right = 1, 1000000
4+
while left < right:
5+
mid = (left + right) >> 1
6+
s = 0
7+
for num in nums:
8+
s += (num + mid - 1) // mid
9+
if s <= threshold:
10+
right = mid
11+
else:
12+
left = mid + 1
13+
return left

0 commit comments

Comments
 (0)