Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 00dfdb4

Browse files
committedJun 14, 2020
add kotlin solutions
1 parent 4a6a7fd commit 00dfdb4

File tree

6 files changed

+235
-0
lines changed

6 files changed

+235
-0
lines changed
 
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class IsSubsequenceKotlin392 {
2+
3+
fun isSubsequence(s: String, t: String): Boolean {
4+
if (s.isEmpty()) {
5+
return true
6+
}
7+
var indexS = 0
8+
t.forEach {
9+
if (it == s[indexS]) {
10+
if (++indexS == s.length) {
11+
return true
12+
}
13+
}
14+
}
15+
return false
16+
}
17+
18+
/*
19+
fun isSubsequence(s: String, t: String): Boolean {
20+
val tMap = mutableMapOf<Char, MutableList<Int>>()
21+
t.forEachIndexed { index, c ->
22+
tMap.computeIfAbsent(c) { mutableListOf() }.add(index)
23+
}
24+
var indexOft = 0
25+
s.forEach {
26+
indexOft = binarySearch(tMap[it], indexOft)
27+
if (indexOft++ == -1) {
28+
return false
29+
}
30+
}
31+
return true
32+
}
33+
34+
private fun binarySearch(nums: List<Int>?, target: Int): Int {
35+
if (nums.isNullOrEmpty()) {
36+
return -1
37+
}
38+
var left = 0
39+
var right = nums.size - 1
40+
while (left + 1 < right) {
41+
val mid = left + (right - left) / 2
42+
when {
43+
nums[mid] <= target -> left = mid
44+
nums[mid] > target -> right = mid
45+
}
46+
}
47+
return when {
48+
nums[left] >= target -> nums[left]
49+
nums[right] >= target -> nums[right]
50+
else -> -1
51+
}
52+
}
53+
*/
54+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class SearchInsertPositionKotlin35 {
2+
fun searchInsert(nums: IntArray, target: Int): Int {
3+
if (nums.isEmpty()) {
4+
return -1
5+
}
6+
var left = 0
7+
var right = nums.size - 1
8+
while (left + 1 < right) {
9+
val mid = left + (right - left) / 2
10+
when {
11+
nums[mid] <= target -> left = mid
12+
nums[mid] > target -> right = mid
13+
}
14+
}
15+
return when {
16+
target <= nums[left] -> left
17+
target <= nums[right] -> right
18+
else -> right + 1
19+
}
20+
}
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class SortColorsKotlin75 {
2+
fun sortColors(nums: IntArray) {
3+
var zeroPosition = 0
4+
var twoPosition = nums.size - 1
5+
var index = 0
6+
while (index <= twoPosition) {
7+
when {
8+
nums[index] == 1 -> ++index
9+
nums[index] == 0 -> swap(nums, zeroPosition++, index++)
10+
else -> swap(nums, twoPosition--, index)
11+
}
12+
}
13+
}
14+
15+
private fun swap(nums: IntArray, i: Int, j: Int) {
16+
if (i == j) {
17+
return
18+
}
19+
nums[i] = nums[i].xor(nums[j])
20+
nums[j] = nums[i].xor(nums[j])
21+
nums[i] = nums[i].xor(nums[j])
22+
}
23+
}
24+
25+
fun main() {
26+
val solution = SortColorsKotlin75()
27+
val test = intArrayOf(2, 2)
28+
println(solution.sortColors(test))
29+
test.forEach(::print)
30+
31+
// 0 3
32+
val test2 = intArrayOf(3, 3)
33+
test2[0] = test2[0].xor(test2[0])
34+
test2[0] = test2[0].xor(test2[0])
35+
test2[0] = test2[0].xor(test2[0])
36+
test2.forEach(::print)
37+
38+
// 4 4
39+
val test3 = intArrayOf(4, 4)
40+
test3[0] = test3[0].xor(test3[1])
41+
test3[1] = test3[0].xor(test3[1])
42+
test3[0] = test3[0].xor(test3[1])
43+
test3.forEach(::print)
44+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import kotlin.random.Random
2+
3+
class InsertDeleteGetRandomO1Kotlin380 {
4+
/** Initialize your data structure here. */
5+
private val arrayList: MutableList<Int> = ArrayList()
6+
private val map: MutableMap<Int, Int> = HashMap()
7+
8+
/** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
9+
fun insert(`val`: Int): Boolean {
10+
if (map.containsKey(`val`)) {
11+
return false
12+
}
13+
map[`val`] = arrayList.size
14+
arrayList.add(`val`)
15+
return true
16+
}
17+
18+
/** Removes a value from the set. Returns true if the set contained the specified element. */
19+
fun remove(`val`: Int): Boolean {
20+
if (!map.containsKey(`val`)) {
21+
return false
22+
}
23+
val removeIndex = map.getValue(`val`)
24+
if (removeIndex != arrayList.size - 1) {
25+
arrayList[removeIndex] = arrayList[arrayList.size - 1]
26+
map[arrayList[arrayList.size - 1]] = removeIndex
27+
}
28+
arrayList.removeAt(arrayList.size - 1)
29+
map.remove(`val`)
30+
return true
31+
}
32+
33+
/** Get a random element from the set. */
34+
fun getRandom(): Int {
35+
return arrayList[Random.Default.nextInt(arrayList.size)]
36+
}
37+
}
38+
39+
fun main() {
40+
val solution = InsertDeleteGetRandomO1Kotlin380()
41+
println(solution.insert(1))
42+
println(solution.remove(2))
43+
println(solution.insert(2))
44+
println(solution.getRandom())
45+
println(solution.remove(1))
46+
println(solution.insert(2))
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class LargestDivisibleSubsetKotlin368 {
2+
fun largestDivisibleSubset(nums: IntArray): List<Int> {
3+
if (nums.isEmpty()) {
4+
return emptyList()
5+
}
6+
nums.sort()
7+
val dp = IntArray(nums.size) { 1 }
8+
val links = IntArray(nums.size) { -1 }
9+
var max = 0
10+
var start = 0
11+
for (i in nums.indices) {
12+
for (j in i + 1 until nums.size) {
13+
if (nums[j] % nums[i] == 0 &&
14+
dp[j] < dp[i] + 1
15+
) {
16+
dp[j] = dp[i] + 1
17+
links[j] = i
18+
if (dp[j] > max) {
19+
max = dp[j]
20+
start = j
21+
}
22+
}
23+
}
24+
}
25+
val result: MutableList<Int> = ArrayList()
26+
while (start != -1) {
27+
result.add(nums[start])
28+
start = links[start]
29+
}
30+
return result
31+
}
32+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class CheapestFlightsWithinKStopsKotlin787 {
2+
/*
3+
fun findCheapestPrice(n: Int, flights: Array<IntArray>, src: Int, dst: Int, K: Int): Int {
4+
val dp = Array(K + 2) { IntArray(n) { 10000 * n } }
5+
dp[0][src] = 0
6+
for (time in 1..K + 1) {
7+
dp[time][src] = 0
8+
for (flight in flights) {
9+
dp[time][flight[1]] = minOf(
10+
dp[time][flight[1]],
11+
dp[time - 1][flight[0]] + flight[2]
12+
)
13+
}
14+
}
15+
return when {
16+
dp[K + 1][dst] >= 10000 * n -> -1
17+
else -> dp[K + 1][dst]
18+
}
19+
}
20+
*/
21+
22+
fun findCheapestPrice(n: Int, flights: Array<IntArray>, src: Int, dst: Int, K: Int): Int {
23+
var dp = IntArray(n) { 10000 * n }
24+
dp[src] = 0
25+
for (time in 0..K) {
26+
val new = dp.copyOf(dp.size)
27+
for (flight in flights) {
28+
new[flight[1]] = minOf(new[flight[1]], dp[flight[0]] + flight[2])
29+
}
30+
dp = new
31+
}
32+
return when {
33+
dp[dst] >= 10000 * n -> -1
34+
else -> dp[dst]
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.