Skip to content

Commit c9bf6ff

Browse files
Merge pull request #175
add a new problem 12.04
2 parents e3eb41a + 89ab3d1 commit c9bf6ff

23 files changed

+1089
-590
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/other.xml

Lines changed: 0 additions & 549 deletions
This file was deleted.

app/src/main/java/com/leetcode_kotlin/AlgorithmLeetcode.kt

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,31 +2612,6 @@ fun findRelativeRanks(score: IntArray): Array<String> {
26122612
return ans
26132613
}
26142614

2615-
/**
2616-
* 347. Top K Frequent Elements
2617-
*/
2618-
2619-
fun topKFrequent(nums: IntArray, k: Int): IntArray? {
2620-
val map: MutableMap<Int, Int> = HashMap()
2621-
for (n in nums) {
2622-
map[n] = map.getOrDefault(n, 0) + 1
2623-
}
2624-
2625-
val heap = PriorityQueue { a: Map.Entry<Int, Int>, b: Map.Entry<Int, Int> ->
2626-
b.value.compareTo(a.value)
2627-
}
2628-
2629-
for (entry in map.entries) {
2630-
heap.offer(entry)
2631-
}
2632-
2633-
val res = IntArray(k)
2634-
for (i in 0 until k) {
2635-
res[i] = heap.poll().key
2636-
}
2637-
2638-
return res
2639-
}
26402615

26412616
/**
26422617
* 437. Path Sum III

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

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

33

4-
import com.github.contest.design.WordDictionary
4+
import com.github.contest.math.numberOfPowerfulInt
5+
import com.github.contest.strings.fullJustify
6+
import com.github.contest.strings.subStrHash
57
import java.util.TreeMap
68

79

@@ -11,23 +13,52 @@ import java.util.TreeMap
1113

1214
fun main() {
1315

16+
fullJustify(
17+
arrayOf(
18+
"Science",
19+
"is",
20+
"what",
21+
"we",
22+
"understand",
23+
"well",
24+
"enough",
25+
"to",
26+
"explain",
27+
"to",
28+
"a",
29+
"computer.",
30+
"Art",
31+
"is",
32+
"everything",
33+
"else",
34+
"we",
35+
"do"
36+
), 20
37+
).also {
38+
println(
39+
it
40+
)
41+
}
42+
43+
}
1444

15-
val wordDictionary = WordDictionary()
16-
wordDictionary.addWord("bad")
17-
wordDictionary.addWord("dad")
18-
wordDictionary.addWord("mad")
19-
wordDictionary.search("pad").also { println(it) } // return False
20-
wordDictionary.search("bad").also { println(it) } // return True
21-
wordDictionary.search(".ad").also { println(it) } // return True
22-
wordDictionary.search("b..").also { println(it) }
45+
fun subStrHashData() {
46+
subStrHash("xxterzixjqrghqyeketqeynekvqhc", 15, 94, 4, 16).also { println(it) }
2347
}
2448

49+
fun numberOfPowerfulIntData() {
50+
val start = 141L
51+
val finish = 148L
52+
val limit = 9
53+
val s = "9"
54+
55+
numberOfPowerfulInt(start, finish, limit, s).also { println(it) }
56+
}
57+
58+
2559
fun generateTesting() {
2660
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
27-
sequence.map { it * 2 }
28-
.filter { it > 3 }
29-
.filter { it > 2 }
30-
.constrainOnce()
61+
sequence.map { it * 2 }.filter { it > 3 }.filter { it > 2 }.constrainOnce()
3162

3263

3364
}

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.contest.array
22

3+
34
/**
45
* 1800. Maximum Ascending Subarray Sum
56
*/
@@ -140,6 +141,42 @@ fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray>
140141
}.toTypedArray()
141142
}
142143

144+
/**
145+
* 2873. Maximum Value of an Ordered Triplet I
146+
*/
147+
148+
fun maximumTripletValueI(nums: IntArray): Long {
149+
var max = 0L
150+
for (i in nums.indices) {
151+
for (j in i + 1 until nums.size) {
152+
for (k in j + 1 until nums.size) {
153+
max = maxOf(max, ((nums[i] - nums[j]).toLong() * nums[k].toLong()))
154+
}
155+
}
156+
}
157+
158+
return max
159+
}
160+
161+
162+
/**
163+
* 2874. Maximum Value of an Ordered Triplet II
164+
*/
165+
166+
167+
fun maximumTripletValue(nums: IntArray): Long {
168+
var maxi = Int.MIN_VALUE
169+
var diff = 0
170+
var res = 0L
171+
172+
for (i in nums.indices) {
173+
maxi = maxOf(maxi, nums[i])
174+
if (i >= 2) res = maxOf(res, (diff.toLong() * nums[i]))
175+
if (i >= 1) diff = maxOf(diff, (maxi - nums[i]))
176+
}
177+
178+
return res
179+
}
143180

144181

145182

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,25 @@
11
package com.github.contest.bitManipulation
22

3+
/**
4+
* 1863. Sum of All Subset XOR Totals
5+
*/
6+
7+
fun subsetXORSum(nums: IntArray): Int {
8+
var totalXORSum = 0
9+
10+
fun calculateSubsetXOR(index: Int, currentXOR: Int) {
11+
if (index == nums.size) {
12+
totalXORSum += currentXOR
13+
return
14+
}
15+
16+
// Include the current element
17+
calculateSubsetXOR(index + 1, currentXOR xor nums[index])
18+
19+
// Exclude the current element
20+
calculateSubsetXOR(index + 1, currentXOR)
21+
}
22+
23+
calculateSubsetXOR(0, 0)
24+
return totalXORSum
25+
}

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/design/DesignProdVariant.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,43 @@ class ProductOfNumbersProdVariant() {
3030
}
3131
return products.last() / products[products.size - k - 1]
3232
}
33+
}
34+
35+
/**
36+
* 981. Time Based Key-Value Store
37+
* Prod Variant
38+
*/
39+
40+
class TimeMapProdVariant {
41+
42+
private val store = mutableMapOf<String, MutableList<Pair<Int, String>>>()
43+
44+
fun set(key: String, value: String, timestamp: Int) {
45+
store.getOrPut(key) { mutableListOf(Pair(timestamp, value)) }.also {
46+
it.add(Pair(timestamp, value))
47+
}
48+
}
49+
50+
fun get(key: String, timestamp: Int): String = when {
51+
store[key] == null -> ""
52+
else -> getFromList(store.getOrDefault(key, listOf()), timestamp)
53+
}
54+
55+
private fun getFromList(list: List<Pair<Int, String>>, timestamp: Int): String =
56+
list.binarySearch {
57+
when {
58+
it.first == timestamp -> 0
59+
it.first < timestamp -> -1
60+
else -> 1
61+
}
62+
}.let { index ->
63+
when {
64+
index >= 0 -> list[index].second
65+
else -> {
66+
val insertionPoint = -index - 1
67+
if (insertionPoint == 0) "" else list[insertionPoint - 1].second
68+
}
69+
}
70+
}
71+
3372
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,47 @@ fun longestSubsequence(arr: IntArray, difference: Int): Int {
541541
return maxLen
542542
}
543543

544+
/**
545+
* 2140. Solving Questions With Brainpower
546+
*/
547+
548+
fun mostPoints(questions: Array<IntArray>): Long {
549+
return solve(questions, 0)
550+
}
551+
552+
private fun solve(questions: Array<IntArray>, index: Int): Long {
553+
if (index >= questions.size) {
554+
return 0
555+
}
556+
557+
val points = questions[index][0]
558+
val brainpower = questions[index][1]
559+
val take = points.toLong() + solve(questions, index + brainpower + 1)
560+
561+
562+
val skip = solve(questions, index + 1)
563+
564+
return maxOf(take, skip)
565+
}
566+
567+
fun mostPointsDp(questions: Array<IntArray>): Long {
568+
val n = questions.size
569+
val dp = LongArray(n + 1) { 0L }
570+
571+
for (i in n - 1 downTo 0) {
572+
val points = questions[i][0]
573+
val brainpower = questions[i][1]
574+
val nextQuestion = i + brainpower + 1
575+
576+
val take = points.toLong() + (if (nextQuestion < n) dp[nextQuestion] else 0L)
577+
val skip = dp[i + 1]
578+
579+
dp[i] = maxOf(take, skip)
580+
}
581+
582+
return dp[0]
583+
}
584+
544585

545586

546587

0 commit comments

Comments
 (0)