Skip to content

Commit 5fa214a

Browse files
committed
update
1 parent 47da9c8 commit 5fa214a

7 files changed

+243
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// AC: 952 ms
2+
// Memory: 800 KB
3+
// .
4+
// T:O(nlog(max(ai))), S:O(n)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_1675B_Make_It_Increasing {
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(), ret = 0;
14+
int[] arr = new int[n];
15+
boolean flag = true;
16+
for (int j = 0; j < n; j++) {
17+
int a = sc.nextInt();
18+
arr[j] = a;
19+
}
20+
for (int j = n - 2; j >= 0; j--) {
21+
if (arr[j + 1] < 1) {
22+
flag = false;
23+
break;
24+
}
25+
while (arr[j] >= arr[j + 1]) {
26+
arr[j] /= 2;
27+
ret++;
28+
}
29+
if (arr[j] < j) {
30+
flag = false;
31+
break;
32+
}
33+
}
34+
35+
System.out.println(flag ? ret : -1);
36+
}
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// AC: 608 ms
2+
// Memory: 14100 KB
3+
// Hashset.
4+
// T:O(sum(ni * k)), S:O(max(ni * k)), k = string length.
5+
//
6+
import java.util.HashSet;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1703D_Double_Strings {
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();
15+
HashSet<String> record = new HashSet<>();
16+
String[] arr = new String[n];
17+
for (int j = 0; j < n; j++) {
18+
String s = sc.next();
19+
record.add(s);
20+
arr[j] = s;
21+
}
22+
StringBuilder ret = new StringBuilder();
23+
for (int j = 0; j < n; j++) {
24+
String item = arr[j];
25+
boolean flag = false;
26+
for (int k = 0; k < item.length() - 1; k++) {
27+
String s1 = item.substring(0, k + 1), s2 = item.substring(k + 1);
28+
if (record.contains(s1) && record.contains(s2)) {
29+
flag = true;
30+
break;
31+
}
32+
}
33+
ret.append(flag ? 1 : 0);
34+
}
35+
36+
System.out.println(ret);
37+
}
38+
}
39+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Runtime 8 ms Beats 39.83% of users with Java
2+
// Memory 45.75 MB Beats 7.76% of users with Java
3+
// Recursion: Notice the path must starting from leaf node. We should always append the current char to the judge string front.
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public String smallestFromLeaf(TreeNode root) {
8+
return solve(root, "");
9+
}
10+
11+
public String solve(TreeNode root, String cur) {
12+
char currentChar = (char) (root.val + 'a');
13+
if (root.left == null && root.right == null) {
14+
return currentChar + cur;
15+
}
16+
if (root.left == null) {
17+
return solve(root.right, currentChar + cur);
18+
}
19+
if (root.right == null) {
20+
return solve(root.left, currentChar + cur);
21+
}
22+
23+
String s1 = solve(root.left, currentChar + cur);
24+
String s2 = solve(root.right, currentChar + cur);
25+
return s1.compareTo(s2) <= 0 ? s1 : s2;
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 41.92 MB Beats 50.00% of users with Java
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
class Solution {
7+
public int scoreOfString(String s) {
8+
int ret = 0;
9+
for (int i = 0; i < s.length() - 1; i++) {
10+
ret += Math.abs(s.charAt(i) - s.charAt(i + 1));
11+
}
12+
13+
return ret;
14+
}
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Runtime 18 ms Beats 50.00% of users with Java
2+
// Memory 81.02 MB Beats 100.00% of users with Java
3+
// HashSet & sort.
4+
// T:O(nlogn), S:O(n)
5+
//
6+
class Solution {
7+
public int minRectanglesToCoverPoints(int[][] points, int w) {
8+
int ret = 0;
9+
HashSet<Integer> xPosRecord = new HashSet<>();
10+
for (int[] point : points) {
11+
xPosRecord.add(point[0]);
12+
}
13+
if (w == 0) {
14+
return xPosRecord.size();
15+
}
16+
int len = xPosRecord.size(), pos = 0, curStart = -1;
17+
int[] arr = new int[len];
18+
for (int i : xPosRecord) {
19+
arr[pos++] = i;
20+
}
21+
Arrays.sort(arr);
22+
for (int i = 0; i < len; i++) {
23+
if (curStart == -1) {
24+
curStart = arr[i];
25+
ret++;
26+
} else {
27+
if (arr[i] > curStart + w) {
28+
curStart = arr[i];
29+
ret++;
30+
}
31+
}
32+
}
33+
34+
return ret;
35+
}
36+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 42.33 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(1), S:O(1)
5+
//
6+
class Solution {
7+
public String findLatestTime(String s) {
8+
if (s.contains("?")) {
9+
StringBuilder ret = new StringBuilder();
10+
if (s.charAt(4) == '?') {
11+
ret.append('9');
12+
} else {
13+
ret.append(s.charAt(4));
14+
}
15+
if (s.charAt(3) == '?') {
16+
ret.append('5');
17+
} else {
18+
ret.append(s.charAt(3));
19+
}
20+
ret.append(':');
21+
char c1 = s.charAt(0), c2 = s.charAt(1);
22+
if (c1 == '?') {
23+
if (c2 != '?' && c2 > '1') {
24+
c1 = '0';
25+
} else {
26+
c1 = '1';
27+
}
28+
}
29+
if (c1 == '1') {
30+
if (c2 == '?') {
31+
c2 = '1';
32+
}
33+
} else {
34+
if (c2 == '?') {
35+
c2 = '9';
36+
}
37+
}
38+
39+
ret.append(c2);
40+
ret.append(c1);
41+
42+
return ret.reverse().toString();
43+
}
44+
45+
return s;
46+
}
47+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Runtime 1 ms Beats 100.00% of users with Java
2+
// Memory 79.60 MB Beats 100.00% of users with Java
3+
// .
4+
// T:O(sum(sqrt(ki)), S:O(1)
5+
//
6+
class Solution {
7+
public int maximumPrimeDifference(int[] nums) {
8+
int len = nums.length, left = -1, right = -1;
9+
for (int i = 0; i < len; i++) {
10+
if (checkPrime(nums[i])) {
11+
left = i;
12+
break;
13+
}
14+
}
15+
if (left == -1 || left == len - 1) {
16+
return 0;
17+
}
18+
for (int i = len - 1; i >= 0; i--) {
19+
if (checkPrime(nums[i])) {
20+
right = i;
21+
break;
22+
}
23+
}
24+
25+
return right - left;
26+
}
27+
28+
private boolean checkPrime(int n) {
29+
if (n <= 2) {
30+
return n == 2;
31+
}
32+
int top = (int) Math.sqrt(n);
33+
for (int i = 2; i <= top; i++) {
34+
if (n % i == 0) {
35+
return false;
36+
}
37+
}
38+
39+
return true;
40+
}
41+
}

0 commit comments

Comments
 (0)