Skip to content

Commit 36f3768

Browse files
committed
Added tasks 3309-3312
1 parent 491efa1 commit 36f3768

File tree

5 files changed

+527
-0
lines changed
  • src/main/kotlin/g3301_3400
    • s3309_maximum_possible_number_by_binary_concatenation
    • s3310_remove_methods_from_project
    • s3311_construct_2d_grid_matching_graph_layout
    • s3312_sorted_gcd_pair_queries

5 files changed

+527
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,6 +1816,10 @@
18161816

18171817
| # | Title | Difficulty | Tag | Time, ms | Time, %
18181818
|------|----------------|-------------|-------------|----------|--------
1819+
| 3312 |[Sorted GCD Pair Queries](src/main/kotlin/g3301_3400/s3312_sorted_gcd_pair_queries)| Hard | Array, Hash_Table, Math, Binary_Search, Prefix_Sum, Counting, Number_Theory, Combinatorics | 734 | 100.00
1820+
| 3311 |[Construct 2D Grid Matching Graph Layout](src/main/kotlin/g3301_3400/s3311_construct_2d_grid_matching_graph_layout)| Hard | Array, Hash_Table, Matrix, Graph | 1423 | 100.00
1821+
| 3310 |[Remove Methods From Project](src/main/kotlin/g3301_3400/s3310_remove_methods_from_project)| Medium | Depth_First_Search, Breadth_First_Search, Graph | 1465 | 100.00
1822+
| 3309 |[Maximum Possible Number by Binary Concatenation](src/main/kotlin/g3301_3400/s3309_maximum_possible_number_by_binary_concatenation)| Medium | Array, Bit_Manipulation, Enumeration | 182 | 73.47
18191823
| 3307 |[Find the K-th Character in String Game II](src/main/kotlin/g3301_3400/s3307_find_the_k_th_character_in_string_game_ii)| Hard | Math, Bit_Manipulation, Recursion | 189 | 50.00
18201824
| 3306 |[Count of Substrings Containing Every Vowel and K Consonants II](src/main/kotlin/g3301_3400/s3306_count_of_substrings_containing_every_vowel_and_k_consonants_ii)| Medium | String, Hash_Table, Sliding_Window | 651 | 100.00
18211825
| 3305 |[Count of Substrings Containing Every Vowel and K Consonants I](src/main/kotlin/g3301_3400/s3305_count_of_substrings_containing_every_vowel_and_k_consonants_i)| Medium | String, Hash_Table, Sliding_Window | 153 | 100.00
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3309\. Maximum Possible Number by Binary Concatenation
5+
6+
Medium
7+
8+
You are given an array of integers `nums` of size 3.
9+
10+
Return the **maximum** possible number whose _binary representation_ can be formed by **concatenating** the _binary representation_ of **all** elements in `nums` in some order.
11+
12+
**Note** that the binary representation of any number _does not_ contain leading zeros.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3]
17+
18+
**Output:** 30
19+
20+
**Explanation:**
21+
22+
Concatenate the numbers in the order `[3, 1, 2]` to get the result `"11110"`, which is the binary representation of 30.
23+
24+
**Example 2:**
25+
26+
**Input:** nums = [2,8,16]
27+
28+
**Output:** 1296
29+
30+
**Explanation:**
31+
32+
Concatenate the numbers in the order `[2, 8, 16]` to get the result `"10100010000"`, which is the binary representation of 1296.
33+
34+
**Constraints:**
35+
36+
* `nums.length == 3`
37+
* `1 <= nums[i] <= 127`
38+
39+
## Solution
40+
41+
```kotlin
42+
class Solution {
43+
private var result = "0"
44+
45+
fun maxGoodNumber(nums: IntArray): Int {
46+
val visited = BooleanArray(nums.size)
47+
val sb = StringBuilder()
48+
solve(nums, visited, 0, sb)
49+
var score = 0
50+
var `val`: Int
51+
for (c in result.toCharArray()) {
52+
`val` = c.code - '0'.code
53+
score *= 2
54+
score += `val`
55+
}
56+
return score
57+
}
58+
59+
private fun solve(nums: IntArray, visited: BooleanArray, pos: Int, sb: StringBuilder) {
60+
if (pos == nums.size) {
61+
val `val` = sb.toString()
62+
if ((result.length == `val`.length && result.compareTo(`val`) < 0) ||
63+
`val`.length > result.length
64+
) {
65+
result = `val`
66+
}
67+
return
68+
}
69+
var cur: String?
70+
for (i in nums.indices) {
71+
if (visited[i]) {
72+
continue
73+
}
74+
visited[i] = true
75+
cur = Integer.toBinaryString(nums[i])
76+
sb.append(cur)
77+
solve(nums, visited, pos + 1, sb)
78+
sb.setLength(sb.length - cur.length)
79+
visited[i] = false
80+
}
81+
}
82+
}
83+
```
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3310\. Remove Methods From Project
5+
6+
Medium
7+
8+
You are maintaining a project that has `n` methods numbered from `0` to `n - 1`.
9+
10+
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>.
11+
12+
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.
13+
14+
A group of methods can only be removed if no method **outside** the group invokes any methods **within** it.
15+
16+
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.
17+
18+
**Example 1:**
19+
20+
**Input:** n = 4, k = 1, invocations = \[\[1,2],[0,1],[3,2]]
21+
22+
**Output:** [0,1,2,3]
23+
24+
**Explanation:**
25+
26+
![](https://assets.leetcode.com/uploads/2024/07/18/graph-2.png)
27+
28+
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.
29+
30+
**Example 2:**
31+
32+
**Input:** n = 5, k = 0, invocations = \[\[1,2],[0,2],[0,1],[3,4]]
33+
34+
**Output:** [3,4]
35+
36+
**Explanation:**
37+
38+
![](https://assets.leetcode.com/uploads/2024/07/18/graph-3.png)
39+
40+
Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.
41+
42+
**Example 3:**
43+
44+
**Input:** n = 3, k = 2, invocations = \[\[1,2],[0,1],[2,0]]
45+
46+
**Output:** []
47+
48+
**Explanation:**
49+
50+
![](https://assets.leetcode.com/uploads/2024/07/20/graph.png)
51+
52+
All methods are suspicious. We can remove them.
53+
54+
**Constraints:**
55+
56+
* <code>1 <= n <= 10<sup>5</sup></code>
57+
* `0 <= k <= n - 1`
58+
* <code>0 <= invocations.length <= 2 * 10<sup>5</sup></code>
59+
* <code>invocations[i] == [a<sub>i</sub>, b<sub>i</sub>]</code>
60+
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code>
61+
* <code>a<sub>i</sub> != b<sub>i</sub></code>
62+
* `invocations[i] != invocations[j]`
63+
64+
## Solution
65+
66+
```kotlin
67+
class Solution {
68+
private lateinit var graph: Array<IntArray?>
69+
private lateinit var suspicious: BooleanArray
70+
private lateinit var visited: BooleanArray
71+
72+
fun remainingMethods(n: Int, k: Int, invocations: Array<IntArray>): List<Int> {
73+
pack(invocations, n)
74+
suspicious = BooleanArray(n)
75+
visited = BooleanArray(n)
76+
dfs(k, true)
77+
visited.fill(false)
78+
for (i in 0 until n) {
79+
if (!suspicious[i] && dfs2(i)) {
80+
visited.fill(false)
81+
dfs(k, false)
82+
break
83+
}
84+
}
85+
val rst = ArrayList<Int>()
86+
for (i in 0 until n) {
87+
if (!suspicious[i]) {
88+
rst.add(i)
89+
}
90+
}
91+
return rst
92+
}
93+
94+
fun dfs(u: Int, sus: Boolean) {
95+
if (visited[u]) {
96+
return
97+
}
98+
visited[u] = true
99+
suspicious[u] = sus
100+
for (v in graph[u]!!) {
101+
dfs(v, sus)
102+
}
103+
}
104+
105+
fun dfs2(u: Int): Boolean {
106+
if (suspicious[u]) {
107+
return true
108+
}
109+
if (visited[u]) {
110+
return false
111+
}
112+
visited[u] = true
113+
for (v in graph[u]!!) {
114+
if (dfs2(v)) {
115+
return true
116+
}
117+
}
118+
return false
119+
}
120+
121+
private fun pack(edges: Array<IntArray>, n: Int) {
122+
val adj = IntArray(n)
123+
for (edge in edges) {
124+
adj[edge[0]]++
125+
}
126+
graph = arrayOfNulls<IntArray>(n)
127+
for (i in 0 until n) {
128+
graph[i] = IntArray(adj[i])
129+
}
130+
for (edge in edges) {
131+
graph[edge[0]]!![--adj[edge[0]]] = edge[1]
132+
}
133+
}
134+
}
135+
```

0 commit comments

Comments
 (0)