Skip to content

Commit 5807b32

Browse files
committed
Added Union Find, Graph and String problems
1 parent 2298fc8 commit 5807b32

10 files changed

+523
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
2+
3+
// You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
4+
5+
// Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
6+
7+
8+
9+
// Example 1:
10+
11+
12+
// Input: n = 4, connections = [[0,1],[0,2],[1,2]]
13+
// Output: 1
14+
// Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
15+
// Example 2:
16+
17+
18+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
19+
// Output: 2
20+
// Example 3:
21+
22+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
23+
// Output: -1
24+
// Explanation: There are not enough cables.
25+
26+
27+
// Constraints:
28+
29+
// 1 <= n <= 105
30+
// 1 <= connections.length <= min(n * (n - 1) / 2, 105)
31+
// connections[i].length == 2
32+
// 0 <= ai, bi < n
33+
// ai != bi
34+
// There are no repeated connections.
35+
// No two computers are connected by more than one cable.
36+
37+
class Solution {
38+
public:
39+
int components = 0;
40+
41+
void dfs(vector<vector<int>> &graph, vector<bool> &visited, int node){
42+
if(visited[node])
43+
return;
44+
visited[node] = 1;
45+
for(int child: graph[node]){
46+
if(!visited[child])
47+
dfs(graph, visited, child);
48+
}
49+
}
50+
51+
int makeConnected(int n, vector<vector<int>>& connections) {
52+
vector<bool> visited(n, 0);
53+
vector<vector<int>> graph(n+1);
54+
int edges = 0;
55+
for(auto edge : connections){
56+
graph[edge[0]].push_back(edge[1]);
57+
graph[edge[1]].push_back(edge[0]);
58+
edges++;
59+
}
60+
if(edges < n-1) return -1;
61+
for(int i = 0; i < n; i++){
62+
if(!visited[i]){
63+
components++;
64+
dfs(graph, visited, i);
65+
}
66+
}
67+
int redundant = edges - ((components - 1) - (n - 1));
68+
if(redundant >= (components - 1))
69+
return components - 1;
70+
return -1;
71+
}
72+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// There are n computers numbered from 0 to n - 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
2+
3+
// You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
4+
5+
// Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.
6+
7+
8+
9+
// Example 1:
10+
11+
12+
// Input: n = 4, connections = [[0,1],[0,2],[1,2]]
13+
// Output: 1
14+
// Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
15+
// Example 2:
16+
17+
18+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
19+
// Output: 2
20+
// Example 3:
21+
22+
// Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
23+
// Output: -1
24+
// Explanation: There are not enough cables.
25+
26+
27+
// Constraints:
28+
29+
// 1 <= n <= 105
30+
// 1 <= connections.length <= min(n * (n - 1) / 2, 105)
31+
// connections[i].length == 2
32+
// 0 <= ai, bi < n
33+
// ai != bi
34+
// There are no repeated connections.
35+
// No two computers are connected by more than one cable.
36+
37+
class Solution {
38+
public int components = 0, edges = 0;
39+
40+
public void dfs(ArrayList<Integer>[] graph, boolean[] visited, int node){
41+
if(visited[node] == true) return;
42+
visited[node] = true;
43+
for(int child : graph[node]){
44+
if(visited[child] == false)
45+
dfs(graph, visited, child);
46+
}
47+
}
48+
49+
public int makeConnected(int n, int[][] connections) {
50+
ArrayList<Integer>[] graph = new ArrayList[n+1];
51+
boolean[] visited = new boolean[n];
52+
Arrays.fill(visited, false);
53+
for(int i = 0; i < n; i++)
54+
graph[i] = new ArrayList<Integer>();
55+
for(int[] edge : connections){
56+
graph[edge[0]].add(edge[1]);
57+
graph[edge[1]].add(edge[0]);
58+
edges++;
59+
}
60+
if(edges < n-1)
61+
return -1;
62+
for(int i = 0; i < n; i++){
63+
if(visited[i] == false){
64+
components++;
65+
dfs(graph, visited, i);
66+
}
67+
}
68+
int redundant = edges - ((components - 1) - (n - 1));
69+
if(redundant >= (components - 1))
70+
return components - 1;
71+
return -1;
72+
}
73+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.
2+
3+
4+
5+
// Example 1:
6+
7+
// Input: nums = [10,5,2,6], k = 100
8+
// Output: 8
9+
// Explanation: The 8 subarrays that have product less than 100 are:
10+
// [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
11+
// Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
12+
// Example 2:
13+
14+
// Input: nums = [1,2,3], k = 0
15+
// Output: 0
16+
17+
18+
// Constraints:
19+
20+
// 1 <= nums.length <= 3 * 104
21+
// 1 <= nums[i] <= 1000
22+
// 0 <= k <= 106
23+
24+
class Solution {
25+
public:
26+
int numSubarrayProductLessThanK(vector<int>& nums, int k) {
27+
if(k<=1) return 0;
28+
int left = 0, right = 0, prod = 1, result = 0;
29+
while(right<nums.size()){
30+
prod *= nums[right];
31+
while(prod>=k){
32+
prod /= nums[left];
33+
left++;
34+
}
35+
result += right-left+1;
36+
right++;
37+
}
38+
return result;
39+
}
40+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Given an array of integers nums and an integer k, return the number of contiguous subarrays where the product of all the elements in the subarray is strictly less than k.
2+
3+
4+
5+
// Example 1:
6+
7+
// Input: nums = [10,5,2,6], k = 100
8+
// Output: 8
9+
// Explanation: The 8 subarrays that have product less than 100 are:
10+
// [10], [5], [2], [6], [10, 5], [5, 2], [2, 6], [5, 2, 6]
11+
// Note that [10, 5, 2] is not included as the product of 100 is not strictly less than k.
12+
// Example 2:
13+
14+
// Input: nums = [1,2,3], k = 0
15+
// Output: 0
16+
17+
18+
// Constraints:
19+
20+
// 1 <= nums.length <= 3 * 104
21+
// 1 <= nums[i] <= 1000
22+
// 0 <= k <= 106
23+
24+
class Solution {
25+
public int numSubarrayProductLessThanK(int[] nums, int k) {
26+
int n = nums.length, left = 0, right = 0, pro = 1, count = 0;
27+
if(k <= 1) return 0;
28+
while(right < n){
29+
pro *= nums[right];
30+
while(pro >= k){
31+
pro /= nums[left++];
32+
}
33+
count += right - left + 1;
34+
right++;
35+
}
36+
return count;
37+
}
38+
}
39+

Strings/BackSpaceStringCompare844.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
2+
3+
// Note that after backspacing an empty text, the text will continue empty.
4+
5+
6+
7+
// Example 1:
8+
9+
// Input: s = "ab#c", t = "ad#c"
10+
// Output: true
11+
// Explanation: Both s and t become "ac".
12+
// Example 2:
13+
14+
// Input: s = "ab##", t = "c#d#"
15+
// Output: true
16+
// Explanation: Both s and t become "".
17+
// Example 3:
18+
19+
// Input: s = "a#c", t = "b"
20+
// Output: false
21+
// Explanation: s becomes "c" while t becomes "b".
22+
23+
24+
// Constraints:
25+
26+
// 1 <= s.length, t.length <= 200
27+
// s and t only contain lowercase letters and '#' characters.
28+
29+
class Solution {
30+
public:
31+
string getString(string s){
32+
int count = 0, i = 0, j = s.size() - 1;
33+
string res = "";
34+
while(j >= 0){
35+
if(s[j] == '#'){
36+
count++; j--;
37+
}
38+
else{
39+
if(count == 0)
40+
res += s[j--];
41+
else{
42+
count--;
43+
j--;
44+
}
45+
}
46+
}
47+
return res;
48+
}
49+
50+
bool backspaceCompare(string s, string t) {
51+
return getString(s) == getString(t);
52+
}
53+
};
54+
55+
// Stack
56+
class Solution {
57+
public:
58+
stack<char> getString(string s){
59+
stack<char> st;
60+
for(char c : s){
61+
if(c != '#')
62+
st.push(c);
63+
else if(!st.empty()) st.pop();
64+
}
65+
return st;
66+
}
67+
68+
bool backspaceCompare(string s, string t) {
69+
return getString(s) == getString(t);
70+
}
71+
};
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Given two strings s and t, return true if they are equal when both are typed into empty text editors. '#' means a backspace character.
2+
3+
// Note that after backspacing an empty text, the text will continue empty.
4+
5+
6+
7+
// Example 1:
8+
9+
// Input: s = "ab#c", t = "ad#c"
10+
// Output: true
11+
// Explanation: Both s and t become "ac".
12+
// Example 2:
13+
14+
// Input: s = "ab##", t = "c#d#"
15+
// Output: true
16+
// Explanation: Both s and t become "".
17+
// Example 3:
18+
19+
// Input: s = "a#c", t = "b"
20+
// Output: false
21+
// Explanation: s becomes "c" while t becomes "b".
22+
23+
24+
// Constraints:
25+
26+
// 1 <= s.length, t.length <= 200
27+
// s and t only contain lowercase letters and '#' characters.
28+
29+
class Solution {
30+
public:
31+
string getString(string s){
32+
int count = 0, i = 0, j = s.size() - 1;
33+
string res = "";
34+
while(j >= 0){
35+
if(s[j] == '#'){
36+
count++; j--;
37+
}
38+
else{
39+
if(count == 0)
40+
res += s[j--];
41+
else{
42+
count--;
43+
j--;
44+
}
45+
}
46+
}
47+
return res;
48+
}
49+
50+
bool backspaceCompare(string s, string t) {
51+
return getString(s) == getString(t);
52+
}
53+
};
54+
55+
// Stack
56+
class Solution {
57+
public Stack<Character> getString(String s){
58+
Stack<Character> st = new Stack<>();
59+
for(Character c : s.toCharArray()){
60+
if(c != '#')
61+
st.push(c);
62+
else if(!st.empty()) st.pop();
63+
}
64+
return st;
65+
}
66+
67+
public boolean backspaceCompare(String s, String t) {
68+
return getString(s).equals(getString(t));
69+
}
70+
}

Union Find/AccountsMerge721.cpp

Whitespace-only changes.

Union Find/AccountsMerge721.java

Whitespace-only changes.

0 commit comments

Comments
 (0)