Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- A20250511b_UberOA
- #binary_search
- given you a list of travel stops by uber drivers in an array and they are strictly increasing
- the fatigue is called as difference between the stops
- we need to decrease the maximum fatigue through out their entiner journey
- we can place a stop at any point of time but only k times
- find the minimum possible max fatigue that can be done after inducing k stops
- -----------------------------------------------------------------------------------------------------
- important thing to be noted here is -
- lets say you have stops at 2 and 8 ,so the fatigue is 6
- if you can place one stop between them you can put at 3
- then it makes 2 3 8 as stops fatigue btw 2-3 is 1 and 3-8 is 5
- so max fatigue shrink down to 5 ,but we can do it better what if
- we put the stop at middle point this makes the distance on either side of stop
- as equidistant which hopefully reduces the overall max
- what's now
- ideally to reduce fatigue we can put many stops and shrink down the max fatigue to 1
- but we can use utmost k stops it might not be possible
- lazily we can leave fatigue as it is and put no stops ,but thats bad
- this makes it as binary search problem
- choose a desiredFatigue x
- check if it is possible
- if possible then try for less desiredFatigue less than x
- else settle in search of possible less desiredFatigue more than x
- assume the currFatigue say 5 between one of the stops is y
- but we need it to be desiredFatigue say 3
- this will give us stopsNeeded
- ceil(5/3)-1 => 1 just one stop is enough
- this brings us to the formula
- ceil(currFatigue /desiredFatigue)-1
- -----------------------------------------------------------------------------------------------------
- input - stops and allowedStops
- 2 5 10
- 3
- output -
- 2
- */
- import java.util.*;
- @SuppressWarnings({"unused","unchecked"})
- public class A20250511b_UberOA {
- static Scanner sc = new Scanner(System.in);
- private static int[] getArray() {
- String[] sArr = sc.nextLine().split(" ");
- int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
- return arr;
- }
- private static char[] getCharArray() {
- String[] sArr = sc.nextLine().split(" ");
- char[] cArr = new char[sArr.length];
- for (int i = 0; i < sArr.length; i++) {
- cArr[i] = sArr[i].charAt(0); // Take the first character of each string
- }
- return cArr;
- }
- private static int getMax(int[] arr) {
- int currMax = Integer.MIN_VALUE;
- for (int curr : arr) {
- currMax = Math.max(currMax, curr);
- }
- return currMax;
- }
- private static boolean checkIfPossible(int desiredFatigue,int[] stops,int availableStops){
- int lenn = stops.length;
- int totalRequireStops = 0;
- // System.out.println("desiredFatigue is " + desiredFatigue);
- for(int i = 0;i<lenn-1;i+=1){
- int currFatigue = stops[i+1]-stops[i];
- int currStops = 0;
- if(currFatigue>desiredFatigue){
- //putting a double is importan other wise java already make your answer rounded down
- currStops += (int)Math.ceil((double)currFatigue/desiredFatigue)-1;
- }
- // System.out.println("currStops is " + currStops);
- totalRequireStops += currStops;
- }
- return totalRequireStops <= availableStops;
- }
- private static int checkminPossibleMaxFatigue(int left,int right,int[] stops,int availableStops){
- int ans = right;
- while(left<=right){
- int mid = (right-left)/2 + left;
- // System.out.println("left is " + left);
- // System.out.println("right is " + right);
- // System.out.println("mid is " + mid);
- if(checkIfPossible(mid,stops,availableStops)){
- right = mid-1;//become greedy and choose for more minimum fatigue
- ans = mid;
- }else{
- left = mid + 1;
- }
- // System.out.println("ans is " + ans);
- }
- return ans;
- }
- public static void main(String args[]) {
- // prepare the inputs
- int[] stops = getArray();
- int lenn = stops.length;
- int availableStops = getArray()[0];
- int maxi = -(int)(1e9+7);
- for(int i = 0;i<lenn-1;i+=1){
- maxi = Math.max(maxi,stops[i+1]-stops[i]);
- }
- // System.out.println("maxi is "+maxi);
- int ans = checkminPossibleMaxFatigue(1,maxi,stops,availableStops);
- System.out.println("ans is "+ans);
- }
- }
- class Pair{
- int row;
- int col;
- public Pair(int i,int j){
- this.row = i;
- this.col = j;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement