Skip to content

Commit a52b4f3

Browse files
authored
Added tasks 3309-3312
1 parent 333c71d commit a52b4f3

File tree

12 files changed

+646
-0
lines changed

12 files changed

+646
-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_12_Time_182_ms_(73.47%)_Space_36.8_MB_(79.59%)
5+
6+
class Solution {
7+
private var result = "0"
8+
9+
fun maxGoodNumber(nums: IntArray): Int {
10+
val visited = BooleanArray(nums.size)
11+
val sb = StringBuilder()
12+
solve(nums, visited, 0, sb)
13+
var score = 0
14+
var `val`: Int
15+
for (c in result.toCharArray()) {
16+
`val` = c.code - '0'.code
17+
score *= 2
18+
score += `val`
19+
}
20+
return score
21+
}
22+
23+
private fun solve(nums: IntArray, visited: BooleanArray, pos: Int, sb: StringBuilder) {
24+
if (pos == nums.size) {
25+
val `val` = sb.toString()
26+
if ((result.length == `val`.length && result.compareTo(`val`) < 0) ||
27+
`val`.length > result.length
28+
) {
29+
result = `val`
30+
}
31+
return
32+
}
33+
var cur: String?
34+
for (i in nums.indices) {
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package g3301_3400.s3310_remove_methods_from_project
2+
3+
// #Medium #Depth_First_Search #Breadth_First_Search #Graph
4+
// #2024_10_12_Time_1465_ms_(100.00%)_Space_201.7_MB_(14.81%)
5+
6+
class Solution {
7+
private lateinit var graph: Array<IntArray?>
8+
private lateinit var suspicious: BooleanArray
9+
private lateinit var visited: BooleanArray
10+
11+
fun remainingMethods(n: Int, k: Int, invocations: Array<IntArray>): List<Int> {
12+
pack(invocations, n)
13+
suspicious = BooleanArray(n)
14+
visited = BooleanArray(n)
15+
dfs(k, true)
16+
visited.fill(false)
17+
for (i in 0 until n) {
18+
if (!suspicious[i] && dfs2(i)) {
19+
visited.fill(false)
20+
dfs(k, false)
21+
break
22+
}
23+
}
24+
val rst = ArrayList<Int>()
25+
for (i in 0 until n) {
26+
if (!suspicious[i]) {
27+
rst.add(i)
28+
}
29+
}
30+
return rst
31+
}
32+
33+
fun dfs(u: Int, sus: Boolean) {
34+
if (visited[u]) {
35+
return
36+
}
37+
visited[u] = true
38+
suspicious[u] = sus
39+
for (v in graph[u]!!) {
40+
dfs(v, sus)
41+
}
42+
}
43+
44+
fun dfs2(u: Int): Boolean {
45+
if (suspicious[u]) {
46+
return true
47+
}
48+
if (visited[u]) {
49+
return false
50+
}
51+
visited[u] = true
52+
for (v in graph[u]!!) {
53+
if (dfs2(v)) {
54+
return true
55+
}
56+
}
57+
return false
58+
}
59+
60+
private fun pack(edges: Array<IntArray>, n: Int) {
61+
val adj = IntArray(n)
62+
for (edge in edges) {
63+
adj[edge[0]]++
64+
}
65+
graph = arrayOfNulls<IntArray>(n)
66+
for (i in 0 until n) {
67+
graph[i] = IntArray(adj[i])
68+
}
69+
for (edge in edges) {
70+
graph[edge[0]]!![--adj[edge[0]]] = edge[1]
71+
}
72+
}
73+
}
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: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package g3301_3400.s3311_construct_2d_grid_matching_graph_layout
2+
3+
// #Hard #Array #Hash_Table #Matrix #Graph
4+
// #2024_10_12_Time_1423_ms_(100.00%)_Space_113.1_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun constructGridLayout(n: Int, edges: Array<IntArray>): Array<IntArray> {
10+
val cs = IntArray(n)
11+
val als: Array<ArrayList<Int>> = Array<ArrayList<Int>>(n) { ArrayList<Int>() }
12+
for (e in edges) {
13+
cs[e[0]]++
14+
cs[e[1]]++
15+
als[e[0]].add(e[1])
16+
als[e[1]].add(e[0])
17+
}
18+
var min = 4
19+
for (a in cs) {
20+
min = min(min, a)
21+
}
22+
val seen = BooleanArray(n)
23+
var res: Array<IntArray>
24+
var st = 0
25+
for (i in 0 until n) {
26+
if (cs[i] == min) {
27+
st = i
28+
break
29+
}
30+
}
31+
if (min == 1) {
32+
res = Array<IntArray>(1) { IntArray(n) }
33+
for (i in 0 until n) {
34+
res[0][i] = st
35+
seen[st] = true
36+
if (i + 1 < n) {
37+
for (a in als[st]) {
38+
if (!seen[a]) {
39+
st = a
40+
break
41+
}
42+
}
43+
}
44+
}
45+
return res
46+
}
47+
var row2 = -1
48+
for (a in als[st]) {
49+
if (cs[a] == min) {
50+
row2 = a
51+
break
52+
}
53+
}
54+
if (row2 >= 0) {
55+
return getInts2(n, st, row2, seen, als)
56+
}
57+
return getInts1(n, seen, st, als, cs)
58+
}
59+
60+
private fun getInts1(
61+
n: Int,
62+
seen: BooleanArray,
63+
st: Int,
64+
als: Array<ArrayList<Int>>,
65+
cs: IntArray
66+
): Array<IntArray> {
67+
var st = st
68+
var res: Array<IntArray>
69+
val al = ArrayList<Int?>()
70+
var f = true
71+
seen[st] = true
72+
al.add(st)
73+
while (f) {
74+
f = false
75+
for (a in als[st]) {
76+
if (!seen[a] && cs[a] <= 3) {
77+
seen[a] = true
78+
al.add(a)
79+
if (cs[a] == 3) {
80+
f = true
81+
st = a
82+
}
83+
break
84+
}
85+
}
86+
}
87+
res = Array<IntArray>(n / al.size) { IntArray(al.size) }
88+
for (i in res[0].indices) {
89+
res[0][i] = al[i]!!
90+
}
91+
for (i in 1 until res.size) {
92+
for (j in res[0].indices) {
93+
for (a in als[res[i - 1][j]]) {
94+
if (!seen[a]) {
95+
res[i][j] = a
96+
seen[a] = true
97+
break
98+
}
99+
}
100+
}
101+
}
102+
return res
103+
}
104+
105+
private fun getInts2(
106+
n: Int,
107+
st: Int,
108+
row2: Int,
109+
seen: BooleanArray,
110+
als: Array<ArrayList<Int>>
111+
): Array<IntArray> {
112+
var res: Array<IntArray> = Array<IntArray>(2) { IntArray(n / 2) }
113+
res[0][0] = st
114+
res[1][0] = row2
115+
seen[row2] = true
116+
seen[st] = seen[row2]
117+
for (i in 1 until res[0].size) {
118+
for (a in als[res[0][i - 1]]) {
119+
if (!seen[a]) {
120+
res[0][i] = a
121+
seen[a] = true
122+
break
123+
}
124+
}
125+
for (a in als[res[1][i - 1]]) {
126+
if (!seen[a]) {
127+
res[1][i] = a
128+
seen[a] = true
129+
break
130+
}
131+
}
132+
}
133+
return res
134+
}
135+
}

0 commit comments

Comments
 (0)