Skip to content

Commit db0d111

Browse files
committed
ADDED
1 parent b8ddcbc commit db0d111

File tree

6 files changed

+273
-26
lines changed

6 files changed

+273
-26
lines changed

.idea/workspace.xml

Lines changed: 43 additions & 26 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: 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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.thealgorithm.rangeq;
2+
3+
4+
/**
5+
* @author: Subham Santra
6+
*/
7+
public class RangeSumSegmentTree {
8+
public static void main(String[] args){
9+
10+
}
11+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithm.slidingwindow;
2+
3+
/**
4+
* @author: Subham Santra
5+
*/
6+
public class MinimumWindowSubstring {
7+
private static class Solution {
8+
int ord(char c) {
9+
return ('A' <= c && c <= 'Z') ? 26 + (c - 'A') : (c - 'a');
10+
}
11+
12+
boolean isMatch(int[] w, int[] t) {
13+
for (int i = 0; i < 52; ++i) {
14+
if (t[i] != 0 && t[i] > w[i]) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
21+
public String minWindow(String s, String t) {
22+
int[] tScore = new int[52];
23+
int[] wScore = new int[52];
24+
25+
for (int i = 0; i < t.length(); ++i) {
26+
tScore[ord(t.charAt(i))]++;
27+
}
28+
29+
int minWindowLength = Integer.MAX_VALUE;
30+
int start = -1;
31+
int end = -1;
32+
33+
for (int lo = 0, hi = 0; hi < s.length(); ++hi) {
34+
//System.out.println("- " + s.charAt(hi) + " - " + s.substring(lo, hi + 1));
35+
wScore[ord(s.charAt(hi))]++;
36+
37+
while (lo <= hi && isMatch(wScore, tScore)) {
38+
//System.out.println(s.charAt(hi) + " - " + s.substring(lo, hi + 1));
39+
if (minWindowLength > (hi - lo + 1)) {
40+
minWindowLength = hi - lo + 1;
41+
start = lo;
42+
end = hi;
43+
}
44+
wScore[ord(s.charAt(lo))]--;
45+
++lo;
46+
//System.out.println(s.charAt(hi) + " - " + s.substring(lo, hi + 1));
47+
}
48+
}
49+
50+
if (start > end) {
51+
return "";
52+
}
53+
if (start == -1) {
54+
return "";
55+
}
56+
return s.substring(start, end + 1);
57+
}
58+
}
59+
60+
public static void main(String[] args){
61+
System.out.println(new Solution().minWindow("ADOBECODEBANC", "ABC"));
62+
System.out.println(new Solution().minWindow("ACACBDEA", "ABC"));
63+
System.out.println(new Solution().minWindow("ADOBECODEBANC", "ABC"));
64+
}
65+
}

0 commit comments

Comments
 (0)