Skip to content

Commit 1de3b31

Browse files
authored
Merge branch 'main' into main
2 parents c8bbffa + 34d876b commit 1de3b31

15 files changed

+428
-27
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Large diffs are not rendered by default.

java/0139-word-break.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ class Solution {
33
public boolean wordBreak(String s, List<String> wordDict) {
44
boolean[] dp = new boolean[s.length() + 1];
55

6-
7-
for (int i = 0; i < dp.length; i++) {
8-
dp[i] = false;
9-
}
10-
116
dp[s.length()] = true;
127

138
for (int i = s.length() - 1; i >= 0; i--) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
int target;
3+
4+
public boolean canPartitionKSubsets(int[] nums, int k) {
5+
int sum = 0;
6+
for(int n : nums){
7+
sum += n;
8+
}
9+
if(sum%k != 0)
10+
return false;
11+
12+
target = sum / k;
13+
boolean[] used = new boolean[nums.length];
14+
return backtrack(nums, 0, k, 0, used);
15+
}
16+
17+
private boolean backtrack(int[] nums, int i, int k, int subsetSum, boolean[] used){
18+
if(k == 0)
19+
return true;
20+
if(subsetSum == target)
21+
return backtrack(nums, 0, k-1, 0, used);
22+
23+
for(int j = i; j < nums.length; j++){
24+
if(j > 0 && !used[j-1] && nums[j] == nums[j-1])
25+
continue;
26+
if(used[j] || subsetSum + nums[j] > target)
27+
continue;
28+
29+
used[j] = true;
30+
if(backtrack(nums, j+1, k, subsetSum + nums[j], used))
31+
return true;
32+
33+
used[j] = false;
34+
}
35+
return false;
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Solution {
2+
public TreeNode insertIntoBST(TreeNode root, int val) {
3+
if(root == null) return new TreeNode(val);
4+
TreeNode curr = root;
5+
while(true){
6+
if(curr.val <= val){
7+
if(curr.right != null){
8+
curr = curr.right;
9+
}else{
10+
curr.right = new TreeNode(val);
11+
break;
12+
}
13+
}else{
14+
if(curr.left != null) curr = curr.left;
15+
else{
16+
curr.left = new TreeNode(val);
17+
break;
18+
}
19+
}
20+
}
21+
return root;
22+
}
23+
24+
/* Using Recursive Solution
25+
-------------------------------------------------------------------
26+
public TreeNode insertIntoBST(TreeNode root, int val) {
27+
if(root == null) return new TreeNode(val);
28+
if(root.val <= val){
29+
root.right = insertIntoBST(root.right, val);
30+
}else{
31+
root.left = insertIntoBST(root.left, val);
32+
}
33+
return root;
34+
}
35+
-------------------------------------------------------------------
36+
*/
37+
}

java/0705-design-hashset.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class MyHashSet {
2+
final int mod = 10000;
3+
ListNode[] set;
4+
5+
class ListNode {
6+
int val;
7+
ListNode next;
8+
private ListNode(int val) {
9+
this.val = val;
10+
this.next = null;
11+
}
12+
}
13+
14+
public MyHashSet() {
15+
this.set = new ListNode[mod];
16+
for (int i = 0; i < set.length; i++) {
17+
set[i] = new ListNode(0);
18+
}
19+
}
20+
21+
public void add(int key) {
22+
ListNode head = set[key % mod];
23+
while (head.next != null) {
24+
if (head.next.val == key) return;
25+
head = head.next;
26+
}
27+
head.next = new ListNode(key);
28+
}
29+
30+
public void remove(int key) {
31+
ListNode head = set[key % mod];
32+
while (head.next != null) {
33+
if (head.next.val == key) {
34+
head.next = head.next.next;
35+
return;
36+
}
37+
head = head.next;
38+
}
39+
}
40+
41+
public boolean contains(int key) {
42+
ListNode head = set[key % mod];
43+
while (head.next != null) {
44+
if (head.next.val == key) return true;
45+
head = head.next;
46+
}
47+
return false;
48+
}
49+
}
50+
51+
/**
52+
* Your MyHashSet object will be instantiated and called as such:
53+
* MyHashSet obj = new MyHashSet();
54+
* obj.add(key);
55+
* obj.remove(key);
56+
* boolean param_3 = obj.contains(key);
57+
*/

java/0721-accounts-merge.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class Solution {
2+
public List<List<String>> accountsMerge(List<List<String>> accounts) {
3+
int n = accounts.size();
4+
DSU dsu = new DSU(n);
5+
6+
Map<String, Integer> map = new HashMap<>(); // email -> index of acc
7+
8+
for(int i = 0; i < n; i++){
9+
for(int j = 1; j < accounts.get(i).size(); j++){
10+
String email = accounts.get(i).get(j);
11+
String name = accounts.get(i).get(0);
12+
13+
if(!map.containsKey(email))
14+
map.put(email, i);
15+
else
16+
dsu.union(i, map.get(email));
17+
}
18+
}
19+
20+
Map<Integer, List<String>> merged = new HashMap<>(); // index of acc -> list of emails
21+
for(String email : map.keySet()){
22+
int group = map.get(email);
23+
int lead = dsu.find(group);
24+
25+
if(!merged.containsKey(lead))
26+
merged.put(lead, new ArrayList<String>());
27+
28+
merged.get(lead).add(email);
29+
}
30+
31+
List<List<String>> res = new ArrayList<>();
32+
for(int ac : merged.keySet()){
33+
List<String> grp = merged.get(ac);
34+
Collections.sort(grp);
35+
grp.add(0, accounts.get(ac).get(0));
36+
res.add(grp);
37+
}
38+
return res;
39+
}
40+
}
41+
42+
class DSU {
43+
int[] parent;
44+
int[] rank;
45+
46+
public DSU(int size) {
47+
rank = new int[size];
48+
parent = new int[size];
49+
for (int i = 0; i < size; i++)
50+
parent[i] = i;
51+
}
52+
53+
public int find(int x) {
54+
if (parent[x] != x)
55+
parent[x] = find(parent[x]);
56+
return parent[x];
57+
}
58+
59+
// Union By Rank
60+
61+
public boolean union(int x, int y) {
62+
int xr = find(x), yr = find(y);
63+
if (xr == yr) {
64+
return false;
65+
} else if (rank[xr] < rank[yr]) {
66+
parent[xr] = yr;
67+
} else if (rank[xr] > rank[yr]) {
68+
parent[yr] = xr;
69+
} else {
70+
parent[yr] = xr;
71+
rank[xr]++;
72+
}
73+
return true;
74+
}
75+
}
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
class Solution {
2-
32
public int minCostClimbingStairs(int[] cost) {
4-
int[] res = new int[cost.length + 1];
5-
res[cost.length] = 0;
6-
res[cost.length - 1] = cost[cost.length - 1];
3+
int one = 0;
4+
int two = 0;
75

8-
for (int i = cost.length - 2; i >= 0; i--) {
9-
res[i] =
10-
(res[i + 1] < res[i + 2])
11-
? res[i + 1] + cost[i]
12-
: res[i + 2] + cost[i];
6+
for (int i = cost.length - 1; i >= 0; i--) {
7+
cost[i] += Math.min(one, two);
8+
two = one;
9+
one = cost[i];
1310
}
1411

15-
return Math.min(res[0], res[1]);
12+
return Math.min(cost[0], cost[1]);
1613
}
1714
}

java/0785-is-graph-bipartite.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public boolean isBipartite(int[][] graph) {
3+
int[] color = new int[graph.length]; // 1 for one color , -1 for second color and 0 for not visited.
4+
5+
for(int i = 0; i < graph.length; i++){
6+
if(color[i] != 0){
7+
continue;
8+
}
9+
10+
Queue<Integer> q = new LinkedList<>();
11+
q.add(i);
12+
color[i] = 1;
13+
14+
while(!q.isEmpty()){
15+
int curr = q.poll();
16+
17+
for(int n : graph[curr]){
18+
if(color[n] == 0){
19+
color[n] = -1 * color[curr];
20+
q.add(n);
21+
}
22+
if(color[n] == color[curr])
23+
return false;
24+
}
25+
}
26+
}
27+
return true;
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public List<Integer> eventualSafeNodes(int[][] graph) {
3+
List<Integer> res = new ArrayList<>();
4+
HashMap<Integer, Boolean> safe = new HashMap<>();
5+
for(int i = 0; i < graph.length; i++){
6+
if(dfs(graph, i, safe))
7+
res.add(i);
8+
}
9+
return res;
10+
}
11+
12+
private boolean dfs(int[][] graph, int src, HashMap<Integer, Boolean> safe) {
13+
if (safe.containsKey(src))
14+
return safe.get(src);
15+
16+
safe.put(src, false);
17+
18+
for (int neighbour : graph[src]) {
19+
if (!dfs(graph, neighbour, safe))
20+
return false;
21+
}
22+
safe.put(src, true);
23+
return true;
24+
}
25+
}

java/1020-number-of-enclaves.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
The basic idea is to iterate through the boundary
3+
If we encounter any island i.e. 1 then we will run DFS
4+
And update all those islands as 2
5+
6+
Then check for the number of 1s remaining in the board
7+
Since those are the ones that have not been visited and return
8+
*/
9+
10+
class Solution {
11+
public int count(int [][]board){
12+
int c = 0;
13+
for(int i=0; i<board.length; i++){
14+
for(int j=0; j<board[0].length; j++){
15+
if(board[i][j] == 1){
16+
c++;
17+
}
18+
}
19+
}
20+
return c;
21+
}
22+
public void dfs(int r, int c, int[][] board){
23+
if(r<0 || c<0 || r>board.length-1 || c>board[0].length-1 || board[r][c] != 1) return;
24+
25+
board[r][c] = 2;
26+
27+
dfs(r+1, c, board);
28+
dfs(r-1, c, board);
29+
dfs(r, c+1, board);
30+
dfs(r, c-1, board);
31+
}
32+
public int numEnclaves(int[][] board) {
33+
int n=board.length, m=board[0].length;
34+
35+
for(int i=0; i<n; i++){
36+
if(board[i][0] == 1) dfs(i, 0, board);
37+
if(board[i][m-1] == 1) dfs(i, m-1, board);
38+
}
39+
40+
for(int i=1; i<m-1; i++){
41+
if(board[0][i] == 1) dfs(0, i, board);
42+
if(board[n-1][i] == 1) dfs(n-1, i, board);
43+
}
44+
return count(board);
45+
}
46+
}

0 commit comments

Comments
 (0)