Skip to content

Commit c06716a

Browse files
Merge pull request #163
add new problems and prod variants
2 parents 96cb5b0 + 8e9b2df commit c06716a

File tree

7 files changed

+212
-7
lines changed

7 files changed

+212
-7
lines changed

Diff for: .idea/other.xml

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.github.contest
22

33

4-
import com.github.contest.dp.longestArithSeqLength
4+
import com.github.contest.dp.longestMountain
55
import java.util.TreeMap
66

77

@@ -11,7 +11,7 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
longestArithSeqLength(intArrayOf(3, 6, 9, 12))
14+
longestMountain(intArrayOf(2, 1, 4, 7, 3, 2, 5))
1515
}
1616

1717
fun generateTesting() {

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

+95-1
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,98 @@ fun applyOperationsAlternativeSolution(nums: IntArray): IntArray {
6969

7070
return nums
7171

72-
}
72+
}
73+
74+
/**
75+
* 2570. Merge Two 2D Arrays by Summing Values
76+
* Alternative Solution
77+
*/
78+
79+
fun mergeArraysAlternativeSolution(
80+
nums1: Array<IntArray>, nums2: Array<IntArray>
81+
): Array<IntArray> {
82+
val result = mutableListOf<IntArray>()
83+
var i = 0
84+
var j = 0
85+
86+
while (i < nums1.size || j < nums2.size) {
87+
when {
88+
i == nums1.size -> {
89+
result.add(nums2[j])
90+
j++
91+
}
92+
93+
j == nums2.size -> {
94+
result.add(nums1[i])
95+
i++
96+
}
97+
98+
nums1[i][0] == nums2[j][0] -> {
99+
result.add(intArrayOf(nums1[i][0], nums1[i][1] + nums2[j][1]))
100+
i++
101+
j++
102+
}
103+
104+
nums1[i][0] < nums2[j][0] -> {
105+
result.add(nums1[i])
106+
i++
107+
}
108+
109+
else -> {
110+
result.add(nums2[j])
111+
j++
112+
}
113+
}
114+
}
115+
116+
return result.toTypedArray()
117+
}
118+
119+
/**
120+
* 2161. Partition Array According to Given Pivot
121+
* Alternative Solution
122+
*/
123+
124+
125+
fun pivotArrayAlternativeSolution(nums: IntArray, pivot: Int): IntArray {
126+
val n = nums.size
127+
val result = IntArray(n)
128+
var lessIndex = 0
129+
var greaterIndex = n - 1
130+
var equalCount = 0
131+
132+
for (num in nums) {
133+
when {
134+
num < pivot -> {
135+
result[lessIndex] = num
136+
lessIndex++
137+
}
138+
139+
num > pivot -> {
140+
result[greaterIndex] = num
141+
greaterIndex--
142+
}
143+
144+
else -> equalCount++
145+
}
146+
}
147+
148+
while (equalCount > 0) {
149+
result[lessIndex] = pivot
150+
lessIndex++
151+
equalCount--
152+
}
153+
154+
155+
var start = lessIndex
156+
var end = n - 1
157+
while (start < end) {
158+
val temp = result[start]
159+
result[start] = result[end]
160+
result[end] = temp
161+
start++
162+
end--
163+
}
164+
165+
return result
166+
}

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

+31-3
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,50 @@ private fun IntArray.swap(from: Int, to: Int) {
9898
/**
9999
* 2161. Partition Array According to Given Pivot
100100
*/
101+
101102
fun pivotArray(nums: IntArray, pivot: Int): IntArray {
102103
if (nums.hasSingle()) return nums
103104
val res = mutableListOf<Int>()
104-
for (i in 0 until nums.size) {
105+
for (i in nums.indices) {
105106
if (nums[i] < pivot) res.add(nums[i])
106107
}
107-
for (i in 0 until nums.size) {
108+
for (i in nums.indices) {
108109
if (nums[i] == pivot) res.add(nums[i])
109110
}
110-
for (i in 0 until nums.size) {
111+
for (i in nums.indices) {
111112
if (nums[i] > pivot) res.add(nums[i])
112113
}
113114

114115
return res.toIntArray()
115116
}
116117

118+
/**
119+
* 2570. Merge Two 2D Arrays by Summing Values
120+
*/
121+
122+
123+
fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray> {
124+
val map = mutableMapOf<Int, Int>()
125+
126+
for (pair in nums1) {
127+
val key = pair[0]
128+
val value = pair[1]
129+
map[key] = map.getOrDefault(key, 0) + value
130+
}
131+
132+
for (pair in nums2) {
133+
val key = pair[0]
134+
val value = pair[1]
135+
map[key] = map.getOrDefault(key, 0) + value
136+
}
137+
138+
return map.toSortedMap().map { (key, value) ->
139+
intArrayOf(key, value)
140+
}.toTypedArray()
141+
}
142+
143+
144+
117145

118146

119147

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

+38
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,41 @@ fun longestArithSeqLength(nums: IntArray): Int {
417417

418418
return longest
419419
}
420+
421+
/**
422+
* 845. Longest Mountain in Array
423+
*/
424+
425+
fun longestMountain(arr: IntArray): Int {
426+
val n = arr.size
427+
if (n < 3) return 0
428+
429+
var longestMountain = 0
430+
var i = 1
431+
432+
while (i < n - 1) {
433+
434+
if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) {
435+
436+
var left = i - 1
437+
while (left > 0 && arr[left - 1] < arr[left]) {
438+
left--
439+
}
440+
441+
442+
var right = i + 1
443+
while (right < n - 1 && arr[right] > arr[right + 1]) {
444+
right++
445+
}
446+
447+
448+
longestMountain = maxOf(longestMountain, right - left + 1)
449+
i = right
450+
}
451+
i++
452+
}
453+
454+
return longestMountain
455+
}
456+
457+

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

+19
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,23 @@ fun longestStrChainProdVariant(words: Array<String>): Int {
129129
dp[word] = currentChain
130130
currentChain
131131
}
132+
}
133+
134+
/**
135+
* 1027. Longest Arithmetic Subsequence
136+
* Prod Variant
137+
*/
138+
139+
fun longestArithSeqLengthProdVariant(nums: IntArray): Int {
140+
val dp = Array(nums.size) { mutableMapOf<Int, Int>() }
141+
var longest = 0
142+
143+
return (1 until nums.size).maxOf { i ->
144+
(0 until i).maxOf { j ->
145+
val diff = nums[i] - nums[j]
146+
val len = dp[j].getOrDefault(diff, 1) + 1
147+
dp[i][diff] = len
148+
len
149+
}
150+
}
132151
}

Diff for: contest/src/main/java/com/github/contest/math/MathLeetcode.kt

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,19 @@ package com.github.contest.math
44
* 1025. Divisor Game
55
*/
66

7-
fun divisorGame(n: Int): Boolean = n % 2 == 0
7+
fun divisorGame(n: Int): Boolean = n % 2 == 0
8+
9+
/**
10+
* 1780. Check if Number is a Sum of Powers of Three
11+
*/
12+
13+
fun checkPowersOfThree(n: Int): Boolean {
14+
var num = n
15+
while (num > 0) {
16+
if (num % 3 == 2) {
17+
return false
18+
}
19+
num /= 3
20+
}
21+
return true
22+
}

0 commit comments

Comments
 (0)