Skip to content

Commit b2dfbd7

Browse files
committed
weekly-237
Signed-off-by: ashKIK <[email protected]>
1 parent dcda603 commit b2dfbd7

5 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
// https://leetcode.com/contest/weekly-contest-237/problems/check-if-the-sentence-is-pangram/
5+
6+
public class CheckIfTheSentenceIsPangram {
7+
8+
public boolean checkIfPangram(String sentence) {
9+
Set<Character> set = new HashSet<>();
10+
for (int i = 0; i < sentence.length(); i++) {
11+
set.add(sentence.charAt(i));
12+
}
13+
return set.size() == 26;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-237/problems/find-xor-sum-of-all-pairs-bitwise-and/
3+
4+
public class FindXORSumOfAllPairsBitwiseAND {
5+
6+
// distributive property
7+
// (a1^a2) & (b1^b2) = (a1&b1) ^ (a1&b2) ^ (a2&b1) ^ (a2&b2)
8+
public int getXORSum(int[] arr1, int[] arr2) {
9+
int xor1 = 0;
10+
for (int val : arr1) {
11+
xor1 ^= val;
12+
}
13+
int xor2 = 0;
14+
for (int val : arr2) {
15+
xor2 ^= val;
16+
}
17+
return xor1 & xor2;
18+
}
19+
}

weekly-237/MaximumIceCreamBars.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Arrays;
2+
3+
// https://leetcode.com/contest/weekly-contest-237/problems/maximum-ice-cream-bars/
4+
5+
public class MaximumIceCreamBars {
6+
7+
public int maxIceCream(int[] costs, int coins) {
8+
int totalCost = 0;
9+
int iceCreams = 0;
10+
Arrays.sort(costs);
11+
for (int cost : costs) {
12+
if (totalCost + cost <= coins) {
13+
totalCost += cost;
14+
iceCreams++;
15+
}
16+
}
17+
return iceCreams;
18+
}
19+
}

weekly-237/SingleThreadedCPU.java

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.PriorityQueue;
2+
3+
// https://leetcode.com/contest/weekly-contest-237/problems/single-threaded-cpu/
4+
5+
public class SingleThreadedCPU {
6+
7+
// O(n*log(n))
8+
public int[] getOrder(int[][] tasks) {
9+
// Sort by enqueue time then by processing time
10+
PriorityQueue<int[]> taskQueue = new PriorityQueue<>(
11+
(a, b) -> a[0] == b[0] ? a[1] - b[1] : a[0] - b[0]);
12+
13+
// Sort by processing time then by id
14+
PriorityQueue<int[]> waitQueue = new PriorityQueue<>(
15+
(a, b) -> a[1] == b[1] ? a[2] - b[2] : a[1] - b[1]);
16+
17+
for (int i = 0; i < tasks.length; i++) {
18+
taskQueue.offer(new int[]{tasks[i][0], tasks[i][1], i});
19+
}
20+
21+
int[] result = new int[tasks.length];
22+
int finishedTime = -1;
23+
int idx = 0;
24+
25+
while (!taskQueue.isEmpty() || !waitQueue.isEmpty()) {
26+
while (!taskQueue.isEmpty() && taskQueue.peek()[0] <= finishedTime) {
27+
waitQueue.offer(taskQueue.poll());
28+
}
29+
int[] task;
30+
if (!waitQueue.isEmpty()) {
31+
task = waitQueue.poll();
32+
finishedTime += task[1];
33+
} else { // taskQueue is empty
34+
task = taskQueue.poll();
35+
finishedTime = task[0] + task[1];
36+
}
37+
result[idx++] = task[2];
38+
}
39+
return result;
40+
}
41+
}

weekly-238/SumOfDigitsInBaseK.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
// https://leetcode.com/contest/weekly-contest-238/problems/sum-of-digits-in-base-k/
3+
4+
import java.util.PriorityQueue;
5+
6+
public class SumOfDigitsInBaseK {
7+
8+
public int sumBase(int n, int k) {
9+
int sum = 0;
10+
do {
11+
sum += n % k;
12+
n /= k;
13+
} while (n != 0);
14+
return sum;
15+
}
16+
17+
public boolean checkZeroOnes(String s) {
18+
char[] chr = s.toCharArray();
19+
int ones = 0;
20+
int mOne = 0;
21+
int zeros = 0;
22+
int mZero = 0;
23+
for (char c : chr) {
24+
if (c == '0') {
25+
zeros++;
26+
ones = 0;
27+
} else {
28+
ones++;
29+
zeros = 0;
30+
}
31+
mOne = Math.max(mOne, ones);
32+
mZero = Math.max(mZero, zeros);
33+
}
34+
return mOne > mZero;
35+
}
36+
}

0 commit comments

Comments
 (0)