Skip to content

Commit 5eaf6ea

Browse files
committed
2023/11/2
1 parent 48d9138 commit 5eaf6ea

36 files changed

+294
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea/
2+
/out/
Binary file not shown.
-1.51 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-612 Bytes
Binary file not shown.

src/main/java/com/leetcode/1155.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.leetcode;
2+
3+
class Solution1155 {
4+
public int numRollsToTarget(int n, int k, int target) {
5+
long MAX = 1000000007;
6+
long[] dp = new long[target + 1];
7+
for (int i = 1; i <= k && i < dp.length; i++) {
8+
dp[i] = 1;
9+
}
10+
for (int i = 1; i < n; i++) {
11+
for (int j = dp.length-1; j > 0; j--) {
12+
dp[j] = 0;
13+
for (int t = 1; t <= k; t++) {
14+
if (j - t <= 0) {
15+
break;
16+
}
17+
dp[j] = (dp[j - t] + dp[j]) % MAX;
18+
}
19+
}
20+
}
21+
return (int)dp[target];
22+
}
23+
24+
public static void main(String[] args) {
25+
Solution1155 so = new Solution1155();
26+
System.out.println(so.numRollsToTarget(4, 6, 7));
27+
}
28+
}

src/main/java/com/leetcode/2034.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.leetcode;
2+
3+
import javafx.util.Pair;
4+
5+
import java.util.*;
6+
7+
class StockPrice {
8+
HashMap<Integer, Integer> map;
9+
Queue<Pair<Integer, Integer>> maxQ;
10+
Queue<Pair<Integer, Integer>> minQ;
11+
int curTimestamp;
12+
13+
public StockPrice() {
14+
this.map = new HashMap<>();
15+
this.minQ = new PriorityQueue<>(
16+
(o1, o2) -> -o2.getValue().compareTo(o1.getValue())
17+
);
18+
this.maxQ = new PriorityQueue<>(
19+
(o1, o2) -> o2.getValue().compareTo(o1.getValue())
20+
);
21+
this.curTimestamp = 0;
22+
}
23+
24+
public void update(int timestamp, int price) {
25+
if (timestamp > this.curTimestamp) {
26+
this.curTimestamp = timestamp;
27+
}
28+
this.map.put(timestamp, price);
29+
this.maxQ.add(new Pair<>(timestamp, price));
30+
this.minQ.add(new Pair<>(timestamp, price));
31+
}
32+
33+
public int current() {
34+
return this.map.get(this.curTimestamp);
35+
}
36+
37+
public int maximum() {
38+
while(true) {
39+
Pair<Integer, Integer> peek = this.maxQ.peek();
40+
if (peek == null) {
41+
return -1;
42+
}
43+
if (Objects.equals(peek.getValue(), this.map.get(peek.getKey()))) {
44+
return peek.getValue();
45+
} else {
46+
this.maxQ.remove();
47+
}
48+
}
49+
}
50+
51+
public int minimum() {
52+
while(true) {
53+
Pair<Integer, Integer> peek = this.minQ.peek();
54+
if (peek == null) {
55+
return -1;
56+
}
57+
if (Objects.equals(peek.getValue(), this.map.get(peek.getKey()))) {
58+
return peek.getValue();
59+
} else {
60+
this.minQ.remove();
61+
}
62+
}
63+
}
64+
65+
public static void main(String[] args) {
66+
StockPrice obj = new StockPrice();
67+
obj.update(3, 9651);
68+
obj.update(3, 6897);
69+
System.out.println(obj.maximum());
70+
}
71+
}

src/main/java/com/leetcode/2103.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashSet;
5+
6+
class Solution2103 {
7+
public int countPoints(String rings) {
8+
ArrayList<HashSet<Integer>> maps = new ArrayList<>();
9+
for (int i = 0; i < 10; i++) {
10+
maps.add(new HashSet<>());
11+
}
12+
for (int i = 0; 2 * i < rings.length(); i++) {
13+
int color = rings.charAt(2*i), idx = rings.charAt(2*i+1)-'0';
14+
maps.get(idx).add(color);
15+
}
16+
int res = 0;
17+
for (HashSet<Integer> map: maps) {
18+
if (map.size() >= 3) {
19+
res++;
20+
}
21+
}
22+
return res;
23+
}
24+
}

src/main/java/com/leetcode/2512.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.leetcode;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
class Solution2512 {
7+
public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
8+
HashSet<String> pos = new HashSet<>(Arrays.asList(positive_feedback));
9+
HashSet<String> neg = new HashSet<>(Arrays.asList(negative_feedback));
10+
HashMap<Integer, Integer> scores = new HashMap<>();
11+
for (int i = 0; i < report.length; i++) {
12+
String[] ss = report[i].split(" ");
13+
int score = 0;
14+
for (String s: ss) {
15+
if (pos.contains(s)) {
16+
score += 3;
17+
}
18+
if (neg.contains(s)) {
19+
score -= 1;
20+
}
21+
}
22+
scores.put(student_id[i], score);
23+
}
24+
List<Integer> res = Arrays.stream(student_id).boxed().collect(Collectors.toList());
25+
Collections.sort(res, new Comparator<Integer>() {
26+
@Override
27+
public int compare(Integer o1, Integer o2) {
28+
Integer score1 = scores.get(o1);
29+
Integer score2 = scores.get(o2);
30+
if (Objects.equals(score1, score2)) {
31+
return o1.compareTo(o2);
32+
}
33+
return -score1.compareTo(score2);
34+
}
35+
});
36+
return res.subList(0, k);
37+
}
38+
}

src/main/java/com/leetcode/2530.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.leetcode;
2+
3+
import java.util.PriorityQueue;
4+
5+
class Solution2530 {
6+
public long maxKelements(int[] nums, int k) {
7+
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> {return -o1.compareTo(o2);});
8+
long s = 0;
9+
for (int num: nums) {
10+
q.add(num);
11+
}
12+
for (int i = 0; i < k; i++) {
13+
long peek = q.remove();
14+
s += peek;
15+
peek = (peek + 2) / 3;
16+
q.add((int) peek);
17+
}
18+
return s;
19+
}
20+
21+
public static void main(String[] args) {
22+
Solution2530 solution2530 = new Solution2530();
23+
System.out.println(solution2530.maxKelements(new int[]{10,10,10,10,10}, 5));
24+
}
25+
}

src/main/java/com/leetcode/2578.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.leetcode;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
8+
class Solution2578 {
9+
public int splitNum(int num) {
10+
ArrayList<Integer> nums = new ArrayList<>();
11+
while (num > 0) {
12+
nums.add(num % 10);
13+
num /= 10;
14+
}
15+
Collections.sort(nums);
16+
int v1 = 0, v2 = 0;
17+
for (int k = 0; k < nums.size(); k++) {
18+
if (k % 2 == 0) {
19+
v1 = v1 * 10 + nums.get(k);
20+
} else {
21+
v2 = v2 * 10 + nums.get(k);
22+
}
23+
}
24+
return v1 + v2;
25+
}
26+
27+
public static void main(String[] args) {
28+
Solution2578 so = new Solution2578();
29+
System.out.println(so.splitNum(4325));
30+
}
31+
}

src/main/java/com/leetcode/2678.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.leetcode;
2+
3+
class Solution2678 {
4+
public int countSeniors(String[] details) {
5+
int res = 0;
6+
for (String detail: details) {
7+
if (Integer.parseInt(detail.substring(11, 13)) > 60) {
8+
res++;
9+
}
10+
}
11+
return res;
12+
}
13+
}

src/main/java/com/leetcode/2698.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.leetcode;
2+
3+
class Solution2698 {
4+
public int punishmentNumber(int n) {
5+
int sum = 0;
6+
for (int i = 1; i <= n; i++) {
7+
int v = i * i;
8+
if (bfs(v, i)) {
9+
sum += v;
10+
}
11+
}
12+
return sum;
13+
}
14+
15+
private boolean bfs (int v, int target) {
16+
for (int i = 10; v / i > 0; i *= 10) {
17+
int r = v % i;
18+
int l = v / i;
19+
if (target - r > 0 && bfs(l, target - r)) {
20+
return true;
21+
}
22+
}
23+
return v == target;
24+
}
25+
}

src/main/java/com/leetcode/274.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
class Solution274 {
6+
public int hIndex(int[] citations) {
7+
int l = 0, r = 1000;
8+
while (l < r) {
9+
int mid = (l + r + 1) / 2;
10+
11+
int cnt = 0;
12+
for (int c: citations) {
13+
if (c >= mid) {
14+
cnt++;
15+
}
16+
}
17+
if (cnt >= mid) {
18+
l = mid;
19+
} else {
20+
r = mid - 1;
21+
}
22+
}
23+
return l;
24+
}
25+
}

src/main/java/com/leetcode/275.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.leetcode;
2+
3+
class Solution275 {
4+
public int hIndex(int[] citations) {
5+
for (int i = citations.length-1; i >= 0; i--) {
6+
if (citations[i] < citations.length - i) {
7+
return citations.length - i - 1;
8+
}
9+
}
10+
return citations.length;
11+
}
12+
}

0 commit comments

Comments
 (0)