Skip to content

Commit 1264ab6

Browse files
authored
Merge pull request #2 from Subham1999/formatting_23aug2024
reformatting
2 parents 54b1565 + 1fe8089 commit 1264ab6

19 files changed

+276
-275
lines changed

src/main/java/com/subham/ta/array/IntQuickSort.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import java.util.Arrays;
44

55
public class IntQuickSort {
6+
public static void main(String[] args) {
7+
int[] nums = {5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 5, 4, 4};
8+
new Solution().sortArray(nums);
9+
System.out.println(Arrays.toString(nums));
10+
}
11+
612
static class Solution {
713
void swap(int[] a, int f, int t) {
814
int x = a[f];
@@ -43,10 +49,4 @@ public int[] sortArray(int[] nums) {
4349
return nums;
4450
}
4551
}
46-
47-
public static void main(String[] args) {
48-
int[] nums = {5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1, 1, 5, 4, 4};
49-
new Solution().sortArray(nums);
50-
System.out.println(Arrays.toString(nums));
51-
}
5252
}

src/main/java/com/subham/ta/array/LeetCode1024Hard.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,14 @@
44
import java.util.Map;
55

66
public class LeetCode1024Hard {
7-
static class Solution {
8-
public int numSubmatrixSumTarget(int[][] matrix, int target) {
9-
int[][] psa = buildPrefixSum2D(matrix);
10-
11-
// start the process
12-
int count = 0;
13-
14-
for (int r1 = 0; r1 < psa.length; ++r1) {
15-
for (int r2 = r1; r2 < psa.length; ++r2) {
16-
17-
Map<Integer, Integer> sumFrequency = new HashMap<>();
18-
for (int c = 0; c < psa[0].length; ++c) {
19-
int querySum = querySum(r1, 0, r2, c, psa);
20-
if (querySum == target) count++;
21-
count += sumFrequency.getOrDefault(querySum - target, 0);
22-
sumFrequency.put(querySum, 1 + sumFrequency.getOrDefault(querySum, 0));
23-
}
24-
}
25-
}
26-
27-
return count;
28-
}
7+
public static void main(String[] args) {
8+
int[][] mat = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
9+
// System.out.println(new Solution().numSubmatrixSumTarget(mat, 0));
10+
int[][] mat2 = {{1, -1}, {-1, 1}};
11+
System.out.println(new Solution().numSubmatrixSumTarget(mat2, 0));
12+
}
2913

14+
static class Solution {
3015
private static int[][] buildPrefixSum2D(int[][] matrix) {
3116
int[][] psa = new int[matrix.length][matrix[0].length];
3217

@@ -66,12 +51,27 @@ private static int querySum(int r, int j, int[][] psa) {
6651
}
6752
return psa[r][j];
6853
}
69-
}
7054

71-
public static void main(String[] args) {
72-
int[][] mat = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}};
73-
// System.out.println(new Solution().numSubmatrixSumTarget(mat, 0));
74-
int[][] mat2 = {{1, -1}, {-1, 1}};
75-
System.out.println(new Solution().numSubmatrixSumTarget(mat2, 0));
55+
public int numSubmatrixSumTarget(int[][] matrix, int target) {
56+
int[][] psa = buildPrefixSum2D(matrix);
57+
58+
// start the process
59+
int count = 0;
60+
61+
for (int r1 = 0; r1 < psa.length; ++r1) {
62+
for (int r2 = r1; r2 < psa.length; ++r2) {
63+
64+
Map<Integer, Integer> sumFrequency = new HashMap<>();
65+
for (int c = 0; c < psa[0].length; ++c) {
66+
int querySum = querySum(r1, 0, r2, c, psa);
67+
if (querySum == target) count++;
68+
count += sumFrequency.getOrDefault(querySum - target, 0);
69+
sumFrequency.put(querySum, 1 + sumFrequency.getOrDefault(querySum, 0));
70+
}
71+
}
72+
}
73+
74+
return count;
75+
}
7676
}
7777
}

src/main/java/com/subham/ta/array/LeetCode1605.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@
33
import java.util.function.Consumer;
44

55
public class LeetCode1605 {
6+
public static void main(String[] args) {
7+
Consumer<int[][]> debug =
8+
(mat) -> {
9+
for (int i = 0; i < mat.length; ++i) {
10+
for (int j = 0; j < mat[i].length; ++j) {
11+
System.out.printf("%d\t", mat[i][j]);
12+
}
13+
System.out.println();
14+
}
15+
};
16+
17+
debug.accept(
18+
new LeetCode1605.Solution()
19+
.restoreMatrix(new int[] {1000_000_000, 1}, new int[] {0, 1000_000_000}));
20+
}
21+
622
static class Solution {
723
public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
824
int ROW = rowSum.length;
@@ -20,18 +36,4 @@ public int[][] restoreMatrix(int[] rowSum, int[] colSum) {
2036
return mat;
2137
}
2238
}
23-
24-
public static void main(String[] args) {
25-
Consumer<int[][]> debug =
26-
(mat) -> {
27-
for (int i = 0; i < mat.length; ++i) {
28-
for (int j = 0; j < mat[i].length; ++j) {
29-
System.out.printf("%d\t", mat[i][j]);
30-
}
31-
System.out.println();
32-
}
33-
};
34-
35-
debug.accept(new LeetCode1605.Solution().restoreMatrix(new int[] {1000_000_000, 1}, new int[] {0, 1000_000_000}));
36-
}
3739
}

src/main/java/com/subham/ta/array/LeetCode1636.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
import java.util.Arrays;
44

55
public class LeetCode1636 {
6+
public static void main(String[] args) {
7+
System.out.println(Arrays.toString(new Solution().frequencySort(new int[] {1, 1, 2, 2, 2, 3})));
8+
System.out.println(
9+
Arrays.toString(new Solution().frequencySort(new int[] {-1, 1, -6, 4, 5, -6, 1, 4, 1})));
10+
}
11+
612
static class Solution {
713
public int[] frequencySort(int[] nums) {
814
int[] a200 = new int[201];
@@ -25,10 +31,4 @@ public int[] frequencySort(int[] nums) {
2531
return nums;
2632
}
2733
}
28-
29-
public static void main(String[] args) {
30-
System.out.println(Arrays.toString(new Solution().frequencySort(new int[] {1, 1, 2, 2, 2, 3})));
31-
System.out.println(
32-
Arrays.toString(new Solution().frequencySort(new int[] {-1, 1, -6, 4, 5, -6, 1, 4, 1})));
33-
}
3434
}

src/main/java/com/subham/ta/array/LeetCode2191.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Objects;
77

88
public class LeetCode2191 {
9+
public static void main(String[] args) {}
10+
911
static class Solution {
1012
public int[] sortJumbled(int[] mapping, int[] nums) {
1113
Integer[] NUMS = new Integer[nums.length];
@@ -45,6 +47,4 @@ private Integer translate(int[] m, int a) {
4547
return res;
4648
}
4749
}
48-
49-
public static void main(String[] args) {}
5050
}

src/main/java/com/subham/ta/array/LeetCode2392.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66
import java.util.concurrent.atomic.AtomicBoolean;
77

88
public class LeetCode2392 {
9+
public static void main(String[] args) {
10+
int k = 8;
11+
int[][] rowConditions = {
12+
{1, 2}, {7, 3}, {4, 3}, {5, 8}, {7, 8}, {8, 2}, {5, 8}, {3, 2}, {1, 3}, {7, 6}, {4, 3},
13+
{7, 4}, {4, 8}, {7, 3}, {7, 5}
14+
};
15+
int[][] colConditions = {{5, 7}, {2, 7}, {4, 3}, {6, 7}, {4, 3}, {2, 3}, {6, 2}};
16+
int[][] ints = new Solution().buildMatrix(k, rowConditions, colConditions);
17+
18+
for (int i = 0; i < ints.length; ++i) {
19+
for (int j = 0; j < ints[i].length; ++j) {
20+
System.out.printf("%d\t", ints[i][j]);
21+
}
22+
System.out.println();
23+
}
24+
}
25+
926
static class Solution {
1027
private static final int VISITED = 3;
1128
private static final int IN_PROGRESS = 2;
@@ -67,21 +84,4 @@ private void dfs(int[][] graph, int node, List<Integer> completed, int[] visited
6784
completed.add(node);
6885
}
6986
}
70-
71-
public static void main(String[] args) {
72-
int k = 8;
73-
int[][] rowConditions = {
74-
{1, 2}, {7, 3}, {4, 3}, {5, 8}, {7, 8}, {8, 2}, {5, 8}, {3, 2}, {1, 3}, {7, 6}, {4, 3},
75-
{7, 4}, {4, 8}, {7, 3}, {7, 5}
76-
};
77-
int[][] colConditions = {{5, 7}, {2, 7}, {4, 3}, {6, 7}, {4, 3}, {2, 3}, {6, 2}};
78-
int[][] ints = new Solution().buildMatrix(k, rowConditions, colConditions);
79-
80-
for (int i = 0; i < ints.length; ++i) {
81-
for (int j = 0; j < ints[i].length; ++j) {
82-
System.out.printf("%d\t", ints[i][j]);
83-
}
84-
System.out.println();
85-
}
86-
}
8787
}

src/main/java/com/subham/ta/array/matrix/SpiralIII.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package com.subham.ta.array.matrix;
22

33
public class SpiralIII {
4+
public static void main(String[] args) {
5+
int[][] matrixIII = new Solution().spiralMatrixIII(3, 3, 2, 2);
6+
7+
for (int i = 0; i < matrixIII.length; i++) {
8+
System.out.println(matrixIII[i][0] + " - " + matrixIII[i][1]);
9+
}
10+
}
11+
412
static class Solution {
513
public int[][] spiralMatrixIII(int rows, int cols, int rStart, int cStart) {
614
final int N = rows * cols;
@@ -62,12 +70,4 @@ boolean valid(int i, int j, int m, int n) {
6270
return (0 <= i && i < m) && (0 <= j && j < n);
6371
}
6472
}
65-
66-
public static void main(String[] args) {
67-
int[][] matrixIII = new Solution().spiralMatrixIII(3, 3, 2, 2);
68-
69-
for (int i = 0; i < matrixIII.length; i++) {
70-
System.out.println(matrixIII[i][0] + " - " + matrixIII[i][1]);
71-
}
72-
}
7373
}

src/main/java/com/subham/ta/dp/lis/Leetcode1653.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
import java.util.Stack;
55

66
public class Leetcode1653 {
7+
public static void main(String[] args) {
8+
System.out.println(new Solution().approach2("bbaaaaabb"));
9+
System.out.println(new Solution().approach2("aababbab"));
10+
}
11+
712
static class Solution {
813
public int minimumDeletions(String s) {
914
int[] lis = new int[s.length()];
@@ -47,9 +52,4 @@ int charVal(char c) {
4752
return c - 'a';
4853
}
4954
}
50-
51-
public static void main(String[] args) {
52-
System.out.println(new Solution().approach2("bbaaaaabb"));
53-
System.out.println(new Solution().approach2("aababbab"));
54-
}
5555
}

src/main/java/com/subham/ta/graph/CloneGraph.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,39 @@
88
import java.util.Set;
99

1010
public class CloneGraph {
11+
public static void main(String[] args) {
12+
Node node1 = new Node(1, new ArrayList<>());
13+
Node node2 = new Node(2, new ArrayList<>());
14+
Node node3 = new Node(3, new ArrayList<>());
15+
Node node4 = new Node(4, new ArrayList<>());
16+
17+
node1.neighbors.add(node2);
18+
node1.neighbors.add(node4);
19+
20+
node2.neighbors.add(node1);
21+
node2.neighbors.add(node3);
22+
23+
node3.neighbors.add(node2);
24+
node3.neighbors.add(node4);
25+
26+
node4.neighbors.add(node1);
27+
node4.neighbors.add(node3);
28+
29+
Node cloned = new Solution().cloneGraph(node1);
30+
printDfs(node1, new HashSet<>());
31+
System.out.println("--- new --- ");
32+
printDfs(cloned, new HashSet<>());
33+
}
34+
35+
static void printDfs(Node node, Set<Integer> visited) {
36+
if (visited.contains(node.val)) {
37+
return;
38+
}
39+
System.out.println(node.val + " neighbours = [" + node.neighbors.size() + "] ");
40+
visited.add(node.val);
41+
node.neighbors.forEach(n -> printDfs(n, visited));
42+
}
43+
1144
static class Node {
1245
public int val;
1346
public List<Node> neighbors;
@@ -71,37 +104,4 @@ public Node cloneGraph(Node node) {
71104
return clonedNodes.get(node.val);
72105
}
73106
}
74-
75-
public static void main(String[] args) {
76-
Node node1 = new Node(1, new ArrayList<>());
77-
Node node2 = new Node(2, new ArrayList<>());
78-
Node node3 = new Node(3, new ArrayList<>());
79-
Node node4 = new Node(4, new ArrayList<>());
80-
81-
node1.neighbors.add(node2);
82-
node1.neighbors.add(node4);
83-
84-
node2.neighbors.add(node1);
85-
node2.neighbors.add(node3);
86-
87-
node3.neighbors.add(node2);
88-
node3.neighbors.add(node4);
89-
90-
node4.neighbors.add(node1);
91-
node4.neighbors.add(node3);
92-
93-
Node cloned = new Solution().cloneGraph(node1);
94-
printDfs(node1, new HashSet<>());
95-
System.out.println("--- new --- ");
96-
printDfs(cloned, new HashSet<>());
97-
}
98-
99-
static void printDfs(Node node, Set<Integer> visited) {
100-
if (visited.contains(node.val)) {
101-
return;
102-
}
103-
System.out.println(node.val + " neighbours = [" + node.neighbors.size() + "] ");
104-
visited.add(node.val);
105-
node.neighbors.forEach(n -> printDfs(n, visited));
106-
}
107107
}

0 commit comments

Comments
 (0)