Skip to content

Commit f15bc3b

Browse files
Merge pull request #158
small commits 27.02
2 parents 03c9d85 + 28b757c commit f15bc3b

File tree

4 files changed

+100
-33
lines changed

4 files changed

+100
-33
lines changed

.idea/other.xml

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

33

4-
import com.github.contest.binaryTree.recoverPreorderTraversal
4+
import com.github.contest.heap.largestInteger
55
import java.util.TreeMap
66

77

@@ -11,7 +11,8 @@ import java.util.TreeMap
1111

1212
fun main() {
1313

14-
recoverPreorderTraversal("1-2--3--4-5--6--7")
14+
largestInteger(1234)
15+
1516
}
1617

1718
fun generateTesting() {

contest/src/main/java/com/github/contest/dp/DpLeetcode.kt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -323,41 +323,31 @@ fun countVowels(word: String): Long {
323323
return totalVowels
324324
}
325325

326+
326327
/**
327-
*
328+
* 1749. Maximum Absolute Sum of Any Subarray
328329
*/
329330

330-
fun maxTwoEvents(events: Array<IntArray>): Int {
331-
val n = events.size
332-
events.sortBy { it[0] } // Sort by start time
333-
val dp = IntArray(n)
334-
var maxVal = 0
335-
for (i in n - 1 downTo 0) {
336-
maxVal = maxOf(maxVal, events[i][2])
337-
dp[i] = maxVal
338-
}
339-
var res = 0
340-
for (i in 0 until n) {
341-
val start = events[i][0]
342-
val end = events[i][1]
343-
val value = events[i][2]
344-
val nextIndex = findNextNonOverlapping(events, end)
345-
val nextValue = if (nextIndex == n) 0 else dp[nextIndex]
346-
res = maxOf(res, value + nextValue)
347-
}
348-
return res
349-
}
331+
fun maxAbsoluteSum(nums: IntArray): Int {
332+
var maxSoFar = 0
333+
var minSoFar = 0
334+
var currentMax = 0
335+
var currentMin = 0
336+
337+
for (num in nums) {
338+
currentMax += num
339+
currentMin += num
340+
341+
maxSoFar = maxOf(maxSoFar, currentMax)
342+
minSoFar = minOf(minSoFar, currentMin)
350343

351-
private fun findNextNonOverlapping(events: Array<IntArray>, end: Int): Int {
352-
var left = 0
353-
var right = events.size
354-
while (left < right) {
355-
val mid = left + (right - left) / 2
356-
if (events[mid][0] <= end) {
357-
left = mid + 1
358-
} else {
359-
right = mid
344+
if (currentMax < 0) {
345+
currentMax = 0
346+
}
347+
if (currentMin > 0) {
348+
currentMin = 0
360349
}
361350
}
362-
return left
351+
352+
return maxOf(maxSoFar, abs(minSoFar))
363353
}

contest/src/main/java/com/github/contest/heap/HeapLeetcode.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,46 @@ fun maxSubsequence(nums: IntArray, k: Int): IntArray {
310310

311311
return indices.map { it.first }.toIntArray()
312312
}
313+
314+
/**
315+
* 2231. Largest Number After Digit Swaps by Parity
316+
*/
317+
318+
319+
fun largestInteger(num: Int): Int {
320+
val s = num.toString()
321+
val n = s.length
322+
val odd = mutableListOf<Int>()
323+
val even = mutableListOf<Int>()
324+
val oddIndices = mutableListOf<Int>()
325+
val evenIndices = mutableListOf<Int>()
326+
327+
for (i in 0 until n) {
328+
val digit = s[i] - '0'
329+
if (digit % 2 == 0) {
330+
even.add(digit)
331+
evenIndices.add(i)
332+
} else {
333+
odd.add(digit)
334+
oddIndices.add(i)
335+
}
336+
}
337+
338+
odd.sortDescending()
339+
even.sortDescending()
340+
341+
val result = CharArray(n)
342+
var oddIndex = 0
343+
var evenIndex = 0
344+
345+
for (i in 0 until n) {
346+
if (s[i].toInt() % 2 == 0) {
347+
result[i] = (even[evenIndex++]).toChar() + '0'.toInt()
348+
} else {
349+
result[i] = (odd[oddIndex++]).toChar() + '0'.toInt()
350+
}
351+
}
352+
353+
return String(result).toInt()
354+
}
355+

0 commit comments

Comments
 (0)