Skip to content

Commit 8f15a36

Browse files
committed
Improved tasks 1, 3, 4
1 parent 9fa7140 commit 8f15a36

File tree

3 files changed

+46
-33
lines changed
  • src/main/kotlin/g0001_0100

3 files changed

+46
-33
lines changed

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: MutableMap<Int, Int> = HashMap()
48+
val indexMap = HashMap<Int, Int>()
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: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,26 @@ 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-
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++
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++
6259
} else {
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-
}
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
6867
}
6968
}
70-
return longest
69+
return maxLen
7170
}
7271
}
7372
```

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,38 @@ 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+
5861
class Solution {
5962
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
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)
63+
if (nums2.size < nums1.size) {
64+
return findMedianSortedArrays(nums2, nums1)
6765
}
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()
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+
}
7488
}
75-
return f
89+
return 0.0
7690
}
7791
}
7892
```

0 commit comments

Comments
 (0)