Skip to content

Commit 99dac72

Browse files
authored
Added tasks 3602-3609
1 parent 0145507 commit 99dac72

File tree

24 files changed

+1343
-0
lines changed

24 files changed

+1343
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package g3601_3700.s3602_hexadecimal_and_hexatrigesimal_conversion
2+
3+
// #Easy #String #Math #2025_07_07_Time_2_ms_(100.00%)_Space_41.91_MB_(100.00%)
4+
5+
class Solution {
6+
fun concatHex36(n: Int): String {
7+
var t = n * n
8+
var k: Int
9+
val st = StringBuilder()
10+
var tmp = StringBuilder()
11+
while (t > 0) {
12+
k = t % 16
13+
t = t / 16
14+
if (k <= 9) {
15+
tmp.append(('0'.code + k).toChar())
16+
} else {
17+
tmp.append(('A'.code + (k - 10)).toChar())
18+
}
19+
}
20+
for (i in tmp.length - 1 downTo 0) {
21+
st.append(tmp[i])
22+
}
23+
tmp = StringBuilder()
24+
t = n * n * n
25+
while (t > 0) {
26+
k = t % 36
27+
t = t / 36
28+
if (k <= 9) {
29+
tmp.append(('0'.code + k).toChar())
30+
} else {
31+
tmp.append(('A'.code + (k - 10)).toChar())
32+
}
33+
}
34+
for (i in tmp.length - 1 downTo 0) {
35+
st.append(tmp[i])
36+
}
37+
return st.toString()
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
3602\. Hexadecimal and Hexatrigesimal Conversion
2+
3+
Easy
4+
5+
You are given an integer `n`.
6+
7+
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>.
8+
9+
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.
10+
11+
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.
12+
13+
**Example 1:**
14+
15+
**Input:** n = 13
16+
17+
**Output:** "A91P1"
18+
19+
**Explanation:**
20+
21+
* <code>n<sup>2</sup> = 13 * 13 = 169</code>. In hexadecimal, it converts to `(10 * 16) + 9 = 169`, which corresponds to `"A9"`.
22+
* <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"`.
23+
* Concatenating both results gives `"A9" + "1P1" = "A91P1"`.
24+
25+
**Example 2:**
26+
27+
**Input:** n = 36
28+
29+
**Output:** "5101000"
30+
31+
**Explanation:**
32+
33+
* <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"`.
34+
* <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"`.
35+
* Concatenating both results gives `"510" + "1000" = "5101000"`.
36+
37+
**Constraints:**
38+
39+
* `1 <= n <= 1000`
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package g3601_3700.s3603_minimum_cost_path_with_alternating_directions_ii
2+
3+
// #Medium #Array #Dynamic_Programming #Matrix
4+
// #2025_07_07_Time_12_ms_(100.00%)_Space_84.57_MB_(100.00%)
5+
6+
import kotlin.math.min
7+
8+
class Solution {
9+
fun minCost(m: Int, n: Int, waitCost: Array<IntArray>): Long {
10+
val dp = LongArray(n)
11+
dp[0] = 1L
12+
for (j in 1..<n) {
13+
val entry = j + 1L
14+
val wait = waitCost[0][j].toLong()
15+
dp[j] = dp[j - 1] + entry + wait
16+
}
17+
for (i in 1..<m) {
18+
val entry00 = i + 1L
19+
val wait00 = waitCost[i][0].toLong()
20+
dp[0] = dp[0] + entry00 + wait00
21+
for (j in 1..<n) {
22+
val entry = (i + 1).toLong() * (j + 1)
23+
val wait = waitCost[i][j].toLong()
24+
val fromAbove = dp[j]
25+
val fromLeft = dp[j - 1]
26+
dp[j] = min(fromAbove, fromLeft) + entry + wait
27+
}
28+
}
29+
return dp[n - 1] - waitCost[m - 1][n - 1]
30+
}
31+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
3603\. Minimum Cost Path with Alternating Directions II
2+
3+
Medium
4+
5+
You are given two integers `m` and `n` representing the number of rows and columns of a grid, respectively.
6+
7+
The cost to enter cell `(i, j)` is defined as `(i + 1) * (j + 1)`.
8+
9+
You are also given a 2D integer array `waitCost` where `waitCost[i][j]` defines the cost to **wait** on that cell.
10+
11+
You start at cell `(0, 0)` at second 1.
12+
13+
At each step, you follow an alternating pattern:
14+
15+
* On **odd-numbered** seconds, you must move **right** or **down** to an **adjacent** cell, paying its entry cost.
16+
* On **even-numbered** seconds, you must **wait** in place, paying `waitCost[i][j]`.
17+
18+
Return the **minimum** total cost required to reach `(m - 1, n - 1)`.
19+
20+
**Example 1:**
21+
22+
**Input:** m = 1, n = 2, waitCost = [[1,2]]
23+
24+
**Output:** 3
25+
26+
**Explanation:**
27+
28+
The optimal path is:
29+
30+
* Start at cell `(0, 0)` at second 1 with entry cost `(0 + 1) * (0 + 1) = 1`.
31+
* **Second 1**: Move right to cell `(0, 1)` with entry cost `(0 + 1) * (1 + 1) = 2`.
32+
33+
Thus, the total cost is `1 + 2 = 3`.
34+
35+
**Example 2:**
36+
37+
**Input:** m = 2, n = 2, waitCost = [[3,5],[2,4]]
38+
39+
**Output:** 9
40+
41+
**Explanation:**
42+
43+
The optimal path is:
44+
45+
* Start at cell `(0, 0)` at second 1 with entry cost `(0 + 1) * (0 + 1) = 1`.
46+
* **Second 1**: Move down to cell `(1, 0)` with entry cost `(1 + 1) * (0 + 1) = 2`.
47+
* **Second 2**: Wait at cell `(1, 0)`, paying `waitCost[1][0] = 2`.
48+
* **Second 3**: Move right to cell `(1, 1)` with entry cost `(1 + 1) * (1 + 1) = 4`.
49+
50+
Thus, the total cost is `1 + 2 + 2 + 4 = 9`.
51+
52+
**Example 3:**
53+
54+
**Input:** m = 2, n = 3, waitCost = [[6,1,4],[3,2,5]]
55+
56+
**Output:** 16
57+
58+
**Explanation:**
59+
60+
The optimal path is:
61+
62+
* Start at cell `(0, 0)` at second 1 with entry cost `(0 + 1) * (0 + 1) = 1`.
63+
* **Second 1**: Move right to cell `(0, 1)` with entry cost `(0 + 1) * (1 + 1) = 2`.
64+
* **Second 2**: Wait at cell `(0, 1)`, paying `waitCost[0][1] = 1`.
65+
* **Second 3**: Move down to cell `(1, 1)` with entry cost `(1 + 1) * (1 + 1) = 4`.
66+
* **Second 4**: Wait at cell `(1, 1)`, paying `waitCost[1][1] = 2`.
67+
* **Second 5**: Move right to cell `(1, 2)` with entry cost `(1 + 1) * (2 + 1) = 6`.
68+
69+
Thus, the total cost is `1 + 2 + 1 + 4 + 2 + 6 = 16`.
70+
71+
**Constraints:**
72+
73+
* <code>1 <= m, n <= 10<sup>5</sup></code>
74+
* <code>2 <= m * n <= 10<sup>5</sup></code>
75+
* `waitCost.length == m`
76+
* `waitCost[0].length == n`
77+
* <code>0 <= waitCost[i][j] <= 10<sup>5</sup></code>
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package g3601_3700.s3604_minimum_time_to_reach_destination_in_directed_graph
2+
3+
// #Medium #Heap_Priority_Queue #Graph #Shortest_Path
4+
// #2025_07_07_Time_18_ms_(100.00%)_Space_132.61_MB_(100.00%)
5+
6+
import kotlin.math.max
7+
8+
class Solution {
9+
fun minTime(n: Int, edges: Array<IntArray>): Int {
10+
val head = IntArray(n)
11+
val to = IntArray(edges.size)
12+
val start = IntArray(edges.size)
13+
val end = IntArray(edges.size)
14+
val next = IntArray(edges.size)
15+
head.fill(-1)
16+
for (i in edges.indices) {
17+
val u = edges[i][0]
18+
to[i] = edges[i][1]
19+
start[i] = edges[i][2]
20+
end[i] = edges[i][3]
21+
next[i] = head[u]
22+
head[u] = i
23+
}
24+
val heap = IntArray(n)
25+
val time = IntArray(n)
26+
val pos = IntArray(n)
27+
val visited = BooleanArray(n)
28+
var size = 0
29+
for (i in 0..<n) {
30+
time[i] = INF
31+
pos[i] = -1
32+
}
33+
time[0] = 0
34+
heap[size] = 0
35+
pos[0] = 0
36+
size++
37+
while (size > 0) {
38+
val u = heap[0]
39+
heap[0] = heap[--size]
40+
pos[heap[0]] = 0
41+
heapifyDown(heap, time, pos, size, 0)
42+
if (visited[u]) {
43+
continue
44+
}
45+
visited[u] = true
46+
if (u == n - 1) {
47+
return time[u]
48+
}
49+
var e = head[u]
50+
while (e != -1) {
51+
val v = to[e]
52+
val t0 = time[u]
53+
if (t0 > end[e]) {
54+
e = next[e]
55+
continue
56+
}
57+
val arrival = max(t0, start[e]) + 1
58+
if (arrival < time[v]) {
59+
time[v] = arrival
60+
if (pos[v] == -1) {
61+
heap[size] = v
62+
pos[v] = size
63+
heapifyUp(heap, time, pos, size)
64+
size++
65+
} else {
66+
heapifyUp(heap, time, pos, pos[v])
67+
}
68+
}
69+
e = next[e]
70+
}
71+
}
72+
return -1
73+
}
74+
75+
private fun heapifyUp(heap: IntArray, time: IntArray, pos: IntArray, i: Int) {
76+
var i = i
77+
while (i > 0) {
78+
val p = (i - 1) / 2
79+
if (time[heap[p]] <= time[heap[i]]) {
80+
break
81+
}
82+
swap(heap, pos, i, p)
83+
i = p
84+
}
85+
}
86+
87+
private fun heapifyDown(heap: IntArray, time: IntArray, pos: IntArray, size: Int, i: Int) {
88+
var i = i
89+
while (2 * i + 1 < size) {
90+
var j = 2 * i + 1
91+
if (j + 1 < size && time[heap[j + 1]] < time[heap[j]]) {
92+
j++
93+
}
94+
if (time[heap[i]] <= time[heap[j]]) {
95+
break
96+
}
97+
swap(heap, pos, i, j)
98+
i = j
99+
}
100+
}
101+
102+
private fun swap(heap: IntArray, pos: IntArray, i: Int, j: Int) {
103+
val tmp = heap[i]
104+
heap[i] = heap[j]
105+
heap[j] = tmp
106+
pos[heap[i]] = i
107+
pos[heap[j]] = j
108+
}
109+
110+
companion object {
111+
private const val INF = Int.Companion.MAX_VALUE
112+
}
113+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
3604\. Minimum Time to Reach Destination in Directed Graph
2+
3+
Medium
4+
5+
You are given an integer `n` and a **directed** graph with `n` nodes labeled from 0 to `n - 1`. This is represented by a 2D array `edges`, where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, start<sub>i</sub>, end<sub>i</sub>]</code> indicates an edge from node <code>u<sub>i</sub></code> to <code>v<sub>i</sub></code> that can **only** be used at any integer time `t` such that <code>start<sub>i</sub> <= t <= end<sub>i</sub></code>.
6+
7+
You start at node 0 at time 0.
8+
9+
In one unit of time, you can either:
10+
11+
* Wait at your current node without moving, or
12+
* Travel along an outgoing edge from your current node if the current time `t` satisfies <code>start<sub>i</sub> <= t <= end<sub>i</sub></code>.
13+
14+
Return the **minimum** time required to reach node `n - 1`. If it is impossible, return `-1`.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 3, edges = [[0,1,0,1],[1,2,2,5]]
19+
20+
**Output:** 3
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2025/06/05/screenshot-2025-06-06-at-004535.png)
25+
26+
The optimal path is:
27+
28+
* At time `t = 0`, take the edge `(0 → 1)` which is available from 0 to 1. You arrive at node 1 at time `t = 1`, then wait until `t = 2`.
29+
* At time ```t = `2` ```, take the edge `(1 → 2)` which is available from 2 to 5. You arrive at node 2 at time 3.
30+
31+
Hence, the minimum time to reach node 2 is 3.
32+
33+
**Example 2:**
34+
35+
**Input:** n = 4, edges = [[0,1,0,3],[1,3,7,8],[0,2,1,5],[2,3,4,7]]
36+
37+
**Output:** 5
38+
39+
**Explanation:**
40+
41+
![](https://assets.leetcode.com/uploads/2025/06/05/screenshot-2025-06-06-at-004757.png)
42+
43+
The optimal path is:
44+
45+
* Wait at node 0 until time `t = 1`, then take the edge `(0 → 2)` which is available from 1 to 5. You arrive at node 2 at `t = 2`.
46+
* Wait at node 2 until time `t = 4`, then take the edge `(2 → 3)` which is available from 4 to 7. You arrive at node 3 at `t = 5`.
47+
48+
Hence, the minimum time to reach node 3 is 5.
49+
50+
**Example 3:**
51+
52+
**Input:** n = 3, edges = [[1,0,1,3],[1,2,3,5]]
53+
54+
**Output:** \-1
55+
56+
**Explanation:**
57+
58+
![](https://assets.leetcode.com/uploads/2025/06/05/screenshot-2025-06-06-at-004914.png)
59+
60+
* Since there is no outgoing edge from node 0, it is impossible to reach node 2. Hence, the output is -1.
61+
62+
**Constraints:**
63+
64+
* <code>1 <= n <= 10<sup>5</sup></code>
65+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
66+
* <code>edges[i] == [u<sub>i</sub>, v<sub>i</sub>, start<sub>i</sub>, end<sub>i</sub>]</code>
67+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> <= n - 1</code>
68+
* <code>u<sub>i</sub> != v<sub>i</sub></code>
69+
* <code>0 <= start<sub>i</sub> <= end<sub>i</sub> <= 10<sup>9</sup></code>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy