Skip to content

Added tasks 3451-3459 #1911

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 11 commits into from
Feb 18, 2025
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
61 changes: 61 additions & 0 deletions src/main/java/g3401_3500/s3451_find_invalid_ip_addresses/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
3451\. Find Invalid IP Addresses

Hard

Table: `logs`

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| log_id | int |
| ip | varchar |
| status_code | int |
+-------------+---------+
log_id is the unique key for this table.
Each row contains server access log information including IP address and HTTP status code.

Write a solution to find **invalid IP addresses**. An IPv4 address is invalid if it meets any of these conditions:

* Contains numbers **greater than** `255` in any octet
* Has **leading zeros** in any octet (like `01.02.03.04`)
* Has **less or more** than `4` octets

Return _the result table_ _ordered by_ `invalid_count`, `ip` _in **descending** order respectively_.

The result format is in the following example.

**Example:**

**Input:**

logs table:

+--------+---------------+-------------+
| log_id | ip | status_code |
+--------+---------------+-------------+
| 1 | 192.168.1.1 | 200 |
| 2 | 256.1.2.3 | 404 |
| 3 | 192.168.001.1 | 200 |
| 4 | 192.168.1.1 | 200 |
| 5 | 192.168.1 | 500 |
| 6 | 256.1.2.3 | 404 |
| 7 | 192.168.001.1 | 200 |
+--------+---------------+-------------+

**Output:**

+---------------+--------------+
| ip | invalid_count|
+---------------+--------------+
| 256.1.2.3 | 2 |
| 192.168.001.1 | 2 |
| 192.168.1 | 1 |
+---------------+--------------+

**Explanation:**

* 256.1.2.3 is invalid because 256 > 255
* 192.168.001.1 is invalid because of leading zeros
* 192.168.1 is invalid because it has only 3 octets

The output table is ordered by invalid\_count, ip in descending order respectively.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Write your MySQL query statement below
# #Hard #Database #2025_02_18_Time_393_ms_(79.56%)_Space_0.0_MB_(100.00%)
WITH cte_invalid_ip AS (
SELECT log_id, ip
FROM logs
WHERE NOT regexp_like(ip, '^(?:[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(?:[.](?:[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$')
),
cte_invalid_ip_count AS (
SELECT ip, count(log_id) as invalid_count
FROM cte_invalid_ip
GROUP BY ip
)
SELECT ip, invalid_count
FROM cte_invalid_ip_count
ORDER BY invalid_count DESC, ip DESC;
20 changes: 20 additions & 0 deletions src/main/java/g3401_3500/s3452_sum_of_good_numbers/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package g3401_3500.s3452_sum_of_good_numbers;

// #Easy #Array #2025_02_18_Time_1_ms_(99.99%)_Space_44.75_MB_(7.31%)

public class Solution {
public int sumOfGoodNumbers(int[] nums, int k) {
int totalSum = 0;
int n = nums.length;
for (int i = 0; i < n; i++) {
boolean isGood = i - k < 0 || nums[i] > nums[i - k];
if (i + k < n && nums[i] <= nums[i + k]) {
isGood = false;
}
if (isGood) {
totalSum += nums[i];
}
}
return totalSum;
}
}
33 changes: 33 additions & 0 deletions src/main/java/g3401_3500/s3452_sum_of_good_numbers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
3452\. Sum of Good Numbers

Easy

Given an array of integers `nums` and an integer `k`, an element `nums[i]` is considered **good** if it is **strictly** greater than the elements at indices `i - k` and `i + k` (if those indices exist). If neither of these indices _exists_, `nums[i]` is still considered **good**.

Return the **sum** of all the **good** elements in the array.

**Example 1:**

**Input:** nums = [1,3,2,1,5,4], k = 2

**Output:** 12

**Explanation:**

The good numbers are `nums[1] = 3`, `nums[4] = 5`, and `nums[5] = 4` because they are strictly greater than the numbers at indices `i - k` and `i + k`.

**Example 2:**

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

**Output:** 2

**Explanation:**

The only good number is `nums[0] = 2` because it is strictly greater than `nums[1]`.

**Constraints:**

* `2 <= nums.length <= 100`
* `1 <= nums[i] <= 1000`
* `1 <= k <= floor(nums.length / 2)`
34 changes: 34 additions & 0 deletions src/main/java/g3401_3500/s3453_separate_squares_i/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package g3401_3500.s3453_separate_squares_i;

// #Medium #Array #Binary_Search #2025_02_18_Time_60_ms_(99.96%)_Space_88.58_MB_(26.92%)

public class Solution {
public double separateSquares(int[][] squares) {
long hi = 0L;
long lo = 1_000_000_000L;
for (int[] q : squares) {
lo = Math.min(lo, q[1]);
hi = Math.max(hi, q[1] + (long) q[2]);
}
while (lo <= hi) {
long mid = (lo + hi) / 2;
if (diff(mid, squares) <= 0) {
hi = mid - 1;
} else {
lo = mid + 1;
}
}
double diff1 = diff(hi, squares);
double diff2 = diff(lo, squares);
return hi + diff1 / (diff1 - diff2);
}

private double diff(long mid, int[][] squares) {
double[] res = new double[2];
for (int[] q : squares) {
res[0] += Math.min(q[2], Math.max(0, mid - q[1])) * q[2];
res[1] += Math.min(q[2], Math.max(0, q[1] + q[2] - mid)) * q[2];
}
return res[1] - res[0];
}
}
48 changes: 48 additions & 0 deletions src/main/java/g3401_3500/s3453_separate_squares_i/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
3453\. Separate Squares I

Medium

You are given a 2D integer array `squares`. Each <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code> represents the coordinates of the bottom-left point and the side length of a square parallel to the x-axis.

Find the **minimum** y-coordinate value of a horizontal line such that the total area of the squares above the line _equals_ the total area of the squares below the line.

Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.

**Note**: Squares **may** overlap. Overlapping areas should be counted **multiple times**.

**Example 1:**

**Input:** squares = [[0,0,1],[2,2,1]]

**Output:** 1.00000

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/01/06/4062example1drawio.png)

Any horizontal line between `y = 1` and `y = 2` will have 1 square unit above it and 1 square unit below it. The lowest option is 1.

**Example 2:**

**Input:** squares = [[0,0,2],[1,1,1]]

**Output:** 1.16667

**Explanation:**

![](https://assets.leetcode.com/uploads/2025/01/15/4062example2drawio.png)

The areas are:

* Below the line: `7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5`.
* Above the line: `5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5`.

Since the areas above and below the line are equal, the output is `7/6 = 1.16667`.

**Constraints:**

* <code>1 <= squares.length <= 5 * 10<sup>4</sup></code>
* <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code>
* `squares[i].length == 3`
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code>
* <code>1 <= l<sub>i</sub> <= 10<sup>9</sup></code>
155 changes: 155 additions & 0 deletions src/main/java/g3401_3500/s3454_separate_squares_ii/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package g3401_3500.s3454_separate_squares_ii;

// #Hard #Array #Binary_Search #Segment_Tree #Line_Sweep
// #2025_02_18_Time_156_ms_(80.18%)_Space_79.96_MB_(64.32%)

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

@SuppressWarnings("java:S1210")
public class Solution {
private static class Event implements Comparable<Event> {
double y;
int x1;
int x2;
int type;

Event(double y, int x1, int x2, int type) {
this.y = y;
this.x1 = x1;
this.x2 = x2;
this.type = type;
}

public int compareTo(Event other) {
return Double.compare(this.y, other.y);
}
}

private static class Segment {
double y1;
double y2;
double unionX;
double cumArea;

Segment(double y1, double y2, double unionX, double cumArea) {
this.y1 = y1;
this.y2 = y2;
this.unionX = unionX;
this.cumArea = cumArea;
}
}

private static class SegmentTree {
int[] count;
double[] len;
int n;
int[] x;

SegmentTree(int[] x) {
this.x = x;
n = x.length - 1;
count = new int[4 * n];
len = new double[4 * n];
}

void update(int idx, int l, int r, int ql, int qr, int val) {
if (qr < l || ql > r) {
return;
}
if (ql <= l && r <= qr) {
count[idx] += val;
} else {
int mid = (l + r) / 2;
update(2 * idx + 1, l, mid, ql, qr, val);
update(2 * idx + 2, mid + 1, r, ql, qr, val);
}
if (count[idx] > 0) {
len[idx] = x[r + 1] - (double) x[l];
} else {
if (l == r) {
len[idx] = 0;
} else {
len[idx] = len[2 * idx + 1] + len[2 * idx + 2];
}
}
}

void update(int ql, int qr, int val) {
update(0, 0, n - 1, ql, qr, val);
}

double query() {
return len[0];
}
}

public double separateSquares(int[][] squares) {
int n = squares.length;
Event[] events = new Event[2 * n];
int idx = 0;
List<Integer> xList = new ArrayList<>();
for (int[] s : squares) {
int x = s[0];
int y = s[1];
int l = s[2];
int x2 = x + l;
int y2 = y + l;
events[idx++] = new Event(y, x, x2, 1);
events[idx++] = new Event(y2, x, x2, -1);
xList.add(x);
xList.add(x2);
}
Arrays.sort(events);
int m = xList.size();
int[] xCords = new int[m];
for (int i = 0; i < m; i++) {
xCords[i] = xList.get(i);
}
Arrays.sort(xCords);
int uniqueCount = 0;
for (int i = 0; i < m; i++) {
if (i == 0 || xCords[i] != xCords[i - 1]) {
xCords[uniqueCount++] = xCords[i];
}
}
int[] x = Arrays.copyOf(xCords, uniqueCount);
SegmentTree segTree = new SegmentTree(x);
List<Segment> segments = new ArrayList<>();
double cumArea = 0.0;
double lastY = events[0].y;
int iEvent = 0;
while (iEvent < events.length) {
double currentY = events[iEvent].y;
double delta = currentY - lastY;
if (delta > 0) {
double unionX = segTree.query();
segments.add(new Segment(lastY, currentY, unionX, cumArea));
cumArea += unionX * delta;
}
while (iEvent < events.length && events[iEvent].y == currentY) {
Event e = events[iEvent];
int left = Arrays.binarySearch(x, e.x1);
int right = Arrays.binarySearch(x, e.x2);
if (left < right) {
segTree.update(left, right - 1, e.type);
}
iEvent++;
}
lastY = currentY;
}
double totalArea = cumArea;
double target = totalArea / 2.0;
double answer;
for (Segment seg : segments) {
double segArea = seg.unionX * (seg.y2 - seg.y1);
if (seg.cumArea + segArea >= target) {
double needed = target - seg.cumArea;
answer = seg.y1 + needed / seg.unionX;
return answer;
}
}
return lastY;
}
}
Loading
Loading