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,45 @@
package g4001_4100.s4022_k_th_digit_in_infinite_string;

// #Medium #Math #Binary_Search #Staff #Biweekly_Contest_189
// #2026_11_25_Time_2_ms_(88.89%)_Space_42.48_MB_(52.78%)

public class Solution {
public int kthDigit(long k) {
if (k <= 9) {
return (int) k;
}
long remaining = k - 9;
long power10 = 10;
int d = 2;
while (true) {
long firstBlock = power10 / 10;
long lastBlock = power10 - 1;
long numberOfBlocks = lastBlock - firstBlock + 1;
long digitsPerBlock = 10L * d;
long groupDigits = numberOfBlocks * digitsPerBlock;
if (remaining > groupDigits) {
remaining -= groupDigits;
power10 *= 10;
d++;
continue;
}
long zeroBased = remaining - 1;
long blockOffset = zeroBased / digitsPerBlock;
long positionInBlock = zeroBased % digitsPerBlock;
long b = firstBlock + blockOffset;
long numberIndex = positionInBlock / d;
int digitIndex = (int) (positionInBlock % d);
long number;
if (b % 2 == 0) {
number = 10 * b + numberIndex;
} else {
number = 10 * b + 9 - numberIndex;
}
long divisor = 1;
for (int i = 0; i < d - 1 - digitIndex; i++) {
divisor *= 10;
}
return (int) ((number / divisor) % 10);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
4022\. K-th Digit in Infinite String

Medium

You are given an integer `k`.

An **infinite** string is formed by **concatenating** the **decimal** representations of the **positive** integers, without separators.

For every nonnegative integer `b`, block `b` contains the **positive** integers from `10 * b` through `10 * b + 9`. The integers in each block are appended as follows:

* If `b` is even, append the integers in **increasing** order.
* If `b` is odd, append the integers in **decreasing** order.

Therefore, the string starts with the integers 1 through 9, followed by 19 through 10, then 20 through 29, then 39 through 30, and so on.

Return the <code>k<sup>th</sup></code> digit (1-indexed) of this string.

**Example 1:**

**Input:** k = 4

**Output:** 4

**Explanation:**

The string begins as <code>"123<ins>4</ins>56789.."</code>. The 4<sup>th</sup> digit is `'4'`.

**Example 2:**

**Input:** k = 15

**Output:** 7

**Explanation:**

The string begins as <code>"12345678919181<ins>7</ins>.."</code>. The 15<sup>th</sup> digit is `'7'`.

**Example 3:**

**Input:** k = 11

**Output:** 9

**Explanation:**

The string begins as <code>"1234567891<ins>9</ins>.."</code>. The 11<sup>th</sup> digit is `'9'`.

**Constraints:**

* <code>1 <= k <= 10<sup>15</sup></code>
71 changes: 71 additions & 0 deletions src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package g4001_4100.s4023_elevator_requests_ii;

// #Hard #Array #Dynamic_Programming #Sorting #Senior_Staff #Biweekly_Contest_189
// #2026_11_25_Time_26_ms_(100.00%)_Space_69.24_MB_(79.07%)

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

public class Solution {
public long elevatorRequests(int n, int start, int[] requests) {

Check warning on line 11 in src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused method parameter "n".

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrUz3Um4WZvldP9&open=AaITSjrUz3Um4WZvldP9&pullRequest=2183
List<Integer> l = new ArrayList<>();
List<Integer> r = new ArrayList<>();
for (int x : requests) {
if (x < start) {
l.add(start - x);
} else if (x > start) {
r.add(x - start);
}
}
Collections.sort(l);

Check warning on line 21 in src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "Collections.sort()" with "List.sort()".

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrUz3Um4WZvldP-&open=AaITSjrUz3Um4WZvldP-&pullRequest=2183
Collections.sort(r);

Check warning on line 22 in src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace this "Collections.sort()" with "List.sort()".

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrUz3Um4WZvldP_&open=AaITSjrUz3Um4WZvldP_&pullRequest=2183
int a = l.size();
int b = r.size();
int t = a + b;
if (t == 0) {
return 0;
}
long[] x = new long[a + 1];
long[] y = new long[b + 1];
for (int i = 1; i <= a; i++) {
x[i] = l.get(i - 1);
}
for (int j = 1; j <= b; j++) {
y[j] = r.get(j - 1);
}
long inf = Long.MAX_VALUE / 4;
long[][] dl = new long[a + 1][b + 1];
long[][] dr = new long[a + 1][b + 1];
for (int i = 1; i <= a; i++) {
dl[i][0] = dl[i - 1][0] + (x[i] - x[i - 1]) * (t - i + 1);
}
for (int j = 1; j <= b; j++) {
dr[0][j] = dr[0][j - 1] + (y[j] - y[j - 1]) * (t - j + 1);
}
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= b; j++) {
long w = t - i - j + 1;

Check warning on line 48 in src/main/java/g4001_4100/s4023_elevator_requests_ii/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Cast one of the operands of this addition operation to a "long".

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrUz3Um4WZvldP8&open=AaITSjrUz3Um4WZvldP8&pullRequest=2183
long p = inf;
if (i >= 2) {
p = dl[i - 1][j] + (x[i] - x[i - 1]) * w;
}
long q = dr[i - 1][j] + (y[j] + x[i]) * w;
dl[i][j] = Math.min(p, q);
long s = inf;
if (j >= 2) {
s = dr[i][j - 1] + (y[j] - y[j - 1]) * w;
}
long z = dl[i][j - 1] + (x[i] + y[j]) * w;
dr[i][j] = Math.min(s, z);
}
}
if (a == 0) {
return dr[0][b];
}
if (b == 0) {
return dl[a][0];
}
return Math.min(dl[a][b], dr[a][b]);
}
}
63 changes: 63 additions & 0 deletions src/main/java/g4001_4100/s4023_elevator_requests_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
4023\. Elevator Requests II

Hard

You are given an integer `n` denoting the number of floors in a building, where the floors are numbered from 0 to `n - 1`.

You are also given an integer `start`, representing the floor where the elevator begins, and an integer array `requests`, where `requests[i]` is a floor that the elevator is requested to reach. All floors in `requests` are **distinct**.

At time 0, the elevator is on floor `start`, and all requests are made **simultaneously**.

During each second before all requests are fulfilled, the elevator moves **exactly** one floor, either **up** or **down**. A request is fulfilled **instantly** when the elevator reaches its requested floor. If `start` appears in `requests`, that request is fulfilled at time 0.

For each second that a request remains unfulfilled, you receive 1 penalty. Equivalently, a request fulfilled at time `t` contributes `t` to the total penalty.

Return the **minimum** total penalty required to fulfill all requests.

**Example 1:**

**Input:** n = 6, start = 4, requests = [1,5]

**Output:** 6

**Explanation:**

* Move from floor 4 (`start`) to floor 5 in 1 second. Penalty for floor 5 is 1.
* Move from floor 5 to floor 1 in 4 seconds. Penalty for floor 1 is 5.

Thus, the total penalty is `1 + 5 = 6`.

**Example 2:**

**Input:** n = 8, start = 3, requests = [3,7,1]

**Output:** 10

**Explanation:**

* Floor 3 (`start`) is fulfilled instantly. Penalty for floor 3 is 0.
* Move from floor 3 to floor 1 in 2 seconds. Penalty for floor 1 is 2.
* Move from floor 1 to floor 7 in 6 seconds. Penalty for floor 7 is 8.

Thus, the total penalty is `0 + 2 + 8 = 10`.

**Example 3:**

**Input:** n = 10, start = 5, requests = [0,2,9]

**Output:** 22

**Explanation:**

* Move from floor 5 (`start`) to floor 2 in 3 seconds. Penalty for floor 2 is 3.
* Move from floor 2 to floor 0 in 2 seconds. Penalty for floor 0 is 5.
* Move from floor 0 to floor 9 in 9 seconds. Penalty for floor 9 is 14.

Thus, the total penalty is `3 + 5 + 14 = 22`.

**Constraints:**

* <code>1 <= n <= 10<sup>9</sup></code>
* `1 <= requests.length <= 1500`
* `0 <= start, requests[i] <= n - 1`
* All values in `requests` are **distinct**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g4001_4100.s4024_nearest_available_drone;

// #Easy #Array #Enumeration #Mid_Level #Weekly_Contest_515
// #2026_11_25_Time_2_ms_(50.35%)_Space_45.70_MB_(93.36%)

public class Solution {
public int nearestDrone(int[][] drones, int[] target) {
int ri = -1;
for (int i = 0, l = drones.length, x = target[0], y = target[1], rv = Integer.MAX_VALUE, d;
i < l;
i++) {
d = Math.abs(x - drones[i][0]) + Math.abs(y - drones[i][1]);

Check warning on line 12 in src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code in order to not assign to this loop counter from within the loop body.

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrKz3Um4WZvldPz&open=AaITSjrKz3Um4WZvldPz&pullRequest=2183
if (d < rv && d <= drones[i][2]) {
rv = d;

Check warning on line 14 in src/main/java/g4001_4100/s4024_nearest_available_drone/Solution.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor the code in order to not assign to this loop counter from within the loop body.

See more on https://sonarcloud.io/project/issues?id=javadev_LeetCode-in-Java&issues=AaITSjrKz3Um4WZvldP0&open=AaITSjrKz3Um4WZvldP0&pullRequest=2183
ri = i;
}
}
return ri;
}
}
55 changes: 55 additions & 0 deletions src/main/java/g4001_4100/s4024_nearest_available_drone/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
4024\. Nearest Available Drone

Easy

You are given a 2D integer array `drones`, where <code>drones[i] = [x<sub>i</sub>, y<sub>i</sub>, range<sub>i</sub>]</code> represents the x-coordinate, y-coordinate, and travel range of the <code>i<sup>th</sup></code> drone.

You are also given an integer array `target = [tx, ty]`, representing the coordinates of the target.

A drone `drones[i]` can reach the target if the Manhattan distance between its coordinates and the target coordinates is **less than or equal** to its <code>range<sub>i</sub></code>.

Return the **index** of the reachable drone with the **minimum Manhattan distance** to the target. If there is a tie, return the **smallest index**. If no drone can reach the target, return `-1`.

**Example 1:**

**Input:** drones = [[0,0,8],[2,2,9]], target = [3,4]

**Output:** 1

**Explanation:**

* The distance between `drones[0]` and `target` is `|0 - 3| + |0 - 4| = 7`, which is within its range of 8.
* The distance between `drones[1]` and `target` is `|2 - 3| + |2 - 4| = 3`, which is within its range of 9.
* Since `drones[1]` is the nearest drone, the answer is 1.

**Example 2:**

**Input:** drones = [[2,1,5],[4,4,5],[6,6,8]], target = [5,5]

**Output:** 1

**Explanation:**

* The distance between `drones[0]` and `target` is `|2 - 5| + |1 - 5| = 7`, which is greater than its range of 5.
* The distance between `drones[1]` and `target` is `|4 - 5| + |4 - 5| = 2`, which is within its range of 5.
* The distance between `drones[2]` and `target` is `|6 - 5| + |6 - 5| = 2`, which is within its range of 8.
* Both `drones[1]` and `drones[2]` are the nearest drones. Since we should return the smallest index, the answer is 1.

**Example 3:**

**Input:** drones = [[4,4,5]], target = [8,6]

**Output:** \-1

**Explanation:**

* The distance between `drones[0]` and `target` is `|4 - 8| + |4 - 6| = 6`, which is greater than its range of 5.
* No drone can reach the target, so the answer is -1.

**Constraints:**

* `1 <= drones.length <= 100`
* <code>drones[i] = [x<sub>i</sub>, y<sub>i</sub>, range<sub>i</sub>]</code>
* `target = [tx, ty]`
* <code>-25 <= x<sub>i</sub>, y<sub>i</sub>, tx, ty <= 25</code>
* <code>1 <= range<sub>i</sub> <= 100</code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package g4001_4100.s4025_minimize_the_maximum_waiting_time_at_synchronized_traffic_lights;

// #Medium #Array #Greedy #Senior #Weekly_Contest_515
// #2026_11_25_Time_3_ms_(100.00%)_Space_121.58_MB_(33.61%)

public class Solution {
public int minPenalty(int period, int[] lights, int[] arrivalTime) {
int greenMax = Integer.MIN_VALUE;
for (int light : lights) {
greenMax = Math.max(greenMax, light);
}
int ans = 0;
for (int i : arrivalTime) {
int r = i % period;
int curr = period - r;
if (r >= greenMax) {
ans = Math.max(ans, curr);
}
}
return ans;
}
}
Loading
Loading