Skip to content

Commit

Permalink
2023/11/2
Browse files Browse the repository at this point in the history
  • Loading branch information
baowj-678 committed Nov 2, 2023
1 parent 48d9138 commit 5eaf6ea
Show file tree
Hide file tree
Showing 36 changed files with 294 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.idea/
/out/
Binary file removed out/production/leetcode/com/leetcode/MapSum$Node.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/MapSum.class
Binary file not shown.
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution1005.class
Binary file not shown.
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution1816.class
Binary file not shown.
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution319.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution372.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution383.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution520.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution56.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution563.class
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution594.class
Binary file not shown.
Binary file not shown.
Binary file removed out/production/leetcode/com/leetcode/Solution700.class
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 removed out/production/leetcode/com/leetcode/TreeNode.class
Binary file not shown.
28 changes: 28 additions & 0 deletions src/main/java/com/leetcode/1155.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.leetcode;

class Solution1155 {
public int numRollsToTarget(int n, int k, int target) {
long MAX = 1000000007;
long[] dp = new long[target + 1];
for (int i = 1; i <= k && i < dp.length; i++) {
dp[i] = 1;
}
for (int i = 1; i < n; i++) {
for (int j = dp.length-1; j > 0; j--) {
dp[j] = 0;
for (int t = 1; t <= k; t++) {
if (j - t <= 0) {
break;
}
dp[j] = (dp[j - t] + dp[j]) % MAX;
}
}
}
return (int)dp[target];
}

public static void main(String[] args) {
Solution1155 so = new Solution1155();
System.out.println(so.numRollsToTarget(4, 6, 7));
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/leetcode/2034.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.leetcode;

import javafx.util.Pair;

import java.util.*;

class StockPrice {
HashMap<Integer, Integer> map;
Queue<Pair<Integer, Integer>> maxQ;
Queue<Pair<Integer, Integer>> minQ;
int curTimestamp;

public StockPrice() {
this.map = new HashMap<>();
this.minQ = new PriorityQueue<>(
(o1, o2) -> -o2.getValue().compareTo(o1.getValue())
);
this.maxQ = new PriorityQueue<>(
(o1, o2) -> o2.getValue().compareTo(o1.getValue())
);
this.curTimestamp = 0;
}

public void update(int timestamp, int price) {
if (timestamp > this.curTimestamp) {
this.curTimestamp = timestamp;
}
this.map.put(timestamp, price);
this.maxQ.add(new Pair<>(timestamp, price));
this.minQ.add(new Pair<>(timestamp, price));
}

public int current() {
return this.map.get(this.curTimestamp);
}

public int maximum() {
while(true) {
Pair<Integer, Integer> peek = this.maxQ.peek();
if (peek == null) {
return -1;
}
if (Objects.equals(peek.getValue(), this.map.get(peek.getKey()))) {
return peek.getValue();
} else {
this.maxQ.remove();
}
}
}

public int minimum() {
while(true) {
Pair<Integer, Integer> peek = this.minQ.peek();
if (peek == null) {
return -1;
}
if (Objects.equals(peek.getValue(), this.map.get(peek.getKey()))) {
return peek.getValue();
} else {
this.minQ.remove();
}
}
}

public static void main(String[] args) {
StockPrice obj = new StockPrice();
obj.update(3, 9651);
obj.update(3, 6897);
System.out.println(obj.maximum());
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/leetcode/2103.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.leetcode;

import java.util.ArrayList;
import java.util.HashSet;

class Solution2103 {
public int countPoints(String rings) {
ArrayList<HashSet<Integer>> maps = new ArrayList<>();
for (int i = 0; i < 10; i++) {
maps.add(new HashSet<>());
}
for (int i = 0; 2 * i < rings.length(); i++) {
int color = rings.charAt(2*i), idx = rings.charAt(2*i+1)-'0';
maps.get(idx).add(color);
}
int res = 0;
for (HashSet<Integer> map: maps) {
if (map.size() >= 3) {
res++;
}
}
return res;
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/leetcode/2512.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.leetcode;

import java.util.*;
import java.util.stream.Collectors;

class Solution2512 {
public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
HashSet<String> pos = new HashSet<>(Arrays.asList(positive_feedback));
HashSet<String> neg = new HashSet<>(Arrays.asList(negative_feedback));
HashMap<Integer, Integer> scores = new HashMap<>();
for (int i = 0; i < report.length; i++) {
String[] ss = report[i].split(" ");
int score = 0;
for (String s: ss) {
if (pos.contains(s)) {
score += 3;
}
if (neg.contains(s)) {
score -= 1;
}
}
scores.put(student_id[i], score);
}
List<Integer> res = Arrays.stream(student_id).boxed().collect(Collectors.toList());
Collections.sort(res, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
Integer score1 = scores.get(o1);
Integer score2 = scores.get(o2);
if (Objects.equals(score1, score2)) {
return o1.compareTo(o2);
}
return -score1.compareTo(score2);
}
});
return res.subList(0, k);
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/leetcode/2530.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.leetcode;

import java.util.PriorityQueue;

class Solution2530 {
public long maxKelements(int[] nums, int k) {
PriorityQueue<Integer> q = new PriorityQueue<>((o1, o2) -> {return -o1.compareTo(o2);});
long s = 0;
for (int num: nums) {
q.add(num);
}
for (int i = 0; i < k; i++) {
long peek = q.remove();
s += peek;
peek = (peek + 2) / 3;
q.add((int) peek);
}
return s;
}

public static void main(String[] args) {
Solution2530 solution2530 = new Solution2530();
System.out.println(solution2530.maxKelements(new int[]{10,10,10,10,10}, 5));
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/leetcode/2578.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Solution2578 {
public int splitNum(int num) {
ArrayList<Integer> nums = new ArrayList<>();
while (num > 0) {
nums.add(num % 10);
num /= 10;
}
Collections.sort(nums);
int v1 = 0, v2 = 0;
for (int k = 0; k < nums.size(); k++) {
if (k % 2 == 0) {
v1 = v1 * 10 + nums.get(k);
} else {
v2 = v2 * 10 + nums.get(k);
}
}
return v1 + v2;
}

public static void main(String[] args) {
Solution2578 so = new Solution2578();
System.out.println(so.splitNum(4325));
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/leetcode/2678.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.leetcode;

class Solution2678 {
public int countSeniors(String[] details) {
int res = 0;
for (String detail: details) {
if (Integer.parseInt(detail.substring(11, 13)) > 60) {
res++;
}
}
return res;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/leetcode/2698.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.leetcode;

class Solution2698 {
public int punishmentNumber(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
int v = i * i;
if (bfs(v, i)) {
sum += v;
}
}
return sum;
}

private boolean bfs (int v, int target) {
for (int i = 10; v / i > 0; i *= 10) {
int r = v % i;
int l = v / i;
if (target - r > 0 && bfs(l, target - r)) {
return true;
}
}
return v == target;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/leetcode/274.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.leetcode;

import java.util.Arrays;

class Solution274 {
public int hIndex(int[] citations) {
int l = 0, r = 1000;
while (l < r) {
int mid = (l + r + 1) / 2;

int cnt = 0;
for (int c: citations) {
if (c >= mid) {
cnt++;
}
}
if (cnt >= mid) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/leetcode/275.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.leetcode;

class Solution275 {
public int hIndex(int[] citations) {
for (int i = citations.length-1; i >= 0; i--) {
if (citations[i] < citations.length - i) {
return citations.length - i - 1;
}
}
return citations.length;
}
}

0 comments on commit 5eaf6ea

Please sign in to comment.