Skip to content

Commit d58787e

Browse files
committed
Added tasks 3612-3615
1 parent 8d835a6 commit d58787e

File tree

8 files changed

+591
-68
lines changed
  • src/main/kotlin
    • g0001_0100
    • g3601_3700
      • s3612_process_string_with_special_operations_i
      • s3613_minimize_maximum_component_cost
      • s3614_process_string_with_special_operations_ii
      • s3615_longest_palindromic_path_in_graph

8 files changed

+591
-68
lines changed

README.md

Lines changed: 26 additions & 22 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0001_0100/s0001_two_sum/readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ You can return the answer in any order.
4545
```kotlin
4646
class Solution {
4747
fun twoSum(numbers: IntArray, target: Int): IntArray {
48-
val indexMap = HashMap<Int, Int>()
48+
val indexMap: MutableMap<Int, Int> = HashMap()
4949
for (i in numbers.indices) {
5050
val requiredNum = target - numbers[i]
5151
if (indexMap.containsKey(requiredNum)) {

src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/readme.md

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,27 @@ Given a string `s`, find the length of the **longest substring** without repeati
4747
```kotlin
4848
class Solution {
4949
fun lengthOfLongestSubstring(s: String): Int {
50-
val lastIndices = IntArray(256) { -1 }
51-
var maxLen = 0
52-
var curLen = 0
53-
var start = 0
54-
for (i in s.indices) {
55-
val cur = s[i]
56-
if (lastIndices[cur.code] < start) {
57-
lastIndices[cur.code] = i
58-
curLen++
50+
var i = 0
51+
var j = 0
52+
var longest = 0
53+
// 1. if string empty, return 0
54+
if (s.isEmpty()) {
55+
return 0
56+
}
57+
while (j < s.length) {
58+
// 2. if the char at index j already seen, update the longest if needs
59+
if (i != j && s.substring(i, j).indexOf(s[j]) > -1) {
60+
longest = Math.max(j - i, longest)
61+
i++
5962
} else {
60-
val lastIndex = lastIndices[cur.code]
61-
start = lastIndex + 1
62-
curLen = i - start + 1
63-
lastIndices[cur.code] = i
64-
}
65-
if (curLen > maxLen) {
66-
maxLen = curLen
63+
// 3. j out of bound already, update longest
64+
if (++j == s.length) {
65+
longest = Math.max(s.length - i, longest)
66+
break
67+
}
6768
}
6869
}
69-
return maxLen
70+
return longest
7071
}
7172
}
7273
```

src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/readme.md

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,24 @@ The overall run time complexity should be `O(log (m+n))`.
5555
## Solution
5656

5757
```kotlin
58-
import kotlin.math.max
59-
import kotlin.math.min
60-
6158
class Solution {
6259
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
63-
if (nums2.size < nums1.size) {
64-
return findMedianSortedArrays(nums2, nums1)
60+
val l: MutableList<Int> = ArrayList()
61+
val f: Double
62+
for (j in nums1) {
63+
l.add(j)
64+
}
65+
for (i in nums2) {
66+
l.add(i)
6567
}
66-
val n1 = nums1.size
67-
val n2 = nums2.size
68-
var low = 0
69-
var high = n1
70-
while (low <= high) {
71-
val cut1 = (low + high) / 2
72-
val cut2 = ((n1 + n2 + 1) / 2) - cut1
73-
val l1 = if (cut1 == 0) Int.MIN_VALUE else nums1[cut1 - 1]
74-
val l2 = if (cut2 == 0) Int.MIN_VALUE else nums2[cut2 - 1]
75-
val r1 = if (cut1 == n1) Int.MAX_VALUE else nums1[cut1]
76-
val r2 = if (cut2 == n2) Int.MAX_VALUE else nums2[cut2]
77-
if (l1 <= r2 && l2 <= r1) {
78-
return if ((n1 + n2) % 2 == 0) {
79-
(max(l1, l2).toDouble() + min(r1, r2).toDouble()) / 2.0
80-
} else {
81-
max(l1, l2).toDouble()
82-
}
83-
} else if (l1 > r2) {
84-
high = cut1 - 1
85-
} else {
86-
low = cut1 + 1
87-
}
68+
l.sort()
69+
val k = l.size
70+
f = if (k % 2 == 0) {
71+
(l[k / 2 - 1] + l[k / 2]).toDouble() / 2
72+
} else {
73+
l[(k + 1) / 2 - 1].toDouble()
8874
}
89-
return 0.0
75+
return f
9076
}
9177
}
9278
```
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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+
## 3612\. Process String with Special Operations I
5+
6+
Medium
7+
8+
You are given a string `s` consisting of lowercase English letters and the special characters: `*`, `#`, and `%`.
9+
10+
Build a new string `result` by processing `s` according to the following rules from left to right:
11+
12+
* If the letter is a **lowercase** English letter append it to `result`.
13+
* A `'*'` **removes** the last character from `result`, if it exists.
14+
* A `'#'` **duplicates** the current `result` and **appends** it to itself.
15+
* A `'%'` **reverses** the current `result`.
16+
17+
Return the final string `result` after processing all characters in `s`.
18+
19+
**Example 1:**
20+
21+
**Input:** s = "a#b%\*"
22+
23+
**Output:** "ba"
24+
25+
**Explanation:**
26+
27+
`i`
28+
29+
`s[i]`
30+
31+
Operation
32+
33+
Current `result`
34+
35+
0
36+
37+
`'a'`
38+
39+
Append `'a'`
40+
41+
`"a"`
42+
43+
1
44+
45+
`'#'`
46+
47+
Duplicate `result`
48+
49+
`"aa"`
50+
51+
2
52+
53+
`'b'`
54+
55+
Append `'b'`
56+
57+
`"aab"`
58+
59+
3
60+
61+
`'%'`
62+
63+
Reverse `result`
64+
65+
`"baa"`
66+
67+
4
68+
69+
`'*'`
70+
71+
Remove the last character
72+
73+
`"ba"`
74+
75+
Thus, the final `result` is `"ba"`.
76+
77+
**Example 2:**
78+
79+
**Input:** s = "z\*#"
80+
81+
**Output:** ""
82+
83+
**Explanation:**
84+
85+
`i`
86+
87+
`s[i]`
88+
89+
Operation
90+
91+
Current `result`
92+
93+
0
94+
95+
`'z'`
96+
97+
Append `'z'`
98+
99+
`"z"`
100+
101+
1
102+
103+
`'*'`
104+
105+
Remove the last character
106+
107+
`""`
108+
109+
2
110+
111+
`'#'`
112+
113+
Duplicate the string
114+
115+
`""`
116+
117+
Thus, the final `result` is `""`.
118+
119+
**Constraints:**
120+
121+
* `1 <= s.length <= 20`
122+
* `s` consists of only lowercase English letters and special characters `*`, `#`, and `%`.
123+
124+
## Solution
125+
126+
```kotlin
127+
class Solution {
128+
fun processStr(s: String): String {
129+
val res = StringBuilder()
130+
for (c in s.toCharArray()) {
131+
if (c != '*' && c != '#' && c != '%') {
132+
res.append(c)
133+
} else if (c == '#') {
134+
res.append(res)
135+
} else if (c == '%') {
136+
res.reverse()
137+
} else {
138+
if (res.isNotEmpty()) {
139+
res.deleteCharAt(res.length - 1)
140+
}
141+
}
142+
}
143+
return res.toString()
144+
}
145+
}
146+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
## 3613\. Minimize Maximum Component Cost
5+
6+
Medium
7+
8+
You are given an undirected connected graph with `n` nodes labeled from 0 to `n - 1` and a 2D integer array `edges` where <code>edges[i] = [u<sub>i</sub>, v<sub>i</sub>, w<sub>i</sub>]</code> denotes an undirected edge between node <code>u<sub>i</sub></code> and node <code>v<sub>i</sub></code> with weight <code>w<sub>i</sub></code>, and an integer `k`.
9+
10+
You are allowed to remove any number of edges from the graph such that the resulting graph has **at most** `k` connected components.
11+
12+
The **cost** of a component is defined as the **maximum** edge weight in that component. If a component has no edges, its cost is 0.
13+
14+
Return the **minimum** possible value of the **maximum** cost among all components **after such removals**.
15+
16+
**Example 1:**
17+
18+
**Input:** n = 5, edges = \[\[0,1,4],[1,2,3],[1,3,2],[3,4,6]], k = 2
19+
20+
**Output:** 4
21+
22+
**Explanation:**
23+
24+
![](https://assets.leetcode.com/uploads/2025/04/19/minimizemaximumm.jpg)
25+
26+
* Remove the edge between nodes 3 and 4 (weight 6).
27+
* The resulting components have costs of 0 and 4, so the overall maximum cost is 4.
28+
29+
**Example 2:**
30+
31+
**Input:** n = 4, edges = \[\[0,1,5],[1,2,5],[2,3,5]], k = 1
32+
33+
**Output:** 5
34+
35+
**Explanation:**
36+
37+
![](https://assets.leetcode.com/uploads/2025/04/19/minmax2.jpg)
38+
39+
* No edge can be removed, since allowing only one component (`k = 1`) requires the graph to stay fully connected.
40+
* That single component’s cost equals its largest edge weight, which is 5.
41+
42+
**Constraints:**
43+
44+
* <code>1 <= n <= 5 * 10<sup>4</sup></code>
45+
* <code>0 <= edges.length <= 10<sup>5</sup></code>
46+
* `edges[i].length == 3`
47+
* <code>0 <= u<sub>i</sub>, v<sub>i</sub> < n</code>
48+
* <code>1 <= w<sub>i</sub> <= 10<sup>6</sup></code>
49+
* `1 <= k <= n`
50+
* The input graph is connected.
51+
52+
## Solution
53+
54+
```kotlin
55+
import kotlin.math.max
56+
57+
class Solution {
58+
fun minCost(ui: Int, pl: Array<IntArray>, zx: Int): Int {
59+
var rt = 0
60+
var gh = 0
61+
var i = 0
62+
while (i < pl.size) {
63+
gh = max(gh, pl[i][2])
64+
i++
65+
}
66+
while (rt < gh) {
67+
val ty = rt + (gh - rt) / 2
68+
if (dfgh(ui, pl, ty, zx)) {
69+
gh = ty
70+
} else {
71+
rt = ty + 1
72+
}
73+
}
74+
return rt
75+
}
76+
77+
private fun dfgh(ui: Int, pl: Array<IntArray>, jk: Int, zx: Int): Boolean {
78+
val wt = IntArray(ui)
79+
var i = 0
80+
while (i < ui) {
81+
wt[i] = i
82+
i++
83+
}
84+
var er = ui
85+
i = 0
86+
while (i < pl.size) {
87+
val df = pl[i]
88+
if (df[2] > jk) {
89+
i++
90+
continue
91+
}
92+
val u = cvb(wt, df[0])
93+
val v = cvb(wt, df[1])
94+
if (u != v) {
95+
wt[u] = v
96+
er--
97+
}
98+
i++
99+
}
100+
return er <= zx
101+
}
102+
103+
private fun cvb(wt: IntArray, i: Int): Int {
104+
var i = i
105+
while (wt[i] != i) {
106+
wt[i] = wt[wt[i]]
107+
i = wt[i]
108+
}
109+
return i
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)