Skip to content

Commit 3ef2824

Browse files
committed
update
1 parent c7ba73b commit 3ef2824

8 files changed

+250
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time: 358 ms
2+
// Memory: 600 KB
3+
// dp | constructive
4+
// T:O(n), S:O(n)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_0732B_The_Best_Friend_Of_a_Man {
9+
public static void main(String[] args) {
10+
Scanner sc = new Scanner(System.in);
11+
int n = sc.nextInt(), k = sc.nextInt(), prev = -1, ret = 0;
12+
int[] arr = new int[n];
13+
for (int i = 0; i < n; i++) {
14+
int a = sc.nextInt();
15+
if (i == 0) {
16+
prev = a;
17+
arr[i] = a;
18+
continue;
19+
}
20+
if (a + prev < k) {
21+
ret += k - a - prev;
22+
arr[i] = k - prev;
23+
} else {
24+
arr[i] = a;
25+
}
26+
prev = arr[i];
27+
}
28+
29+
System.out.println(ret);
30+
for (int i = 0; i < n; i++) {
31+
System.out.print(arr[i]);
32+
if (i != n - 1) {
33+
System.out.print(" ");
34+
} else {
35+
System.out.println();
36+
}
37+
}
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// AC: 280 ms
2+
// Memory: 200 KB
3+
// Greedy. To Cover all possible k-alphabet character formed strings, just repeat "abc..." substring with length k,
4+
// repeat N times, this string can meets the requirement.
5+
// T:O(sum(ni * ki)), S:O(max(ni * ki))
6+
//
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1925A_We_Got_Everything_Covered {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt();
13+
for (int i = 0; i < t; i++) {
14+
int n = sc.nextInt(), k = sc.nextInt();
15+
String alphabetStr = "abcdefghijklmnopqrstuvwxyz";
16+
17+
System.out.println(alphabetStr.substring(0, k).repeat(n));
18+
}
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// AC: 217 ms
2+
// Memory: 0 KB
3+
// .
4+
// T:O(t), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1926A_Vlad_and_the_Best_of_Five {
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+
String s = sc.next();
14+
int countA = 0;
15+
for (char c : s.toCharArray()) {
16+
if (c == 'A') {
17+
countA++;
18+
}
19+
}
20+
21+
System.out.println(countA > s.length() - countA ? 'A' : 'B');
22+
}
23+
}
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// AC: 280 ms
2+
// Memory: 100 KB
3+
// .
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1926B_Vlad_and_Shapes {
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(), firstRowLen = 0, ret = 0;
14+
for (int j = 0; j < n; j++) {
15+
String row = sc.next();
16+
if (ret == 0 && row.contains("1")) {
17+
int countChar = 0;
18+
for (char c : row.toCharArray()) {
19+
if (c == '1') {
20+
countChar++;
21+
}
22+
}
23+
if (firstRowLen == 0) {
24+
firstRowLen = countChar;
25+
} else {
26+
if (countChar != firstRowLen) {
27+
ret = 1;
28+
} else {
29+
ret = -1;
30+
}
31+
}
32+
}
33+
}
34+
System.out.println(ret == 1 ? "TRIANGLE" : "SQUARE");
35+
}
36+
}
37+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// AC: 685 ms
2+
// Memory: 600 KB
3+
// .
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1931B_Make_Equal {
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();
14+
long sum = 0, remain = 0;
15+
int[] arr = new int[n];
16+
for (int j = 0; j < n; j++) {
17+
arr[j] = sc.nextInt();
18+
sum += arr[j];
19+
}
20+
boolean flag = true;
21+
long avg = sum / n;
22+
for (int j = 0; j < n; j++) {
23+
if (arr[j] + remain < avg) {
24+
flag = false;
25+
break;
26+
}
27+
remain = arr[j] + remain - avg;
28+
}
29+
30+
System.out.println(flag ? "YES" : "NO");
31+
}
32+
}
33+
}
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 36.22 MB Beats 100.00% of users with Java
3+
// Recursion.
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public boolean isSameTree(TreeNode p, TreeNode q) {
8+
if (p == null && q == null) {
9+
return true;
10+
}
11+
if ((p != null && q == null) || (p == null && q != null)) {
12+
return false;
13+
}
14+
if (p.val != q.val) {
15+
return false;
16+
}
17+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
18+
}
19+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Runtime 6 ms Beats 42.58% of users with Java
2+
// Memory 44.26 MB Beats 43.54% of users with Java
3+
// Judge from 6 surfaces with each cell, sum up the surface area.
4+
// T:O(n ^ 2), S:O(1)
5+
//
6+
class Solution {
7+
public int surfaceArea(int[][] grid) {
8+
int len = grid.length, ret = 0;
9+
for (int i = 0; i < len; i++) {
10+
for (int j = 0; j < len; j++) {
11+
// top,bottom
12+
if (grid[i][j] > 0) {
13+
ret += 2;
14+
}
15+
// up
16+
if (i == 0) {
17+
ret += grid[i][j];
18+
} else {
19+
if (grid[i - 1][j] < grid[i][j]) {
20+
ret += grid[i][j] - grid[i - 1][j];
21+
}
22+
}
23+
// bottom
24+
if (i == len - 1) {
25+
ret += grid[i][j];
26+
} else {
27+
if (grid[i + 1][j] < grid[i][j]) {
28+
ret += grid[i][j] - grid[i + 1][j];
29+
}
30+
}
31+
// left
32+
if (j == 0) {
33+
ret += grid[i][j];
34+
} else {
35+
if (grid[i][j - 1] < grid[i][j]) {
36+
ret += grid[i][j] - grid[i][j - 1];
37+
}
38+
}
39+
// right
40+
if (j == len - 1) {
41+
ret += grid[i][j];
42+
} else {
43+
if (grid[i][j + 1] < grid[i][j]) {
44+
ret += grid[i][j] - grid[i][j + 1];
45+
}
46+
}
47+
}
48+
}
49+
50+
return ret;
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Runtime 3 ms Beats 50.00% of users with Java
2+
// Memory 42.78 MB Beats 50.00% of users with Java
3+
// Hashmap. Just check every element's occurence <= 2, if not, the answer is false.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public boolean isPossibleToSplit(int[] nums) {
8+
HashMap<Integer, Integer> record = new HashMap<>();
9+
for (int num : nums) {
10+
record.merge(num, 1, Integer::sum);
11+
}
12+
boolean ret = true;
13+
if (record.size() >= nums.length / 2) {
14+
for (int num : record.keySet()) {
15+
if (record.get(num) > 2) {
16+
ret = false;
17+
break;
18+
}
19+
}
20+
} else {
21+
ret = false;
22+
}
23+
24+
return ret;
25+
}
26+
}

0 commit comments

Comments
 (0)