Skip to content

Commit fbac70e

Browse files
authored
Merge pull request #9 from Subham1999/back_tracking
Back tracking
2 parents 1a914a4 + 94843ce commit fbac70e

File tree

7 files changed

+338
-39
lines changed

7 files changed

+338
-39
lines changed

.idea/workspace.xml

Lines changed: 61 additions & 39 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.thealgorithm.array;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class TheThreeSum {
7+
public static void main(String[] args){
8+
9+
}
10+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.thealgorithm.binsearch;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class CapacityToShipPackagesWithInDDays {
7+
8+
public int shipWithinDays(int[] weights, int days) {
9+
int n = weights.length;
10+
int[] prefixSum = new int[n + 1];
11+
int weightMin = Integer.MIN_VALUE;
12+
13+
for (int i = 0; i < n; ++i) {
14+
prefixSum[i + 1] = prefixSum[i] + weights[i];
15+
weightMin = Math.max(weightMin, weights[i]);
16+
}
17+
18+
int weightMax = prefixSum[prefixSum.length - 1];
19+
20+
int ans = Integer.MAX_VALUE;
21+
22+
while (weightMin <= weightMax) {
23+
int mid = (weightMax + weightMin) / 2;
24+
int checkDays = checkDays(prefixSum, mid, 1, n);
25+
System.out.printf("midWeight = %d days = %d\n", mid, checkDays);
26+
if (checkDays <= days) {
27+
ans = mid;
28+
weightMax = mid - 1;
29+
} else {
30+
weightMin = mid + 1;
31+
}
32+
}
33+
34+
return ans;
35+
}
36+
37+
private int checkDays(int[] prefixSum, int weight, int lo, int hi) {
38+
if (lo > hi) {
39+
return 0;
40+
}
41+
if (lo == hi) {
42+
return (prefixSum[lo] - prefixSum[lo - 1]) <= weight ? 1 : 0;
43+
}
44+
int pos = lowerBound(prefixSum, lo, weight);
45+
if (pos == -1) {
46+
return Integer.MAX_VALUE;
47+
}
48+
return 1 + checkDays(prefixSum, weight, pos + 1, hi);
49+
}
50+
51+
private int lowerBound(int[] prefixSum, int lo, int target) {
52+
int right = prefixSum.length - 1;
53+
int left = lo;
54+
int pos = -1;
55+
while (left <= right) {
56+
int mid = (left + right) / 2;
57+
int midWeight = querySum(prefixSum, lo, mid);
58+
if (midWeight <= target) {
59+
pos = mid;
60+
left = mid + 1;
61+
} else {
62+
right = mid - 1;
63+
}
64+
}
65+
return pos;
66+
}
67+
68+
private int querySum(int[] prefixSum, int lo, int hi) {
69+
return prefixSum[hi] - prefixSum[lo - 1];
70+
}
71+
72+
public static void main(String[] args) {
73+
System.out.println(
74+
new CapacityToShipPackagesWithInDDays().shipWithinDays(new int[] {3, 2, 2, 4, 1, 4}, 3));
75+
76+
System.out.println(
77+
new CapacityToShipPackagesWithInDDays().shipWithinDays(new int[] {1, 2, 3, 1, 1}, 4));
78+
79+
System.out.println(
80+
new CapacityToShipPackagesWithInDDays()
81+
.shipWithinDays(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 5));
82+
}
83+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithm.binsearch;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class KokoEatingBananas {
7+
public int minEatingSpeed(int[] piles, int h) {
8+
long hi = 0L;
9+
long lo = 1;
10+
for (int pile : piles) {
11+
hi = Math.max(pile, hi);
12+
}
13+
// System.out.printf("Range speed %d to %d\n", lo, hi);
14+
// start binary search
15+
long minimumSpeed = 1L;
16+
while (lo <= hi) {
17+
long mid = ((hi - lo) >> 1) + lo;
18+
long kokoFinishTime = kokoFinishTime(piles, mid);
19+
// System.out.printf("finish time %d for speed %d\n", kokoFinishTime, mid);
20+
if (kokoFinishTime <= h) {
21+
// Taking exact same time or even less, we shall reduce speed and check
22+
minimumSpeed = mid;
23+
hi = mid - 1;
24+
} else {
25+
// Taking more time to finish, to reduce time, we need to increase speed
26+
lo = mid + 1;
27+
}
28+
}
29+
return (int) minimumSpeed;
30+
}
31+
32+
long kokoFinishTime(int[] piles, long speed) {
33+
long carry = 0L;
34+
for (double pile : piles) {
35+
carry += (long) Math.ceil(pile / speed);
36+
}
37+
return carry;
38+
}
39+
40+
public static void main(String[] args) {
41+
System.out.println(new KokoEatingBananas().minEatingSpeed(new int[] {3, 6, 7, 11}, 8));
42+
System.out.println(new KokoEatingBananas().minEatingSpeed(new int[] {100}, 99));
43+
System.out.println(
44+
new KokoEatingBananas()
45+
.minEatingSpeed(new int[] {805306368, 805306368, 805306368}, 1000000000));
46+
}
47+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithm.miscelleneous;
2+
3+
import com.thealgorithm.commons.Pair;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* @author: Subham Santra
9+
*/
10+
public class WalkingRobotSimulation {
11+
12+
static class Solution {
13+
public int robotSim(int[] commands, int[][] obstacles) {
14+
15+
Set<Pair<Integer, Integer>> obstacleSet = new HashSet<>();
16+
for (int[] obs : obstacles) {
17+
obstacleSet.add(Pair.of(obs[0], obs[1]));
18+
}
19+
20+
int[] current = new int[2];
21+
22+
char direction = 'e';
23+
24+
int max = 0;
25+
26+
for (int command : commands) {
27+
if (command < 0) {
28+
direction = changeDirection(direction, command == -1);
29+
} else {
30+
// move(dir, current, command, obstacleSet);
31+
int x = current[0];
32+
int y = current[1];
33+
34+
for (int c = 1; c <= command; ++c) {
35+
int xx = x + (direction == 'e' ? 1 : (direction == 'w' ? -1 : 0));
36+
int yy = y + (direction == 'n' ? 1 : (direction == 's' ? -1 : 0));
37+
if (obstacleSet.contains(Pair.of(xx, yy))) {
38+
break;
39+
}
40+
current[0] = x = xx;
41+
current[1] = y = yy;
42+
max = Math.max(max, (int) Math.sqrt(x * x + y * y));
43+
}
44+
}
45+
}
46+
return max;
47+
}
48+
49+
char changeDirection(char dir, boolean left) {
50+
return switch (dir) {
51+
case 'e' -> left ? 'n' : 's';
52+
case 'n' -> left ? 'w' : 'e';
53+
case 'w' -> left ? 's' : 'n';
54+
case 's' -> left ? 'e' : 'w';
55+
default -> '0';
56+
};
57+
}
58+
}
59+
60+
public static void main(String[] args) {}
61+
}

0 commit comments

Comments
 (0)