diff --git a/src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt b/src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt index bd87acd71..3df093092 100644 --- a/src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0001_two_sum/Solution.kt @@ -3,11 +3,11 @@ package g0001_0100.s0001_two_sum // #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table // #Data_Structure_I_Day_2_Array #Level_1_Day_13_Hashmap #Udemy_Arrays #Top_Interview_150_Hashmap // #Big_O_Time_O(n)_Space_O(n) #AI_can_be_used_to_solve_the_task -// #2023_07_03_Time_202_ms_(91.18%)_Space_38.1_MB_(76.07%) +// #2025_07_11_Time_2_ms_(98.77%)_Space_48.00_MB_(52.59%) class Solution { fun twoSum(numbers: IntArray, target: Int): IntArray { - val indexMap: MutableMap = HashMap() + val indexMap = HashMap() for (i in numbers.indices) { val requiredNum = target - numbers[i] if (indexMap.containsKey(requiredNum)) { diff --git a/src/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt b/src/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt index ad1ca0c51..4df6e6694 100644 --- a/src/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0002_add_two_numbers/Solution.kt @@ -3,7 +3,7 @@ package g0001_0100.s0002_add_two_numbers // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Math #Linked_List #Recursion // #Data_Structure_II_Day_10_Linked_List #Programming_Skills_II_Day_15 // #Top_Interview_150_Linked_List #Big_O_Time_O(max(N,M))_Space_O(max(N,M)) -// #AI_can_be_used_to_solve_the_task #2023_07_03_Time_203_ms_(96.13%)_Space_41_MB_(77.03%) +// #AI_can_be_used_to_solve_the_task #2025_07_11_Time_2_ms_(87.63%)_Space_45.71_MB_(80.15%) import com_github_leetcode.ListNode diff --git a/src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt b/src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt index e84071a46..8f30beff0 100644 --- a/src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt @@ -3,30 +3,29 @@ package g0001_0100.s0003_longest_substring_without_repeating_characters // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Hash_Table #Sliding_Window // #Algorithm_I_Day_6_Sliding_Window #Level_2_Day_14_Sliding_Window/Two_Pointer #Udemy_Strings // #Top_Interview_150_Sliding_Window #Big_O_Time_O(n)_Space_O(1) #AI_can_be_used_to_solve_the_task -// #2023_07_03_Time_201_ms_(87.28%)_Space_38.3_MB_(60.85%) +// #2025_07_11_Time_3_ms_(99.17%)_Space_46.05_MB_(66.40%) class Solution { fun lengthOfLongestSubstring(s: String): Int { - var i = 0 - var j = 0 - var longest = 0 - // 1. if string empty, return 0 - if (s.isEmpty()) { - return 0 - } - while (j < s.length) { - // 2. if the char at index j already seen, update the longest if needs - if (i != j && s.substring(i, j).indexOf(s[j]) > -1) { - longest = Math.max(j - i, longest) - i++ + val lastIndices = IntArray(256) { -1 } + var maxLen = 0 + var curLen = 0 + var start = 0 + for (i in s.indices) { + val cur = s[i] + if (lastIndices[cur.code] < start) { + lastIndices[cur.code] = i + curLen++ } else { - // 3. j out of bound already, update longest - if (++j == s.length) { - longest = Math.max(s.length - i, longest) - break - } + val lastIndex = lastIndices[cur.code] + start = lastIndex + 1 + curLen = i - start + 1 + lastIndices[cur.code] = i + } + if (curLen > maxLen) { + maxLen = curLen } } - return longest + return maxLen } } diff --git a/src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.kt b/src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.kt index 06d87954e..e43c2e39a 100644 --- a/src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.kt @@ -2,25 +2,39 @@ package g0001_0100.s0004_median_of_two_sorted_arrays // #Hard #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search #Divide_and_Conquer // #Top_Interview_150_Binary_Search #Big_O_Time_O(log(min(N,M)))_Space_O(1) -// #AI_can_be_used_to_solve_the_task #2023_07_03_Time_293_ms_(75.96%)_Space_47.5_MB_(64.85%) +// #AI_can_be_used_to_solve_the_task #2025_07_11_Time_2_ms_(99.23%)_Space_51.04_MB_(73.69%) + +import kotlin.math.max +import kotlin.math.min class Solution { fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double { - val l: MutableList = ArrayList() - val f: Double - for (j in nums1) { - l.add(j) - } - for (i in nums2) { - l.add(i) + if (nums2.size < nums1.size) { + return findMedianSortedArrays(nums2, nums1) } - l.sort() - val k = l.size - f = if (k % 2 == 0) { - (l[k / 2 - 1] + l[k / 2]).toDouble() / 2 - } else { - l[(k + 1) / 2 - 1].toDouble() + val n1 = nums1.size + val n2 = nums2.size + var low = 0 + var high = n1 + while (low <= high) { + val cut1 = (low + high) / 2 + val cut2 = ((n1 + n2 + 1) / 2) - cut1 + val l1 = if (cut1 == 0) Int.MIN_VALUE else nums1[cut1 - 1] + val l2 = if (cut2 == 0) Int.MIN_VALUE else nums2[cut2 - 1] + val r1 = if (cut1 == n1) Int.MAX_VALUE else nums1[cut1] + val r2 = if (cut2 == n2) Int.MAX_VALUE else nums2[cut2] + if (l1 <= r2 && l2 <= r1) { + return if ((n1 + n2) % 2 == 0) { + (max(l1, l2).toDouble() + min(r1, r2).toDouble()) / 2.0 + } else { + max(l1, l2).toDouble() + } + } else if (l1 > r2) { + high = cut1 - 1 + } else { + low = cut1 + 1 + } } - return f + return 0.0 } } diff --git a/src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt b/src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt index 76a5fc17b..12f3703b3 100644 --- a/src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0005_longest_palindromic_substring/Solution.kt @@ -3,7 +3,7 @@ package g0001_0100.s0005_longest_palindromic_substring // #Medium #Top_100_Liked_Questions #Top_Interview_Questions #String #Dynamic_Programming // #Data_Structure_II_Day_9_String #Algorithm_II_Day_14_Dynamic_Programming // #Dynamic_Programming_I_Day_17 #Udemy_Strings #Top_Interview_150_Multidimensional_DP -// #Big_O_Time_O(n)_Space_O(n) #2023_07_03_Time_162_ms_(99.00%)_Space_36.6_MB_(79.10%) +// #Big_O_Time_O(n)_Space_O(n) #2025_07_11_Time_8_ms_(96.61%)_Space_43.12_MB_(69.70%) class Solution { fun longestPalindrome(s: String): String { 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