Skip to content

Commit 4a6cc58

Browse files
authored
added solution for 1283 (fishercoder1534#129)
1 parent 59725e6 commit 4a6cc58

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ _If you like this project, please leave me a star._ ★
174174
|1289|[Minimum Falling Path Sum II](https://leetcode.com/problems/minimum-falling-path-sum-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1289.java) | |Hard|Dynamic Programming|
175175
|1287|[Element Appearing More Than 25% In Sorted Array](https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1287.java) | [:tv:](https://youtu.be/G74W8v2yVjY) |Easy||
176176
|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1286.java) | |Medium|Backtracking, Design|
177+
|1283|[Find the Smallest Divisor Given a Threshold](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1283.java) | Medium |
177178
|1282|[Group the People Given the Group Size They Belong To](https://leetcode.com/problems/group-the-people-given-the-group-size-they-belong-to/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1282.java) | [:tv:](https://www.youtube.com/watch?v=wGgcRCpSAa8)|Medium||
178179
|1281|[Subtract the Product and Sum of Digits of an Integer](https://leetcode.com/problems/subtract-the-product-and-sum-of-digits-of-an-integer/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1281.java) | |Easy||
179180
|1277|[Count Square Submatrices with All Ones](https://leetcode.com/problems/count-square-submatrices-with-all-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1277.java) | |Medium||
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _1283 {
4+
public static class Solution{
5+
private boolean isSumLessThanThreshold(int middle, int[] nums, int threshold){
6+
int sum = 0;
7+
for(int i = 0; i<nums.length; i++){
8+
if(nums[i] % middle == 0)
9+
sum += nums[i]/middle;
10+
else
11+
sum += nums[i]/middle + 1;
12+
}
13+
return sum <= threshold;
14+
}
15+
public int smallestDivisor(int[] nums, int threshold) {
16+
17+
int start = 1, result = 0;
18+
int end = Integer.MAX_VALUE;
19+
while(start <= end){
20+
int middle = start + (end - start)/2;
21+
if(isSumLessThanThreshold(middle, nums, threshold)){
22+
result = middle;
23+
end = middle - 1;
24+
}
25+
else{
26+
start = middle + 1;
27+
}
28+
}
29+
return result;
30+
}
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.utils.TreeUtils;
4+
import com.fishercoder.solutions._1283;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import java.util.Arrays;
9+
10+
import static junit.framework.TestCase.assertEquals;
11+
12+
public class _1283Test {
13+
private static _1283.Solution solution;
14+
private static int[] nums;
15+
private static int threshold;
16+
@BeforeClass
17+
public static void setup() {
18+
solution = new _1283.Solution();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
nums = new int []{1,2,5,9};
24+
threshold = 6;
25+
assertEquals(5, solution.smallestDivisor(nums, threshold));
26+
}
27+
@Test
28+
public void test2() {
29+
nums = new int []{2,3,5,7,11};
30+
threshold = 11;
31+
assertEquals(3, solution.smallestDivisor(nums, threshold));
32+
}
33+
}

0 commit comments

Comments
 (0)