Skip to content

Commit 322b586

Browse files
committed
update
1 parent 6712a58 commit 322b586

7 files changed

+183
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// AC: 327 ms
2+
// Memory: 0 KB
3+
// Constructive.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1914B_Preparing_for_the_Contest {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
for (int i = 0; i < t; i++) {
13+
int n = sc.nextInt(), k = sc.nextInt(), cur = n;
14+
StringBuilder ret = new StringBuilder();
15+
for (int j = 0; j < n - k - 1; j++) {
16+
ret.append(cur--);
17+
ret.append(" ");
18+
}
19+
ret.append(1);
20+
if (k != 0) {
21+
ret.append(" ");
22+
}
23+
cur = 2;
24+
for (int j = 0; j < k; j++) {
25+
ret.append(cur++);
26+
if (j != k - 1) {
27+
ret.append(" ");
28+
}
29+
}
30+
31+
System.out.println(ret);
32+
}
33+
}
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// AC: 342 ms
2+
// Memory: 400 KB
3+
// All bricks place horizontally, if m % 2, let (m / 2 - 1) be 1✖️2,the last be 1✖️3.
4+
// T:O(t), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1918A_Brick_Wall {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
for (int i = 0; i < t; i++) {
13+
int n = sc.nextInt(), m = sc.nextInt();
14+
System.out.println(n * (m / 2));
15+
}
16+
}
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// AC: 280 ms
2+
// Memory: 100 KB
3+
// .
4+
// T:O(sum(ni)), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1927A_Make_it_White {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int t = sc.nextInt();
12+
for (int i = 0; i < t; i++) {
13+
int n = sc.nextInt(), left = -1, right = -1;
14+
String s = sc.next();
15+
for (int j = 0; j < n; j++) {
16+
if (s.charAt(j) == 'B') {
17+
left = j;
18+
break;
19+
}
20+
}
21+
for (int j = n - 1; j >= 0; j--) {
22+
if (s.charAt(j) == 'B') {
23+
right = j;
24+
break;
25+
}
26+
}
27+
28+
System.out.println(left == -1 ? 0 : (right - left + 1));
29+
}
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Runtime 0 ms Beats 100.00% of users with Java
2+
// Memory 41.68 MB Beats 95.33% of users with Java
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int returnToBoundaryCount(int[] nums) {
8+
int cur = 0, ret = 0;
9+
for (int num : nums) {
10+
cur += num;
11+
if (cur == 0) {
12+
ret++;
13+
}
14+
}
15+
16+
return ret;
17+
}
18+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 45.32 MB Beats 80.24% of users with Java
3+
// .
4+
// T:O(m * n), S:O(n)
5+
//
6+
class Solution {
7+
public int[][] modifiedMatrix(int[][] matrix) {
8+
int row = matrix.length, col = matrix[0].length;
9+
int[] colMax = new int[col];
10+
for (int i = 0; i < col; i++) {
11+
int colMaxElem = Integer.MIN_VALUE;
12+
for (int[] ints : matrix) {
13+
colMaxElem = Math.max(colMaxElem, ints[i]);
14+
}
15+
colMax[i] = colMaxElem;
16+
}
17+
18+
for (int i = 0; i < row; i++) {
19+
for (int j = 0; j < col; j++) {
20+
if (matrix[i][j] == -1) {
21+
matrix[i][j] = colMax[j];
22+
}
23+
}
24+
}
25+
26+
return matrix;
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Runtime 0 ms Beats 100.00% of users with Java
2+
// Memory 41.60 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int maxOperations(int[] nums) {
8+
int ret = 1, sum1 = nums[0] + nums[1];
9+
for (int i = 2; i + 1 < nums.length; i += 2) {
10+
if (nums[i] + nums[i + 1] == sum1) {
11+
ret++;
12+
} else {
13+
break;
14+
}
15+
}
16+
17+
return ret;
18+
}
19+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Runtime 142 ms Beats 33.33% of users with Java
2+
// Memory 56.10 MB Beats 33.33% of users with Java
3+
// Hashmap.
4+
// T:O(1), S:O(1)
5+
//
6+
class Solution {
7+
public String lastNonEmptyString(String s) {
8+
HashMap<Character, Integer> record = new HashMap<>();
9+
for (char c : s.toCharArray()) {
10+
record.merge(c, 1, Integer::sum);
11+
}
12+
int len = record.size(), pos = 0, maxCount = 0;
13+
int[][] count = new int[len][2];
14+
for (char c : record.keySet()) {
15+
count[pos++] = new int[]{c - 'a', record.get(c)};
16+
maxCount = Math.max(maxCount, record.get(c));
17+
}
18+
Arrays.sort(count, Comparator.comparingInt(a -> a[1]));
19+
StringBuilder ret = new StringBuilder();
20+
21+
HashSet<Character> added = new HashSet<>();
22+
for (int i = s.length() - 1; i >= 0; i--) {
23+
char c = s.charAt(i);
24+
if (!added.contains(c) && record.get(c) == maxCount) {
25+
ret.append(c);
26+
added.add(c);
27+
if (added.size() == 26) {
28+
break;
29+
}
30+
}
31+
}
32+
ret.reverse();
33+
34+
return ret.toString();
35+
}
36+
}

0 commit comments

Comments
 (0)