LeetCode
LeetCode problems that are solved.
This website collects cookies to deliver better user experience
class Solution{
static long minTime(int[] arr,int n,int k){
//code here
long sum = getSum(arr);
long max = getMax(arr);
return binarySearch(arr, max, sum, k);
}
static long binarySearch(int [] arr, long low, long high, int k) {
while (low < high) {
long middle = low + (high - low) / 2;
int painters = findPainters(arr, middle);
if (painters <= k) {
high = middle;
}
else {
low = middle + 1;
}
}
return low;
}
static int findPainters(int [] arr, long maxTime) {
int painter = 1;
long sum = 0;
int length = arr.length;
for (int i=0; i<length; i++) {
sum += arr[i];
if (sum > maxTime) {
painter ++;
sum = arr[i];
}
}
return painter;
}
static long getSum(int [] arr) {
long total = 0;
for (int number : arr) {
total += number;
}
return total;
}
static long getMax(int [] arr) {
long max = Integer.MIN_VALUE;
for (int number : arr) {
max = Math.max(max, number);
}
return max;
}
}
LeetCode problems that are solved.
Leetcode Top Interview questions discussed in Leetcode. https://leetcode.com/explore/interview/card/top-interview-questions-easy/
Also Question answered from CodeSignal.com : https://app.codesignal.com/