Skip to content

Back tracking #12

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 4 commits into from
Apr 21, 2025
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
596 changes: 67 additions & 529 deletions .idea/workspace.xml

Large diffs are not rendered by default.

52 changes: 51 additions & 1 deletion data-structures/src/main/java/com/thealgorithm/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
package com.thealgorithm;

import java.util.Arrays;

public class Main {
public static void main(String[] args) {
System.out.println("Say! Hello Algoithm!!");

System.out.println(new Solution().takeCharacters("aabbccca", 2));
}
}

class Solution {
private int[] count;
private int k;
private int[][] DP;

public int takeCharacters(String s, int k) {
if (k == 0) {
return 0;
}
count = new int[3];
this.k = k;
DP = new int[s.length()][s.length()];
for (int[] _d : DP) Arrays.fill(_d, -1);

takeAndReturn(s, 0, s.length() - 1, 0);

return DP[0][s.length() - 1] == Integer.MAX_VALUE ? -1 : DP[0][s.length() - 1];
}

int takeAndReturn(String s, int left, int right, int minutes) {
// System.out.println(Arrays.toString(count) + " " + left + " " + right);
if (count[0] >= k && count[1] >= k && count[2] >= k) {
return minutes;
}

if (left > right) {
return Integer.MAX_VALUE;
}

if (DP[left][right] != -1) {
return DP[left][right];
}

int ans;

count[s.charAt(left) - 'a']++;
ans = takeAndReturn(s, left + 1, right, minutes + 1);
count[s.charAt(left) - 'a']--;

count[s.charAt(right) - 'a']++;
ans = Math.min(ans, takeAndReturn(s, left, right - 1, minutes + 1));
count[s.charAt(right) - 'a']--;

return DP[left][right] = ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.thealgorithm.array;

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

static void sort(int[] arr) {
int left = 0;
int right = arr.length - 1;

while (left <= right) {
if (arr[left] == 2 && arr[right] == 0) {}
}
}

static void swap(int[] a, int i, int j) {
int x = a[i];
a[i] = a[j];
a[j] = x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.thealgorithm.dp;

public class MoveOnMatrix {

public static void main(String[] args) {
System.out.println(new MoveOnMatrix().solve(3, 3));
}

int solve(int m, int n) {
return solve(m - 1, 0, m - 1, n - 1);
}

private int solve(int i, int j, int I, int J) {
if (i == I && j == J) {
return 1;
}
if (i > I || j > J) return 0;

int w1 = solve(i - 1, j + 1, I, J);
int w2 = solve(i, j + 1, I, J);
int w3 = solve(i + 1, j + 1, I, J);
return w1 + w2 + w3;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.thealgorithm.graph;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MakeLexicographicallySmallestArrayBySwappingElements {

private static class Solution {
private static class Point {
final int val;
final int index;

public Point(int val, int index) {
this.val = val;
this.index = index;
}
}

public int[] lexicographicallySmallestArray(int[] nums, int limit) {
final Point[] points = new Point[nums.length];
for (int i = 0; i < nums.length; ++i) {
points[i] = new Point(nums[i], i);
}

Arrays.sort(points, Comparator.comparingInt(p -> p.val));

// Connected components
List<List<Point>> list = new ArrayList<>();
List<Point> tempList = new ArrayList<>();

for (int i = 0; i < points.length; ++i) {
if (i == 0) {
tempList.add(points[i]);
} else {
if (points[i].val - tempList.getLast().val > limit) {
list.add(tempList);
tempList = new ArrayList<>();
}
tempList.add(points[i]);
}
}
list.add(tempList);

Map<Integer, Iterator<Point>> map = new HashMap<>();
for (var l : list) {
for (var ll : l) {
map.put(ll.index, l.iterator());
}
}

int[] answer = new int[nums.length];
for (int i = 0; i < nums.length; ++i) {
answer[i] = map.get(i).next().val;
}

return answer;
}
}

public static void main(String[] args){
System.out.println(Arrays.toString(new Solution().lexicographicallySmallestArray(new int[]{1, 4, 2, 1, 4, 2, 1}, 1)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.thealgorithm.linkedlist;

import java.util.ArrayList;
import java.util.List;

public class ReverseSentence {

public static class Node {
char c;
Node next;

public Node(char c, Node next) {
this.c = c;
this.next = next;
}

public Node() {}
}

public static class Creator {
public static Node prepare(String sentence) {
if (sentence.isBlank()) {
return null;
}

Node head = new Node();
Node node = head;

for (char c : sentence.toCharArray()) {
node.c = c;
node.next = new Node();
}

return head;
}
}

public static class Printer {
static void print(Node node) {
for (; node != null; node = node.next) {
System.out.print(node.c);
System.out.print(' ');
}
System.out.println();
}
}

public static class Solution {
public static Node reverseIt(Node head) {
Node node = head;
Node left, right;
left = right = node;

while (node != null) {
if (node.c == ' ') {
partialReverse(left, right);
right = left = node.next;
} else {
right = node;
}
}
return head;
}

private static void partialReverse(Node left, Node right) {
Node head = left;

}
}

public static void main(String[] args) {
Node sentence = Creator.prepare("Hi this is Subham, are you ok?");
Printer.print(sentence);
Printer.print(Solution.reverseIt(sentence));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.thealgorithm.multithreading;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class Print1To100 {
static class Counter {
transient int val;

synchronized void incrementOnce() {
val++;
}

void print() {
System.out.println(Thread.currentThread().getName() + " - " + val);
}
}

public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(10);
Counter counter = new Counter();
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();

for (int i = 0; i < 100; ++i) {
executorService.submit(
() -> {
readWriteLock.writeLock().lock();
counter.incrementOnce();
counter.print();
readWriteLock.writeLock().unlock();
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.thealgorithm.stacks;

import java.util.Arrays;

public class ShortestSubArrayToBeRemovedToMakeArraySorted {

public int findLengthOfShortestSubarray(int[] arr) {
int leftSequenceStop = 0, rightSequenceStop = arr.length - 1;

for (int i = 1; i < arr.length; ++i) {
if (arr[i - 1] > arr[i]) {
leftSequenceStop = i - 1;
break;
}
}

for (int i = arr.length - 2; i >= leftSequenceStop; --i) {
if (arr[i] > arr[i + 1]) {
rightSequenceStop = i + 1;
break;
}
}

int answer = 0;

System.out.println(Arrays.toString(Arrays.copyOfRange(arr, 0, leftSequenceStop)));
System.out.println(Arrays.toString(Arrays.copyOfRange(arr, rightSequenceStop, arr.length)));

for (int i = rightSequenceStop; i < arr.length; ++i) {
answer = Math.max(answer, upperBoundIndex(arr, 0, leftSequenceStop, arr[i]) + arr.length - i);
}

for (int i = leftSequenceStop; i >= 0; --i) {
answer =
Math.max(
answer,
lowerBoundIndex(arr, rightSequenceStop, arr.length - 1, arr[leftSequenceStop]));
}

return answer == 0 ? 0 : arr.length - answer;
}

int upperBoundIndex(int[] arr, int lo, int hi, int t) {
int m;
int ans = -1;

while (lo <= hi) {
m = (lo + hi) >> 1;
if (arr[m] <= t) {
lo = m + 1;
} else {
ans = m;
hi = m - 1;
}
}

return ans;
}

int lowerBoundIndex(int[] arr, int lo, int hi, int t) {
int m;
int ans = arr.length;

while (lo <= hi) {
m = (lo + hi) >> 1;
if (arr[m] >= t) {
hi = m - 1;
} else {
ans = m;
lo = m + 1;
}
}

return ans;
}

public static void main(String[] args) {
System.out.println(
new ShortestSubArrayToBeRemovedToMakeArraySorted()
.findLengthOfShortestSubarray(new int[] {1, 2, 11, 10, 4, 11, 12, 15}));
//
// System.out.println(
// new ShortestSubArrayToBeRemovedToMakeArraySorted()
// .findLengthOfShortestSubarray(new int[] {1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 6}));
//
// System.out.println(
// new ShortestSubArrayToBeRemovedToMakeArraySorted()
// .findLengthOfShortestSubarray(new int[] {5, 4, 3, 2, 1}));

System.out.println(
new ShortestSubArrayToBeRemovedToMakeArraySorted()
.findLengthOfShortestSubarray(new int[] {1, 2, 3, 10, 0, 7, 8, 9}));
}
}
Loading