Skip to content
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,37 @@
package g3901_4000.s3922_minimum_flips_to_make_binary_string_coherent;

// #Medium #String #Senior #Biweekly_Contest_182
// #2026_09_29_Time_4_ms_(100.00%)_Space_47.56_MB_(76.92%)

public class Solution {
public int minFlips(String s) {
int n = s.length();
int count1 = 0;
for (char c : s.toCharArray()) {
count1 += c - '0';
}
int count0 = n - count1;
// Option 1: all 0s
int ans = count1;
// Option 2: all 1s
ans = Math.min(ans, count0);
// Option 3: exactly one 1 (at any position)
if (count1 >= 1) {
ans = Math.min(ans, count1 - 1);
} else {
ans = Math.min(ans, 1);
}
// Option 4: "10...01" (1 at position 0 and position n-1)
if (n >= 2 && count1 >= 2) {
int cost4 = count1;
if (s.charAt(0) == '1') {
cost4--;
}
if (s.charAt(n - 1) == '1') {
cost4--;
}
ans = Math.min(ans, cost4);
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
3922\. Minimum Flips to Make Binary String Coherent

Medium

You are given a binary string `s`.

A string is considered **coherent** if it does **not** contain `"011"` or `"110"` as subsequences.

In one operation, you can **flip** any character in `s` (`'0'` to `'1'` or `'1'` to `'0'`).

Return an integer denoting the **minimum** number of operations required to make `s` coherent.

**Example 1:**

**Input:** s = "1010"

**Output:** 1

**Explanation:**

Flip `s[0]` to get `"0010"`, which contains no `"011"` or `"110"` subsequences.

**Example 2:**

**Input:** s = "0110"

**Output:** 1

**Explanation:**

Flip `s[1]` to get `"0010"`, removing all forbidden subsequences `"011"` and `"110"`.

**Example 3:**

**Input:** s = "1000"

**Output:** 0

**Explanation:**

The string already has no `"011"` or `"110"` subsequences, so no flips are needed.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s[i]` is either `'0'` or `'1'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package g3901_4000.s3923_minimum_generations_to_target_point;

// #Medium #Array #Hash_Table #Simulation #Staff #Biweekly_Contest_182
// #2026_09_29_Time_24_ms_(100.00%)_Space_46.83_MB_(67.86%)

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

public class Solution {
private static final int SIZE = 7;
private static final int TOTAL_POINTS = SIZE * SIZE * SIZE;

public int minGenerations(int[][] points, int[] target) {
boolean[] visited = new boolean[TOTAL_POINTS];
List<Integer> allPoints = new ArrayList<>();
List<Integer> frontier = new ArrayList<>();
int targetCode = encode(target[0], target[1], target[2]);
initializePoints(points, visited, allPoints, frontier);
if (frontier.contains(targetCode)) {
return 0;
}
int generation = 0;
while (!frontier.isEmpty()) {
generation++;
List<Integer> nextFrontier = generateNextFrontier(frontier, allPoints, visited);
if (nextFrontier.contains(targetCode)) {
return generation;
}
addPoints(nextFrontier, visited, allPoints);
frontier = nextFrontier;
}
return -1;
}

private void initializePoints(
int[][] points, boolean[] visited, List<Integer> allPoints, List<Integer> frontier) {
for (int[] point : points) {
int code = encode(point[0], point[1], point[2]);
visited[code] = true;
allPoints.add(code);
frontier.add(code);
}
}

private List<Integer> generateNextFrontier(
List<Integer> frontier, List<Integer> allPoints, boolean[] visited) {
List<Integer> nextFrontier = new ArrayList<>();
boolean[] addedThisGeneration = new boolean[TOTAL_POINTS];
for (int pointA : frontier) {
addGeneratedPoints(pointA, allPoints, visited, addedThisGeneration, nextFrontier);
}
return nextFrontier;
}

private void addGeneratedPoints(
int pointA,
List<Integer> allPoints,
boolean[] visited,
boolean[] addedThisGeneration,
List<Integer> nextFrontier) {
int[] a = decode(pointA);
for (int pointB : allPoints) {
if (pointA != pointB) {
addGeneratedPoint(a, pointB, visited, addedThisGeneration, nextFrontier);
}
}
}

private void addGeneratedPoint(
int[] a,
int pointB,
boolean[] visited,
boolean[] addedThisGeneration,
List<Integer> nextFrontier) {
int[] b = decode(pointB);
int x = (a[0] + b[0]) / 2;
int y = (a[1] + b[1]) / 2;
int z = (a[2] + b[2]) / 2;
int newPoint = encode(x, y, z);
if (!visited[newPoint] && !addedThisGeneration[newPoint]) {
addedThisGeneration[newPoint] = true;
nextFrontier.add(newPoint);
}
}

private void addPoints(List<Integer> points, boolean[] visited, List<Integer> allPoints) {
for (int point : points) {
visited[point] = true;
allPoints.add(point);
}
}

private int encode(int x, int y, int z) {
return x * SIZE * SIZE + y * SIZE + z;
}

private int[] decode(int code) {
int x = code / (SIZE * SIZE);
code %= SIZE * SIZE;
int y = code / SIZE;
int z = code % SIZE;
return new int[] {x, y, z};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
3923\. Minimum Generations to Target Point

Medium

You are given a 2D integer array `points` where <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>, z<sub>i</sub>]</code> represents a point in 3D space, and an integer array `target` representing a target point.

Define **generation** 0 as the initial list of points. For each integer `k >= 1`, form generation `k` as follows:

* Consider every pair of two **distinct** points <code>a = [x<sub>1</sub>, y<sub>1</sub>, z<sub>1</sub>]</code> and <code>b = [x<sub>2</sub>, y<sub>2</sub>, z<sub>2</sub>]</code> taken from all points produced in generations 0 through `k - 1`.
* For each such pair, compute <code>c = [floor((x<sub>1</sub> + x<sub>2</sub>) / 2), floor((y<sub>1</sub> + y<sub>2</sub>) / 2), floor((z<sub>1</sub> + z<sub>2</sub>) / 2)]</code> and collect every such `c` into a generation `k`.
* All points in the generation `k` are produced **simultaneously** from points in generations 0 through `k - 1`.
* After generation `k` is formed, the points in the generation `k` are considered available for forming later generations.

Return the **smallest** integer `k` such that the `target` appears in one of the generations 0 through `k`. If the `target` is already in the initial points, return 0. If it is impossible to obtain the `target`, return -1.

Notes:

* **floor** denotes rounding **down** to the nearest integer.
* "Two **distinct** points" means the two chosen points must have **different** `(x, y, z)` coordinates. A point cannot be paired with itself, and pairing two points with **identical** coordinates is not possible.

**Example 1:**

**Input:** points = [[0,0,0],[6,6,6]], target = [3,3,3]

**Output:** 1

**Explanation:**

* **Generation 0:** The initial `points = [[0, 0, 0], [6, 6, 6]]`.
* The `target = [3, 3, 3]` does not exist in generation 0.
* **Generation 1:** For each pair of points in generation 0, we create new points.
* Using `[0, 0, 0]` and `[6, 6, 6]`, we generate `[3, 3, 3]`.
* After generation 1, `points = [[0, 0, 0], [6, 6, 6], [3, 3, 3]]`.
* The `target = [3, 3, 3]` is found in generation 1, so the smallest `k` is 1.

**Example 2:**

**Input:** points = [[0,0,0],[5,5,5]], target = [1,1,1]

**Output:** 2

**Explanation:**

* **Generation 0:** The initial `points = [[0, 0, 0], [5, 5, 5]]`.
* The `target = [1, 1, 1]` does not exist in generation 0.
* **Generation 1:** For each pair of points in generation 0, we create new points.
* Using `[0, 0, 0]` and `[5, 5, 5]`, we generate `[2, 2, 2]`.
* After generation 1, `points = [[0, 0, 0], [5, 5, 5], [2, 2, 2]]`.
* **Generation 2:** For each pair of points available after generation 1, we create new points.
* Using `[0, 0, 0]` and `[5, 5, 5]`, we generate `[2, 2, 2]`.
* Using `[0, 0, 0]` and `[2, 2, 2]`, we generate `[1, 1, 1]`.
* Using `[5, 5, 5]` and `[2, 2, 2]`, we generate `[3, 3, 3]`.
* After generation 2, `points = [[0, 0, 0], [5, 5, 5], [2, 2, 2], [1, 1, 1], [3, 3, 3]]`.
* The `target = [1, 1, 1]` is found in generation 2, so the smallest `k` is 2.

**Example 3:**

**Input:** points = [[0,0,0],[2,2,2],[3,3,3]], target = [2,2,2]

**Output:** 0

**Explanation:**

* **Generation 0:** The initial `points = [[0, 0, 0], [2, 2, 2], [3, 3, 3]]`.
* The `target = [2, 2, 2]` already exists in generation 0, so the smallest `k` is 0.

**Example 4:**

**Input:** points = [[1,2,3]], target = [5,5,5]

**Output:** \-1

**Explanation:**

* Only one initial point is available, so no new points can be generated.
* Therefore, the target cannot be obtained, and the answer is -1.

**Constraints:**

* `1 <= points.length <= 20`
* <code>points[i] = [x<sub>i</sub>, y<sub>i</sub>, z<sub>i</sub>]</code>
* <code>0 <= x<sub>i</sub>, y<sub>i</sub>, z<sub>i</sub> <= 6</code>
* `target.length == 3`
* `0 <= target[i] <= 6`
* The initial set of points contains no duplicates.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package g3901_4000.s3924_minimum_threshold_path_with_limited_heavy_edges;

// #Hard #Binary_Search #Senior_Staff #Biweekly_Contest_182 #Breadth_First_Search #Graph_Theory
// #2026_09_29_Time_30_ms_(98.28%)_Space_47.81_MB_(58.62%)

import java.util.ArrayDeque;
import java.util.Arrays;

public class Solution {
private int[] head;
private int[] to;
private int[] weight;
private int[] next;
private int n;
private int source;
private int target;
private int k;

public int minimumThreshold(int n, int[][] edges, int source, int target, int k) {
if (source == target) {
return 0;
}
this.n = n;
this.source = source;
this.target = target;
this.k = k;
// make adj list (you dont have to do this fancy version, standard adj list works)
int m = edges.length;
this.head = new int[n];
this.to = new int[m << 1];
this.weight = new int[m << 1];
this.next = new int[m << 1];
Arrays.fill(head, -1);
for (int i = 0; i < m; i++) {
int a = edges[i][0];
int b = edges[i][1];
int c = edges[i][2];
to[i << 1] = b;
next[i << 1] = head[a];
head[a] = i << 1;
to[i << 1 | 1] = a;
next[i << 1 | 1] = head[b];
head[b] = i << 1 | 1;
weight[i << 1] = weight[i << 1 | 1] = c;
}
// check if it's possible to reach the target at all
if (!check(Integer.MAX_VALUE)) {
return -1;
}
if (k == m) {
return 0;
}
int left = 0;
int right = 0;
// set right pointer to max edge weight, cuz any threshold larger than that is pointless
for (int[] edge : edges) {
if (edge[2] > right) {
right = edge[2];
}
}
// binary search on the answer
while (left < right) {
int mid = left + right >>> 1;
if (check(mid)) {
right = mid;
} else {
left = mid + 1;
}
}
return left;
}

private boolean check(int threshold) {
// 0-1 BFS
int[] dist = new int[n];
Arrays.fill(dist, k + 1);
dist[source] = 0;
ArrayDeque<Integer> queue = new ArrayDeque<>(n);
queue.add(source);
while (!queue.isEmpty()) {
int current = queue.poll();
if (current == target) {
return true;
}
int currentDist = dist[current];
// go through each adj node (a regular adj list works here as well)
for (int i = head[current]; i != -1; i = next[i]) {
int node = to[i];
int newWeight = weight[i] > threshold ? 1 : 0;
int newDist = newWeight + currentDist;
if (newDist < dist[node]) {
dist[node] = newDist;
if (newWeight == 0) {
queue.addFirst(node);
} else {
queue.addLast(node);
}
}
}
}
return false;
}
}
Loading
Loading