Skip to content

Back tracking #9

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 2 commits into from
Sep 8, 2024
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
100 changes: 61 additions & 39 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.thealgorithm.array;

/**
* @author: Subham Santra
*/
public class TheThreeSum {
public static void main(String[] args){

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.thealgorithm.binsearch;

/**
* @author: Subham Santra
*/
public class CapacityToShipPackagesWithInDDays {

public int shipWithinDays(int[] weights, int days) {
int n = weights.length;
int[] prefixSum = new int[n + 1];
int weightMin = Integer.MIN_VALUE;

for (int i = 0; i < n; ++i) {
prefixSum[i + 1] = prefixSum[i] + weights[i];
weightMin = Math.max(weightMin, weights[i]);
}

int weightMax = prefixSum[prefixSum.length - 1];

int ans = Integer.MAX_VALUE;

while (weightMin <= weightMax) {
int mid = (weightMax + weightMin) / 2;
int checkDays = checkDays(prefixSum, mid, 1, n);
System.out.printf("midWeight = %d days = %d\n", mid, checkDays);
if (checkDays <= days) {
ans = mid;
weightMax = mid - 1;
} else {
weightMin = mid + 1;
}
}

return ans;
}

private int checkDays(int[] prefixSum, int weight, int lo, int hi) {
if (lo > hi) {
return 0;
}
if (lo == hi) {
return (prefixSum[lo] - prefixSum[lo - 1]) <= weight ? 1 : 0;
}
int pos = lowerBound(prefixSum, lo, weight);
if (pos == -1) {
return Integer.MAX_VALUE;
}
return 1 + checkDays(prefixSum, weight, pos + 1, hi);
}

private int lowerBound(int[] prefixSum, int lo, int target) {
int right = prefixSum.length - 1;
int left = lo;
int pos = -1;
while (left <= right) {
int mid = (left + right) / 2;
int midWeight = querySum(prefixSum, lo, mid);
if (midWeight <= target) {
pos = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
return pos;
}

private int querySum(int[] prefixSum, int lo, int hi) {
return prefixSum[hi] - prefixSum[lo - 1];
}

public static void main(String[] args) {
System.out.println(
new CapacityToShipPackagesWithInDDays().shipWithinDays(new int[] {3, 2, 2, 4, 1, 4}, 3));

System.out.println(
new CapacityToShipPackagesWithInDDays().shipWithinDays(new int[] {1, 2, 3, 1, 1}, 4));

System.out.println(
new CapacityToShipPackagesWithInDDays()
.shipWithinDays(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.thealgorithm.binsearch;

/**
* @author: Subham Santra
*/
public class KokoEatingBananas {
public int minEatingSpeed(int[] piles, int h) {
long hi = 0L;
long lo = 1;
for (int pile : piles) {
hi = Math.max(pile, hi);
}
// System.out.printf("Range speed %d to %d\n", lo, hi);
// start binary search
long minimumSpeed = 1L;
while (lo <= hi) {
long mid = ((hi - lo) >> 1) + lo;
long kokoFinishTime = kokoFinishTime(piles, mid);
// System.out.printf("finish time %d for speed %d\n", kokoFinishTime, mid);
if (kokoFinishTime <= h) {
// Taking exact same time or even less, we shall reduce speed and check
minimumSpeed = mid;
hi = mid - 1;
} else {
// Taking more time to finish, to reduce time, we need to increase speed
lo = mid + 1;
}
}
return (int) minimumSpeed;
}

long kokoFinishTime(int[] piles, long speed) {
long carry = 0L;
for (double pile : piles) {
carry += (long) Math.ceil(pile / speed);
}
return carry;
}

public static void main(String[] args) {
System.out.println(new KokoEatingBananas().minEatingSpeed(new int[] {3, 6, 7, 11}, 8));
System.out.println(new KokoEatingBananas().minEatingSpeed(new int[] {100}, 99));
System.out.println(
new KokoEatingBananas()
.minEatingSpeed(new int[] {805306368, 805306368, 805306368}, 1000000000));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.thealgorithm.miscelleneous;

import com.thealgorithm.commons.Pair;
import java.util.HashSet;
import java.util.Set;

/**
* @author: Subham Santra
*/
public class WalkingRobotSimulation {

static class Solution {
public int robotSim(int[] commands, int[][] obstacles) {

Set<Pair<Integer, Integer>> obstacleSet = new HashSet<>();
for (int[] obs : obstacles) {
obstacleSet.add(Pair.of(obs[0], obs[1]));
}

int[] current = new int[2];

char direction = 'e';

int max = 0;

for (int command : commands) {
if (command < 0) {
direction = changeDirection(direction, command == -1);
} else {
// move(dir, current, command, obstacleSet);
int x = current[0];
int y = current[1];

for (int c = 1; c <= command; ++c) {
int xx = x + (direction == 'e' ? 1 : (direction == 'w' ? -1 : 0));
int yy = y + (direction == 'n' ? 1 : (direction == 's' ? -1 : 0));
if (obstacleSet.contains(Pair.of(xx, yy))) {
break;
}
current[0] = x = xx;
current[1] = y = yy;
max = Math.max(max, (int) Math.sqrt(x * x + y * y));
}
}
}
return max;
}

char changeDirection(char dir, boolean left) {
return switch (dir) {
case 'e' -> left ? 'n' : 's';
case 'n' -> left ? 'w' : 'e';
case 'w' -> left ? 's' : 'n';
case 's' -> left ? 'e' : 'w';
default -> '0';
};
}
}

public static void main(String[] args) {}
}
Loading