Skip to content

Added tasks 3309-3312 #701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package g3301_3400.s3309_maximum_possible_number_by_binary_concatenation

// #Medium #Array #Bit_Manipulation #Enumeration
// #2024_10_12_Time_182_ms_(73.47%)_Space_36.8_MB_(79.59%)

class Solution {
private var result = "0"

fun maxGoodNumber(nums: IntArray): Int {
val visited = BooleanArray(nums.size)
val sb = StringBuilder()
solve(nums, visited, 0, sb)
var score = 0
var `val`: Int
for (c in result.toCharArray()) {
`val` = c.code - '0'.code
score *= 2
score += `val`
}
return score
}

private fun solve(nums: IntArray, visited: BooleanArray, pos: Int, sb: StringBuilder) {
if (pos == nums.size) {
val `val` = sb.toString()
if ((result.length == `val`.length && result.compareTo(`val`) < 0) ||
`val`.length > result.length
) {
result = `val`
}
return
}
var cur: String?
for (i in nums.indices) {
if (visited[i]) {
continue
}
visited[i] = true
cur = Integer.toBinaryString(nums[i])
sb.append(cur)
solve(nums, visited, pos + 1, sb)
sb.setLength(sb.length - cur.length)
visited[i] = false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
3309\. Maximum Possible Number by Binary Concatenation

Medium

You are given an array of integers `nums` of size 3.

Return the **maximum** possible number whose _binary representation_ can be formed by **concatenating** the _binary representation_ of **all** elements in `nums` in some order.

**Note** that the binary representation of any number _does not_ contain leading zeros.

**Example 1:**

**Input:** nums = [1,2,3]

**Output:** 30

**Explanation:**

Concatenate the numbers in the order `[3, 1, 2]` to get the result `"11110"`, which is the binary representation of 30.

**Example 2:**

**Input:** nums = [2,8,16]

**Output:** 1296

**Explanation:**

Concatenate the numbers in the order `[2, 8, 16]` to get the result `"10100010000"`, which is the binary representation of 1296.

**Constraints:**

* `nums.length == 3`
* `1 <= nums[i] <= 127`
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package g3301_3400.s3310_remove_methods_from_project

// #Medium #Depth_First_Search #Breadth_First_Search #Graph
// #2024_10_12_Time_1465_ms_(100.00%)_Space_201.7_MB_(14.81%)

class Solution {
private lateinit var graph: Array<IntArray?>
private lateinit var suspicious: BooleanArray
private lateinit var visited: BooleanArray

fun remainingMethods(n: Int, k: Int, invocations: Array<IntArray>): List<Int> {
pack(invocations, n)
suspicious = BooleanArray(n)
visited = BooleanArray(n)
dfs(k, true)
visited.fill(false)
for (i in 0 until n) {
if (!suspicious[i] && dfs2(i)) {
visited.fill(false)
dfs(k, false)
break
}
}
val rst = ArrayList<Int>()
for (i in 0 until n) {
if (!suspicious[i]) {
rst.add(i)
}
}
return rst
}

fun dfs(u: Int, sus: Boolean) {
if (visited[u]) {
return
}
visited[u] = true
suspicious[u] = sus
for (v in graph[u]!!) {
dfs(v, sus)
}
}

fun dfs2(u: Int): Boolean {
if (suspicious[u]) {
return true
}
if (visited[u]) {
return false
}
visited[u] = true
for (v in graph[u]!!) {
if (dfs2(v)) {
return true
}
}
return false
}

private fun pack(edges: Array<IntArray>, n: Int) {
val adj = IntArray(n)
for (edge in edges) {
adj[edge[0]]++
}
graph = arrayOfNulls<IntArray>(n)
for (i in 0 until n) {
graph[i] = IntArray(adj[i])
}
for (edge in edges) {
graph[edge[0]]!![--adj[edge[0]]] = edge[1]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
3310\. Remove Methods From Project

Medium

You are maintaining a project that has `n` methods numbered from `0` to `n - 1`.

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>.

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.

A group of methods can only be removed if no method **outside** the group invokes any methods **within** it.

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.

**Example 1:**

**Input:** n = 4, k = 1, invocations = [[1,2],[0,1],[3,2]]

**Output:** [0,1,2,3]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/07/18/graph-2.png)

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.

**Example 2:**

**Input:** n = 5, k = 0, invocations = [[1,2],[0,2],[0,1],[3,4]]

**Output:** [3,4]

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/07/18/graph-3.png)

Methods 0, 1, and 2 are suspicious and they are not directly invoked by any other method. We can remove them.

**Example 3:**

**Input:** n = 3, k = 2, invocations = [[1,2],[0,1],[2,0]]

**Output:** []

**Explanation:**

![](https://assets.leetcode.com/uploads/2024/07/20/graph.png)

All methods are suspicious. We can remove them.

**Constraints:**

* <code>1 <= n <= 10<sup>5</sup></code>
* `0 <= k <= n - 1`
* <code>0 <= invocations.length <= 2 * 10<sup>5</sup></code>
* <code>invocations[i] == [a<sub>i</sub>, b<sub>i</sub>]</code>
* <code>0 <= a<sub>i</sub>, b<sub>i</sub> <= n - 1</code>
* <code>a<sub>i</sub> != b<sub>i</sub></code>
* `invocations[i] != invocations[j]`
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package g3301_3400.s3311_construct_2d_grid_matching_graph_layout

// #Hard #Array #Hash_Table #Matrix #Graph
// #2024_10_12_Time_1423_ms_(100.00%)_Space_113.1_MB_(100.00%)

import kotlin.math.min

class Solution {
fun constructGridLayout(n: Int, edges: Array<IntArray>): Array<IntArray> {
val cs = IntArray(n)
val als: Array<ArrayList<Int>> = Array<ArrayList<Int>>(n) { ArrayList<Int>() }
for (e in edges) {
cs[e[0]]++
cs[e[1]]++
als[e[0]].add(e[1])
als[e[1]].add(e[0])
}
var min = 4
for (a in cs) {
min = min(min, a)
}
val seen = BooleanArray(n)
var res: Array<IntArray>
var st = 0
for (i in 0 until n) {
if (cs[i] == min) {
st = i
break
}
}
if (min == 1) {
res = Array<IntArray>(1) { IntArray(n) }
for (i in 0 until n) {
res[0][i] = st
seen[st] = true
if (i + 1 < n) {
for (a in als[st]) {
if (!seen[a]) {
st = a
break
}
}
}
}
return res
}
var row2 = -1
for (a in als[st]) {
if (cs[a] == min) {
row2 = a
break
}
}
if (row2 >= 0) {
return getInts2(n, st, row2, seen, als)
}
return getInts1(n, seen, st, als, cs)
}

private fun getInts1(
n: Int,
seen: BooleanArray,
st: Int,
als: Array<ArrayList<Int>>,
cs: IntArray
): Array<IntArray> {
var st = st
var res: Array<IntArray>
val al = ArrayList<Int?>()
var f = true
seen[st] = true
al.add(st)
while (f) {
f = false
for (a in als[st]) {
if (!seen[a] && cs[a] <= 3) {
seen[a] = true
al.add(a)
if (cs[a] == 3) {
f = true
st = a
}
break
}
}
}
res = Array<IntArray>(n / al.size) { IntArray(al.size) }
for (i in res[0].indices) {
res[0][i] = al[i]!!
}
for (i in 1 until res.size) {
for (j in res[0].indices) {
for (a in als[res[i - 1][j]]) {
if (!seen[a]) {
res[i][j] = a
seen[a] = true
break
}
}
}
}
return res
}

private fun getInts2(
n: Int,
st: Int,
row2: Int,
seen: BooleanArray,
als: Array<ArrayList<Int>>
): Array<IntArray> {
var res: Array<IntArray> = Array<IntArray>(2) { IntArray(n / 2) }
res[0][0] = st
res[1][0] = row2
seen[row2] = true
seen[st] = seen[row2]
for (i in 1 until res[0].size) {
for (a in als[res[0][i - 1]]) {
if (!seen[a]) {
res[0][i] = a
seen[a] = true
break
}
}
for (a in als[res[1][i - 1]]) {
if (!seen[a]) {
res[1][i] = a
seen[a] = true
break
}
}
}
return res
}
}
Loading