Skip to content

Commit 96cb5b0

Browse files
Merge pull request #162
add new problem 3.03
2 parents 6f1dcbd + f3c05ab commit 96cb5b0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Diff for: contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

+21
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,25 @@ private fun IntArray.swap(from: Int, to: Int) {
9595
this[to] = temp
9696
}
9797

98+
/**
99+
* 2161. Partition Array According to Given Pivot
100+
*/
101+
fun pivotArray(nums: IntArray, pivot: Int): IntArray {
102+
if (nums.hasSingle()) return nums
103+
val res = mutableListOf<Int>()
104+
for (i in 0 until nums.size) {
105+
if (nums[i] < pivot) res.add(nums[i])
106+
}
107+
for (i in 0 until nums.size) {
108+
if (nums[i] == pivot) res.add(nums[i])
109+
}
110+
for (i in 0 until nums.size) {
111+
if (nums[i] > pivot) res.add(nums[i])
112+
}
113+
114+
return res.toIntArray()
115+
}
116+
117+
118+
98119

Diff for: contest/src/main/java/com/github/contest/dp/DpProdVariant.kt

+21
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,24 @@ fun longestCommonSubsequenceProdVariant(text1: String, text2: String): Int {
109109
}
110110

111111
private fun Array<IntArray>.lastElement(): Int = this.last().last()
112+
113+
114+
/**
115+
* 1048. Longest String Chain
116+
* Prod Variant
117+
*/
118+
119+
fun longestStrChainProdVariant(words: Array<String>): Int {
120+
val sortedWords = words.sortedBy { it.length }
121+
val dp = mutableMapOf<String, Int>()
122+
123+
124+
return sortedWords.maxOf { word ->
125+
var currentChain = word.indices.maxOf { index ->
126+
val predecessor = word.removeRange(index, index + 1)
127+
dp.getOrDefault(predecessor, 0) + 1
128+
}
129+
dp[word] = currentChain
130+
currentChain
131+
}
132+
}

0 commit comments

Comments
 (0)