Skip to content

Commit b31f305

Browse files
authored
Improved tasks 999, 1825, 3425
1 parent 093a41b commit b31f305

File tree

5 files changed

+167
-275
lines changed

5 files changed

+167
-275
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,61 @@
11
package g0901_1000.s0999_available_captures_for_rook;
22

3-
// #Easy #Array #Matrix #Simulation #2022_03_31_Time_0_ms_(100.00%)_Space_41.8_MB_(28.74%)
3+
// #Easy #Array #Matrix #Simulation #2025_03_13_Time_0_ms_(100.00%)_Space_40.75_MB_(94.97%)
44

55
@SuppressWarnings("java:S135")
66
public class Solution {
7-
private int[] directions = new int[] {0, 1, 0, -1, 0};
8-
97
public int numRookCaptures(char[][] board) {
10-
int m = board.length;
11-
int n = board[0].length;
12-
int rowR = -1;
13-
int colR = -1;
14-
for (int i = 0; i < m; i++) {
15-
for (int j = 0; j < n; j++) {
8+
// Find the position of the rook
9+
int rookRow = -1;
10+
int rookCol = -1;
11+
for (int i = 0; i < 8; i++) {
12+
for (int j = 0; j < 8; j++) {
1613
if (board[i][j] == 'R') {
17-
rowR = i;
18-
colR = j;
14+
rookRow = i;
15+
rookCol = j;
1916
break;
2017
}
2118
}
22-
}
23-
int[] count = {0};
24-
for (int i = 0; i < 4; i++) {
25-
int neighborRow = rowR + directions[i];
26-
int neighborCol = colR + directions[i + 1];
27-
if (neighborRow >= 0
28-
&& neighborRow < m
29-
&& neighborCol >= 0
30-
&& neighborCol < n
31-
&& board[neighborRow][neighborCol] != 'B') {
32-
if (directions[i] == 0 && directions[i + 1] == 1) {
33-
extracted(board, n, count, neighborRow, neighborCol);
34-
} else if (directions[i] == 1 && directions[i + 1] == 0) {
35-
extracted1(board, m, count, neighborRow, neighborCol);
36-
} else if (directions[i] == 0 && directions[i + 1] == -1) {
37-
extracted(board, count, neighborRow, neighborCol);
38-
} else {
39-
extracted1(board, count, neighborRow, neighborCol);
40-
}
41-
}
42-
}
43-
return count[0];
44-
}
45-
46-
private void extracted(char[][] board, int[] count, int neighborRow, int neighborCol) {
47-
while (neighborCol >= 0) {
48-
if (board[neighborRow][neighborCol] == 'p') {
49-
count[0]++;
50-
break;
51-
} else if (board[neighborRow][neighborCol] == 'B') {
52-
break;
53-
} else {
54-
neighborCol--;
55-
}
56-
}
57-
}
58-
59-
private void extracted(char[][] board, int n, int[] count, int neighborRow, int neighborCol) {
60-
while (neighborCol < n) {
61-
if (board[neighborRow][neighborCol] == 'p') {
62-
count[0]++;
63-
break;
64-
} else if (board[neighborRow][neighborCol] == 'B') {
65-
break;
66-
} else {
67-
neighborCol++;
68-
}
69-
}
70-
}
71-
72-
private void extracted1(char[][] board, int[] count, int neighborRow, int neighborCol) {
73-
while (neighborRow >= 0) {
74-
if (board[neighborRow][neighborCol] == 'p') {
75-
count[0]++;
19+
if (rookRow != -1) {
7620
break;
77-
} else if (board[neighborRow][neighborCol] == 'B') {
78-
break;
79-
} else {
80-
neighborRow--;
8121
}
8222
}
83-
}
84-
85-
private void extracted1(char[][] board, int m, int[] count, int neighborRow, int neighborCol) {
86-
while (neighborRow < m) {
87-
if (board[neighborRow][neighborCol] == 'p') {
88-
count[0]++;
89-
break;
90-
} else if (board[neighborRow][neighborCol] == 'B') {
91-
break;
92-
} else {
93-
neighborRow++;
23+
// Define the four directions: up, right, down, left
24+
int[][] directions = {
25+
// up
26+
{-1, 0},
27+
// right
28+
{0, 1},
29+
// down
30+
{1, 0},
31+
// left
32+
{0, -1}
33+
};
34+
int captureCount = 0;
35+
// Check each direction
36+
for (int[] dir : directions) {
37+
int row = rookRow;
38+
int col = rookCol;
39+
while (true) {
40+
// Move one step in the current direction
41+
row += dir[0];
42+
col += dir[1];
43+
// Check if out of bounds
44+
if (row < 0 || row >= 8 || col < 0 || col >= 8) {
45+
break;
46+
}
47+
// If we hit a bishop, we're blocked
48+
if (board[row][col] == 'B') {
49+
break;
50+
}
51+
// If we hit a pawn, we can capture it and then we're blocked
52+
if (board[row][col] == 'p') {
53+
captureCount++;
54+
break;
55+
}
56+
// Otherwise (empty square), continue in the same direction
9457
}
9558
}
59+
return captureCount;
9660
}
9761
}
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,70 @@
11
package g1801_1900.s1825_finding_mk_average;
22

33
// #Hard #Design #Heap_Priority_Queue #Ordered_Set #Queue
4-
// #2022_05_06_Time_83_ms_(60.59%)_Space_96.3_MB_(77.83%)
4+
// #2025_03_13_Time_37_ms_(100.00%)_Space_100.84_MB_(46.09%)
55

6-
import java.util.ArrayDeque;
7-
import java.util.Deque;
8-
import java.util.TreeMap;
6+
import java.util.LinkedList;
7+
import java.util.TreeSet;
98

10-
@SuppressWarnings("java:S2184")
119
public class MKAverage {
12-
private final double m;
13-
private final double k;
14-
private final double c;
15-
private double avg;
16-
private final Bst middle;
17-
private final Bst min;
18-
private final Bst max;
19-
private final Deque<Integer> q;
10+
private final int capacity;
11+
private final int boundary;
12+
private final int[] nums;
13+
private final TreeSet<Integer> numSet;
14+
private final LinkedList<Integer> order;
2015

2116
public MKAverage(int m, int k) {
22-
this.m = m;
23-
this.k = k;
24-
this.c = m - k * 2;
25-
this.avg = 0;
26-
this.middle = new Bst();
27-
this.min = new Bst();
28-
this.max = new Bst();
29-
this.q = new ArrayDeque<>();
17+
this.capacity = m;
18+
this.boundary = k;
19+
nums = new int[100001];
20+
numSet = new TreeSet<>();
21+
order = new LinkedList<>();
3022
}
3123

3224
public void addElement(int num) {
33-
if (min.size < k) {
34-
min.add(num);
35-
q.offer(num);
36-
return;
37-
}
38-
if (max.size < k) {
39-
min.add(num);
40-
max.add(min.removeMax());
41-
q.offer(num);
42-
return;
43-
}
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-
int val = min.removeMax();
51-
middle.add(val);
52-
avg += val / c;
53-
} else if (num > max.firstKey()) {
54-
max.add(num);
55-
int val = max.removeMin();
56-
middle.add(val);
57-
avg += val / c;
58-
}
59-
60-
q.offer(num);
61-
62-
if (q.size() > m) {
63-
num = q.poll();
64-
if (middle.containsKey(num)) {
65-
avg -= num / c;
66-
middle.remove(num);
67-
} else if (min.containsKey(num)) {
68-
min.remove(num);
69-
int val = middle.removeMin();
70-
avg -= val / c;
71-
min.add(val);
72-
} else if (max.containsKey(num)) {
73-
max.remove(num);
74-
int val = middle.removeMax();
75-
avg -= val / c;
76-
max.add(val);
25+
if (order.size() == capacity) {
26+
int numToDelete = order.removeFirst();
27+
nums[numToDelete] = nums[numToDelete] - 1;
28+
if (nums[numToDelete] == 0) {
29+
numSet.remove(numToDelete);
7730
}
7831
}
32+
nums[num]++;
33+
numSet.add(num);
34+
order.add(num);
7935
}
8036

8137
public int calculateMKAverage() {
82-
if (q.size() < m) {
83-
return -1;
84-
}
85-
return (int) avg;
86-
}
87-
88-
static class Bst {
89-
TreeMap<Integer, Integer> map;
90-
int size;
91-
92-
public Bst() {
93-
this.map = new TreeMap<>();
94-
this.size = 0;
95-
}
96-
97-
void add(int num) {
98-
int count = map.getOrDefault(num, 0) + 1;
99-
map.put(num, count);
100-
size++;
101-
}
102-
103-
void remove(int num) {
104-
int count = map.getOrDefault(num, 1) - 1;
105-
if (count > 0) {
106-
map.put(num, count);
107-
} else {
108-
map.remove(num);
38+
if (order.size() == capacity) {
39+
int skipCount = boundary;
40+
int numsCount = capacity - 2 * boundary;
41+
int freq = capacity - 2 * boundary;
42+
int sum = 0;
43+
for (int num : numSet) {
44+
int count = nums[num];
45+
if (skipCount < 0) {
46+
sum += num * Math.min(count, numsCount);
47+
numsCount -= Math.min(count, numsCount);
48+
} else {
49+
skipCount -= count;
50+
if (skipCount < 0) {
51+
sum += num * Math.min(Math.abs(skipCount), numsCount);
52+
numsCount -= Math.min(Math.abs(skipCount), numsCount);
53+
}
54+
}
55+
if (numsCount == 0) {
56+
break;
57+
}
10958
}
110-
size--;
111-
}
112-
113-
int removeMin() {
114-
int key = map.firstKey();
115-
116-
remove(key);
117-
118-
return key;
119-
}
120-
121-
int removeMax() {
122-
int key = map.lastKey();
123-
124-
remove(key);
125-
126-
return key;
127-
}
128-
129-
boolean containsKey(int key) {
130-
return map.containsKey(key);
131-
}
132-
133-
int firstKey() {
134-
return map.firstKey();
135-
}
136-
137-
int lastKey() {
138-
return map.lastKey();
59+
return sum / freq;
13960
}
61+
return -1;
14062
}
14163
}
64+
65+
/*
66+
* Your MKAverage object will be instantiated and called as such:
67+
* MKAverage obj = new MKAverage(m, k);
68+
* obj.addElement(num);
69+
* int param_2 = obj.calculateMKAverage();
70+
*/

0 commit comments

Comments
 (0)