Skip to content

Commit c2c8c01

Browse files
committed
Added tasks 3451-3459
1 parent ec9dcbd commit c2c8c01

File tree

11 files changed

+1030
-0
lines changed

11 files changed

+1030
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<head>
2+
<link rel="icon" type="image/x-icon" href="favicon.ico"/>
3+
</head>
4+
15
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
26
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
37
> ["For coding interview preparation, LeetCode is one of the best online resource providing a rich library of more than 300 real coding interview questions for you to practice from using one of the 7 supported languages - C, C++, Java, Python, C#, JavaScript, Ruby."](https://www.quora.com/How-effective-is-Leetcode-for-preparing-for-technical-interviews)
@@ -2084,6 +2088,15 @@
20842088

20852089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20862090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3459 |[Length of Longest V-Shaped Diagonal Segment](src/main/kotlin/g3401_3500/s3459_length_of_longest_v_shaped_diagonal_segment)| Hard | Array, Dynamic_Programming, Matrix, Memoization | 287 | 100.00
2092+
| 3458 |[Select K Disjoint Special Substrings](src/main/kotlin/g3401_3500/s3458_select_k_disjoint_special_substrings)| Medium | String, Hash_Table, Dynamic_Programming, Sorting, Greedy | 24 | 100.00
2093+
| 3457 |[Eat Pizzas!](src/main/kotlin/g3401_3500/s3457_eat_pizzas)| Medium | Array, Sorting, Greedy | 101 | 90.91
2094+
| 3456 |[Find Special Substring of Length K](src/main/kotlin/g3401_3500/s3456_find_special_substring_of_length_k)| Easy | String | 1 | 100.00
2095+
| 3455 |[Shortest Matching Substring](src/main/kotlin/g3401_3500/s3455_shortest_matching_substring)| Hard | String, Binary_Search, Two_Pointers, String_Matching | 100 | 100.00
2096+
| 3454 |[Separate Squares II](src/main/kotlin/g3401_3500/s3454_separate_squares_ii)| Hard | Array, Binary_Search, Segment_Tree, Line_Sweep | 277 | 100.00
2097+
| 3453 |[Separate Squares I](src/main/kotlin/g3401_3500/s3453_separate_squares_i)| Medium | Array, Binary_Search | 57 | 100.00
2098+
| 3452 |[Sum of Good Numbers](src/main/kotlin/g3401_3500/s3452_sum_of_good_numbers)| Easy | Array | 1 | 100.00
2099+
| 3451 |[Find Invalid IP Addresses](src/main/kotlin/g3401_3500/s3451_find_invalid_ip_addresses)| Hard | Database | 309 | 90.61
20872100
| 3449 |[Maximize the Minimum Game Score](src/main/kotlin/g3401_3500/s3449_maximize_the_minimum_game_score)| Hard | Array, Greedy, Binary_Search | 123 | 100.00
20882101
| 3448 |[Count Substrings Divisible By Last Digit](src/main/kotlin/g3401_3500/s3448_count_substrings_divisible_by_last_digit)| Hard | String, Dynamic_Programming | 28 | 77.78
20892102
| 3447 |[Assign Elements to Groups with Constraints](src/main/kotlin/g3401_3500/s3447_assign_elements_to_groups_with_constraints)| Medium | Array, Hash_Table | 24 | 100.00

favicon.ico

15 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3451\. Find Invalid IP Addresses
5+
6+
Hard
7+
8+
Table: `logs`
9+
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| log_id | int |
14+
| ip | varchar |
15+
| status_code | int |
16+
+-------------+---------+
17+
log_id is the unique key for this table.
18+
Each row contains server access log information including IP address and HTTP status code.
19+
20+
Write a solution to find **invalid IP addresses**. An IPv4 address is invalid if it meets any of these conditions:
21+
22+
* Contains numbers **greater than** `255` in any octet
23+
* Has **leading zeros** in any octet (like `01.02.03.04`)
24+
* Has **less or more** than `4` octets
25+
26+
Return _the result table_ _ordered by_ `invalid_count`, `ip` _in **descending** order respectively_.
27+
28+
The result format is in the following example.
29+
30+
**Example:**
31+
32+
**Input:**
33+
34+
logs table:
35+
36+
+--------+---------------+-------------+
37+
| log_id | ip | status_code |
38+
+--------+---------------+-------------+
39+
| 1 | 192.168.1.1 | 200 |
40+
| 2 | 256.1.2.3 | 404 |
41+
| 3 | 192.168.001.1 | 200 |
42+
| 4 | 192.168.1.1 | 200 |
43+
| 5 | 192.168.1 | 500 |
44+
| 6 | 256.1.2.3 | 404 |
45+
| 7 | 192.168.001.1 | 200 |
46+
+--------+---------------+-------------+
47+
48+
**Output:**
49+
50+
+---------------+--------------+
51+
| ip | invalid_count|
52+
+---------------+--------------+
53+
| 256.1.2.3 | 2 |
54+
| 192.168.001.1 | 2 |
55+
| 192.168.1 | 1 |
56+
+---------------+--------------+
57+
58+
**Explanation:**
59+
60+
* 256.1.2.3 is invalid because 256 > 255
61+
* 192.168.001.1 is invalid because of leading zeros
62+
* 192.168.1 is invalid because it has only 3 octets
63+
64+
The output table is ordered by invalid\_count, ip in descending order respectively.
65+
66+
## Solution
67+
68+
```sql
69+
# Write your MySQL query statement below
70+
WITH cte_invalid_ip AS (
71+
SELECT log_id, ip
72+
FROM logs
73+
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}$')
74+
),
75+
cte_invalid_ip_count AS (
76+
SELECT ip, count(log_id) as invalid_count
77+
FROM cte_invalid_ip
78+
GROUP BY ip
79+
)
80+
SELECT ip, invalid_count
81+
FROM cte_invalid_ip_count
82+
ORDER BY invalid_count DESC, ip DESC;
83+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3452\. Sum of Good Numbers
5+
6+
Easy
7+
8+
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**.
9+
10+
Return the **sum** of all the **good** elements in the array.
11+
12+
**Example 1:**
13+
14+
**Input:** nums = [1,3,2,1,5,4], k = 2
15+
16+
**Output:** 12
17+
18+
**Explanation:**
19+
20+
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`.
21+
22+
**Example 2:**
23+
24+
**Input:** nums = [2,1], k = 1
25+
26+
**Output:** 2
27+
28+
**Explanation:**
29+
30+
The only good number is `nums[0] = 2` because it is strictly greater than `nums[1]`.
31+
32+
**Constraints:**
33+
34+
* `2 <= nums.length <= 100`
35+
* `1 <= nums[i] <= 1000`
36+
* `1 <= k <= floor(nums.length / 2)`
37+
38+
## Solution
39+
40+
```kotlin
41+
class Solution {
42+
fun sumOfGoodNumbers(nums: IntArray, k: Int): Int {
43+
var totalSum = 0
44+
val n = nums.size
45+
for (i in 0..<n) {
46+
var isGood = true
47+
if (i - k >= 0 && nums[i] <= nums[i - k]) {
48+
isGood = false
49+
}
50+
if (i + k < n && nums[i] <= nums[i + k]) {
51+
isGood = false
52+
}
53+
if (isGood) {
54+
totalSum += nums[i]
55+
}
56+
}
57+
return totalSum
58+
}
59+
}
60+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)
3+
4+
## 3453\. Separate Squares I
5+
6+
Medium
7+
8+
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.
9+
10+
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.
11+
12+
Answers within <code>10<sup>-5</sup></code> of the actual answer will be accepted.
13+
14+
**Note**: Squares **may** overlap. Overlapping areas should be counted **multiple times**.
15+
16+
**Example 1:**
17+
18+
**Input:** squares = \[\[0,0,1],[2,2,1]]
19+
20+
**Output:** 1.00000
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2025/01/06/4062example1drawio.png)
25+
26+
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.
27+
28+
**Example 2:**
29+
30+
**Input:** squares = \[\[0,0,2],[1,1,1]]
31+
32+
**Output:** 1.16667
33+
34+
**Explanation:**
35+
36+
![](https://assets.leetcode.com/uploads/2025/01/15/4062example2drawio.png)
37+
38+
The areas are:
39+
40+
* Below the line: `7/6 * 2 (Red) + 1/6 (Blue) = 15/6 = 2.5`.
41+
* Above the line: `5/6 * 2 (Red) + 5/6 (Blue) = 15/6 = 2.5`.
42+
43+
Since the areas above and below the line are equal, the output is `7/6 = 1.16667`.
44+
45+
**Constraints:**
46+
47+
* <code>1 <= squares.length <= 5 * 10<sup>4</sup></code>
48+
* <code>squares[i] = [x<sub>i</sub>, y<sub>i</sub>, l<sub>i</sub>]</code>
49+
* `squares[i].length == 3`
50+
* <code>0 <= x<sub>i</sub>, y<sub>i</sub> <= 10<sup>9</sup></code>
51+
* <code>1 <= l<sub>i</sub> <= 10<sup>9</sup></code>
52+
53+
## Solution
54+
55+
```kotlin
56+
class Solution {
57+
fun separateSquares(squares: Array<IntArray>): Double {
58+
val n = squares.size
59+
val arr = Array(n) { LongArray(3) }
60+
var total = 0.0
61+
var left = Long.MAX_VALUE
62+
var right = Long.MIN_VALUE
63+
for (i in 0..n - 1) {
64+
val y = squares[i][1].toLong()
65+
val z = squares[i][2].toLong()
66+
arr[i][0] = y
67+
arr[i][1] = y + z
68+
arr[i][2] = z
69+
total += (z * z).toDouble()
70+
left = minOf(left, arr[i][0])
71+
right = maxOf(right, arr[i][1])
72+
}
73+
while (left < right) {
74+
val mid = (left + right) / 2
75+
var low = 0.0
76+
for (a in arr) {
77+
if (a[0] >= mid) {
78+
continue
79+
} else if (a[1] <= mid) {
80+
low += a[2] * a[2]
81+
} else {
82+
low += a[2] * (mid - a[0])
83+
}
84+
}
85+
if (low + low + 0.00001 >= total) {
86+
right = mid
87+
} else {
88+
left = mid + 1
89+
}
90+
}
91+
left = right - 1
92+
var a1 = 0.0
93+
var a2 = 0.0
94+
for (a in arr) {
95+
val x = a[0]
96+
val y = a[1]
97+
val z = a[2]
98+
if (left > x) {
99+
a1 += (minOf(y, left) - x) * z.toDouble()
100+
}
101+
if (right < y) {
102+
a2 += (y - maxOf(x, right)) * z.toDouble()
103+
}
104+
}
105+
val goal = (total - a1 - a1) / 2
106+
val len = total - a1 - a2
107+
return right - 1 + (goal / len)
108+
}
109+
}
110+
```

0 commit comments

Comments
 (0)