Skip to content

Add solution for 1283 #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ _If you like this project, please leave me a star._ ★
|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|
|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||
|1286|[Iterator for Combination](https://leetcode.com/problems/iterator-for-combination/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1286.java) | |Medium|Backtracking, Design|
|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 |
|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||
|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||
|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||
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/fishercoder/solutions/_1283.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fishercoder.solutions;

public class _1283 {
public static class Solution{
private boolean isSumLessThanThreshold(int middle, int[] nums, int threshold){
int sum = 0;
for(int i = 0; i<nums.length; i++){
if(nums[i] % middle == 0)
sum += nums[i]/middle;
else
sum += nums[i]/middle + 1;
}
return sum <= threshold;
}
public int smallestDivisor(int[] nums, int threshold) {

int start = 1, result = 0;
int end = Integer.MAX_VALUE;
while(start <= end){
int middle = start + (end - start)/2;
if(isSumLessThanThreshold(middle, nums, threshold)){
result = middle;
end = middle - 1;
}
else{
start = middle + 1;
}
}
return result;
}
}
}
33 changes: 33 additions & 0 deletions src/test/java/com/fishercoder/_1283Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.fishercoder;

import com.fishercoder.common.utils.TreeUtils;
import com.fishercoder.solutions._1283;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.Arrays;

import static junit.framework.TestCase.assertEquals;

public class _1283Test {
private static _1283.Solution solution;
private static int[] nums;
private static int threshold;
@BeforeClass
public static void setup() {
solution = new _1283.Solution();
}

@Test
public void test1() {
nums = new int []{1,2,5,9};
threshold = 6;
assertEquals(5, solution.smallestDivisor(nums, threshold));
}
@Test
public void test2() {
nums = new int []{2,3,5,7,11};
threshold = 11;
assertEquals(3, solution.smallestDivisor(nums, threshold));
}
}