Skip to content

Commit 7cdd22d

Browse files
authored
Added tasks 3309-3312
1 parent 0f174c8 commit 7cdd22d

File tree

12 files changed

+603
-0
lines changed

12 files changed

+603
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package g3301_3400.s3309_maximum_possible_number_by_binary_concatenation;
2+
3+
// #Medium #Array #Bit_Manipulation #Enumeration
4+
// #2024_10_08_Time_3_ms_(97.01%)_Space_42.2_MB_(90.32%)
5+
6+
public class Solution {
7+
private String result = "0";
8+
9+
public int maxGoodNumber(int[] nums) {
10+
boolean[] visited = new boolean[nums.length];
11+
StringBuilder sb = new StringBuilder();
12+
solve(nums, visited, 0, sb);
13+
int score = 0;
14+
int val;
15+
for (char c : result.toCharArray()) {
16+
val = c - '0';
17+
score *= 2;
18+
score += val;
19+
}
20+
return score;
21+
}
22+
23+
private void solve(int[] nums, boolean[] visited, int pos, StringBuilder sb) {
24+
if (pos == nums.length) {
25+
String val = sb.toString();
26+
if ((result.length() == val.length() && result.compareTo(val) < 0)
27+
|| val.length() > result.length()) {
28+
result = val;
29+
}
30+
return;
31+
}
32+
String cur;
33+
for (int i = 0; i < nums.length; ++i) {
34+
if (visited[i]) {
35+
continue;
36+
}
37+
visited[i] = true;
38+
cur = Integer.toBinaryString(nums[i]);
39+
sb.append(cur);
40+
solve(nums, visited, pos + 1, sb);
41+
sb.setLength(sb.length() - cur.length());
42+
visited[i] = false;
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
3309\. Maximum Possible Number by Binary Concatenation
2+
3+
Medium
4+
5+
You are given an array of integers `nums` of size 3.
6+
7+
Return the **maximum** possible number whose _binary representation_ can be formed by **concatenating** the _binary representation_ of **all** elements in `nums` in some order.
8+
9+
**Note** that the binary representation of any number _does not_ contain leading zeros.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3]
14+
15+
**Output:** 30
16+
17+
**Explanation:**
18+
19+
Concatenate the numbers in the order `[3, 1, 2]` to get the result `"11110"`, which is the binary representation of 30.
20+
21+
**Example 2:**
22+
23+
**Input:** nums = [2,8,16]
24+
25+
**Output:** 1296
26+
27+
**Explanation:**
28+
29+
Concatenate the numbers in the order `[2, 8, 16]` to get the result `"10100010000"`, which is the binary representation of 1296.
30+
31+
**Constraints:**
32+
33+
* `nums.length == 3`
34+
* `1 <= nums[i] <= 127`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package g3301_3400.s3310_remove_methods_from_project;
2+
3+
// #Medium #Graph #Depth_First_Search #Breadth_First_Search
4+
// #2024_10_08_Time_41_ms_(99.76%)_Space_154.8_MB_(55.29%)
5+
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class Solution {
11+
private int[][] graph;
12+
private boolean[] suspicious;
13+
private boolean[] visited;
14+
15+
public List<Integer> remainingMethods(int n, int k, int[][] invocations) {
16+
pack(invocations, n);
17+
suspicious = new boolean[n];
18+
visited = new boolean[n];
19+
dfs(k, true);
20+
Arrays.fill(visited, false);
21+
for (int i = 0; i < n; i++) {
22+
if (!suspicious[i] && dfs2(i)) {
23+
Arrays.fill(visited, false);
24+
dfs(k, false);
25+
break;
26+
}
27+
}
28+
ArrayList<Integer> rst = new ArrayList<>();
29+
for (int i = 0; i < n; i++) {
30+
if (!suspicious[i]) {
31+
rst.add(i);
32+
}
33+
}
34+
return rst;
35+
}
36+
37+
public void dfs(int u, boolean sus) {
38+
if (visited[u]) {
39+
return;
40+
}
41+
visited[u] = true;
42+
suspicious[u] = sus;
43+
for (int v : graph[u]) {
44+
dfs(v, sus);
45+
}
46+
}
47+
48+
public boolean dfs2(int u) {
49+
if (suspicious[u]) {
50+
return true;
51+
}
52+
if (visited[u]) {
53+
return false;
54+
}
55+
visited[u] = true;
56+
for (int v : graph[u]) {
57+
if (dfs2(v)) {
58+
return true;
59+
}
60+
}
61+
return false;
62+
}
63+
64+
private void pack(int[][] edges, int n) {
65+
int[] adj = new int[n];
66+
for (int[] edge : edges) {
67+
adj[edge[0]]++;
68+
}
69+
graph = new int[n][];
70+
for (int i = 0; i < n; i++) {
71+
graph[i] = new int[adj[i]];
72+
}
73+
for (int[] edge : edges) {
74+
graph[edge[0]][--adj[edge[0]]] = edge[1];
75+
}
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
3310\. Remove Methods From Project
2+
3+
Medium
4+
5+
You are maintaining a project that has `n` methods numbered from `0` to `n - 1`.
6+
7+
You are given two integers `n` and `k`, and a 2D integer array `invocations`, where <code>invocations[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that method <code>a<sub>i</sub></code> invokes method <code>b<sub>i</sub></code>.
8+
9+
There is a known bug in method `k`. Method `k`, along with any method invoked by it, either **directly** or **indirectly**, are considered **suspicious** and we aim to remove them.
10+
11+
A group of methods can only be removed if no method **outside** the group invokes any methods **within** it.
12+
13+
Return an array containing all the remaining methods after removing all the **suspicious** methods. You may return the answer in _any order_. If it is not possible to remove **all** the suspicious methods, **none** should be removed.
14+
15+
**Example 1:**
16+
17+
**Input:** n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]
18+
19+
**Output:** [0,1,2,3]
20+
21+
**Explanation:**
22+
23+
![](https://assets.leetcode.com/uploads/2024/07/18/graph-2.png)
24+
25+
Method 2 and method 1 are suspicious, but they are directly invoked by methods 3 and 0, which are not suspicious. We return all elements without removing anything.
26+
27+
**Example 2:**
28+
29+
**Input:** n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]
30+
31+
**Output:** [3,4]
32+
33+
**Explanation:**
34+
35+
![](https://assets.leetcode.com/uploads/2024/07/18/graph-3.png)
36+
37+
Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.
38+
39+
**Example 3:**
40+
41+
**Input:** n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]
42+
43+
**Output:** []
44+
45+
**Explanation:**
46+
47+
![](https://assets.leetcode.com/uploads/2024/07/20/graph.png)
48+
49+
All methods are suspicious. We can remove them.
50+
51+
**Constraints:**
52+
53+
* <code>1 <= n <= 10<sup>5</sup></code>
54+
* `0 <= k <= n - 1`
55+
* <code>0 <= invocations.length <= 2 * 10<sup>5</sup></code>
56+
* <code>invocations[i] == [a<sub>i</sub>, b<sub>i</sub>]</code>
57+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code>
58+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
59+
* `invocations[i] != invocations[j]`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package g3301_3400.s3311_construct_2d_grid_matching_graph_layout;
2+
3+
// #Hard #Array #Hash_Table #Matrix #Graph #2024_10_08_Time_43_ms_(94.34%)_Space_103.6_MB_(79.25%)
4+
5+
import java.util.ArrayList;
6+
7+
@SuppressWarnings("unchecked")
8+
public class Solution {
9+
public int[][] constructGridLayout(int n, int[][] edges) {
10+
final int[] cs = new int[n];
11+
final ArrayList<Integer>[] als = new ArrayList[n];
12+
for (int i = 0; i < n; ++i) {
13+
als[i] = new ArrayList<>();
14+
}
15+
for (int[] e : edges) {
16+
cs[e[0]]++;
17+
cs[e[1]]++;
18+
als[e[0]].add(e[1]);
19+
als[e[1]].add(e[0]);
20+
}
21+
int min = 4;
22+
for (int a : cs) {
23+
min = Math.min(min, a);
24+
}
25+
final boolean[] seen = new boolean[n];
26+
int[][] res;
27+
int st = 0;
28+
for (int i = 0; i < n; ++i) {
29+
if (cs[i] == min) {
30+
st = i;
31+
break;
32+
}
33+
}
34+
if (min == 1) {
35+
res = new int[1][n];
36+
for (int i = 0; i < n; ++i) {
37+
res[0][i] = st;
38+
seen[st] = true;
39+
if (i + 1 < n) {
40+
for (int a : als[st]) {
41+
if (!seen[a]) {
42+
st = a;
43+
break;
44+
}
45+
}
46+
}
47+
}
48+
return res;
49+
}
50+
int row2 = -1;
51+
for (int a : als[st]) {
52+
if (cs[a] == min) {
53+
row2 = a;
54+
break;
55+
}
56+
}
57+
if (row2 >= 0) {
58+
return getInts(n, st, row2, seen, als);
59+
}
60+
return getInts(n, seen, st, als, cs);
61+
}
62+
63+
private int[][] getInts(int n, boolean[] seen, int st, ArrayList<Integer>[] als, int[] cs) {
64+
int[][] res;
65+
final ArrayList<Integer> al = new ArrayList<>();
66+
boolean f = true;
67+
seen[st] = true;
68+
al.add(st);
69+
while (f) {
70+
f = false;
71+
for (int a : als[st]) {
72+
if (!seen[a] && cs[a] <= 3) {
73+
seen[a] = true;
74+
al.add(a);
75+
if (cs[a] == 3) {
76+
f = true;
77+
st = a;
78+
}
79+
break;
80+
}
81+
}
82+
}
83+
res = new int[n / al.size()][al.size()];
84+
for (int i = 0; i < res[0].length; ++i) {
85+
res[0][i] = al.get(i);
86+
}
87+
for (int i = 1; i < res.length; ++i) {
88+
for (int j = 0; j < res[0].length; ++j) {
89+
for (int a : als[res[i - 1][j]]) {
90+
if (!seen[a]) {
91+
res[i][j] = a;
92+
seen[a] = true;
93+
break;
94+
}
95+
}
96+
}
97+
}
98+
return res;
99+
}
100+
101+
private int[][] getInts(int n, int st, int row2, boolean[] seen, ArrayList<Integer>[] als) {
102+
int[][] res;
103+
res = new int[2][n / 2];
104+
res[0][0] = st;
105+
res[1][0] = row2;
106+
seen[st] = seen[row2] = true;
107+
for (int i = 1; i < res[0].length; ++i) {
108+
for (int a : als[res[0][i - 1]]) {
109+
if (!seen[a]) {
110+
res[0][i] = a;
111+
seen[a] = true;
112+
break;
113+
}
114+
}
115+
for (int a : als[res[1][i - 1]]) {
116+
if (!seen[a]) {
117+
res[1][i] = a;
118+
seen[a] = true;
119+
break;
120+
}
121+
}
122+
}
123+
return res;
124+
}
125+
}

0 commit comments

Comments
 (0)