Skip to content

Commit e757db9

Browse files
authored
Merge pull request #12 from Subham1999/back_tracking
Back tracking
2 parents 346ab27 + 8f3d190 commit e757db9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1300
-530
lines changed

.idea/workspace.xml

Lines changed: 67 additions & 529 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
11
package com.thealgorithm;
22

3+
import java.util.Arrays;
4+
35
public class Main {
46
public static void main(String[] args) {
5-
System.out.println("Say! Hello Algoithm!!");
7+
8+
System.out.println(new Solution().takeCharacters("aabbccca", 2));
9+
}
10+
}
11+
12+
class Solution {
13+
private int[] count;
14+
private int k;
15+
private int[][] DP;
16+
17+
public int takeCharacters(String s, int k) {
18+
if (k == 0) {
19+
return 0;
20+
}
21+
count = new int[3];
22+
this.k = k;
23+
DP = new int[s.length()][s.length()];
24+
for (int[] _d : DP) Arrays.fill(_d, -1);
25+
26+
takeAndReturn(s, 0, s.length() - 1, 0);
27+
28+
return DP[0][s.length() - 1] == Integer.MAX_VALUE ? -1 : DP[0][s.length() - 1];
29+
}
30+
31+
int takeAndReturn(String s, int left, int right, int minutes) {
32+
// System.out.println(Arrays.toString(count) + " " + left + " " + right);
33+
if (count[0] >= k && count[1] >= k && count[2] >= k) {
34+
return minutes;
35+
}
36+
37+
if (left > right) {
38+
return Integer.MAX_VALUE;
39+
}
40+
41+
if (DP[left][right] != -1) {
42+
return DP[left][right];
43+
}
44+
45+
int ans;
46+
47+
count[s.charAt(left) - 'a']++;
48+
ans = takeAndReturn(s, left + 1, right, minutes + 1);
49+
count[s.charAt(left) - 'a']--;
50+
51+
count[s.charAt(right) - 'a']++;
52+
ans = Math.min(ans, takeAndReturn(s, left, right - 1, minutes + 1));
53+
count[s.charAt(right) - 'a']--;
54+
55+
return DP[left][right] = ans;
656
}
757
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.thealgorithm.array;
2+
3+
public class SortZeroOneTwo {
4+
public static void main(String[] args) {}
5+
6+
static void sort(int[] arr) {
7+
int left = 0;
8+
int right = arr.length - 1;
9+
10+
while (left <= right) {
11+
if (arr[left] == 2 && arr[right] == 0) {}
12+
}
13+
}
14+
15+
static void swap(int[] a, int i, int j) {
16+
int x = a[i];
17+
a[i] = a[j];
18+
a[j] = x;
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.thealgorithm.dp;
2+
3+
public class MoveOnMatrix {
4+
5+
public static void main(String[] args) {
6+
System.out.println(new MoveOnMatrix().solve(3, 3));
7+
}
8+
9+
int solve(int m, int n) {
10+
return solve(m - 1, 0, m - 1, n - 1);
11+
}
12+
13+
private int solve(int i, int j, int I, int J) {
14+
if (i == I && j == J) {
15+
return 1;
16+
}
17+
if (i > I || j > J) return 0;
18+
19+
int w1 = solve(i - 1, j + 1, I, J);
20+
int w2 = solve(i, j + 1, I, J);
21+
int w3 = solve(i + 1, j + 1, I, J);
22+
return w1 + w2 + w3;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.thealgorithm.graph;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Comparator;
6+
import java.util.HashMap;
7+
import java.util.Iterator;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class MakeLexicographicallySmallestArrayBySwappingElements {
12+
13+
private static class Solution {
14+
private static class Point {
15+
final int val;
16+
final int index;
17+
18+
public Point(int val, int index) {
19+
this.val = val;
20+
this.index = index;
21+
}
22+
}
23+
24+
public int[] lexicographicallySmallestArray(int[] nums, int limit) {
25+
final Point[] points = new Point[nums.length];
26+
for (int i = 0; i < nums.length; ++i) {
27+
points[i] = new Point(nums[i], i);
28+
}
29+
30+
Arrays.sort(points, Comparator.comparingInt(p -> p.val));
31+
32+
// Connected components
33+
List<List<Point>> list = new ArrayList<>();
34+
List<Point> tempList = new ArrayList<>();
35+
36+
for (int i = 0; i < points.length; ++i) {
37+
if (i == 0) {
38+
tempList.add(points[i]);
39+
} else {
40+
if (points[i].val - tempList.getLast().val > limit) {
41+
list.add(tempList);
42+
tempList = new ArrayList<>();
43+
}
44+
tempList.add(points[i]);
45+
}
46+
}
47+
list.add(tempList);
48+
49+
Map<Integer, Iterator<Point>> map = new HashMap<>();
50+
for (var l : list) {
51+
for (var ll : l) {
52+
map.put(ll.index, l.iterator());
53+
}
54+
}
55+
56+
int[] answer = new int[nums.length];
57+
for (int i = 0; i < nums.length; ++i) {
58+
answer[i] = map.get(i).next().val;
59+
}
60+
61+
return answer;
62+
}
63+
}
64+
65+
public static void main(String[] args){
66+
System.out.println(Arrays.toString(new Solution().lexicographicallySmallestArray(new int[]{1, 4, 2, 1, 4, 2, 1}, 1)));
67+
}
68+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.thealgorithm.linkedlist;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class ReverseSentence {
7+
8+
public static class Node {
9+
char c;
10+
Node next;
11+
12+
public Node(char c, Node next) {
13+
this.c = c;
14+
this.next = next;
15+
}
16+
17+
public Node() {}
18+
}
19+
20+
public static class Creator {
21+
public static Node prepare(String sentence) {
22+
if (sentence.isBlank()) {
23+
return null;
24+
}
25+
26+
Node head = new Node();
27+
Node node = head;
28+
29+
for (char c : sentence.toCharArray()) {
30+
node.c = c;
31+
node.next = new Node();
32+
}
33+
34+
return head;
35+
}
36+
}
37+
38+
public static class Printer {
39+
static void print(Node node) {
40+
for (; node != null; node = node.next) {
41+
System.out.print(node.c);
42+
System.out.print(' ');
43+
}
44+
System.out.println();
45+
}
46+
}
47+
48+
public static class Solution {
49+
public static Node reverseIt(Node head) {
50+
Node node = head;
51+
Node left, right;
52+
left = right = node;
53+
54+
while (node != null) {
55+
if (node.c == ' ') {
56+
partialReverse(left, right);
57+
right = left = node.next;
58+
} else {
59+
right = node;
60+
}
61+
}
62+
return head;
63+
}
64+
65+
private static void partialReverse(Node left, Node right) {
66+
Node head = left;
67+
68+
}
69+
}
70+
71+
public static void main(String[] args) {
72+
Node sentence = Creator.prepare("Hi this is Subham, are you ok?");
73+
Printer.print(sentence);
74+
Printer.print(Solution.reverseIt(sentence));
75+
}
76+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.thealgorithm.multithreading;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
import java.util.concurrent.locks.ReentrantReadWriteLock;
6+
7+
public class Print1To100 {
8+
static class Counter {
9+
transient int val;
10+
11+
synchronized void incrementOnce() {
12+
val++;
13+
}
14+
15+
void print() {
16+
System.out.println(Thread.currentThread().getName() + " - " + val);
17+
}
18+
}
19+
20+
public static void main(String[] args) {
21+
ExecutorService executorService = Executors.newFixedThreadPool(10);
22+
Counter counter = new Counter();
23+
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
24+
25+
for (int i = 0; i < 100; ++i) {
26+
executorService.submit(
27+
() -> {
28+
readWriteLock.writeLock().lock();
29+
counter.incrementOnce();
30+
counter.print();
31+
readWriteLock.writeLock().unlock();
32+
});
33+
}
34+
}
35+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.thealgorithm.stacks;
2+
3+
import java.util.Arrays;
4+
5+
public class ShortestSubArrayToBeRemovedToMakeArraySorted {
6+
7+
public int findLengthOfShortestSubarray(int[] arr) {
8+
int leftSequenceStop = 0, rightSequenceStop = arr.length - 1;
9+
10+
for (int i = 1; i < arr.length; ++i) {
11+
if (arr[i - 1] > arr[i]) {
12+
leftSequenceStop = i - 1;
13+
break;
14+
}
15+
}
16+
17+
for (int i = arr.length - 2; i >= leftSequenceStop; --i) {
18+
if (arr[i] > arr[i + 1]) {
19+
rightSequenceStop = i + 1;
20+
break;
21+
}
22+
}
23+
24+
int answer = 0;
25+
26+
System.out.println(Arrays.toString(Arrays.copyOfRange(arr, 0, leftSequenceStop)));
27+
System.out.println(Arrays.toString(Arrays.copyOfRange(arr, rightSequenceStop, arr.length)));
28+
29+
for (int i = rightSequenceStop; i < arr.length; ++i) {
30+
answer = Math.max(answer, upperBoundIndex(arr, 0, leftSequenceStop, arr[i]) + arr.length - i);
31+
}
32+
33+
for (int i = leftSequenceStop; i >= 0; --i) {
34+
answer =
35+
Math.max(
36+
answer,
37+
lowerBoundIndex(arr, rightSequenceStop, arr.length - 1, arr[leftSequenceStop]));
38+
}
39+
40+
return answer == 0 ? 0 : arr.length - answer;
41+
}
42+
43+
int upperBoundIndex(int[] arr, int lo, int hi, int t) {
44+
int m;
45+
int ans = -1;
46+
47+
while (lo <= hi) {
48+
m = (lo + hi) >> 1;
49+
if (arr[m] <= t) {
50+
lo = m + 1;
51+
} else {
52+
ans = m;
53+
hi = m - 1;
54+
}
55+
}
56+
57+
return ans;
58+
}
59+
60+
int lowerBoundIndex(int[] arr, int lo, int hi, int t) {
61+
int m;
62+
int ans = arr.length;
63+
64+
while (lo <= hi) {
65+
m = (lo + hi) >> 1;
66+
if (arr[m] >= t) {
67+
hi = m - 1;
68+
} else {
69+
ans = m;
70+
lo = m + 1;
71+
}
72+
}
73+
74+
return ans;
75+
}
76+
77+
public static void main(String[] args) {
78+
System.out.println(
79+
new ShortestSubArrayToBeRemovedToMakeArraySorted()
80+
.findLengthOfShortestSubarray(new int[] {1, 2, 11, 10, 4, 11, 12, 15}));
81+
//
82+
// System.out.println(
83+
// new ShortestSubArrayToBeRemovedToMakeArraySorted()
84+
// .findLengthOfShortestSubarray(new int[] {1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6}));
85+
//
86+
// System.out.println(
87+
// new ShortestSubArrayToBeRemovedToMakeArraySorted()
88+
// .findLengthOfShortestSubarray(new int[] {5, 4, 3, 2, 1}));
89+
90+
System.out.println(
91+
new ShortestSubArrayToBeRemovedToMakeArraySorted()
92+
.findLengthOfShortestSubarray(new int[] {1, 2, 3, 10, 0, 7, 8, 9}));
93+
}
94+
}

0 commit comments

Comments
 (0)