Skip to content

Commit 7821ec2

Browse files
Merge pull request #171
add new problem 7.04
2 parents c1f59c4 + c01c589 commit 7821ec2

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.bitManipulation.subsetXORSum
5-
import com.github.contest.strings.shiftingLetters
6-
import com.github.contest.strings.smallestStringProdVariant
7-
4+
import com.github.contest.strings.isNumber
85
import java.util.TreeMap
96

107

@@ -14,7 +11,7 @@ import java.util.TreeMap
1411

1512
fun main() {
1613

17-
subsetXORSum(intArrayOf(5, 1, 6))
14+
isNumber("6+1").also { println(it) }
1815
}
1916

2017
fun testing() {

contest/src/main/java/com/github/contest/design/DesignLeetcode.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,39 @@ class Trie() {
117117
return curr
118118
}
119119

120+
}
121+
122+
/**
123+
* 981. Time Based Key-Value Store
124+
*/
125+
126+
class TimeMap() {
127+
128+
private val store = mutableMapOf<String, MutableList<Pair<Int, String>>>()
129+
130+
fun set(key: String, value: String, timestamp: Int) {
131+
store.getOrPut(key) { mutableListOf(Pair(timestamp, value)) }.add(Pair(timestamp, value))
132+
}
133+
134+
fun get(key: String, timestamp: Int): String {
135+
val list = store[key] ?: return emptyString()
136+
var left = 0
137+
var right = list.size - 1
138+
var res = emptyString()
139+
140+
while (left <= right) {
141+
val mid = (left + right) / 2
142+
val (currentTimeStamp, currentValue) = list[mid]
143+
if (currentTimeStamp == timestamp) return currentValue
144+
if (currentTimeStamp < timestamp) {
145+
left = mid + 1
146+
res = currentValue
147+
} else right = mid
148+
}
149+
150+
return res
151+
}
152+
153+
private fun emptyString() = ""
154+
120155
}

contest/src/main/java/com/github/contest/dp/DpProdVariant.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,30 @@ fun longestArithSeqLengthProdVariant(nums: IntArray): Int {
148148
len
149149
}
150150
}
151+
}
152+
153+
/**
154+
* 2140. Solving Questions With Brainpower
155+
* Prod Variant
156+
*/
157+
158+
159+
fun mostPointsProdVariant(questions: Array<IntArray>): Long {
160+
val n = questions.size
161+
val dp = LongArray(n + 1)
162+
questions.forEachReversedIndexed { index, arr ->
163+
val points = arr[0]
164+
val brainpower = arr[1]
165+
dp[index] = points + when {
166+
brainpower + index + 1 < n -> dp[brainpower + index + 1]
167+
else -> 0L
168+
}
169+
dp[index] = maxOf(dp[index], dp[index + 1])
170+
}
171+
172+
return dp.first()
173+
}
174+
175+
fun <T> Array<T>.forEachReversedIndexed(action: (Int, T) -> Unit) {
176+
for (i in this.size - 1 downTo 0) action(i, this[i])
151177
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.contest.twoPointer
2+
3+
/**
4+
* 165. Compare Version Numbers
5+
*/
6+
7+
fun compareVersion(version1: String, version2: String): Int {
8+
val firstNumber = version1.split(".").map { it.toInt() }
9+
val secondNumber = version2.split(".").map { it.toInt() }
10+
11+
for (i in 0 until maxOf(firstNumber.size, secondNumber.size)) {
12+
val p1 = if (i < firstNumber.size) firstNumber[i] else 0
13+
val p2 = if (i < secondNumber.size) secondNumber[i] else 0
14+
15+
when {
16+
p1 != p2 -> {
17+
return if (p1 > p2) 1 else -1
18+
}
19+
}
20+
}
21+
return 0
22+
}

0 commit comments

Comments
 (0)