Skip to content

Commit 0890b14

Browse files
committed
Added tasks 3451-3459
1 parent eb8b465 commit 0890b14

File tree

27 files changed

+1259
-0
lines changed

27 files changed

+1259
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
3451\. Find Invalid IP Addresses
2+
3+
Hard
4+
5+
Table: `logs`
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| log_id | int |
11+
| ip | varchar |
12+
| status_code | int |
13+
+-------------+---------+
14+
log_id is the unique key for this table.
15+
Each row contains server access log information including IP address and HTTP status code.
16+
17+
Write a solution to find **invalid IP addresses**. An IPv4 address is invalid if it meets any of these conditions:
18+
19+
* Contains numbers **greater than** `255` in any octet
20+
* Has **leading zeros** in any octet (like `01.02.03.04`)
21+
* Has **less or more** than `4` octets
22+
23+
Return _the result table_ _ordered by_ `invalid_count`, `ip` _in **descending** order respectively_.
24+
25+
The result format is in the following example.
26+
27+
**Example:**
28+
29+
**Input:**
30+
31+
logs table:
32+
33+
+--------+---------------+-------------+
34+
| log_id | ip | status_code |
35+
+--------+---------------+-------------+
36+
| 1 | 192.168.1.1 | 200 |
37+
| 2 | 256.1.2.3 | 404 |
38+
| 3 | 192.168.001.1 | 200 |
39+
| 4 | 192.168.1.1 | 200 |
40+
| 5 | 192.168.1 | 500 |
41+
| 6 | 256.1.2.3 | 404 |
42+
| 7 | 192.168.001.1 | 200 |
43+
+--------+---------------+-------------+
44+
45+
**Output:**
46+
47+
+---------------+--------------+
48+
| ip | invalid_count|
49+
+---------------+--------------+
50+
| 256.1.2.3 | 2 |
51+
| 192.168.001.1 | 2 |
52+
| 192.168.1 | 1 |
53+
+---------------+--------------+
54+
55+
**Explanation:**
56+
57+
* 256.1.2.3 is invalid because 256 > 255
58+
* 192.168.001.1 is invalid because of leading zeros
59+
* 192.168.1 is invalid because it has only 3 octets
60+
61+
The output table is ordered by invalid\_count, ip in descending order respectively.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Write your MySQL query statement below
2+
# #Hard #2025_02_16_Time_383_ms_(85.47%)_Space_0.0_MB_(100.00%)
3+
WITH cte_invalid_ip AS (
4+
SELECT log_id, ip
5+
FROM logs
6+
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}$')
7+
),
8+
cte_invalid_ip_count AS (
9+
SELECT ip, count(log_id) as invalid_count
10+
FROM cte_invalid_ip
11+
GROUP BY ip
12+
)
13+
SELECT ip, invalid_count
14+
FROM cte_invalid_ip_count
15+
ORDER BY invalid_count DESC, ip DESC;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package g3401_3500.s3452_sum_of_good_numbers;
2+
3+
// #Easy #2025_02_16_Time_1_ms_(100.00%)_Space_44.30_MB_(100.00%)
4+
5+
public class Solution {
6+
public int sumOfGoodNumbers(int[] nums, int k) {
7+
int totalSum = 0;
8+
int n = nums.length;
9+
for (int i = 0; i < n; i++) {
10+
boolean isGood = true;
11+
if (i - k >= 0) {
12+
if (nums[i] <= nums[i - k]) {
13+
isGood = false;
14+
}
15+
}
16+
if (i + k < n) {
17+
if (nums[i] <= nums[i + k]) {
18+
isGood = false;
19+
}
20+
}
21+
if (isGood) {
22+
totalSum += nums[i];
23+
}
24+
}
25+
return totalSum;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
3452\. Sum of Good Numbers
2+
3+
Easy
4+
5+
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**.
6+
7+
Return the **sum** of all the **good** elements in the array.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [1,3,2,1,5,4], k = 2
12+
13+
**Output:** 12
14+
15+
**Explanation:**
16+
17+
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`.
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [2,1], k = 1
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
The only good number is `nums[0] = 2` because it is strictly greater than `nums[1]`.
28+
29+
**Constraints:**
30+
31+
* `2 <= nums.length <= 100`
32+
* `1 <= nums[i] <= 1000`
33+
* `1 <= k <= floor(nums.length / 2)`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package g3401_3500.s3453_separate_squares_i;
2+
3+
// #Medium #2025_02_16_Time_123_ms_(100.00%)_Space_87.97_MB_(_%)
4+
5+
public class Solution {
6+
private double helper(double line, int[][] squares) {
7+
double aAbove = 0;
8+
double aBelow = 0;
9+
for (int[] square : squares) {
10+
int y = square[1];
11+
int l = square[2];
12+
double total = (double) l * l;
13+
if (line <= y) {
14+
aAbove += total;
15+
} else if (line >= y + l) {
16+
aBelow += total;
17+
} else {
18+
// The line intersects the square.
19+
double aboveHeight = (y + l) - line;
20+
double belowHeight = line - y;
21+
aAbove += l * aboveHeight;
22+
aBelow += l * belowHeight;
23+
}
24+
}
25+
return aAbove - aBelow;
26+
}
27+
28+
public double separateSquares(int[][] squares) {
29+
double lo = 0;
30+
double hi = 2 * 1e9;
31+
for (int i = 0; i < 60; i++) {
32+
double mid = (lo + hi) / 2.0;
33+
double diff = helper(mid, squares);
34+
if (diff > 0) {
35+
lo = mid;
36+
} else {
37+
hi = mid;
38+
}
39+
}
40+
return hi;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
3453\. Separate Squares I
2+
3+
Medium
4+
5+
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.
6+
7+
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.
8+
9+
Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
10+
11+
**Note**: Squares **may** overlap. Overlapping areas should be counted **multiple times**.
12+
13+
**Example 1:**
14+
15+
**Input:** squares = [[0,0,1],[2,2,1]]
16+
17+
**Output:** 1.00000
18+
19+
**Explanation:**
20+
21+
![](https://assets.leetcode.com/uploads/2025/01/06/4062example1drawio.png)
22+
23+
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.
24+
25+
**Example 2:**
26+
27+
**Input:** squares = [[0,0,2],[1,1,1]]
28+
29+
**Output:** 1.16667
30+
31+
**Explanation:**
32+
33+
![](https://assets.leetcode.com/uploads/2025/01/15/4062example2drawio.png)
34+
35+
The areas are:
36+
37+
* Below the line: `7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5`.
38+
* Above the line: `5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5`.
39+
40+
Since the areas above and below the line are equal, the output is `7/6 = 1.16667`.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= squares.length <= 5 * 10<sup>4</sup></code>
45+
* <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code>
46+
* `squares[i].length == 3`
47+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code>
48+
* <code>1 <= l<sub>i</sub> <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)