Skip to content

Improved tasks 999, 1825, 3425, 3441 #775

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 13, 2025
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
Original file line number Diff line number Diff line change
@@ -1,98 +1,56 @@
package g0901_1000.s0999_available_captures_for_rook

// #Easy #Array #Matrix #Simulation #2023_05_13_Time_143_ms_(80.00%)_Space_34.6_MB_(60.00%)
// #Easy #Array #Matrix #Simulation #2025_03_13_Time_0_ms_(100.00%)_Space_40.08_MB_(8.33%)

@Suppress("NAME_SHADOWING")
class Solution {
private val directions = intArrayOf(0, 1, 0, -1, 0)
fun numRookCaptures(board: Array<CharArray>): Int {
val m = board.size
val n = board[0].size
var rowR = -1
var colR = -1
for (i in 0 until m) {
for (j in 0 until n) {
// Find the position of the rook
var rookRow = -1
var rookCol = -1
for (i in 0..7) {
for (j in 0..7) {
if (board[i][j] == 'R') {
rowR = i
colR = j
rookRow = i
rookCol = j
break
}
}
}
val count = intArrayOf(0)
for (i in 0..3) {
val neighborRow = rowR + directions[i]
val neighborCol = colR + directions[i + 1]
if (neighborRow in 0 until m && neighborCol >= 0 && neighborCol < n &&
board[neighborRow][neighborCol] != 'B'
) {
if (directions[i] == 0 && directions[i + 1] == 1) {
extracted(board, n, count, neighborRow, neighborCol)
} else if (directions[i] == 1 && directions[i + 1] == 0) {
extracted1(board, m, count, neighborRow, neighborCol)
} else if (directions[i] == 0 && directions[i + 1] == -1) {
extracted(board, count, neighborRow, neighborCol)
} else {
extracted1(board, count, neighborRow, neighborCol)
}
}
}
return count[0]
}

private fun extracted(board: Array<CharArray>, count: IntArray, neighborRow: Int, neighborCol: Int) {
var neighborCol = neighborCol
while (neighborCol >= 0) {
if (board[neighborRow][neighborCol] == 'p') {
count[0]++
if (rookRow != -1) {
break
} else if (board[neighborRow][neighborCol] == 'B') {
break
} else {
neighborCol--
}
}
}

private fun extracted(board: Array<CharArray>, n: Int, count: IntArray, neighborRow: Int, neighborCol: Int) {
var neighborCol = neighborCol
while (neighborCol < n) {
if (board[neighborRow][neighborCol] == 'p') {
count[0]++
break
} else if (board[neighborRow][neighborCol] == 'B') {
break
} else {
neighborCol++
}
}
}

private fun extracted1(board: Array<CharArray>, count: IntArray, neighborRow: Int, neighborCol: Int) {
var neighborRow = neighborRow
while (neighborRow >= 0) {
if (board[neighborRow][neighborCol] == 'p') {
count[0]++
break
} else if (board[neighborRow][neighborCol] == 'B') {
break
} else {
neighborRow--
}
}
}

private fun extracted1(board: Array<CharArray>, m: Int, count: IntArray, neighborRow: Int, neighborCol: Int) {
var neighborRow = neighborRow
while (neighborRow < m) {
if (board[neighborRow][neighborCol] == 'p') {
count[0]++
break
} else if (board[neighborRow][neighborCol] == 'B') {
break
} else {
neighborRow++
// Define the four directions: up, right, down, left
val directions = arrayOf<IntArray>( // up
intArrayOf(-1, 0), // right
intArrayOf(0, 1), // down
intArrayOf(1, 0), // left
intArrayOf(0, -1),
)
var captureCount = 0
// Check each direction
for (dir in directions) {
var row = rookRow
var col = rookCol
while (true) {
// Move one step in the current direction
row += dir[0]
col += dir[1]
// Check if out of bounds
if (row < 0 || row >= 8 || col < 0 || col >= 8) {
break
}
// If we hit a bishop, we're blocked
if (board[row][col] == 'B') {
break
}
// If we hit a pawn, we can capture it and then we're blocked
if (board[row][col] == 'p') {
captureCount++
break
}
// Otherwise (empty square), continue in the same direction
}
}
return captureCount
}
}
159 changes: 45 additions & 114 deletions src/main/kotlin/g1801_1900/s1825_finding_mk_average/MKAverage.kt
Original file line number Diff line number Diff line change
@@ -1,131 +1,62 @@
package g1801_1900.s1825_finding_mk_average

// #Hard #Design #Heap_Priority_Queue #Ordered_Set #Queue
// #2023_06_21_Time_1101_ms_(100.00%)_Space_122.8_MB_(100.00%)
// #2025_03_13_Time_69_ms_(100.00%)_Space_98.49_MB_(100.00%)

import java.util.Deque
import java.util.LinkedList
import java.util.TreeMap
import java.util.TreeSet
import kotlin.math.abs
import kotlin.math.min

@Suppress("NAME_SHADOWING")
class MKAverage(m: Int, k: Int) {
private val m: Double
private val k: Double
private val c: Double
private var avg: Double
private val middle: Bst
private val min: Bst
private val max: Bst
private val q: Deque<Int>

init {
this.m = m.toDouble()
this.k = k.toDouble()
c = (m - k * 2).toDouble()
avg = 0.0
middle = Bst()
min = Bst()
max = Bst()
q = LinkedList()
}
class MKAverage(private val capacity: Int, private val boundary: Int) {
private val nums: IntArray = IntArray(100001)
private val numSet: TreeSet<Int> = TreeSet<Int>()
private val order: LinkedList<Int> = LinkedList<Int>()

fun addElement(num: Int) {
var num = num
if (min.size < k) {
min.add(num)
q.offer(num)
return
}
if (max.size < k) {
min.add(num)
max.add(min.removeMax())
q.offer(num)
return
}
if (num >= min.lastKey() && num <= max.firstKey()) {
middle.add(num)
avg += num / c
} else if (num < min.lastKey()) {
min.add(num)
val `val` = min.removeMax()
middle.add(`val`)
avg += `val` / c
} else if (num > max.firstKey()) {
max.add(num)
val `val` = max.removeMin()
middle.add(`val`)
avg += `val` / c
}
q.offer(num)
if (q.size > m) {
num = q.poll()
if (middle.containsKey(num)) {
avg -= num / c
middle.remove(num)
} else if (min.containsKey(num)) {
min.remove(num)
val `val` = middle.removeMin()
avg -= `val` / c
min.add(`val`)
} else if (max.containsKey(num)) {
max.remove(num)
val `val` = middle.removeMax()
avg -= `val` / c
max.add(`val`)
if (order.size == capacity) {
val numToDelete = order.removeFirst()
nums[numToDelete] = nums[numToDelete] - 1
if (nums[numToDelete] == 0) {
numSet.remove(numToDelete)
}
}
nums[num]++
numSet.add(num)
order.add(num)
}

fun calculateMKAverage(): Int {
return if (q.size < m) {
-1
} else {
avg.toInt()
}
}

internal class Bst {
var map: TreeMap<Int, Int> = TreeMap()
var size: Int = 0

fun add(num: Int) {
val count = map.getOrDefault(num, 0) + 1
map[num] = count
size++
}

fun remove(num: Int) {
val count = map.getOrDefault(num, 1) - 1
if (count > 0) {
map[num] = count
} else {
map.remove(num)
if (order.size == capacity) {
var skipCount = boundary
var numsCount = capacity - 2 * boundary
val freq = capacity - 2 * boundary
var sum = 0
for (num in numSet) {
val count = nums[num]
if (skipCount < 0) {
sum += num * min(count, numsCount)
numsCount = (numsCount - min(count, numsCount)).toInt()
} else {
skipCount -= count
if (skipCount < 0) {
sum += num * min(abs(skipCount), numsCount)
numsCount = (numsCount - min(abs(skipCount), numsCount)).toInt()
}
}
if (numsCount == 0) {
break
}
}
size--
}

fun removeMin(): Int {
val key = map.firstKey()
remove(key)
return key
}

fun removeMax(): Int {
val key = map.lastKey()
remove(key)
return key
}

fun containsKey(key: Int): Boolean {
return map.containsKey(key)
}

fun firstKey(): Int {
return map.firstKey()
}

fun lastKey(): Int {
return map.lastKey()
return sum / freq
}
return -1
}
}

/*
* Your MKAverage object will be instantiated and called as such:
* var obj = MKAverage(m, k)
* obj.addElement(num)
* var param_2 = obj.calculateMKAverage()
*/
Loading