Skip to content

Commit 6d78997

Browse files
committed
Improved tasks 1-5
1 parent 2491b37 commit 6d78997

File tree

5 files changed

+52
-39
lines changed

5 files changed

+52
-39
lines changed

src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package g0001_0100.s0001_two_sum
33
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table
44
// #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Top_Interview_150_Hashmap
55
// #Big_O_Time_O(n)_Space_O(n) #AI_can_be_used_to_solve_the_task
6-
// #2023_07_03_Time_202_ms_(91.18%)_Space_38.1_MB_(76.07%)
6+
// #2025_07_11_Time_2_ms_(98.77%)_Space_48.00_MB_(52.59%)
77

88
class Solution {
99
fun twoSum(numbers: IntArray, target: Int): IntArray {
10-
val indexMap: MutableMap<Int, Int> = HashMap()
10+
val indexMap = HashMap<Int, Int>()
1111
for (i in numbers.indices) {
1212
val requiredNum = target - numbers[i]
1313
if (indexMap.containsKey(requiredNum)) {
14-
return intArrayOf(indexMap.getValue(requiredNum), i)
14+
return intArrayOf(indexMap[requiredNum]!!, i)
1515
}
1616
indexMap[numbers[i]] = i
1717
}

src/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package g0001_0100.s0002_add_two_numbers
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion
44
// #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15
55
// #Top_Interview_150_Linked_List #Big_O_Time_O(max(N,M))_Space_O(max(N,M))
6-
// #AI_can_be_used_to_solve_the_task #2023_07_03_Time_203_ms_(96.13%)_Space_41_MB_(77.03%)
6+
// #AI_can_be_used_to_solve_the_task #2025_07_11_Time_2_ms_(87.63%)_Space_45.71_MB_(80.15%)
77

88
import com_github_leetcode.ListNode
99

src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,29 @@ package g0001_0100.s0003_longest_substring_without_repeating_characters
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window
44
// #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings
55
// #Top_Interview_150_Sliding_Window #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task
6-
// #2023_07_03_Time_201_ms_(87.28%)_Space_38.3_MB_(60.85%)
6+
// #2025_07_11_Time_3_ms_(99.17%)_Space_46.05_MB_(66.40%)
77

88
class Solution {
99
fun lengthOfLongestSubstring(s: String): Int {
10-
var i = 0
11-
var j = 0
12-
var longest = 0
13-
// 1. if string empty, return 0
14-
if (s.isEmpty()) {
15-
return 0
16-
}
17-
while (j < s.length) {
18-
// 2. if the char at index j already seen, update the longest if needs
19-
if (i != j && s.substring(i, j).indexOf(s[j]) > -1) {
20-
longest = Math.max(j - i, longest)
21-
i++
10+
val lastIndices = IntArray(256) { -1 }
11+
var maxLen = 0
12+
var curLen = 0
13+
var start = 0
14+
for (i in s.indices) {
15+
val cur = s[i]
16+
if (lastIndices[cur.code] < start) {
17+
lastIndices[cur.code] = i
18+
curLen++
2219
} else {
23-
// 3. j out of bound already, update longest
24-
if (++j == s.length) {
25-
longest = Math.max(s.length - i, longest)
26-
break
27-
}
20+
val lastIndex = lastIndices[cur.code]
21+
start = lastIndex + 1
22+
curLen = i - start + 1
23+
lastIndices[cur.code] = i
24+
}
25+
if (curLen > maxLen) {
26+
maxLen = curLen
2827
}
2928
}
30-
return longest
29+
return maxLen
3130
}
3231
}

src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.kt

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,39 @@ package g0001_0100.s0004_median_of_two_sorted_arrays
22

33
// #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer
44
// #Top_Interview_150_Binary_Search #Big_O_Time_O(log(min(N,M)))_Space_O(1)
5-
// #AI_can_be_used_to_solve_the_task #2023_07_03_Time_293_ms_(75.96%)_Space_47.5_MB_(64.85%)
5+
// #AI_can_be_used_to_solve_the_task #2025_07_11_Time_2_ms_(99.23%)_Space_51.04_MB_(73.69%)
6+
7+
import kotlin.math.max
8+
import kotlin.math.min
69

710
class Solution {
811
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
9-
val l: MutableList<Int> = ArrayList()
10-
val f: Double
11-
for (j in nums1) {
12-
l.add(j)
13-
}
14-
for (i in nums2) {
15-
l.add(i)
12+
if (nums2.size < nums1.size) {
13+
return findMedianSortedArrays(nums2, nums1)
1614
}
17-
l.sort()
18-
val k = l.size
19-
f = if (k % 2 == 0) {
20-
(l[k / 2 - 1] + l[k / 2]).toDouble() / 2
21-
} else {
22-
l[(k + 1) / 2 - 1].toDouble()
15+
val n1 = nums1.size
16+
val n2 = nums2.size
17+
var low = 0
18+
var high = n1
19+
while (low <= high) {
20+
val cut1 = (low + high) / 2
21+
val cut2 = ((n1 + n2 + 1) / 2) - cut1
22+
val l1 = if (cut1 == 0) Int.MIN_VALUE else nums1[cut1 - 1]
23+
val l2 = if (cut2 == 0) Int.MIN_VALUE else nums2[cut2 - 1]
24+
val r1 = if (cut1 == n1) Int.MAX_VALUE else nums1[cut1]
25+
val r2 = if (cut2 == n2) Int.MAX_VALUE else nums2[cut2]
26+
if (l1 <= r2 && l2 <= r1) {
27+
return if ((n1 + n2) % 2 == 0) {
28+
(max(l1, l2).toDouble() + min(r1, r2).toDouble()) / 2.0
29+
} else {
30+
max(l1, l2).toDouble()
31+
}
32+
} else if (l1 > r2) {
33+
high = cut1 - 1
34+
} else {
35+
low = cut1 + 1
36+
}
2337
}
24-
return f
38+
return 0.0
2539
}
2640
}

src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package g0001_0100.s0005_longest_palindromic_substring
33
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming
44
// #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming
55
// #Dynamic_Programming_I_Day_17 #Udemy_Strings #Top_Interview_150_Multidimensional_DP
6-
// #Big_O_Time_O(n)_Space_O(n) #2023_07_03_Time_162_ms_(99.00%)_Space_36.6_MB_(79.10%)
6+
// #Big_O_Time_O(n)_Space_O(n) #2025_07_11_Time_8_ms_(96.61%)_Space_43.12_MB_(69.70%)
77

88
class Solution {
99
fun longestPalindrome(s: String): String {

0 commit comments

Comments
 (0)