Skip to content

Commit 30e4860

Browse files
Merge pull request #198
add new problems 24.05
2 parents 3c26a02 + bfeea60 commit 30e4860

File tree

4 files changed

+150
-6
lines changed

4 files changed

+150
-6
lines changed

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

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

33

4-
import com.github.contest.array.isZeroArray
54
import com.github.contest.math.numberOfPowerfulInt
65
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
76
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
7+
import com.github.contest.slidingWindow.maximumRobots
88
import com.github.contest.strings.fullJustify
99
import com.github.contest.strings.subStrHash
1010
import java.util.TreeMap
@@ -15,11 +15,7 @@ import java.util.TreeMap
1515
*/
1616

1717
fun main() {
18-
19-
val nums = intArrayOf(3, 3, 2, 1)
20-
val queries = arrayOf(intArrayOf(1, 3), intArrayOf(0, 2), intArrayOf(0, 0), intArrayOf(0, 0))
21-
22-
isZeroArray(nums, queries).also { println(it) }
18+
maximumRobots(intArrayOf(3, 6, 1, 3, 4), intArrayOf(2, 1, 3, 4, 5), 25).also { println(it) }
2319
}
2420

2521

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

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

33
import java.util.PriorityQueue
4+
import kotlin.math.abs
45

56

67
/**
@@ -403,4 +404,23 @@ fun isZeroArray(nums: IntArray, queries: Array<IntArray>): Boolean {
403404
return true
404405
}
405406

407+
/**
408+
* 2932. Maximum Strong Pair XOR I
409+
*/
410+
411+
fun maximumStrongPairXor(nums: IntArray): Int {
412+
val pairs = mutableListOf<Pair<Int, Int>>()
413+
414+
for (i in nums.indices) {
415+
pairs.add(Pair(nums[i], nums[i]))
416+
for (j in i + 1 until nums.size) {
417+
pairs.add(Pair(nums[i], nums[j]))
418+
}
419+
}
420+
421+
return pairs
422+
.filter { abs(it.first - it.second) <= minOf(it.first, it.second) }
423+
.maxOf { it.first xor it.second }
424+
}
425+
406426

contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowAlternativeSolution.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,33 @@ fun getSubArrayBeautyAlternativeSolution(nums: IntArray, k: Int, x: Int): IntArr
109109
return res.toIntArray()
110110
}
111111

112+
/**
113+
* 413. Arithmetic Slices
114+
* Alternative Solution with O(n)
115+
*/
116+
117+
fun numberOfArithmeticSlicesAlternativeSolution(nums: IntArray): Int {
118+
if (nums.size < 3) return 0
119+
120+
var left = 0
121+
var count = 0
122+
val k = 3
123+
var diff = nums[1] - nums[0]
124+
125+
for (right in 2 until nums.size) {
126+
127+
if (nums[right] - nums[right - 1] != diff) {
128+
diff = nums[right] - nums[right - 1]
129+
left = right - 1
130+
}
131+
132+
if (right - left >= k - 1) {
133+
count++
134+
count += ((right - left) - (k - 1))
135+
}
136+
}
137+
138+
return count
139+
}
140+
112141

contest/src/main/java/com/github/contest/slidingWindow/SlidingWindowLeetcode.kt

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,105 @@ fun maxSatisfied(customers: IntArray, grumpy: IntArray, minutes: Int): Int {
546546
return maxCustomers
547547
}
548548

549+
/**
550+
* 3090. Maximum Length Substring With Two Occurrences
551+
*/
552+
553+
fun maximumLengthSubstring(s: String): Int {
554+
if (s.length == 2) return 2
555+
556+
var maxLen = 0
557+
var left = 0
558+
val freq = mutableMapOf<Char, Int>()
559+
560+
for (right in s.indices) {
561+
562+
val char = s[right]
563+
freq[char] = freq.getOrDefault(char, 0) + 1
564+
565+
if (!isValidSubString(freq)) {
566+
freq[s[left]] = freq.getOrDefault(s[left], 0) - 1
567+
if (freq[s[left]] == 0) freq.remove(s[left])
568+
left++
569+
}
570+
571+
maxLen = maxOf(maxLen, right - left + 1)
572+
}
573+
574+
return maxLen
575+
}
576+
577+
private fun isValidSubString(map: Map<Char, Int>): Boolean {
578+
579+
for ((_, value) in map) {
580+
if (value > 2) return false
581+
}
582+
583+
return true
584+
}
585+
586+
/**
587+
* 2398. Maximum Number of Robots Within Budget
588+
*/
589+
590+
fun maximumRobots(chargeTimes: IntArray, runningCosts: IntArray, budget: Long): Int {
591+
var maxRobots = 0
592+
var left = 0
593+
var sum = 0L
594+
val window = mutableListOf<Long>()
595+
596+
for (right in chargeTimes.indices) {
597+
window.add(chargeTimes[right].toLong())
598+
sum += runningCosts[right].toLong()
599+
var max = window.maxOrNull() ?: 0L
600+
var k = (right - left + 1).toLong()
601+
var cost = max + (k * sum)
602+
603+
while (cost > budget) {
604+
sum -= runningCosts[left]
605+
window.removeFirst()
606+
left++
607+
max = window.maxOrNull() ?: 0L
608+
k = (right - left + 1).toLong()
609+
cost = max + (k * sum)
610+
}
611+
612+
maxRobots = maxOf(maxRobots, right - left + 1)
613+
}
614+
615+
return maxRobots
616+
}
617+
618+
/**
619+
* 413. Arithmetic Slices
620+
*/
621+
622+
fun numberOfArithmeticSlices(nums: IntArray): Int {
623+
if (nums.size < 3) return 0
624+
625+
var count = 0
626+
627+
(3..nums.size).forEach { window ->
628+
nums.toList().windowed(window) {
629+
var isValid = true
630+
val diff = it[1] - it[0]
631+
632+
for (i in 2 until it.size) {
633+
if (it[i] - it[i - 1] != diff) {
634+
isValid = false
635+
break
636+
}
637+
}
638+
639+
if (isValid) count++
640+
}
641+
}
642+
643+
return count
644+
}
645+
646+
647+
549648

550649

551650

0 commit comments

Comments
 (0)