Skip to content

Commit 0f11c5c

Browse files
committed
update
1 parent 67069f9 commit 0f11c5c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// AC: 702 ms
2+
// Memory: 1400 KB
3+
// sort & greedy.
4+
// T:O(sum(ni)), S:O(max(ni))
5+
//
6+
import java.util.Arrays;
7+
import java.util.Scanner;
8+
9+
public class Codeforces_1300B_Assigning_to_Classes {
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(), ret = 0;
15+
int[] arr = new int[2 * n];
16+
for (int j = 0; j < 2 * n; j++) {
17+
int a = sc.nextInt();
18+
arr[j] = a;
19+
}
20+
Arrays.sort(arr);
21+
22+
System.out.println(arr[n] - arr[n - 1]);
23+
}
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time: 374 ms
2+
// Memory: 500 KB
3+
// .
4+
// T:O(n), S:O(1)
5+
//
6+
import java.util.Scanner;
7+
8+
public class Codeforces_2062A_String {
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 ret = 0;
15+
for (char c : s.toCharArray()) {
16+
if (c == '1') {
17+
ret++;
18+
}
19+
}
20+
21+
System.out.println(ret);
22+
}
23+
}
24+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Runtime 3 ms Beats 62.16%
2+
// Memory 42.87 MB Beats 64.36%
3+
// brute-force.
4+
// T:O(n^2), S:O(n)
5+
//
6+
class Solution {
7+
public int minimumPairRemoval(int[] nums) {
8+
ArrayList<Integer> a = new ArrayList<>();
9+
for (int i : nums) a.add(i);
10+
int ans = 0;
11+
while (a.size() >= 2 && !f(a)) {
12+
ans++;
13+
int l1 = -1;
14+
int l2 = -1;
15+
int max = Integer.MAX_VALUE;
16+
for (int i = 1; i < a.size(); i++) {
17+
int s = a.get(i) + a.get(i - 1);
18+
if (s < max) {
19+
l1 = i;
20+
l2 = i - 1;
21+
max = s;
22+
}
23+
24+
}
25+
26+
a.remove(l1);
27+
a.add(l1, max);
28+
a.remove(l2);
29+
}
30+
return ans;
31+
}
32+
33+
public boolean f(ArrayList<Integer> a) {
34+
for (int i = 1; i < a.size(); i++) {
35+
if (a.get(i) < a.get(i - 1)) {
36+
return false;
37+
}
38+
}
39+
return true;
40+
}
41+
}

0 commit comments

Comments
 (0)