Skip to content

Commit 191d684

Browse files
Merge pull request #169
workBranch
2 parents 1bc3a54 + 31fd53a commit 191d684

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

contest/src/main/java/com/github/contest/Execute.kt

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.github.contest
22

33

4-
import com.github.contest.graph.mostProfitablePath
4+
import com.github.contest.strings.shiftingLetters
5+
56
import java.util.TreeMap
67

78

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

1213
fun main() {
1314

14-
// val edges = arrayOf(intArrayOf(0, 1), intArrayOf(1, 2), intArrayOf(1, 3), intArrayOf(3, 4))
15-
// mostProfitablePath(edges, 3, intArrayOf(-2, 4, 2, -4, 6)).also { println(it) }
16-
//
17-
testing()
18-
15+
shiftingLetters("abc", intArrayOf(3, 5, 9)).also { println(it) }
1916
}
2017

2118
fun testing() {
@@ -28,10 +25,7 @@ fun testing() {
2825

2926
fun generateTesting() {
3027
val sequence = sequenceOf(3, 5, 6, 7, 7, 8, 8, 8, 9, 3)
31-
sequence.map { it * 2 }
32-
.filter { it > 3 }
33-
.filter { it > 2 }
34-
.constrainOnce()
28+
sequence.map { it * 2 }.filter { it > 3 }.filter { it > 2 }.constrainOnce()
3529

3630

3731
}

contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.contest.array
22

3+
34
/**
45
* 1800. Maximum Ascending Subarray Sum
56
*/
@@ -144,7 +145,7 @@ fun mergeArrays(nums1: Array<IntArray>, nums2: Array<IntArray>): Array<IntArray>
144145
* 2873. Maximum Value of an Ordered Triplet I
145146
*/
146147

147-
fun maximumTripletValue(nums: IntArray): Long {
148+
fun maximumTripletValueI(nums: IntArray): Long {
148149
var max = 0L
149150
for (i in nums.indices) {
150151
for (j in i + 1 until nums.size) {
@@ -158,6 +159,25 @@ fun maximumTripletValue(nums: IntArray): Long {
158159
}
159160

160161

162+
/**
163+
* 2874. Maximum Value of an Ordered Triplet II
164+
*/
165+
166+
167+
fun maximumTripletValue(nums: IntArray): Long {
168+
var maxi = Int.MIN_VALUE
169+
var diff = 0
170+
var res = 0L
171+
172+
for (i in nums.indices) {
173+
maxi = maxOf(maxi, nums[i])
174+
if (i >= 2) res = maxOf(res, (diff.toLong() * nums[i]))
175+
if (i >= 1) diff = maxOf(diff, (maxi - nums[i]))
176+
}
177+
178+
return res
179+
}
180+
161181

162182

163183

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
package com.github.contest.strings
22

3+
/**
4+
* 848. Shifting Letters
5+
*/
6+
7+
fun shiftingLetters(str: String, shifts: IntArray): String {
8+
val shifting = IntArray(shifts.size)
9+
var sum = shifts.sum()
10+
var res = ""
11+
12+
for (i in shifting.indices) {
13+
shifting[i] = sum
14+
sum -= shifts[i]
15+
}
16+
17+
for (i in str.indices) {
18+
val newCharValue = shiftLetter(str[i], shifting[i])
19+
res += newCharValue
20+
21+
}
22+
23+
return res
24+
}
25+
26+
private fun shiftLetter(char: Char, shift: Int): Char = when {
27+
char in 'a'..'z' -> {
28+
val base = 'a'.code
29+
val offset = char.code - base
30+
val shifted = (offset + shift) % 26
31+
(base + shifted).toChar()
32+
}
33+
else -> char
34+
}
35+

0 commit comments

Comments
 (0)