-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.java
More file actions
70 lines (62 loc) · 1.64 KB
/
day14.java
File metadata and controls
70 lines (62 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Problem:https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/description/
class Solution {
public int smallestDivisor(int[] nums, int threshold) {
int start = 1;
int end = Integer.MIN_VALUE;
for(int i=0;i<nums.length;i++){
if(nums[i]>end){
end = nums[i];
}
}
int res = -1;
while (start<=end){
int mid = start + (end-start)/2;
if(isDivisionPossible(nums, mid, threshold)){
res = mid;
end = mid-1;
} else {
start = mid+1;
}
}
return res;
}
SC-O(1)
TC-O(n*LOG(MAX(num)))
//Problem:https://www.geeksforgeeks.org/problems/square-root/0?utm_source=youtube&utm_medium=collab_striver_ytdescription&utm_campaign=square-root
class Solution {
int floorSqrt(int n) {
if (n == 0 || n == 1)
return n;
int s=1,e=n,sqrt=1;
while (s <= e) {
int mid = (s+ e) / 2;
if (mid * mid == n)
return mid;
if (mid * mid < n) {
s = mid + 1;
sqrt = mid;
}
else
e = mid - 1;
}
return sqrt;
}
}
//TC:O(LOGN)
// SC:O(1)
boolean isDivisionPossible(int nums[], int divisor, int threshold){
int sumOfDivision = 0;
for(int i=0; i<nums.length;i++){
sumOfDivision += nums[i] / divisor;
if(nums[i]%divisor!=0){
sumOfDivision+=1;
}
if(sumOfDivision > threshold){
return false;
}
}
return true;
}
}
//TC:O(N)
//SC:O(1)