Skip to content

Commit 78877f5

Browse files
committed
Added tasks 3601-3609
1 parent 1729b0b commit 78877f5

File tree

11 files changed

+1215
-80
lines changed
  • src/main/kotlin
    • g0101_0200/s0138_copy_list_with_random_pointer
    • g3601_3700
      • s3601_find_drivers_with_improved_fuel_efficiency
      • s3602_hexadecimal_and_hexatrigesimal_conversion
      • s3603_minimum_cost_path_with_alternating_directions_ii
      • s3604_minimum_time_to_reach_destination_in_directed_graph
      • s3605_minimum_stability_factor_of_array
      • s3606_coupon_code_validator
      • s3607_power_grid_maintenance
      • s3608_minimum_time_for_k_connected_components
      • s3609_minimum_moves_to_reach_target_in_grid

11 files changed

+1215
-80
lines changed

README.md

Lines changed: 48 additions & 39 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0101_0200/s0138_copy_list_with_random_pointer/readme.md

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,49 +67,20 @@ import com_github_leetcode.random.Node
6767
*/
6868
class Solution {
6969
fun copyRandomList(head: Node?): Node? {
70-
if (head == null) {
71-
return null
70+
val hashMap: MutableMap<Node?, Node> = HashMap()
71+
var cur = head
72+
while (cur != null) {
73+
hashMap.put(cur, Node(cur.`val`))
74+
cur = cur.next
7275
}
73-
// first pass to have a clone node point to the next node. ie A->B becomes A->clonedNode->B
74-
var curr: Node? = head
75-
while (curr != null) {
76-
val clonedNode = Node(curr.`val`)
77-
clonedNode.next = curr.next
78-
curr.next = clonedNode
79-
curr = clonedNode.next
76+
cur = head
77+
while (cur != null) {
78+
val copy: Node = hashMap[cur]!!
79+
copy.next = hashMap[cur.next]
80+
copy.random = hashMap[cur.random]
81+
cur = cur.next
8082
}
81-
curr = head
82-
// second pass to make the cloned node's random pointer point to the orginal node's randome
83-
// pointer.
84-
// ie. A's random pointer becomes ClonedNode's random pointer
85-
while (curr != null) {
86-
if (curr.random != null) {
87-
curr.next?.random = curr.random!!.next
88-
} else {
89-
curr.next?.random = null
90-
}
91-
curr = curr.next?.next
92-
}
93-
curr = head
94-
// third pass to restore the links and return the head of the cloned nodes' list.
95-
var newHead: Node? = null
96-
while (curr != null) {
97-
var clonedNode: Node
98-
if (newHead == null) {
99-
clonedNode = curr.next!!
100-
newHead = clonedNode
101-
} else {
102-
clonedNode = curr.next!!
103-
}
104-
curr.next = clonedNode.next
105-
if (curr.next != null) {
106-
clonedNode.next = curr.next!!.next
107-
} else {
108-
clonedNode.next = null
109-
}
110-
curr = curr.next
111-
}
112-
return newHead
83+
return hashMap[head]
11384
}
11485
}
11586
```
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
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+
## 3601\. Find Drivers with Improved Fuel Efficiency
5+
6+
Medium
7+
8+
Table: `drivers`
9+
10+
+-------------+---------+
11+
| Column Name | Type |
12+
+-------------+---------+
13+
| driver_id | int |
14+
| driver_name | varchar |
15+
+-------------+---------+
16+
driver_id is the unique identifier for this table. Each row contains information about a driver.
17+
18+
Table: `trips`
19+
20+
+---------------+---------+
21+
| Column Name | Type |
22+
+---------------+---------+
23+
| trip_id | int |
24+
| driver_id | int |
25+
| trip_date | date |
26+
| distance_km | decimal |
27+
| fuel_consumed | decimal |
28+
+---------------+---------+
29+
trip_id is the unique identifier for this table.
30+
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
31+
32+
Write a solution to find drivers whose **fuel efficiency has improved** by **comparing** their average fuel efficiency in the **first half** of the year with the **second half** of the year.
33+
34+
* Calculate **fuel efficiency** as `distance_km / fuel_consumed` for **each** trip
35+
* **First half**: January to June, **Second half**: July to December
36+
* Only include drivers who have trips in **both halves** of the year
37+
* Calculate the **efficiency improvement** as (`second_half_avg - first_half_avg`)
38+
* **Round** all results to **`2`** decimal places
39+
40+
Return _the result table ordered by efficiency improvement in **descending** order, then by driver name in **ascending** order_.
41+
42+
The result format is in the following example.
43+
44+
**Example:**
45+
46+
**Input:**
47+
48+
drivers table:
49+
50+
+-----------+---------------+
51+
| driver_id | driver_name |
52+
+-----------+---------------+
53+
| 1 | Alice Johnson |
54+
| 2 | Bob Smith |
55+
| 3 | Carol Davis |
56+
| 4 | David Wilson |
57+
| 5 | Emma Brown |
58+
+-----------+---------------+
59+
60+
trips table:
61+
62+
+---------+-----------+------------+-------------+---------------+
63+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
64+
+---------+-----------+------------+-------------+---------------+
65+
| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |
66+
| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |
67+
| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |
68+
| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |
69+
| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |
70+
| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |
71+
| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |
72+
| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |
73+
| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |
74+
| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |
75+
| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |
76+
| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |
77+
+---------+-----------+------------+-------------+---------------+
78+
79+
**Output:**
80+
81+
+-----------+---------------+------------------+-------------------+------------------------+
82+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
83+
+-----------+---------------+------------------+-------------------+------------------------+
84+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
85+
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
86+
+-----------+---------------+------------------+-------------------+------------------------+
87+
88+
**Explanation:**
89+
90+
* **Alice Johnson (driver\_id = 1):**
91+
* First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)
92+
* First half average efficiency: (11.81 + 12.12) / 2 = 11.97
93+
* Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)
94+
* Second half average efficiency: (13.64 + 14.40) / 2 = 14.02
95+
* Efficiency improvement: 14.02 - 11.97 = 2.05
96+
* **Bob Smith (driver\_id = 2):**
97+
* First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)
98+
* First half average efficiency: (11.11 + 11.36) / 2 = 11.24
99+
* Second half trips: Oct 5 (200.0/15.0 = 13.33)
100+
* Second half average efficiency: 13.33
101+
* Efficiency improvement: 13.33 - 11.24 = 2.09
102+
* **Drivers not included:**
103+
* Carol Davis (driver\_id = 3): Only has trips in first half (Mar, May)
104+
* David Wilson (driver\_id = 4): Only has trips in second half (Jul, Nov)
105+
* Emma Brown (driver\_id = 5): Only has trips in first half (Feb)
106+
107+
The output table is ordered by efficiency improvement in descending order then by name in ascending order.
108+
109+
## Solution
110+
111+
```sql
112+
# Write your MySQL query statement below
113+
WITH main_process AS (
114+
SELECT
115+
t.driver_id,
116+
d.driver_name,
117+
ROUND(AVG(t.distance_km / t.fuel_consumed), 2) AS first_half_avg,
118+
ROUND(AVG(t1.distance_km / t1.fuel_consumed), 2) AS second_half_avg,
119+
ROUND(
120+
AVG(t1.distance_km / t1.fuel_consumed) - AVG(t.distance_km / t.fuel_consumed),
121+
2
122+
) AS efficiency_improvement
123+
FROM
124+
trips t
125+
INNER JOIN trips t1 ON t.driver_id = t1.driver_id
126+
INNER JOIN drivers d ON t.driver_id = d.driver_id
127+
AND EXTRACT(MONTH FROM t.trip_date) BETWEEN 1 AND 6
128+
AND EXTRACT(MONTH FROM t1.trip_date) BETWEEN 7 AND 12
129+
GROUP BY
130+
t.driver_id,
131+
d.driver_name
132+
ORDER BY
133+
efficiency_improvement DESC,
134+
d.driver_name ASC
135+
)
136+
SELECT
137+
driver_id,
138+
driver_name,
139+
first_half_avg,
140+
second_half_avg,
141+
efficiency_improvement
142+
FROM main_process
143+
WHERE efficiency_improvement > 0;
144+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
## 3602\. Hexadecimal and Hexatrigesimal Conversion
5+
6+
Easy
7+
8+
You are given an integer `n`.
9+
10+
Return the concatenation of the **hexadecimal** representation of <code>n<sup>2</sup></code> and the **hexatrigesimal** representation of <code>n<sup>3</sup></code>.
11+
12+
A **hexadecimal** number is defined as a base-16 numeral system that uses the digits `0 – 9` and the uppercase letters `A - F` to represent values from 0 to 15.
13+
14+
A **hexatrigesimal** number is defined as a base-36 numeral system that uses the digits `0 – 9` and the uppercase letters `A - Z` to represent values from 0 to 35.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 13
19+
20+
**Output:** "A91P1"
21+
22+
**Explanation:**
23+
24+
* <code>n<sup>2</sup> = 13 * 13 = 169</code>. In hexadecimal, it converts to `(10 * 16) + 9 = 169`, which corresponds to `"A9"`.
25+
* <code>n<sup>3</sup> = 13 * 13 * 13 = 2197</code>. In hexatrigesimal, it converts to <code>(1 * 36<sup>2</sup>) + (25 * 36) + 1 = 2197</code>, which corresponds to `"1P1"`.
26+
* Concatenating both results gives `"A9" + "1P1" = "A91P1"`.
27+
28+
**Example 2:**
29+
30+
**Input:** n = 36
31+
32+
**Output:** "5101000"
33+
34+
**Explanation:**
35+
36+
* <code>n<sup>2</sup> = 36 * 36 = 1296</code>. In hexadecimal, it converts to <code>(5 * 16<sup>2</sup>) + (1 * 16) + 0 = 1296</code>, which corresponds to `"510"`.
37+
* <code>n<sup>3</sup> = 36 * 36 * 36 = 46656</code>. In hexatrigesimal, it converts to <code>(1 * 36<sup>3</sup>) + (0 * 36<sup>2</sup>) + (0 * 36) + 0 = 46656</code>, which corresponds to `"1000"`.
38+
* Concatenating both results gives `"510" + "1000" = "5101000"`.
39+
40+
**Constraints:**
41+
42+
* `1 <= n <= 1000`
43+
44+
## Solution
45+
46+
```kotlin
47+
class Solution {
48+
fun concatHex36(n: Int): String {
49+
var t = n * n
50+
var k: Int
51+
val st = StringBuilder()
52+
var tmp = StringBuilder()
53+
while (t > 0) {
54+
k = t % 16
55+
t = t / 16
56+
if (k <= 9) {
57+
tmp.append(('0'.code + k).toChar())
58+
} else {
59+
tmp.append(('A'.code + (k - 10)).toChar())
60+
}
61+
}
62+
for (i in tmp.length - 1 downTo 0) {
63+
st.append(tmp[i])
64+
}
65+
tmp = StringBuilder()
66+
t = n * n * n
67+
while (t > 0) {
68+
k = t % 36
69+
t = t / 36
70+
if (k <= 9) {
71+
tmp.append(('0'.code + k).toChar())
72+
} else {
73+
tmp.append(('A'.code + (k - 10)).toChar())
74+
}
75+
}
76+
for (i in tmp.length - 1 downTo 0) {
77+
st.append(tmp[i])
78+
}
79+
return st.toString()
80+
}
81+
}
82+
```

0 commit comments

Comments
 (0)