Skip to content

Commit baab228

Browse files
committed
Added tasks 3673-3677
1 parent e413ac3 commit baab228

File tree

6 files changed

+428
-0
lines changed

6 files changed

+428
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,11 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3677 |[Count Binary Palindromic Numbers](src/main/kotlin/g3601_3700/s3677_count_binary_palindromic_numbers)| Hard | Weekly_Contest_466 | 1 | 100.00
2092+
| 3676 |[Count Bowl Subarrays](src/main/kotlin/g3601_3700/s3676_count_bowl_subarrays)| Medium | Weekly_Contest_466 | 3 | 100.00
2093+
| 3675 |[Minimum Operations to Transform String](src/main/kotlin/g3601_3700/s3675_minimum_operations_to_transform_string)| Medium | Weekly_Contest_466 | 6 | 97.92
2094+
| 3674 |[Minimum Operations to Equalize Array](src/main/kotlin/g3601_3700/s3674_minimum_operations_to_equalize_array)| Easy | Weekly_Contest_466 | 1 | 100.00
2095+
| 3673 |[Find Zombie Sessions](src/main/kotlin/g3601_3700/s3673_find_zombie_sessions)| Hard | Database | 278 | 100.00
20912096
| 3671 |[Sum of Beautiful Subsequences](src/main/kotlin/g3601_3700/s3671_sum_of_beautiful_subsequences)| Hard | Weekly_Contest_465 | 225 | 100.00
20922097
| 3670 |[Maximum Product of Two Integers With No Common Bits](src/main/kotlin/g3601_3700/s3670_maximum_product_of_two_integers_with_no_common_bits)| Medium | Weekly_Contest_465 | 113 | 88.89
20932098
| 3669 |[Balanced K-Factor Decomposition](src/main/kotlin/g3601_3700/s3669_balanced_k_factor_decomposition)| Medium | Weekly_Contest_465 | 30 | 85.71
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
## 3673\. Find Zombie Sessions
5+
6+
Hard
7+
8+
Table: `app_events`
9+
10+
+------------------+----------+
11+
| Column Name | Type |
12+
+------------------+----------+
13+
| event_id | int |
14+
| user_id | int |
15+
| event_timestamp | datetime |
16+
| event_type | varchar |
17+
| session_id | varchar |
18+
| event_value | int |
19+
+------------------+----------+
20+
event_id is the unique identifier for this table.
21+
event_type can be app_open, click, scroll, purchase, or app_close.
22+
session_id groups events within the same user session.
23+
event_value represents: for purchase - amount in dollars, for scroll - pixels scrolled, for others - NULL.
24+
25+
Write a solution to identify **zombie sessions, **sessions where users appear active but show abnormal behavior patterns. A session is considered a **zombie session** if it meets ALL the following criteria:
26+
27+
* The session duration is **more than** `30` minutes.
28+
* Has **at least** `5` scroll events.
29+
* The **click-to-scroll ratio** is less than `0.20` .
30+
* **No purchases** were made during the session.
31+
32+
Return _the result table ordered by_ `scroll_count` _in **descending** order, then by_ `session_id` _in **ascending** order_.
33+
34+
The result format is in the following example.
35+
36+
**Example:**
37+
38+
**Input:**
39+
40+
app\_events table:
41+
42+
+----------+---------+---------------------+------------+------------+-------------+
43+
| event_id | user_id | event_timestamp | event_type | session_id | event_value |
44+
+----------+---------+---------------------+------------+------------+-------------+
45+
| 1 | 201 | 2024-03-01 10:00:00 | app_open | S001 | NULL |
46+
| 2 | 201 | 2024-03-01 10:05:00 | scroll | S001 | 500 |
47+
| 3 | 201 | 2024-03-01 10:10:00 | scroll | S001 | 750 |
48+
| 4 | 201 | 2024-03-01 10:15:00 | scroll | S001 | 600 |
49+
| 5 | 201 | 2024-03-01 10:20:00 | scroll | S001 | 800 |
50+
| 6 | 201 | 2024-03-01 10:25:00 | scroll | S001 | 550 |
51+
| 7 | 201 | 2024-03-01 10:30:00 | scroll | S001 | 900 |
52+
| 8 | 201 | 2024-03-01 10:35:00 | app_close | S001 | NULL |
53+
| 9 | 202 | 2024-03-01 11:00:00 | app_open | S002 | NULL |
54+
| 10 | 202 | 2024-03-01 11:02:00 | click | S002 | NULL |
55+
| 11 | 202 | 2024-03-01 11:05:00 | scroll | S002 | 400 |
56+
| 12 | 202 | 2024-03-01 11:08:00 | click | S002 | NULL |
57+
| 13 | 202 | 2024-03-01 11:10:00 | scroll | S002 | 350 |
58+
| 14 | 202 | 2024-03-01 11:15:00 | purchase | S002 | 50 |
59+
| 15 | 202 | 2024-03-01 11:20:00 | app_close | S002 | NULL |
60+
| 16 | 203 | 2024-03-01 12:00:00 | app_open | S003 | NULL |
61+
| 17 | 203 | 2024-03-01 12:10:00 | scroll | S003 | 1000 |
62+
| 18 | 203 | 2024-03-01 12:20:00 | scroll | S003 | 1200 |
63+
| 19 | 203 | 2024-03-01 12:25:00 | click | S003 | NULL |
64+
| 20 | 203 | 2024-03-01 12:30:00 | scroll | S003 | 800 |
65+
| 21 | 203 | 2024-03-01 12:40:00 | scroll | S003 | 900 |
66+
| 22 | 203 | 2024-03-01 12:50:00 | scroll | S003 | 1100 |
67+
| 23 | 203 | 2024-03-01 13:00:00 | app_close | S003 | NULL |
68+
| 24 | 204 | 2024-03-01 14:00:00 | app_open | S004 | NULL |
69+
| 25 | 204 | 2024-03-01 14:05:00 | scroll | S004 | 600 |
70+
| 26 | 204 | 2024-03-01 14:08:00 | scroll | S004 | 700 |
71+
| 27 | 204 | 2024-03-01 14:10:00 | click | S004 | NULL |
72+
| 28 | 204 | 2024-03-01 14:12:00 | app_close | S004 | NULL |
73+
+----------+---------+---------------------+------------+------------+-------------+
74+
75+
**Output:**
76+
77+
+------------+---------+--------------------------+--------------+
78+
| session_id | user_id | session_duration_minutes | scroll_count |
79+
+------------+---------+--------------------------+--------------+
80+
| S001 | 201 | 35 | 6 |
81+
+------------+---------+--------------------------+--------------+
82+
83+
**Explanation:**
84+
85+
* **Session S001 (User 201)**:
86+
* Duration: 10:00:00 to 10:35:00 = 35 minutes (more than 30)
87+
* Scroll events: 6 (at least 5)
88+
* Click events: 0
89+
* Click-to-scroll ratio: 0/6 = 0.00 (less than 0.20)
90+
* Purchases: 0 (no purchases)
91+
* S001 is a zombie session (meets all criteria)
92+
* **Session S002 (User 202)**:
93+
* Duration: 11:00:00 to 11:20:00 = 20 minutes (less than 30)
94+
* Has a purchase event
95+
* S002 is not a zombie session
96+
* **Session S003 (User 203)**:
97+
* Duration: 12:00:00 to 13:00:00 = 60 minutes (more than 30)
98+
* Scroll events: 5 (at least 5)
99+
* Click events: 1
100+
* Click-to-scroll ratio: 1/5 = 0.20 (not less than 0.20)
101+
* Purchases: 0 (no purchases)
102+
* S003 is not a zombie session (click-to-scroll ratio equals 0.20, needs to be less)
103+
* **Session S004 (User 204)**:
104+
* Duration: 14:00:00 to 14:12:00 = 12 minutes (less than 30)
105+
* Scroll events: 2 (less than 5)
106+
* S004 is not a zombie session
107+
108+
The result table is ordered by scroll\_count in descending order, then by session\_id in ascending order.
109+
110+
## Solution
111+
112+
```sql
113+
# Write your MySQL query statement below
114+
SELECT
115+
session_id,
116+
user_id,
117+
TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) AS session_duration_minutes,
118+
SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) AS scroll_count -- NOSONAR
119+
FROM
120+
app_events
121+
GROUP BY
122+
session_id,
123+
user_id
124+
HAVING
125+
TIMESTAMPDIFF(MINUTE, MIN(event_timestamp), MAX(event_timestamp)) > 30
126+
AND SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END) > 4 -- NOSONAR
127+
AND (
128+
CAST(SUM(CASE WHEN event_type = 'click' THEN 1 ELSE 0 END) AS DOUBLE) /
129+
NULLIF(SUM(CASE WHEN event_type = 'scroll' THEN 1 ELSE 0 END), 0) -- NOSONAR
130+
) < 0.2
131+
AND SUM(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) = 0
132+
ORDER BY
133+
scroll_count DESC,
134+
session_id ASC;
135+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
## 3674\. Minimum Operations to Equalize Array
5+
6+
Easy
7+
8+
You are given an integer array `nums` of length `n`.
9+
10+
In one operation, choose any subarray `nums[l...r]` (`0 <= l <= r < n`) and **replace** each element in that subarray with the **bitwise AND** of all elements.
11+
12+
Return the **minimum** number of operations required to make all elements of `nums` equal.
13+
14+
A **subarray** is a contiguous **non-empty** sequence of elements within an array.
15+
16+
**Example 1:**
17+
18+
**Input:** nums = [1,2]
19+
20+
**Output:** 1
21+
22+
**Explanation:**
23+
24+
Choose `nums[0...1]`: `(1 AND 2) = 0`, so the array becomes `[0, 0]` and all elements are equal in 1 operation.
25+
26+
**Example 2:**
27+
28+
**Input:** nums = [5,5,5]
29+
30+
**Output:** 0
31+
32+
**Explanation:**
33+
34+
`nums` is `[5, 5, 5]` which already has all elements equal, so 0 operations are required.
35+
36+
**Constraints:**
37+
38+
* `1 <= n == nums.length <= 100`
39+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>
40+
41+
## Solution
42+
43+
```kotlin
44+
class Solution {
45+
fun minOperations(nums: IntArray): Int {
46+
for (num in nums) {
47+
if (num != nums[0]) {
48+
return 1
49+
}
50+
}
51+
return 0
52+
}
53+
}
54+
```
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
## 3675\. Minimum Operations to Transform String
5+
6+
Medium
7+
8+
You are given a string `s` consisting only of lowercase English letters.
9+
10+
You can perform the following operation any number of times (including zero):
11+
12+
* Choose any character `c` in the string and replace **every** occurrence of `c` with the **next** lowercase letter in the English alphabet.
13+
14+
15+
Return the **minimum** number of operations required to transform `s` into a string consisting of **only** `'a'` characters.
16+
17+
**Note:** Consider the alphabet as circular, thus `'a'` comes after `'z'`.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "yz"
22+
23+
**Output:** 2
24+
25+
**Explanation:**
26+
27+
* Change `'y'` to `'z'` to get `"zz"`.
28+
* Change `'z'` to `'a'` to get `"aa"`.
29+
* Thus, the answer is 2.
30+
31+
**Example 2:**
32+
33+
**Input:** s = "a"
34+
35+
**Output:** 0
36+
37+
**Explanation:**
38+
39+
* The string `"a"` only consists of `'a'` characters. Thus, the answer is 0.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= s.length <= 5 * 10<sup>5</sup></code>
44+
* `s` consists only of lowercase English letters.
45+
46+
## Solution
47+
48+
```kotlin
49+
class Solution {
50+
fun minOperations(s: String): Int {
51+
val n = s.length
52+
var ans = 0
53+
for (i in 0..<n) {
54+
val c = s.get(i)
55+
if (c != 'a') {
56+
val ops = 'z'.code - c.code + 1
57+
if (ops > ans) {
58+
ans = ops
59+
}
60+
if (ops == 25) {
61+
break
62+
}
63+
}
64+
}
65+
return ans
66+
}
67+
}
68+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
## 3676\. Count Bowl Subarrays
5+
6+
Medium
7+
8+
You are given an integer array `nums` with **distinct** elements.
9+
10+
A subarray `nums[l...r]` of `nums` is called a **bowl** if:
11+
12+
* The subarray has length at least 3. That is, `r - l + 1 >= 3`.
13+
* The **minimum** of its two ends is **strictly greater** than the **maximum** of all elements in between. That is, `min(nums[l], nums[r]) > max(nums[l + 1], ..., nums[r - 1])`.
14+
15+
Return the number of **bowl** subarrays in `nums`.
16+
17+
**Example 1:**
18+
19+
**Input:** nums = [2,5,3,1,4]
20+
21+
**Output:** 2
22+
23+
**Explanation:**
24+
25+
The bowl subarrays are `[3, 1, 4]` and `[5, 3, 1, 4]`.
26+
27+
* `[3, 1, 4]` is a bowl because `min(3, 4) = 3 > max(1) = 1`.
28+
* `[5, 3, 1, 4]` is a bowl because `min(5, 4) = 4 > max(3, 1) = 3`.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [5,1,2,3,4]
33+
34+
**Output:** 3
35+
36+
**Explanation:**
37+
38+
The bowl subarrays are `[5, 1, 2]`, `[5, 1, 2, 3]` and `[5, 1, 2, 3, 4]`.
39+
40+
**Example 3:**
41+
42+
**Input:** nums = [1000000000,999999999,999999998]
43+
44+
**Output:** 0
45+
46+
**Explanation:**
47+
48+
No subarray is a bowl.
49+
50+
**Constraints:**
51+
52+
* <code>3 <= nums.length <= 10<sup>5</sup></code>
53+
* <code>1 <= nums[i] <= 10<sup>9</sup></code>
54+
* `nums` consists of distinct elements.
55+
56+
## Solution
57+
58+
```kotlin
59+
class Solution {
60+
fun bowlSubarrays(nums: IntArray): Long {
61+
val n = nums.size
62+
var res = n
63+
var pre = 0
64+
for (a in nums) {
65+
if (a > pre) {
66+
res--
67+
pre = a
68+
}
69+
}
70+
pre = 0
71+
for (i in n - 1 downTo 0) {
72+
if (nums[i] > pre) {
73+
res--
74+
pre = nums[i]
75+
}
76+
}
77+
return res + 1L
78+
}
79+
}
80+
```

0 commit comments

Comments
 (0)