Skip to content

Commit 2674eaa

Browse files
authored
Added tasks 3451-3459
1 parent 8bfb872 commit 2674eaa

File tree

27 files changed

+1309
-0
lines changed

27 files changed

+1309
-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 #Database #2025_02_18_Time_309_ms_(90.61%)_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,23 @@
1+
package g3401_3500.s3452_sum_of_good_numbers
2+
3+
// #Easy #Array #2025_02_18_Time_1_ms_(100.00%)_Space_38.08_MB_(84.85%)
4+
5+
class Solution {
6+
fun sumOfGoodNumbers(nums: IntArray, k: Int): Int {
7+
var totalSum = 0
8+
val n = nums.size
9+
for (i in 0..<n) {
10+
var isGood = true
11+
if (i - k >= 0 && nums[i] <= nums[i - k]) {
12+
isGood = false
13+
}
14+
if (i + k < n && nums[i] <= nums[i + k]) {
15+
isGood = false
16+
}
17+
if (isGood) {
18+
totalSum += nums[i]
19+
}
20+
}
21+
return totalSum
22+
}
23+
}
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,58 @@
1+
package g3401_3500.s3453_separate_squares_i
2+
3+
// #Medium #Array #Binary_Search #2025_02_18_Time_57_ms_(100.00%)_Space_102.84_MB_(84.85%)
4+
5+
class Solution {
6+
fun separateSquares(squares: Array<IntArray>): Double {
7+
val n = squares.size
8+
val arr = Array(n) { LongArray(3) }
9+
var total = 0.0
10+
var left = Long.MAX_VALUE
11+
var right = Long.MIN_VALUE
12+
for (i in 0..n - 1) {
13+
val y = squares[i][1].toLong()
14+
val z = squares[i][2].toLong()
15+
arr[i][0] = y
16+
arr[i][1] = y + z
17+
arr[i][2] = z
18+
total += (z * z).toDouble()
19+
left = minOf(left, arr[i][0])
20+
right = maxOf(right, arr[i][1])
21+
}
22+
while (left < right) {
23+
val mid = (left + right) / 2
24+
var low = 0.0
25+
for (a in arr) {
26+
if (a[0] >= mid) {
27+
continue
28+
} else if (a[1] <= mid) {
29+
low += a[2] * a[2]
30+
} else {
31+
low += a[2] * (mid - a[0])
32+
}
33+
}
34+
if (low + low + 0.00001 >= total) {
35+
right = mid
36+
} else {
37+
left = mid + 1
38+
}
39+
}
40+
left = right - 1
41+
var a1 = 0.0
42+
var a2 = 0.0
43+
for (a in arr) {
44+
val x = a[0]
45+
val y = a[1]
46+
val z = a[2]
47+
if (left > x) {
48+
a1 += (minOf(y, left) - x) * z.toDouble()
49+
}
50+
if (right < y) {
51+
a2 += (y - maxOf(x, right)) * z.toDouble()
52+
}
53+
}
54+
val goal = (total - a1 - a1) / 2
55+
val len = total - a1 - a2
56+
return right - 1 + (goal / len)
57+
}
58+
}
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)