Skip to content

Commit 5ae4f42

Browse files
committed
leetcode-weekly-237
1 parent becaa32 commit 5ae4f42

4 files changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// AC
2+
// Runtime: 5 ms, faster than 25.00% of Java online submissions for Check if the Sentence Is Pangram.
3+
// Memory Usage: 39 MB, less than 25.00% of Java online submissions for Check if the Sentence Is Pangram.
4+
class Solution {
5+
public boolean checkIfPangram(String sentence) {
6+
int size = sentence.length();
7+
Map<String, Integer> record = new HashMap<>();
8+
for(int i = 0; i < size; i++) {
9+
String temp = String.valueOf(sentence.charAt(i));
10+
record.merge(temp, 1, Integer::sum);
11+
}
12+
return record.size() == 26;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// AC:
2+
// Runtime: 28 ms, faster than 100.00% of Java online submissions for Maximum Ice Cream Bars.
3+
// Memory Usage: 55.9 MB, less than 25.00% of Java online submissions for Maximum Ice Cream Bars.
4+
class Solution {
5+
public int maxIceCream(int[] costs, int coins) {
6+
Arrays.sort(costs);
7+
int sum = 0;
8+
for(int i = 0; i < costs.length; i++) {
9+
sum += costs[i];
10+
if (sum == coins) {
11+
return i + 1;
12+
}
13+
if (sum > coins) {
14+
if (sum == 0) {
15+
return 0;
16+
} else {
17+
return i;
18+
}
19+
}
20+
}
21+
return costs.length;
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// AC:
2+
// Runtime: 103 ms, faster than 57.14% of Java online submissions for Single-Threaded CPU.
3+
// Memory Usage: 96.7 MB, less than 14.29% of Java online submissions for Single-Threaded CPU.
4+
// 思路:用优先级队列,模拟当前 CPU 可选的任务池,
5+
//
6+
class Solution {
7+
public int[] getOrder(int[][] tasks) {
8+
int taskCount = tasks.length;
9+
int[] ret = new int[taskCount];
10+
int[][] taskRecord = new int[taskCount][3];
11+
for(int i = 0; i < taskCount; i++) {
12+
taskRecord[i][0] = i;
13+
taskRecord[i][1] = tasks[i][0];
14+
taskRecord[i][2] = tasks[i][1];
15+
}
16+
Arrays.sort(taskRecord, (a, b) -> (a[1] - b[1]));
17+
// 按执行时间越短排越前
18+
PriorityQueue<int[]> queue = new PriorityQueue<int[]>((a, b) -> a[2] == b[2] ? a[0] - b[0] : a[2] - b[2]);
19+
int time = 0, step = 0, timeStep = 0;
20+
while (step < taskCount) {
21+
while (timeStep < taskCount && taskRecord[timeStep][1] <= time) {
22+
queue.offer(taskRecord[timeStep++]);
23+
}
24+
if (queue.isEmpty()) {
25+
time = taskRecord[timeStep][1];
26+
continue;
27+
}
28+
int[] curTask = queue.poll();
29+
ret[step++] = curTask[0];
30+
time += curTask[2];
31+
}
32+
33+
return ret;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// AC:
2+
// Runtime: 1 ms, faster than 100.00% of Java online submissions for Find XOR Sum of All Pairs Bitwise AND.
3+
// Memory Usage: 52.3 MB, less than 25.00% of Java online submissions for Find XOR Sum of All Pairs Bitwise AND.
4+
//
5+
// 思路:这个运算场景满足结合律: (a1 ^ a2) & (b1 ^ b2) = (a1 & b1) ^ (a1 & b2) ^ (a2 & b1) ^ (a2 & b2)
6+
// 复杂度:T:O(m + n), S:O(1)
7+
class Solution {
8+
public int getXORSum(int[] arr1, int[] arr2) {
9+
int ret1 = 0, ret2 = 0;
10+
for(int i: arr1) {
11+
ret1 ^= i;
12+
}
13+
for(int i: arr2) {
14+
ret2 ^= i;
15+
}
16+
return ret1 & ret2;
17+
}
18+
}

0 commit comments

Comments
 (0)