Skip to content

Commit 4720597

Browse files
committed
Added task 3497
1 parent f9a7e81 commit 4720597

File tree

8 files changed

+125
-11
lines changed
  • src/main/kotlin
    • g0801_0900/s0855_exam_room
    • g1101_1200/s1171_remove_zero_sum_consecutive_nodes_from_linked_list
    • g2801_2900/s2815_max_pair_sum_in_an_array
    • g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob
    • g3301_3400
      • s3321_find_x_sum_of_all_k_long_subarrays_ii
      • s3382_maximum_area_rectangle_with_point_constraints_ii
    • g3401_3500/s3497_analyze_subscription_conversion

8 files changed

+125
-11
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -2088,6 +2088,7 @@
20882088

20892089
| # | Title | Difficulty | Tag | Time, ms | Time, %
20902090
|------|----------------|-------------|-------------|----------|--------
2091+
| 3497 |[Analyze Subscription Conversion](src/main/kotlin/g3401_3500/s3497_analyze_subscription_conversion)| Medium | Database | 347 | 100.00
20912092
| 3495 |[Minimum Operations to Make Array Elements Zero](src/main/kotlin/g3401_3500/s3495_minimum_operations_to_make_array_elements_zero)| Hard | Array, Math, Bit_Manipulation | 12 | 100.00
20922093
| 3494 |[Find the Minimum Amount of Time to Brew Potions](src/main/kotlin/g3401_3500/s3494_find_the_minimum_amount_of_time_to_brew_potions)| Medium | Array, Simulation, Prefix_Sum | 70 | 100.00
20932094
| 3493 |[Properties Graph](src/main/kotlin/g3401_3500/s3493_properties_graph)| Medium | Array, Hash_Table, Depth_First_Search, Breadth_First_Search, Graph, Union_Find | 45 | 100.00

src/main/kotlin/g0801_0900/s0855_exam_room/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class ExamRoom() {
9090
var maxAtLeft: Node? = null
9191
var cur = tail.pre
9292
while (cur !== head && cur!!.pre !== head) {
93-
val pre = cur!!.pre
93+
val pre = cur.pre
9494
val at = (cur.`val` + pre!!.`val`) / 2
9595
val distance = at - pre.`val`
9696
if (distance >= max) {

src/main/kotlin/g1101_1200/s1171_remove_zero_sum_consecutive_nodes_from_linked_list/readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class Solution {
6565
curr = curr!!.next
6666
key += curr!!.`val`
6767
}
68-
map.getValue(preSum).next = curr!!.next
68+
map.getValue(preSum).next = curr.next
6969
} else {
7070
map[preSum] = curr
7171
}

src/main/kotlin/g2801_2900/s2815_max_pair_sum_in_an_array/readme.md

-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ It can be shown that there are no other pairs with equal maximum digits, so the
4040

4141
```kotlin
4242
import java.util.PriorityQueue
43-
import kotlin.collections.HashMap
4443
import kotlin.math.max
4544

4645
@Suppress("NAME_SHADOWING")

src/main/kotlin/g3201_3300/s3273_minimum_amount_of_damage_dealt_to_bob/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ class Solution {
7474
}
7575

7676
internal class Pair(var key: Int, var `val`: Int) : Comparable<Pair> {
77-
override fun compareTo(p: Pair): Int {
78-
return `val` * p.key - key * p.`val`
77+
override fun compareTo(other: Pair): Int {
78+
return `val` * other.key - key * other.`val`
7979
}
8080
}
8181
}

src/main/kotlin/g3301_3400/s3321_find_x_sum_of_all_k_long_subarrays_ii/readme.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ class Solution {
6262
cnt = c
6363
}
6464

65-
override fun compareTo(o: RC): Int {
66-
if (cnt != o.cnt) {
67-
return cnt - o.cnt
65+
override fun compareTo(other: RC): Int {
66+
if (cnt != other.cnt) {
67+
return cnt - other.cnt
6868
}
69-
return `val` - o.`val`
69+
return `val` - other.`val`
7070
}
7171
}
7272

src/main/kotlin/g3301_3400/s3382_maximum_area_rectangle_with_point_constraints_ii/readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ class Solution {
102102
}
103103

104104
private class Pair(val x: Int, val y: Int) : Comparable<Pair> {
105-
override fun compareTo(p: Pair): Int {
106-
return if (x == p.x) y - p.y else x - p.x
105+
override fun compareTo(other: Pair): Int {
106+
return if (x == other.x) y - other.y else x - other.x
107107
}
108108
}
109109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
## 3497\. Analyze Subscription Conversion
5+
6+
Medium
7+
8+
Table: `UserActivity`
9+
10+
+------------------+---------+
11+
| Column Name | Type |
12+
+------------------+---------+
13+
| user_id | int |
14+
| activity_date | date |
15+
| activity_type | varchar |
16+
| activity_duration| int |
17+
+------------------+---------+
18+
(user_id, activity_date, activity_type) is the unique key for this table. activity_type is one of ('free_trial', 'paid', 'cancelled').
19+
activity_duration is the number of minutes the user spent on the platform that day.
20+
Each row represents a user's activity on a specific date.
21+
22+
A subscription service wants to analyze user behavior patterns. The company offers a `7`\-day **free trial**, after which users can subscribe to a **paid plan** or **cancel**. Write a solution to:
23+
24+
1. Find users who converted from free trial to paid subscription
25+
2. Calculate each user's **average daily activity duration** during their **free trial** period (rounded to `2` decimal places)
26+
3. Calculate each user's **average daily activity duration** during their **paid** subscription period (rounded to `2` decimal places)
27+
28+
Return _the result table ordered by_ `user_id` _in **ascending** order_.
29+
30+
The result format is in the following example.
31+
32+
**Example:**
33+
34+
**Input:**
35+
36+
UserActivity table:
37+
38+
| user_id | activity_date | activity_type | activity_duration |
39+
|---------|---------------|---------------|-------------------|
40+
| 1 | 2023-01-01 | free_trial | 45 |
41+
| 1 | 2023-01-02 | free_trial | 30 |
42+
| 1 | 2023-01-05 | free_trial | 60 |
43+
| 1 | 2023-01-10 | paid | 75 |
44+
| 1 | 2023-01-12 | paid | 90 |
45+
| 1 | 2023-01-15 | paid | 65 |
46+
| 2 | 2023-02-01 | free_trial | 55 |
47+
| 2 | 2023-02-03 | free_trial | 25 |
48+
| 2 | 2023-02-07 | free_trial | 50 |
49+
| 2 | 2023-02-10 | cancelled | 0 |
50+
| 3 | 2023-03-05 | free_trial | 70 |
51+
| 3 | 2023-03-06 | free_trial | 60 |
52+
| 3 | 2023-03-08 | free_trial | 80 |
53+
| 3 | 2023-03-12 | paid | 50 |
54+
| 3 | 2023-03-15 | paid | 55 |
55+
| 3 | 2023-03-20 | paid | 85 |
56+
| 4 | 2023-04-01 | free_trial | 40 |
57+
| 4 | 2023-04-03 | free_trial | 35 |
58+
| 4 | 2023-04-05 | paid | 45 |
59+
| 4 | 2023-04-07 | cancelled | 0 |
60+
61+
**Output:**
62+
63+
| user_id | trial_avg_duration | paid_avg_duration |
64+
|---------|--------------------|-------------------|
65+
| 1 | 45.00 | 76.67 |
66+
| 3 | 70.00 | 63.33 |
67+
| 4 | 37.50 | 45.00 |
68+
69+
**Explanation:**
70+
71+
* **User 1:**
72+
* Had 3 days of free trial with durations of 45, 30, and 60 minutes.
73+
* Average trial duration: (45 + 30 + 60) / 3 = 45.00 minutes.
74+
* Had 3 days of paid subscription with durations of 75, 90, and 65 minutes.
75+
* Average paid duration: (75 + 90 + 65) / 3 = 76.67 minutes.
76+
* **User 2:**
77+
* Had 3 days of free trial with durations of 55, 25, and 50 minutes.
78+
* Average trial duration: (55 + 25 + 50) / 3 = 43.33 minutes.
79+
* Did not convert to a paid subscription (only had free\_trial and cancelled activities).
80+
* Not included in the output because they didn't convert to paid.
81+
* **User 3:**
82+
* Had 3 days of free trial with durations of 70, 60, and 80 minutes.
83+
* Average trial duration: (70 + 60 + 80) / 3 = 70.00 minutes.
84+
* Had 3 days of paid subscription with durations of 50, 55, and 85 minutes.
85+
* Average paid duration: (50 + 55 + 85) / 3 = 63.33 minutes.
86+
* **User 4:**
87+
* Had 2 days of free trial with durations of 40 and 35 minutes.
88+
* Average trial duration: (40 + 35) / 2 = 37.50 minutes.
89+
* Had 1 day of paid subscription with duration of 45 minutes before cancelling.
90+
* Average paid duration: 45.00 minutes.
91+
92+
The result table only includes users who converted from free trial to paid subscription (users 1, 3, and 4), and is ordered by user\_id in ascending order.
93+
94+
## Solution
95+
96+
```sql
97+
# Write your MySQL query statement below
98+
SELECT
99+
ft.user_id,
100+
ROUND(ft.avg_trial, 2) AS trial_avg_duration,
101+
ROUND(pt.avg_paid, 2) AS paid_avg_duration
102+
FROM
103+
(SELECT user_id, AVG(activity_duration) AS avg_trial
104+
FROM UserActivity
105+
WHERE activity_type = 'free_trial'
106+
GROUP BY user_id) ft
107+
JOIN
108+
(SELECT user_id, AVG(activity_duration) AS avg_paid
109+
FROM UserActivity
110+
WHERE activity_type = 'paid'
111+
GROUP BY user_id) pt
112+
ON ft.user_id = pt.user_id
113+
ORDER BY ft.user_id ASC;
114+
```

0 commit comments

Comments
 (0)