Skip to content

Commit 2b85843

Browse files
committed
update
1 parent c2327e2 commit 2b85843

7 files changed

+195
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// AC: 312 ms
2+
// Memory: 700 KB
3+
// Greedy.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.HashMap;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_2001A_Make_All_Equal {
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+
HashMap<Integer, Integer> record = new HashMap<>();
16+
for (int j = 0; j < n; j++) {
17+
int a = sc.nextInt();
18+
record.merge(a, 1, Integer::sum);
19+
}
20+
int maxTime = 1;
21+
for (int key : record.keySet()) {
22+
maxTime = Math.max(maxTime, record.get(key));
23+
}
24+
25+
System.out.println(n - maxTime);
26+
}
27+
}
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// AC: 328 ms
2+
// Memory: 700 KB
3+
// Greedy. Only check head and tail.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_2003A_Turtle_and_Good_Strings {
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+
String s = sc.next();
15+
16+
System.out.println(s.charAt(0) != s.charAt(n - 1) ? "YES" : "NO");
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Solution 1: Sort and see regular.
2+
// Runtime 42 ms Beats 75.31%
3+
// Memory 55.70 MB Beats 46.94%
4+
// If sorted, the sequence like "/a", "/a/b", "/aa" can be compared straightly, we check the last one is start with the former one + '/'.
5+
// T:O(n), S:O(n)
6+
//
7+
class Solution {
8+
public List<String> removeSubfolders(String[] folder) {
9+
List<String> ret = new ArrayList<>();
10+
Arrays.sort(folder);
11+
String prev = "";
12+
for (String s : folder) {
13+
if (ret.isEmpty() || !s.startsWith(prev + '/')) {
14+
ret.add(s);
15+
prev = s;
16+
}
17+
}
18+
19+
return ret;
20+
}
21+
}
22+
23+
24+
// Solution 2: Trie tree solution.
25+
// todo-understand: https://leetcode.com/problems/remove-sub-folders-from-the-filesystem/solutions/409626/java-trie-solution/?envType=daily-question&envId=2024-10-25
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Runtime 2 ms Beats 92.44%
2+
// Memory 45.00 MB Beats 13.50%
3+
// Brute-force.
4+
// T:O(n * k), S:O(1)
5+
//
6+
class Solution {
7+
public int[] minBitwiseArray(List<Integer> nums) {
8+
int len = nums.size(), pos = 0;
9+
int[] ret = new int[len];
10+
for (int num : nums) {
11+
if (num % 2 == 0) {
12+
ret[pos++] = -1;
13+
} else {
14+
for (int i = num / 2; i <= num - 1; i++) {
15+
if ((i | (i + 1)) == num) {
16+
ret[pos++] = i;
17+
break;
18+
}
19+
}
20+
}
21+
}
22+
23+
return ret;
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Runtime 1 ms Beats 100.00%
2+
// Memory 44.85 MB Beats 59.36%
3+
// Bitmap: 找到从右边起,第一个 0 bit 位,然后原数字 A 减去这个 0 位对应的 1XXX 二进制对应值的一半 1XX. 减去这个 1XX 得到的 ans 可满足 ans|(ans+1) = A.
4+
// T:O(n * logk), S:O(1)
5+
//
6+
class Solution {
7+
public int[] minBitwiseArray(List<Integer> nums) {
8+
int len = nums.size(), pos = 0;
9+
int[] ret = new int[len];
10+
for (int num : nums) {
11+
if (num % 2 == 0) {
12+
ret[pos++] = -1;
13+
} else {
14+
ret[pos++] = num - ((num + 1) & (-num - 1)) / 2;
15+
}
16+
}
17+
18+
return ret;
19+
}
20+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Runtime 17 ms Beats 19.09%
2+
// Memory 45.83 MB Beats 6.72%
3+
// Hashmap & sort.
4+
// T:O(n * k), S:O(k)
5+
//
6+
class Solution {
7+
public int[] findXSum(int[] nums, int k, int x) {
8+
int len = nums.length, pos = 0;
9+
int[] ret = new int[len - k + 1];
10+
HashMap<Integer, Integer> record = new HashMap<>();
11+
for (int i = 0; i < len; i++) {
12+
record.merge(nums[i], 1, Integer::sum);
13+
if (i < k - 1) {
14+
continue;
15+
}
16+
int sum = 0, countX = x;
17+
TreeMap<Integer, List<Integer>> sort = new TreeMap<>(Comparator.reverseOrder());
18+
for (int key : record.keySet()) {
19+
int freq = record.get(key);
20+
if (sort.containsKey(freq)) {
21+
sort.get(freq).add(key);
22+
} else {
23+
List<Integer> list1 = new ArrayList<>();
24+
list1.add(key);
25+
sort.put(freq, list1);
26+
}
27+
}
28+
for (int freq : sort.keySet()) {
29+
List<Integer> elems = sort.get(freq);
30+
if (elems.size() <= countX) {
31+
for (int elem : elems) {
32+
sum += elem * freq;
33+
}
34+
countX -= elems.size();
35+
} else {
36+
elems.sort(Collections.reverseOrder());
37+
for (int j = 0; j < countX; j++) {
38+
sum += elems.get(j) * freq;
39+
}
40+
countX = 0;
41+
}
42+
43+
if (countX == 0) {
44+
break;
45+
}
46+
}
47+
48+
record.merge(nums[i + 1 - k], -1, Integer::sum);
49+
ret[pos++] = sum;
50+
}
51+
52+
return ret;
53+
}
54+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Runtime 7 ms Beats 55.54%
2+
// Memory 55.34 MB Beats 90.57%
3+
// .
4+
// T:O(n), S:O(n)
5+
//
6+
class Solution {
7+
public List<String> stringSequence(String target) {
8+
List<String> ret = new ArrayList<>();
9+
StringBuilder sb = new StringBuilder();
10+
for (char c : target.toCharArray()) {
11+
int c1 = c - 'a';
12+
for (int i = 0; i <= c1; i++) {
13+
char c2 = (char) ('a' + i);
14+
sb.append(c2);
15+
ret.add(sb.toString());
16+
if (i != c1) {
17+
sb.deleteCharAt(sb.length() - 1);
18+
}
19+
}
20+
}
21+
22+
return ret;
23+
}
24+
}

0 commit comments

Comments
 (0)