-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.idea/ | ||
/out/ |
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.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |