diff --git a/.idea/other.xml b/.idea/other.xml
index e965764..2a84d40 100644
--- a/.idea/other.xml
+++ b/.idea/other.xml
@@ -102,6 +102,17 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt
index dd43a43..bbef012 100644
--- a/contest/src/main/java/com/github/contest/Execute.kt
+++ b/contest/src/main/java/com/github/contest/Execute.kt
@@ -1,7 +1,7 @@
package com.github.contest
-import com.github.contest.dp.longestArithSeqLength
+import com.github.contest.dp.longestMountain
import java.util.TreeMap
@@ -11,7 +11,7 @@ import java.util.TreeMap
fun main() {
- longestArithSeqLength(intArrayOf(3, 6, 9, 12))
+ longestMountain(intArrayOf(2, 1, 4, 7, 3, 2, 5))
}
fun generateTesting() {
diff --git a/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt b/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt
index 3d629b9..df153d6 100644
--- a/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt
+++ b/contest/src/main/java/com/github/contest/array/AlternativeSolutionArray.kt
@@ -69,4 +69,98 @@ fun applyOperationsAlternativeSolution(nums: IntArray): IntArray {
return nums
-}
\ No newline at end of file
+}
+
+/**
+ * 2570. Merge Two 2D Arrays by Summing Values
+ * Alternative Solution
+ */
+
+fun mergeArraysAlternativeSolution(
+ nums1: Array, nums2: Array
+): Array {
+ val result = mutableListOf()
+ var i = 0
+ var j = 0
+
+ while (i < nums1.size || j < nums2.size) {
+ when {
+ i == nums1.size -> {
+ result.add(nums2[j])
+ j++
+ }
+
+ j == nums2.size -> {
+ result.add(nums1[i])
+ i++
+ }
+
+ nums1[i][0] == nums2[j][0] -> {
+ result.add(intArrayOf(nums1[i][0], nums1[i][1] + nums2[j][1]))
+ i++
+ j++
+ }
+
+ nums1[i][0] < nums2[j][0] -> {
+ result.add(nums1[i])
+ i++
+ }
+
+ else -> {
+ result.add(nums2[j])
+ j++
+ }
+ }
+ }
+
+ return result.toTypedArray()
+}
+
+/**
+ * 2161. Partition Array According to Given Pivot
+ * Alternative Solution
+ */
+
+
+fun pivotArrayAlternativeSolution(nums: IntArray, pivot: Int): IntArray {
+ val n = nums.size
+ val result = IntArray(n)
+ var lessIndex = 0
+ var greaterIndex = n - 1
+ var equalCount = 0
+
+ for (num in nums) {
+ when {
+ num < pivot -> {
+ result[lessIndex] = num
+ lessIndex++
+ }
+
+ num > pivot -> {
+ result[greaterIndex] = num
+ greaterIndex--
+ }
+
+ else -> equalCount++
+ }
+ }
+
+ while (equalCount > 0) {
+ result[lessIndex] = pivot
+ lessIndex++
+ equalCount--
+ }
+
+
+ var start = lessIndex
+ var end = n - 1
+ while (start < end) {
+ val temp = result[start]
+ result[start] = result[end]
+ result[end] = temp
+ start++
+ end--
+ }
+
+ return result
+}
diff --git a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
index 527899b..767dfb5 100644
--- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
@@ -98,22 +98,50 @@ private fun IntArray.swap(from: Int, to: Int) {
/**
* 2161. Partition Array According to Given Pivot
*/
+
fun pivotArray(nums: IntArray, pivot: Int): IntArray {
if (nums.hasSingle()) return nums
val res = mutableListOf()
- for (i in 0 until nums.size) {
+ for (i in nums.indices) {
if (nums[i] < pivot) res.add(nums[i])
}
- for (i in 0 until nums.size) {
+ for (i in nums.indices) {
if (nums[i] == pivot) res.add(nums[i])
}
- for (i in 0 until nums.size) {
+ for (i in nums.indices) {
if (nums[i] > pivot) res.add(nums[i])
}
return res.toIntArray()
}
+/**
+ * 2570. Merge Two 2D Arrays by Summing Values
+ */
+
+
+fun mergeArrays(nums1: Array, nums2: Array): Array {
+ val map = mutableMapOf()
+
+ for (pair in nums1) {
+ val key = pair[0]
+ val value = pair[1]
+ map[key] = map.getOrDefault(key, 0) + value
+ }
+
+ for (pair in nums2) {
+ val key = pair[0]
+ val value = pair[1]
+ map[key] = map.getOrDefault(key, 0) + value
+ }
+
+ return map.toSortedMap().map { (key, value) ->
+ intArrayOf(key, value)
+ }.toTypedArray()
+}
+
+
+
diff --git a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
index 9e7c9d9..391c97a 100644
--- a/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
@@ -417,3 +417,41 @@ fun longestArithSeqLength(nums: IntArray): Int {
return longest
}
+
+/**
+ * 845. Longest Mountain in Array
+ */
+
+fun longestMountain(arr: IntArray): Int {
+ val n = arr.size
+ if (n < 3) return 0
+
+ var longestMountain = 0
+ var i = 1
+
+ while (i < n - 1) {
+
+ if (arr[i - 1] < arr[i] && arr[i] > arr[i + 1]) {
+
+ var left = i - 1
+ while (left > 0 && arr[left - 1] < arr[left]) {
+ left--
+ }
+
+
+ var right = i + 1
+ while (right < n - 1 && arr[right] > arr[right + 1]) {
+ right++
+ }
+
+
+ longestMountain = maxOf(longestMountain, right - left + 1)
+ i = right
+ }
+ i++
+ }
+
+ return longestMountain
+}
+
+
diff --git a/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt b/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt
index f8d6cc3..1a60b05 100644
--- a/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt
+++ b/contest/src/main/java/com/github/contest/dp/DpProdVariant.kt
@@ -129,4 +129,23 @@ fun longestStrChainProdVariant(words: Array): Int {
dp[word] = currentChain
currentChain
}
+}
+
+/**
+ * 1027. Longest Arithmetic Subsequence
+ * Prod Variant
+ */
+
+fun longestArithSeqLengthProdVariant(nums: IntArray): Int {
+ val dp = Array(nums.size) { mutableMapOf() }
+ var longest = 0
+
+ return (1 until nums.size).maxOf { i ->
+ (0 until i).maxOf { j ->
+ val diff = nums[i] - nums[j]
+ val len = dp[j].getOrDefault(diff, 1) + 1
+ dp[i][diff] = len
+ len
+ }
+ }
}
\ No newline at end of file
diff --git a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt
index ef48858..d13e495 100644
--- a/contest/src/main/java/com/github/contest/math/MathLeetcode.kt
+++ b/contest/src/main/java/com/github/contest/math/MathLeetcode.kt
@@ -4,4 +4,19 @@ package com.github.contest.math
* 1025. Divisor Game
*/
-fun divisorGame(n: Int): Boolean = n % 2 == 0
\ No newline at end of file
+fun divisorGame(n: Int): Boolean = n % 2 == 0
+
+/**
+ * 1780. Check if Number is a Sum of Powers of Three
+ */
+
+fun checkPowersOfThree(n: Int): Boolean {
+ var num = n
+ while (num > 0) {
+ if (num % 3 == 2) {
+ return false
+ }
+ num /= 3
+ }
+ return true
+}
\ No newline at end of file