Skip to content

Commit 5013f5f

Browse files
Merge pull request #179
20.04 new problem in progress
2 parents 8c21643 + 2b90bf1 commit 5013f5f

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.contest
22

33

4+
import com.github.contest.binarySearch.twoSum
45
import com.github.contest.hashTable.countGoodAlternativeSolution
56
import com.github.contest.math.numberOfPowerfulInt
67
import com.github.contest.strings.fullJustify
@@ -14,10 +15,15 @@ import java.util.TreeMap
1415

1516
fun main() {
1617

17-
countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) }
18+
twoSum(intArrayOf(-1, 0), -1).printArray()
19+
1820

1921
}
2022

23+
fun countGoodData() {
24+
countGoodAlternativeSolution(intArrayOf(3, 1, 4, 3, 2, 2, 4), 2).also { println(it) }
25+
}
26+
2127
fun fullJustifyData() {
2228
fullJustify(
2329
arrayOf(

contest/src/main/java/com/github/contest/binarySearch/BinarySearchLeetcode.kt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,33 @@ fun minCapability(nums: IntArray, k: Int): Int {
3737

3838
return ans
3939
}
40+
41+
/**
42+
*
43+
*/
44+
45+
fun twoSum(numbers: IntArray, target: Int): IntArray {
46+
var boundIndex = numbers.size - 1
47+
var nullIndex = 0
48+
for (i in numbers.indices) {
49+
if (numbers[i] > target) {
50+
boundIndex = i - 1
51+
}
52+
if (numbers[i] == 0) nullIndex = i
53+
}
54+
55+
for (i in boundIndex downTo 0) {
56+
var left = 0
57+
var right = i
58+
val tar = target - numbers[i]
59+
if (tar == 0) return intArrayOf(i + 1, nullIndex + 1)
60+
while (left <= right) {
61+
var mid = (left + right) / 2
62+
if (numbers[mid] == tar) return intArrayOf(mid + 1, i + 1)
63+
if (numbers[mid] < tar) mid = left + 1
64+
else right = mid - 1
65+
}
66+
}
67+
68+
return intArrayOf()
69+
}

0 commit comments

Comments
 (0)