Skip to content

Commit cec1211

Browse files
committed
Added tasks 3309-3312
1 parent 0f174c8 commit cec1211

File tree

12 files changed

+599
-0
lines changed

12 files changed

+599
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
result = val;
28+
} else if (val.length() > result.length()) {
29+
result = val;
30+
}
31+
return;
32+
}
33+
String cur;
34+
for (int i = 0; i < nums.length; ++i) {
35+
if (visited[i]) {
36+
continue;
37+
}
38+
visited[i] = true;
39+
cur = Integer.toBinaryString(nums[i]);
40+
sb.append(cur);
41+
solve(nums, visited, pos + 1, sb);
42+
sb.setLength(sb.length() - cur.length());
43+
visited[i] = false;
44+
}
45+
}
46+
}
Lines changed: 34 additions & 0 deletions
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`
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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]) {
23+
if (dfs2(i)) {
24+
Arrays.fill(visited, false);
25+
dfs(k, false);
26+
break;
27+
}
28+
}
29+
}
30+
ArrayList<Integer> rst = new ArrayList<>();
31+
for (int i = 0; i < n; i++) {
32+
if (!suspicious[i]) {
33+
rst.add(i);
34+
}
35+
}
36+
return rst;
37+
}
38+
39+
public void dfs(int u, boolean sus) {
40+
if (visited[u]) {
41+
return;
42+
}
43+
visited[u] = true;
44+
suspicious[u] = sus;
45+
for (int v : graph[u]) {
46+
dfs(v, sus);
47+
}
48+
}
49+
50+
public boolean dfs2(int u) {
51+
if (suspicious[u]) {
52+
return true;
53+
}
54+
if (visited[u]) {
55+
return false;
56+
}
57+
visited[u] = true;
58+
for (int v : graph[u]) {
59+
if (dfs2(v)) {
60+
return true;
61+
}
62+
}
63+
return false;
64+
}
65+
66+
private void pack(int[][] edges, int n) {
67+
int m = edges.length;
68+
int[] adj = new int[n];
69+
for (int[] edge : edges) {
70+
adj[edge[0]]++;
71+
}
72+
73+
graph = new int[n][];
74+
for (int i = 0; i < n; i++) {
75+
graph[i] = new int[adj[i]];
76+
}
77+
78+
for (int[] edge : edges) {
79+
graph[edge[0]][--adj[edge[0]]] = edge[1];
80+
}
81+
}
82+
}
Lines changed: 59 additions & 0 deletions
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]`
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
res = new int[2][n / 2];
59+
res[0][0] = st;
60+
res[1][0] = row2;
61+
seen[st] = seen[row2] = true;
62+
for (int i = 1; i < res[0].length; ++i) {
63+
for (int a : als[res[0][i - 1]]) {
64+
if (!seen[a]) {
65+
res[0][i] = a;
66+
seen[a] = true;
67+
break;
68+
}
69+
}
70+
for (int a : als[res[1][i - 1]]) {
71+
if (!seen[a]) {
72+
res[1][i] = a;
73+
seen[a] = true;
74+
break;
75+
}
76+
}
77+
}
78+
return res;
79+
}
80+
final ArrayList<Integer> al = new ArrayList<>();
81+
boolean f = true;
82+
seen[st] = true;
83+
al.add(st);
84+
while (f) {
85+
f = false;
86+
for (int a : als[st]) {
87+
if (!seen[a] && cs[a] <= 3) {
88+
seen[a] = true;
89+
al.add(a);
90+
if (cs[a] == 3) {
91+
f = true;
92+
st = a;
93+
}
94+
break;
95+
}
96+
}
97+
}
98+
res = new int[n / al.size()][al.size()];
99+
for (int i = 0; i < res[0].length; ++i) {
100+
res[0][i] = al.get(i);
101+
}
102+
for (int i = 1; i < res.length; ++i) {
103+
for (int j = 0; j < res[0].length; ++j) {
104+
for (int a : als[res[i - 1][j]]) {
105+
if (!seen[a]) {
106+
res[i][j] = a;
107+
seen[a] = true;
108+
break;
109+
}
110+
}
111+
}
112+
}
113+
return res;
114+
}
115+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
3311\. Construct 2D Grid Matching Graph Layout
2+
3+
Hard
4+
5+
You are given a 2D integer array `edges` representing an **undirected** graph having `n` nodes, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> denotes an edge between nodes <code>u<sub>i</sub></code> and <code>v<sub>i</sub></code>.
6+
7+
Construct a 2D grid that satisfies these conditions:
8+
9+
* The grid contains **all nodes** from `0` to `n - 1` in its cells, with each node appearing exactly **once**.
10+
* Two nodes should be in adjacent grid cells (**horizontally** or **vertically**) **if and only if** there is an edge between them in `edges`.
11+
12+
It is guaranteed that `edges` can form a 2D grid that satisfies the conditions.
13+
14+
Return a 2D integer array satisfying the conditions above. If there are multiple solutions, return _any_ of them.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 4, edges = [[0,1],[0,2],[1,3],[2,3]]
19+
20+
**Output:** [[3,1],[2,0]]
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-07-59.png)
25+
26+
**Example 2:**
27+
28+
**Input:** n = 5, edges = [[0,1],[1,3],[2,3],[2,4]]
29+
30+
**Output:** [[4,2,3,1,0]]
31+
32+
**Explanation:**
33+
34+
![](https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-06-02.png)
35+
36+
**Example 3:**
37+
38+
**Input:** n = 9, edges = [[0,1],[0,4],[0,5],[1,7],[2,3],[2,4],[2,5],[3,6],[4,6],[4,7],[6,8],[7,8]]
39+
40+
**Output:** [[8,6,3],[7,4,2],[1,0,5]]
41+
42+
**Explanation:**
43+
44+
![](https://assets.leetcode.com/uploads/2024/08/11/screenshot-from-2024-08-11-14-06-38.png)
45+
46+
**Constraints:**
47+
48+
* <code>2 <= n <= 5 * 10<sup>4</sup></code>
49+
* <code>1 <= edges.length <= 10<sup>5</sup></code>
50+
* <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>]</code>
51+
* <code>0 <= u<sub>i</sub> < v<sub>i</sub> < n</code>
52+
* All the edges are distinct.
53+
* The input is generated such that `edges` can form a 2D grid that satisfies the conditions.

0 commit comments

Comments
 (0)