Skip to content

Commit e27ea8e

Browse files
commit
1 parent c5c4ded commit e27ea8e

File tree

7 files changed

+286
-2
lines changed

7 files changed

+286
-2
lines changed

src/arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
- [Find Polygon With the Largest Perimeter](com/problems/array/FindPolygonWithTheLargestPerimeter.java)
115115

116116
- [Range Sum Query 2D - Immutable](com/problems/array/RangeSumQuery2DImmutable.java)
117-
117+
- [Longest Common Prefix Between Adjacent Strings After Removals](com/problems/array/LongestCommonPrefixBetweenAdjacentStringsAfterRemovals.java)
118118

119119

120120
### Sliding window
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.problems.array;
2+
/*
3+
* Problem link:
4+
* https://leetcode.com/problems/longest-common-prefix-between-adjacent-strings-after-removals/description/
5+
*
6+
* Solution link:
7+
*
8+
* */
9+
10+
import com.util.PrintUtl;
11+
12+
import java.util.PriorityQueue;
13+
import java.util.Stack;
14+
15+
// Tags: Arrays, hashing, prefix array
16+
public class LongestCommonPrefixBetweenAdjacentStringsAfterRemovals {
17+
public static void main(String[] args) {
18+
type1();
19+
type2();
20+
}
21+
22+
// using a prefix and suffix array
23+
private static void type2() {
24+
String[] words = {"jump", "run", "run", "jump", "run"};
25+
int[] ans = longestCommonPrefix2(words);
26+
PrintUtl.print(ans);
27+
}
28+
29+
private static int[] longestCommonPrefix2(String[] words) {
30+
int n = words.length;
31+
int[] ans = new int[n];
32+
int[] suffix = new int[n];
33+
int sMax = 0;
34+
int[] prefTemp = new int[n];
35+
for (int i = n - 1; i >= 0; i--) {
36+
37+
}
38+
return ans;
39+
}
40+
41+
// brute force solution
42+
// using priority queue
43+
private static void type1() {
44+
String[] words = {"jump", "run", "run", "jump", "run"};
45+
int[] ans = longestCommonPrefix1(words);
46+
PrintUtl.print(ans);
47+
}
48+
49+
public static int[] longestCommonPrefix1(String[] words) {
50+
int n = words.length;
51+
PriorityQueue<Pair> pq = new PriorityQueue<>();
52+
for (int i = 0; i < n - 1; i++) {
53+
pq.add(new Pair(i, i + 1, prefix(words[i], words[i + 1])));
54+
}
55+
Stack<Pair> st = new Stack<>();
56+
int[] ans = new int[n];
57+
for (int i = 0; i < n; i++) {
58+
int pre = i - 1, post = i + 1;
59+
int max = 0;
60+
if (pre >= 0 && post < n) {
61+
max = prefix(words[pre], words[post]);
62+
}
63+
while (!pq.isEmpty()) {
64+
Pair p = pq.poll();
65+
st.push(p);
66+
if (p.start != i && p.end != i) {
67+
max = Math.max(max, p.prefix);
68+
break;
69+
}
70+
}
71+
ans[i] = max;
72+
while (!st.isEmpty()) pq.add(st.pop());
73+
}
74+
return ans;
75+
}
76+
77+
public static int prefix(String a, String b) {
78+
int n1 = a.length();
79+
int n2 = b.length();
80+
int i1 = 0, i2 = 0;
81+
while (i1 < n1 && i2 < n2) {
82+
if (a.charAt(i1) == b.charAt(i2)) {
83+
i1++;
84+
i2++;
85+
} else {
86+
return i1;
87+
}
88+
}
89+
return Math.min(n1, n2);
90+
}
91+
92+
static class Pair implements Comparable<Pair> {
93+
int start;
94+
int end;
95+
int prefix;
96+
97+
Pair(int start, int end, int prefix) {
98+
this.start = start;
99+
this.end = end;
100+
this.prefix = prefix;
101+
}
102+
103+
public int compareTo(Pair pair) {
104+
return Integer.compare(pair.prefix, this.prefix);
105+
}
106+
107+
public String toString() {
108+
return "[" + start + "," + end + ":" + prefix + "]";
109+
}
110+
}
111+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.problems.graph;
2+
3+
import com.util.PrintUtl;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.TreeSet;
9+
10+
/*
11+
* Problem link:
12+
* https://leetcode.com/problems/power-grid-maintenance/description/
13+
* Solution link:
14+
*
15+
*/
16+
public class PowerGridMaintenance {
17+
public static void main(String[] args) {
18+
type1();
19+
type2();
20+
}
21+
22+
private static void type2() {
23+
}
24+
25+
// Using union find to group the power stations then resolve the queries one by one
26+
// todo this is not the optimal solution, we can use a better approach
27+
private static void type1() {
28+
int c = 5;
29+
int[][] connections = {{1, 2}, {2, 3}, {3, 4}, {4, 5}};
30+
int[][] queries = {{1, 3}, {2, 1}, {1, 1}, {2, 2}, {1, 2}};
31+
int[] ans = processQueries1(c, connections, queries);
32+
PrintUtl.print(ans);
33+
}
34+
35+
public static int[] processQueries1(int c, int[][] connections, int[][] queries) {
36+
// creating union find structure
37+
int[] parent = new int[c + 1];
38+
int[] wt = new int[c + 1];
39+
for (int i = 1; i <= c; i++) parent[i] = i;
40+
Arrays.fill(wt, 1);
41+
buildUnionFind(connections, parent, wt);
42+
43+
TreeSet<Integer>[] powerStations = new TreeSet[c + 1];
44+
for (int node = 1; node <= c; node++) {
45+
// redoing the union find to get the parent of the node, if the path compression is not done
46+
int parentNode = parent[node] = parent(parent, node);
47+
if (powerStations[parentNode] == null) {
48+
TreeSet<Integer> set = new TreeSet<>();
49+
set.add(parentNode);
50+
powerStations[parentNode] = set;
51+
}
52+
powerStations[parentNode].add(node);
53+
}
54+
// even if the node is removed, we will still need to find the parent node, we will keep a copy of the parent array
55+
int[] copyParent = Arrays.copyOf(parent, parent.length);
56+
List<Integer> ans = new ArrayList<>();
57+
// Process each query
58+
for (int[] query : queries) {
59+
int op = query[0], node = query[1];
60+
int parentNode = parent[node];
61+
if (op == 1) {
62+
// copyParent[node] != -1 means it is not removed yet, so the ans will be the same node
63+
if (copyParent[node] != -1) {
64+
ans.add(node);
65+
} else {
66+
// the node is removed, we need to find the parent node
67+
TreeSet<Integer> set = powerStations[parentNode];
68+
int queryAns = !set.isEmpty() ? set.first() : -1;
69+
ans.add(queryAns);
70+
}
71+
} else {
72+
// we will remove the node from the power station and update the copyParent
73+
powerStations[parentNode].remove(node);
74+
copyParent[node] = -1;
75+
}
76+
}
77+
// Convert the list to an array
78+
return convertListToArray(ans);
79+
}
80+
81+
private static void buildUnionFind(int[][] connections, int[] parent, int[] wt) {
82+
for (int[] edge : connections) {
83+
int x = edge[0], y = edge[1];
84+
int root1 = parent(parent, x);
85+
int root2 = parent(parent, y);
86+
if (root1 == root2) continue;
87+
if (wt[root1] >= wt[root2]) {
88+
wt[root1] += wt[root2];
89+
wt[root2] = 0;
90+
parent[root2] = root1;
91+
} else {
92+
wt[root2] += wt[root1];
93+
wt[root1] = 0;
94+
parent[root1] = root2;
95+
}
96+
}
97+
}
98+
99+
private static int[] convertListToArray(List<Integer> list) {
100+
int[] arr = new int[list.size()];
101+
for (int i = 0; i < list.size(); i++) {
102+
arr[i] = list.get(i);
103+
}
104+
return arr;
105+
}
106+
107+
static int parent(int[] par, int node) {
108+
int pt = node;
109+
while (pt != par[pt]) {
110+
pt = par[pt];
111+
}
112+
int root = pt;
113+
pt = node;
114+
while (pt != par[pt]) {
115+
int nextPt = par[pt];
116+
par[pt] = root;
117+
pt = nextPt;
118+
}
119+
return root;
120+
}
121+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.problems.string;
2+
3+
4+
import java.util.ArrayList;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Set;
8+
9+
/*
10+
* Problem link:
11+
* https://leetcode.com/problems/partition-string/description/
12+
*
13+
* Solution link:
14+
*
15+
*/
16+
// Tags: Array, String
17+
public class PartitionString {
18+
public static void main(String[] args) {
19+
type1();
20+
type2();
21+
}
22+
23+
private static void type2() {
24+
}
25+
26+
// brute force solution using set
27+
private static void type1() {
28+
String s = "abbccccd";
29+
List<String> ans = partitionString1(s);
30+
System.out.println(ans);
31+
}
32+
33+
public static List<String> partitionString1(String s) {
34+
int n = s.length();
35+
Set<String> seen = new HashSet<>();
36+
List<String> ans = new ArrayList<>();
37+
int start = 0;
38+
for (int i = 0; i < n; i++) {
39+
String sub = s.substring(start, i + 1);
40+
if (!seen.contains(sub)) {
41+
seen.add(sub);
42+
ans.add(sub);
43+
start = i + 1;
44+
}
45+
}
46+
return ans;
47+
}
48+
}

src/graph.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,5 @@
7878
- [Maximize the Number of Target Nodes After Connecting Trees I](com/problems/graph/MaximizeTheNumberOfTargetNodesAfterConnectingTrees1.java)
7979
- [Maximize the Number of Target Nodes After Connecting Trees II](com/problems/graph/MaximizeTheNumberOfTargetNodesAfterConnectingTrees2.java)
8080
- [Maximize Amount After Two Days of Conversions](com/problems/graph/MaximizeAmountAfterTwoDaysOfConversions.java)
81-
- [Properties Graph](com/problems/graph/PropertiesGraph.java)
81+
- [Properties Graph](com/problems/graph/PropertiesGraph.java)
82+
- [Power Grid Maintenance](com/problems/graph/PowerGridMaintenance.java)

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## My programming profiles
44
- [github](https://github.com/abhishekghoshh)
55
- [leetcode](https://leetcode.com/u/abhishekghoshh/)
6+
- [algo.monster](https://algo.monster/dashboard)
67

78
## Projects
89

src/strings.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
- [Find the Sequence of Strings Appeared on the Screen](com/problems/string/FindTheSequenceOfStringsAppearedOnTheScreen.java)
6767
- [Minimum Remove to Make Valid Parentheses](com/problems/string/MinimumRemoveToMakeValidParentheses.java)
6868

69+
- [Partition String](com/problems/string/PartitionString.java)
70+
6971
### Advanced
7072
- [Rabin Karp](com/algo/string/RabinKarp.java)
7173
- [Z-Function](com/algo/string/ZFunction.java)

0 commit comments

Comments
 (0)