Skip to content

Commit 1417b8e

Browse files
authored
Improved tasks 999, 1825, 3425, 3441
1 parent e31e5c1 commit 1417b8e

File tree

4 files changed

+184
-418
lines changed

4 files changed

+184
-418
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,56 @@
11
package g0901_1000.s0999_available_captures_for_rook
22

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

5-
@Suppress("NAME_SHADOWING")
65
class Solution {
7-
private val directions = intArrayOf(0, 1, 0, -1, 0)
86
fun numRookCaptures(board: Array<CharArray>): Int {
9-
val m = board.size
10-
val n = board[0].size
11-
var rowR = -1
12-
var colR = -1
13-
for (i in 0 until m) {
14-
for (j in 0 until n) {
7+
// Find the position of the rook
8+
var rookRow = -1
9+
var rookCol = -1
10+
for (i in 0..7) {
11+
for (j in 0..7) {
1512
if (board[i][j] == 'R') {
16-
rowR = i
17-
colR = j
13+
rookRow = i
14+
rookCol = j
1815
break
1916
}
2017
}
21-
}
22-
val count = intArrayOf(0)
23-
for (i in 0..3) {
24-
val neighborRow = rowR + directions[i]
25-
val neighborCol = colR + directions[i + 1]
26-
if (neighborRow in 0 until m && neighborCol >= 0 && neighborCol < n &&
27-
board[neighborRow][neighborCol] != 'B'
28-
) {
29-
if (directions[i] == 0 && directions[i + 1] == 1) {
30-
extracted(board, n, count, neighborRow, neighborCol)
31-
} else if (directions[i] == 1 && directions[i + 1] == 0) {
32-
extracted1(board, m, count, neighborRow, neighborCol)
33-
} else if (directions[i] == 0 && directions[i + 1] == -1) {
34-
extracted(board, count, neighborRow, neighborCol)
35-
} else {
36-
extracted1(board, count, neighborRow, neighborCol)
37-
}
38-
}
39-
}
40-
return count[0]
41-
}
42-
43-
private fun extracted(board: Array<CharArray>, count: IntArray, neighborRow: Int, neighborCol: Int) {
44-
var neighborCol = neighborCol
45-
while (neighborCol >= 0) {
46-
if (board[neighborRow][neighborCol] == 'p') {
47-
count[0]++
18+
if (rookRow != -1) {
4819
break
49-
} else if (board[neighborRow][neighborCol] == 'B') {
50-
break
51-
} else {
52-
neighborCol--
5320
}
5421
}
55-
}
56-
57-
private fun extracted(board: Array<CharArray>, n: Int, count: IntArray, neighborRow: Int, neighborCol: Int) {
58-
var neighborCol = neighborCol
59-
while (neighborCol < n) {
60-
if (board[neighborRow][neighborCol] == 'p') {
61-
count[0]++
62-
break
63-
} else if (board[neighborRow][neighborCol] == 'B') {
64-
break
65-
} else {
66-
neighborCol++
67-
}
68-
}
69-
}
70-
71-
private fun extracted1(board: Array<CharArray>, count: IntArray, neighborRow: Int, neighborCol: Int) {
72-
var neighborRow = neighborRow
73-
while (neighborRow >= 0) {
74-
if (board[neighborRow][neighborCol] == 'p') {
75-
count[0]++
76-
break
77-
} else if (board[neighborRow][neighborCol] == 'B') {
78-
break
79-
} else {
80-
neighborRow--
81-
}
82-
}
83-
}
84-
85-
private fun extracted1(board: Array<CharArray>, m: Int, count: IntArray, neighborRow: Int, neighborCol: Int) {
86-
var neighborRow = neighborRow
87-
while (neighborRow < m) {
88-
if (board[neighborRow][neighborCol] == 'p') {
89-
count[0]++
90-
break
91-
} else if (board[neighborRow][neighborCol] == 'B') {
92-
break
93-
} else {
94-
neighborRow++
22+
// Define the four directions: up, right, down, left
23+
val directions = arrayOf<IntArray>( // up
24+
intArrayOf(-1, 0), // right
25+
intArrayOf(0, 1), // down
26+
intArrayOf(1, 0), // left
27+
intArrayOf(0, -1),
28+
)
29+
var captureCount = 0
30+
// Check each direction
31+
for (dir in directions) {
32+
var row = rookRow
33+
var col = rookCol
34+
while (true) {
35+
// Move one step in the current direction
36+
row += dir[0]
37+
col += dir[1]
38+
// Check if out of bounds
39+
if (row < 0 || row >= 8 || col < 0 || col >= 8) {
40+
break
41+
}
42+
// If we hit a bishop, we're blocked
43+
if (board[row][col] == 'B') {
44+
break
45+
}
46+
// If we hit a pawn, we can capture it and then we're blocked
47+
if (board[row][col] == 'p') {
48+
captureCount++
49+
break
50+
}
51+
// Otherwise (empty square), continue in the same direction
9552
}
9653
}
54+
return captureCount
9755
}
9856
}
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,62 @@
11
package g1801_1900.s1825_finding_mk_average
22

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

6-
import java.util.Deque
76
import java.util.LinkedList
8-
import java.util.TreeMap
7+
import java.util.TreeSet
8+
import kotlin.math.abs
9+
import kotlin.math.min
910

10-
@Suppress("NAME_SHADOWING")
11-
class MKAverage(m: Int, k: Int) {
12-
private val m: Double
13-
private val k: Double
14-
private val c: Double
15-
private var avg: Double
16-
private val middle: Bst
17-
private val min: Bst
18-
private val max: Bst
19-
private val q: Deque<Int>
20-
21-
init {
22-
this.m = m.toDouble()
23-
this.k = k.toDouble()
24-
c = (m - k * 2).toDouble()
25-
avg = 0.0
26-
middle = Bst()
27-
min = Bst()
28-
max = Bst()
29-
q = LinkedList()
30-
}
11+
class MKAverage(private val capacity: Int, private val boundary: Int) {
12+
private val nums: IntArray = IntArray(100001)
13+
private val numSet: TreeSet<Int> = TreeSet<Int>()
14+
private val order: LinkedList<Int> = LinkedList<Int>()
3115

3216
fun addElement(num: Int) {
33-
var num = num
34-
if (min.size < k) {
35-
min.add(num)
36-
q.offer(num)
37-
return
38-
}
39-
if (max.size < k) {
40-
min.add(num)
41-
max.add(min.removeMax())
42-
q.offer(num)
43-
return
44-
}
45-
if (num >= min.lastKey() && num <= max.firstKey()) {
46-
middle.add(num)
47-
avg += num / c
48-
} else if (num < min.lastKey()) {
49-
min.add(num)
50-
val `val` = min.removeMax()
51-
middle.add(`val`)
52-
avg += `val` / c
53-
} else if (num > max.firstKey()) {
54-
max.add(num)
55-
val `val` = max.removeMin()
56-
middle.add(`val`)
57-
avg += `val` / c
58-
}
59-
q.offer(num)
60-
if (q.size > m) {
61-
num = q.poll()
62-
if (middle.containsKey(num)) {
63-
avg -= num / c
64-
middle.remove(num)
65-
} else if (min.containsKey(num)) {
66-
min.remove(num)
67-
val `val` = middle.removeMin()
68-
avg -= `val` / c
69-
min.add(`val`)
70-
} else if (max.containsKey(num)) {
71-
max.remove(num)
72-
val `val` = middle.removeMax()
73-
avg -= `val` / c
74-
max.add(`val`)
17+
if (order.size == capacity) {
18+
val numToDelete = order.removeFirst()
19+
nums[numToDelete] = nums[numToDelete] - 1
20+
if (nums[numToDelete] == 0) {
21+
numSet.remove(numToDelete)
7522
}
7623
}
24+
nums[num]++
25+
numSet.add(num)
26+
order.add(num)
7727
}
7828

7929
fun calculateMKAverage(): Int {
80-
return if (q.size < m) {
81-
-1
82-
} else {
83-
avg.toInt()
84-
}
85-
}
86-
87-
internal class Bst {
88-
var map: TreeMap<Int, Int> = TreeMap()
89-
var size: Int = 0
90-
91-
fun add(num: Int) {
92-
val count = map.getOrDefault(num, 0) + 1
93-
map[num] = count
94-
size++
95-
}
96-
97-
fun remove(num: Int) {
98-
val count = map.getOrDefault(num, 1) - 1
99-
if (count > 0) {
100-
map[num] = count
101-
} else {
102-
map.remove(num)
30+
if (order.size == capacity) {
31+
var skipCount = boundary
32+
var numsCount = capacity - 2 * boundary
33+
val freq = capacity - 2 * boundary
34+
var sum = 0
35+
for (num in numSet) {
36+
val count = nums[num]
37+
if (skipCount < 0) {
38+
sum += num * min(count, numsCount)
39+
numsCount = (numsCount - min(count, numsCount)).toInt()
40+
} else {
41+
skipCount -= count
42+
if (skipCount < 0) {
43+
sum += num * min(abs(skipCount), numsCount)
44+
numsCount = (numsCount - min(abs(skipCount), numsCount)).toInt()
45+
}
46+
}
47+
if (numsCount == 0) {
48+
break
49+
}
10350
}
104-
size--
105-
}
106-
107-
fun removeMin(): Int {
108-
val key = map.firstKey()
109-
remove(key)
110-
return key
111-
}
112-
113-
fun removeMax(): Int {
114-
val key = map.lastKey()
115-
remove(key)
116-
return key
117-
}
118-
119-
fun containsKey(key: Int): Boolean {
120-
return map.containsKey(key)
121-
}
122-
123-
fun firstKey(): Int {
124-
return map.firstKey()
125-
}
126-
127-
fun lastKey(): Int {
128-
return map.lastKey()
51+
return sum / freq
12952
}
53+
return -1
13054
}
13155
}
56+
57+
/*
58+
* Your MKAverage object will be instantiated and called as such:
59+
* var obj = MKAverage(m, k)
60+
* obj.addElement(num)
61+
* var param_2 = obj.calculateMKAverage()
62+
*/

0 commit comments

Comments
 (0)