Skip to content

add new problems and prod variants #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.contest


import com.github.contest.dp.longestArithSeqLength
import com.github.contest.dp.longestMountain
import java.util.TreeMap


Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,98 @@ fun applyOperationsAlternativeSolution(nums: IntArray): IntArray {

return nums

}
}

/**
* 2570. Merge Two 2D Arrays by Summing Values
* Alternative Solution
*/

fun mergeArraysAlternativeSolution(
nums1: Array<IntArray>, nums2: Array<IntArray>
): Array<IntArray> {
val result = mutableListOf<IntArray>()
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
}
34 changes: 31 additions & 3 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>()
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<IntArray>, nums2: Array<IntArray>): Array<IntArray> {
val map = mutableMapOf<Int, Int>()

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()
}






38 changes: 38 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


19 changes: 19 additions & 0 deletions contest/src/main/java/com/github/contest/dp/DpProdVariant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,23 @@ fun longestStrChainProdVariant(words: Array<String>): Int {
dp[word] = currentChain
currentChain
}
}

/**
* 1027. Longest Arithmetic Subsequence
* Prod Variant
*/

fun longestArithSeqLengthProdVariant(nums: IntArray): Int {
val dp = Array(nums.size) { mutableMapOf<Int, Int>() }
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
}
}
}
17 changes: 16 additions & 1 deletion contest/src/main/java/com/github/contest/math/MathLeetcode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,19 @@ package com.github.contest.math
* 1025. Divisor Game
*/

fun divisorGame(n: Int): Boolean = n % 2 == 0
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
}