Skip to content

Added tasks 3309-3312 #1833

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 3 commits into from
Oct 8, 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,45 @@
package g3301_3400.s3309_maximum_possible_number_by_binary_concatenation;

// #Medium #Array #Bit_Manipulation #Enumeration
// #2024_10_08_Time_3_ms_(97.01%)_Space_42.2_MB_(90.32%)

public class Solution {
private String result = "0";

public int maxGoodNumber(int[] nums) {
boolean[] visited = new boolean[nums.length];
StringBuilder sb = new StringBuilder();
solve(nums, visited, 0, sb);
int score = 0;
int val;
for (char c : result.toCharArray()) {
val = c - '0';
score *= 2;
score += val;
}
return score;
}

private void solve(int[] nums, boolean[] visited, int pos, StringBuilder sb) {
if (pos == nums.length) {
String val = sb.toString();
if ((result.length() == val.length() && result.compareTo(val) < 0)
|| val.length() > result.length()) {
result = val;
}
return;
}
String cur;
for (int i = 0; i < nums.length; ++i) {
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,77 @@
package g3301_3400.s3310_remove_methods_from_project;

// #Medium #Graph #Depth_First_Search #Breadth_First_Search
// #2024_10_08_Time_41_ms_(99.76%)_Space_154.8_MB_(55.29%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
private int[][] graph;
private boolean[] suspicious;
private boolean[] visited;

public List<Integer> remainingMethods(int n, int k, int[][] invocations) {
pack(invocations, n);
suspicious = new boolean[n];
visited = new boolean[n];
dfs(k, true);
Arrays.fill(visited, false);
for (int i = 0; i < n; i++) {
if (!suspicious[i] && dfs2(i)) {
Arrays.fill(visited, false);
dfs(k, false);
break;
}
}
ArrayList<Integer> rst = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (!suspicious[i]) {
rst.add(i);
}
}
return rst;
}

public void dfs(int u, boolean sus) {
if (visited[u]) {
return;
}
visited[u] = true;
suspicious[u] = sus;
for (int v : graph[u]) {
dfs(v, sus);
}
}

public boolean dfs2(int u) {
if (suspicious[u]) {
return true;
}
if (visited[u]) {
return false;
}
visited[u] = true;
for (int v : graph[u]) {
if (dfs2(v)) {
return true;
}
}
return false;
}

private void pack(int[][] edges, int n) {
int[] adj = new int[n];
for (int[] edge : edges) {
adj[edge[0]]++;
}
graph = new int[n][];
for (int i = 0; i < n; i++) {
graph[i] = new int[adj[i]];
}
for (int[] edge : 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,125 @@
package g3301_3400.s3311_construct_2d_grid_matching_graph_layout;

// #Hard #Array #Hash_Table #Matrix #Graph #2024_10_08_Time_43_ms_(94.34%)_Space_103.6_MB_(79.25%)

import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class Solution {
public int[][] constructGridLayout(int n, int[][] edges) {
final int[] cs = new int[n];
final ArrayList<Integer>[] als = new ArrayList[n];
for (int i = 0; i < n; ++i) {
als[i] = new ArrayList<>();
}
for (int[] e : edges) {
cs[e[0]]++;
cs[e[1]]++;
als[e[0]].add(e[1]);
als[e[1]].add(e[0]);
}
int min = 4;
for (int a : cs) {
min = Math.min(min, a);
}
final boolean[] seen = new boolean[n];
int[][] res;
int st = 0;
for (int i = 0; i < n; ++i) {
if (cs[i] == min) {
st = i;
break;
}
}
if (min == 1) {
res = new int[1][n];
for (int i = 0; i < n; ++i) {
res[0][i] = st;
seen[st] = true;
if (i + 1 < n) {
for (int a : als[st]) {
if (!seen[a]) {
st = a;
break;
}
}
}
}
return res;
}
int row2 = -1;
for (int a : als[st]) {
if (cs[a] == min) {
row2 = a;
break;
}
}
if (row2 >= 0) {
return getInts(n, st, row2, seen, als);
}
return getInts(n, seen, st, als, cs);
}

private int[][] getInts(int n, boolean[] seen, int st, ArrayList<Integer>[] als, int[] cs) {
int[][] res;
final ArrayList<Integer> al = new ArrayList<>();
boolean f = true;
seen[st] = true;
al.add(st);
while (f) {
f = false;
for (int a : als[st]) {
if (!seen[a] && cs[a] <= 3) {
seen[a] = true;
al.add(a);
if (cs[a] == 3) {
f = true;
st = a;
}
break;
}
}
}
res = new int[n / al.size()][al.size()];
for (int i = 0; i < res[0].length; ++i) {
res[0][i] = al.get(i);
}
for (int i = 1; i < res.length; ++i) {
for (int j = 0; j < res[0].length; ++j) {
for (int a : als[res[i - 1][j]]) {
if (!seen[a]) {
res[i][j] = a;
seen[a] = true;
break;
}
}
}
}
return res;
}

private int[][] getInts(int n, int st, int row2, boolean[] seen, ArrayList<Integer>[] als) {
int[][] res;
res = new int[2][n / 2];
res[0][0] = st;
res[1][0] = row2;
seen[st] = seen[row2] = true;
for (int i = 1; i < res[0].length; ++i) {
for (int a : als[res[0][i - 1]]) {
if (!seen[a]) {
res[0][i] = a;
seen[a] = true;
break;
}
}
for (int a : als[res[1][i - 1]]) {
if (!seen[a]) {
res[1][i] = a;
seen[a] = true;
break;
}
}
}
return res;
}
}
Loading
Loading