From 8aed66a8adb235ee17a7dd5cd72434b118ae5be6 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Thu, 3 Apr 2025 17:13:05 +0100 Subject: [PATCH 1/2] add 2874 --- .../main/java/com/github/contest/Execute.kt | 18 +++++++-------- .../com/github/contest/array/ArrayLeetcode.kt | 22 ++++++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index 4190f537..e5c46429 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,7 +1,9 @@ package com.github.contest -import com.github.contest.graph.mostProfitablePath +import com.github.contest.array.maximumTripletValue +import com.github.contest.array.maximumTripletValue + import java.util.TreeMap @@ -11,10 +13,11 @@ 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() + maximumTripletValue( + intArrayOf( + 15, 3, 3, 18, 19, 13, 7, 5, 18, 1, 8, 5 + ) + ).also { println(it) } } @@ -28,10 +31,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() } 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 761e796b..ff335c50 100644 --- a/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt +++ b/contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt @@ -1,5 +1,6 @@ package com.github.contest.array + /** * 1800. Maximum Ascending Subarray Sum */ @@ -144,7 +145,7 @@ fun mergeArrays(nums1: Array, nums2: Array): Array * 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) { @@ -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 +} + From 31fd53aa30be88707c1f6a4048d172d77cc45227 Mon Sep 17 00:00:00 2001 From: Ashwagandha-coder Date: Fri, 4 Apr 2025 19:32:47 +0100 Subject: [PATCH 2/2] add 848 --- .../main/java/com/github/contest/Execute.kt | 10 ++---- .../github/contest/strings/StringsLeetcode.kt | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/contest/src/main/java/com/github/contest/Execute.kt b/contest/src/main/java/com/github/contest/Execute.kt index e5c46429..23b2a76a 100644 --- a/contest/src/main/java/com/github/contest/Execute.kt +++ b/contest/src/main/java/com/github/contest/Execute.kt @@ -1,8 +1,7 @@ package com.github.contest -import com.github.contest.array.maximumTripletValue -import com.github.contest.array.maximumTripletValue +import com.github.contest.strings.shiftingLetters import java.util.TreeMap @@ -13,12 +12,7 @@ import java.util.TreeMap fun main() { - maximumTripletValue( - intArrayOf( - 15, 3, 3, 18, 19, 13, 7, 5, 18, 1, 8, 5 - ) - ).also { println(it) } - + shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) } } fun testing() { diff --git a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt index f9382ac4..bbba6e3a 100644 --- a/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt +++ b/contest/src/main/java/com/github/contest/strings/StringsLeetcode.kt @@ -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 +} +