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


import com.github.contest.array.isZeroArray
import com.github.contest.math.numberOfPowerfulInt
import com.github.contest.slidingWindow.customStructure.rabinKarpMultiPattern
import com.github.contest.slidingWindow.customStructure.slidingWindowClassic
import com.github.contest.slidingWindow.maximumRobots
import com.github.contest.strings.fullJustify
import com.github.contest.strings.subStrHash
import java.util.TreeMap
Expand All @@ -15,11 +15,7 @@ import java.util.TreeMap
*/

fun main() {

val nums = intArrayOf(3, 3, 2, 1)
val queries = arrayOf(intArrayOf(1, 3), intArrayOf(0, 2), intArrayOf(0, 0), intArrayOf(0, 0))

isZeroArray(nums, queries).also { println(it) }
maximumRobots(intArrayOf(3, 6, 1, 3, 4), intArrayOf(2, 1, 3, 4, 5), 25).also { println(it) }
}


Expand Down
20 changes: 20 additions & 0 deletions contest/src/main/java/com/github/contest/array/ArrayLeetcode.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.contest.array

import java.util.PriorityQueue
import kotlin.math.abs


/**
Expand Down Expand Up @@ -403,4 +404,23 @@ fun isZeroArray(nums: IntArray, queries: Array<IntArray>): Boolean {
return true
}

/**
* 2932. Maximum Strong Pair XOR I
*/

fun maximumStrongPairXor(nums: IntArray): Int {
val pairs = mutableListOf<Pair<Int, Int>>()

for (i in nums.indices) {
pairs.add(Pair(nums[i], nums[i]))
for (j in i + 1 until nums.size) {
pairs.add(Pair(nums[i], nums[j]))
}
}

return pairs
.filter { abs(it.first - it.second) <= minOf(it.first, it.second) }
.maxOf { it.first xor it.second }
}


Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,33 @@ fun getSubArrayBeautyAlternativeSolution(nums: IntArray, k: Int, x: Int): IntArr
return res.toIntArray()
}

/**
* 413. Arithmetic Slices
* Alternative Solution with O(n)
*/

fun numberOfArithmeticSlicesAlternativeSolution(nums: IntArray): Int {
if (nums.size < 3) return 0

var left = 0
var count = 0
val k = 3
var diff = nums[1] - nums[0]

for (right in 2 until nums.size) {

if (nums[right] - nums[right - 1] != diff) {
diff = nums[right] - nums[right - 1]
left = right - 1
}

if (right - left >= k - 1) {
count++
count += ((right - left) - (k - 1))
}
}

return count
}


Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,105 @@ fun maxSatisfied(customers: IntArray, grumpy: IntArray, minutes: Int): Int {
return maxCustomers
}

/**
* 3090. Maximum Length Substring With Two Occurrences
*/

fun maximumLengthSubstring(s: String): Int {
if (s.length == 2) return 2

var maxLen = 0
var left = 0
val freq = mutableMapOf<Char, Int>()

for (right in s.indices) {

val char = s[right]
freq[char] = freq.getOrDefault(char, 0) + 1

if (!isValidSubString(freq)) {
freq[s[left]] = freq.getOrDefault(s[left], 0) - 1
if (freq[s[left]] == 0) freq.remove(s[left])
left++
}

maxLen = maxOf(maxLen, right - left + 1)
}

return maxLen
}

private fun isValidSubString(map: Map<Char, Int>): Boolean {

for ((_, value) in map) {
if (value > 2) return false
}

return true
}

/**
* 2398. Maximum Number of Robots Within Budget
*/

fun maximumRobots(chargeTimes: IntArray, runningCosts: IntArray, budget: Long): Int {
var maxRobots = 0
var left = 0
var sum = 0L
val window = mutableListOf<Long>()

for (right in chargeTimes.indices) {
window.add(chargeTimes[right].toLong())
sum += runningCosts[right].toLong()
var max = window.maxOrNull() ?: 0L
var k = (right - left + 1).toLong()
var cost = max + (k * sum)

while (cost > budget) {
sum -= runningCosts[left]
window.removeFirst()
left++
max = window.maxOrNull() ?: 0L
k = (right - left + 1).toLong()
cost = max + (k * sum)
}

maxRobots = maxOf(maxRobots, right - left + 1)
}

return maxRobots
}

/**
* 413. Arithmetic Slices
*/

fun numberOfArithmeticSlices(nums: IntArray): Int {
if (nums.size < 3) return 0

var count = 0

(3..nums.size).forEach { window ->
nums.toList().windowed(window) {
var isValid = true
val diff = it[1] - it[0]

for (i in 2 until it.size) {
if (it[i] - it[i - 1] != diff) {
isValid = false
break
}
}

if (isValid) count++
}
}

return count
}






Expand Down