Skip to content

reformatting #2

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 1 commit into from
Aug 23, 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
12 changes: 6 additions & 6 deletions src/main/java/com/subham/ta/array/IntQuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import java.util.Arrays;

public class IntQuickSort {
public static void main(String[] args) {
int[] nums = {5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 5, 4, 4};
new Solution().sortArray(nums);
System.out.println(Arrays.toString(nums));
}

static class Solution {
void swap(int[] a, int f, int t) {
int x = a[f];
Expand Down Expand Up @@ -43,10 +49,4 @@ public int[] sortArray(int[] nums) {
return nums;
}
}

public static void main(String[] args) {
int[] nums = {5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 5, 4, 4};
new Solution().sortArray(nums);
System.out.println(Arrays.toString(nums));
}
}
56 changes: 28 additions & 28 deletions src/main/java/com/subham/ta/array/LeetCode1024Hard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,14 @@
import java.util.Map;

public class LeetCode1024Hard {
static class Solution {
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int[][] psa = buildPrefixSum2D(matrix);

// start the process
int count = 0;

for (int r1 = 0; r1 < psa.length; ++r1) {
for (int r2 = r1; r2 < psa.length; ++r2) {

Map<Integer, Integer> sumFrequency = new HashMap<>();
for (int c = 0; c < psa[0].length; ++c) {
int querySum = querySum(r1, 0, r2, c, psa);
if (querySum == target) count++;
count += sumFrequency.getOrDefault(querySum - target, 0);
sumFrequency.put(querySum, 1 + sumFrequency.getOrDefault(querySum, 0));
}
}
}

return count;
}
public static void main(String[] args) {
int[][] mat = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
// System.out.println(new Solution().numSubmatrixSumTarget(mat, 0));
int[][] mat2 = {{1, -1}, {-1, 1}};
System.out.println(new Solution().numSubmatrixSumTarget(mat2, 0));
}

static class Solution {
private static int[][] buildPrefixSum2D(int[][] matrix) {
int[][] psa = new int[matrix.length][matrix[0].length];

Expand Down Expand Up @@ -66,12 +51,27 @@ private static int querySum(int r, int j, int[][] psa) {
}
return psa[r][j];
}
}

public static void main(String[] args) {
int[][] mat = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
// System.out.println(new Solution().numSubmatrixSumTarget(mat, 0));
int[][] mat2 = {{1, -1}, {-1, 1}};
System.out.println(new Solution().numSubmatrixSumTarget(mat2, 0));
public int numSubmatrixSumTarget(int[][] matrix, int target) {
int[][] psa = buildPrefixSum2D(matrix);

// start the process
int count = 0;

for (int r1 = 0; r1 < psa.length; ++r1) {
for (int r2 = r1; r2 < psa.length; ++r2) {

Map<Integer, Integer> sumFrequency = new HashMap<>();
for (int c = 0; c < psa[0].length; ++c) {
int querySum = querySum(r1, 0, r2, c, psa);
if (querySum == target) count++;
count += sumFrequency.getOrDefault(querySum - target, 0);
sumFrequency.put(querySum, 1 + sumFrequency.getOrDefault(querySum, 0));
}
}
}

return count;
}
}
}
30 changes: 16 additions & 14 deletions src/main/java/com/subham/ta/array/LeetCode1605.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
import java.util.function.Consumer;

public class LeetCode1605 {
public static void main(String[] args) {
Consumer<int[][]> debug =
(mat) -> {
for (int i = 0; i < mat.length; ++i) {
for (int j = 0; j < mat[i].length; ++j) {
System.out.printf("%d\t", mat[i][j]);
}
System.out.println();
}
};

debug.accept(
new LeetCode1605.Solution()
.restoreMatrix(new int[] {1000_000_000, 1}, new int[] {0, 1000_000_000}));
}

static class Solution {
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
int ROW = rowSum.length;
Expand All @@ -20,18 +36,4 @@ public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
return mat;
}
}

public static void main(String[] args) {
Consumer<int[][]> debug =
(mat) -> {
for (int i = 0; i < mat.length; ++i) {
for (int j = 0; j < mat[i].length; ++j) {
System.out.printf("%d\t", mat[i][j]);
}
System.out.println();
}
};

debug.accept(new LeetCode1605.Solution().restoreMatrix(new int[] {1000_000_000, 1}, new int[] {0, 1000_000_000}));
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/subham/ta/array/LeetCode1636.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
import java.util.Arrays;

public class LeetCode1636 {
public static void main(String[] args) {
System.out.println(Arrays.toString(new Solution().frequencySort(new int[] {1, 1, 2, 2, 2, 3})));
System.out.println(
Arrays.toString(new Solution().frequencySort(new int[] {-1, 1, -6, 4, 5, -6, 1, 4, 1})));
}

static class Solution {
public int[] frequencySort(int[] nums) {
int[] a200 = new int[201];
Expand All @@ -25,10 +31,4 @@ public int[] frequencySort(int[] nums) {
return nums;
}
}

public static void main(String[] args) {
System.out.println(Arrays.toString(new Solution().frequencySort(new int[] {1, 1, 2, 2, 2, 3})));
System.out.println(
Arrays.toString(new Solution().frequencySort(new int[] {-1, 1, -6, 4, 5, -6, 1, 4, 1})));
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/subham/ta/array/LeetCode2191.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.Objects;

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

static class Solution {
public int[] sortJumbled(int[] mapping, int[] nums) {
Integer[] NUMS = new Integer[nums.length];
Expand Down Expand Up @@ -45,6 +47,4 @@ private Integer translate(int[] m, int a) {
return res;
}
}

public static void main(String[] args) {}
}
34 changes: 17 additions & 17 deletions src/main/java/com/subham/ta/array/LeetCode2392.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@
import java.util.concurrent.atomic.AtomicBoolean;

public class LeetCode2392 {
public static void main(String[] args) {
int k = 8;
int[][] rowConditions = {
{1, 2}, {7, 3}, {4, 3}, {5, 8}, {7, 8}, {8, 2}, {5, 8}, {3, 2}, {1, 3}, {7, 6}, {4, 3},
{7, 4}, {4, 8}, {7, 3}, {7, 5}
};
int[][] colConditions = {{5, 7}, {2, 7}, {4, 3}, {6, 7}, {4, 3}, {2, 3}, {6, 2}};
int[][] ints = new Solution().buildMatrix(k, rowConditions, colConditions);

for (int i = 0; i < ints.length; ++i) {
for (int j = 0; j < ints[i].length; ++j) {
System.out.printf("%d\t", ints[i][j]);
}
System.out.println();
}
}

static class Solution {
private static final int VISITED = 3;
private static final int IN_PROGRESS = 2;
Expand Down Expand Up @@ -67,21 +84,4 @@ private void dfs(int[][] graph, int node, List<Integer> completed, int[] visited
completed.add(node);
}
}

public static void main(String[] args) {
int k = 8;
int[][] rowConditions = {
{1, 2}, {7, 3}, {4, 3}, {5, 8}, {7, 8}, {8, 2}, {5, 8}, {3, 2}, {1, 3}, {7, 6}, {4, 3},
{7, 4}, {4, 8}, {7, 3}, {7, 5}
};
int[][] colConditions = {{5, 7}, {2, 7}, {4, 3}, {6, 7}, {4, 3}, {2, 3}, {6, 2}};
int[][] ints = new Solution().buildMatrix(k, rowConditions, colConditions);

for (int i = 0; i < ints.length; ++i) {
for (int j = 0; j < ints[i].length; ++j) {
System.out.printf("%d\t", ints[i][j]);
}
System.out.println();
}
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/subham/ta/array/matrix/SpiralIII.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.subham.ta.array.matrix;

public class SpiralIII {
public static void main(String[] args) {
int[][] matrixIII = new Solution().spiralMatrixIII(3, 3, 2, 2);

for (int i = 0; i < matrixIII.length; i++) {
System.out.println(matrixIII[i][0] + " - " + matrixIII[i][1]);
}
}

static class Solution {
public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
final int N = rows * cols;
Expand Down Expand Up @@ -62,12 +70,4 @@ boolean valid(int i, int j, int m, int n) {
return (0 <= i && i < m) && (0 <= j && j < n);
}
}

public static void main(String[] args) {
int[][] matrixIII = new Solution().spiralMatrixIII(3, 3, 2, 2);

for (int i = 0; i < matrixIII.length; i++) {
System.out.println(matrixIII[i][0] + " - " + matrixIII[i][1]);
}
}
}
10 changes: 5 additions & 5 deletions src/main/java/com/subham/ta/dp/lis/Leetcode1653.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import java.util.Stack;

public class Leetcode1653 {
public static void main(String[] args) {
System.out.println(new Solution().approach2("bbaaaaabb"));
System.out.println(new Solution().approach2("aababbab"));
}

static class Solution {
public int minimumDeletions(String s) {
int[] lis = new int[s.length()];
Expand Down Expand Up @@ -47,9 +52,4 @@ int charVal(char c) {
return c - 'a';
}
}

public static void main(String[] args) {
System.out.println(new Solution().approach2("bbaaaaabb"));
System.out.println(new Solution().approach2("aababbab"));
}
}
66 changes: 33 additions & 33 deletions src/main/java/com/subham/ta/graph/CloneGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@
import java.util.Set;

public class CloneGraph {
public static void main(String[] args) {
Node node1 = new Node(1, new ArrayList<>());
Node node2 = new Node(2, new ArrayList<>());
Node node3 = new Node(3, new ArrayList<>());
Node node4 = new Node(4, new ArrayList<>());

node1.neighbors.add(node2);
node1.neighbors.add(node4);

node2.neighbors.add(node1);
node2.neighbors.add(node3);

node3.neighbors.add(node2);
node3.neighbors.add(node4);

node4.neighbors.add(node1);
node4.neighbors.add(node3);

Node cloned = new Solution().cloneGraph(node1);
printDfs(node1, new HashSet<>());
System.out.println("--- new --- ");
printDfs(cloned, new HashSet<>());
}

static void printDfs(Node node, Set<Integer> visited) {
if (visited.contains(node.val)) {
return;
}
System.out.println(node.val + " neighbours = [" + node.neighbors.size() + "] ");
visited.add(node.val);
node.neighbors.forEach(n -> printDfs(n, visited));
}

static class Node {
public int val;
public List<Node> neighbors;
Expand Down Expand Up @@ -71,37 +104,4 @@ public Node cloneGraph(Node node) {
return clonedNodes.get(node.val);
}
}

public static void main(String[] args) {
Node node1 = new Node(1, new ArrayList<>());
Node node2 = new Node(2, new ArrayList<>());
Node node3 = new Node(3, new ArrayList<>());
Node node4 = new Node(4, new ArrayList<>());

node1.neighbors.add(node2);
node1.neighbors.add(node4);

node2.neighbors.add(node1);
node2.neighbors.add(node3);

node3.neighbors.add(node2);
node3.neighbors.add(node4);

node4.neighbors.add(node1);
node4.neighbors.add(node3);

Node cloned = new Solution().cloneGraph(node1);
printDfs(node1, new HashSet<>());
System.out.println("--- new --- ");
printDfs(cloned, new HashSet<>());
}

static void printDfs(Node node, Set<Integer> visited) {
if (visited.contains(node.val)) {
return;
}
System.out.println(node.val + " neighbours = [" + node.neighbors.size() + "] ");
visited.add(node.val);
node.neighbors.forEach(n -> printDfs(n, visited));
}
}
Loading