Skip to content
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
14 changes: 4 additions & 10 deletions contest/src/main/java/com/github/contest/Execute.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.contest


import com.github.contest.graph.mostProfitablePath
import com.github.contest.strings.shiftingLetters

import java.util.TreeMap


Expand All @@ -11,11 +12,7 @@ import java.util.TreeMap

fun main() {

// val edges = arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(3, 4))
// mostProfitablePath(edges, 3, intArrayOf(-2, 4, 2, -4, 6)).also { println(it) }
//
testing()

shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) }
}

fun testing() {
Expand All @@ -28,10 +25,7 @@ fun testing() {

fun generateTesting() {
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
sequence.map { it * 2 }
.filter { it > 3 }
.filter { it > 2 }
.constrainOnce()
sequence.map { it * 2 }.filter { it > 3 }.filter { it > 2 }.constrainOnce()


}
Expand Down
22 changes: 21 additions & 1 deletion contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.contest.array


/**
* 1800. Maximum Ascending Subarray Sum
*/
Expand Down Expand Up @@ -144,7 +145,7 @@ fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray>
* 2873. Maximum Value of an Ordered Triplet I
*/

fun maximumTripletValue(nums: IntArray): Long {
fun maximumTripletValueI(nums: IntArray): Long {
var max = 0L
for (i in nums.indices) {
for (j in i + 1 until nums.size) {
Expand All @@ -158,6 +159,25 @@ fun maximumTripletValue(nums: IntArray): Long {
}


/**
* 2874. Maximum Value of an Ordered Triplet II
*/


fun maximumTripletValue(nums: IntArray): Long {
var maxi = Int.MIN_VALUE
var diff = 0
var res = 0L

for (i in nums.indices) {
maxi = maxOf(maxi, nums[i])
if (i >= 2) res = maxOf(res, (diff.toLong() * nums[i]))
if (i >= 1) diff = maxOf(diff, (maxi - nums[i]))
}

return res
}




Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
package com.github.contest.strings

/**
* 848. Shifting Letters
*/

fun shiftingLetters(str: String, shifts: IntArray): String {
val shifting = IntArray(shifts.size)
var sum = shifts.sum()
var res = ""

for (i in shifting.indices) {
shifting[i] = sum
sum -= shifts[i]
}

for (i in str.indices) {
val newCharValue = shiftLetter(str[i], shifting[i])
res += newCharValue

}

return res
}

private fun shiftLetter(char: Char, shift: Int): Char = when {
char in 'a'..'z' -> {
val base = 'a'.code
val offset = char.code - base
val shifted = (offset + shift) % 26
(base + shifted).toChar()
}
else -> char
}