diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..37b1922 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/out/ diff --git a/out/production/leetcode/com/leetcode/MapSum$Node.class b/out/production/leetcode/com/leetcode/MapSum$Node.class deleted file mode 100644 index 12f7f1d..0000000 Binary files a/out/production/leetcode/com/leetcode/MapSum$Node.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/MapSum.class b/out/production/leetcode/com/leetcode/MapSum.class deleted file mode 100644 index d55a1ad..0000000 Binary files a/out/production/leetcode/com/leetcode/MapSum.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/MyCalendarTwo.class b/out/production/leetcode/com/leetcode/MyCalendarTwo.class deleted file mode 100644 index cb5e882..0000000 Binary files a/out/production/leetcode/com/leetcode/MyCalendarTwo.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution.class b/out/production/leetcode/com/leetcode/Solution.class deleted file mode 100644 index 9804046..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution1005.class b/out/production/leetcode/com/leetcode/Solution1005.class deleted file mode 100644 index 9e51e86..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution1005.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution1034.class b/out/production/leetcode/com/leetcode/Solution1034.class deleted file mode 100644 index fc004ae..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution1034.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution1816.class b/out/production/leetcode/com/leetcode/Solution1816.class deleted file mode 100644 index e6edcf2..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution1816.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution318.class b/out/production/leetcode/com/leetcode/Solution318.class deleted file mode 100644 index d4c7e1a..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution318.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution319.class b/out/production/leetcode/com/leetcode/Solution319.class deleted file mode 100644 index c896e4a..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution319.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution372.class b/out/production/leetcode/com/leetcode/Solution372.class deleted file mode 100644 index 33e4217..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution372.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution383.class b/out/production/leetcode/com/leetcode/Solution383.class deleted file mode 100644 index 535e18a..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution383.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution397.class b/out/production/leetcode/com/leetcode/Solution397.class deleted file mode 100644 index e89f1e1..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution397.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution519.class b/out/production/leetcode/com/leetcode/Solution519.class deleted file mode 100644 index 3e42772..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution519.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution520.class b/out/production/leetcode/com/leetcode/Solution520.class deleted file mode 100644 index aa5b232..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution520.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution56.class b/out/production/leetcode/com/leetcode/Solution56.class deleted file mode 100644 index 602e807..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution56.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution563.class b/out/production/leetcode/com/leetcode/Solution563.class deleted file mode 100644 index 4ee0502..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution563.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution594.class b/out/production/leetcode/com/leetcode/Solution594.class deleted file mode 100644 index 4d43ec0..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution594.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution689.class b/out/production/leetcode/com/leetcode/Solution689.class deleted file mode 100644 index fbc7189..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution689.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution700.class b/out/production/leetcode/com/leetcode/Solution700.class deleted file mode 100644 index f6dee6c..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution700.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution709.class b/out/production/leetcode/com/leetcode/Solution709.class deleted file mode 100644 index 5a16c59..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution709.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution748.class b/out/production/leetcode/com/leetcode/Solution748.class deleted file mode 100644 index b94ba65..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution748.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution794.class b/out/production/leetcode/com/leetcode/Solution794.class deleted file mode 100644 index eae0e42..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution794.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/Solution851.class b/out/production/leetcode/com/leetcode/Solution851.class deleted file mode 100644 index 7d518eb..0000000 Binary files a/out/production/leetcode/com/leetcode/Solution851.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/TopVotedCandidate.class b/out/production/leetcode/com/leetcode/TopVotedCandidate.class deleted file mode 100644 index e1e504b..0000000 Binary files a/out/production/leetcode/com/leetcode/TopVotedCandidate.class and /dev/null differ diff --git a/out/production/leetcode/com/leetcode/TreeNode.class b/out/production/leetcode/com/leetcode/TreeNode.class deleted file mode 100644 index c05fa0a..0000000 Binary files a/out/production/leetcode/com/leetcode/TreeNode.class and /dev/null differ diff --git a/src/main/java/com/leetcode/1155.java b/src/main/java/com/leetcode/1155.java new file mode 100644 index 0000000..2e9e955 --- /dev/null +++ b/src/main/java/com/leetcode/1155.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2034.java b/src/main/java/com/leetcode/2034.java new file mode 100644 index 0000000..922da2f --- /dev/null +++ b/src/main/java/com/leetcode/2034.java @@ -0,0 +1,71 @@ +package com.leetcode; + +import javafx.util.Pair; + +import java.util.*; + +class StockPrice { + HashMap map; + Queue> maxQ; + Queue> 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 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 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()); + } +} diff --git a/src/main/java/com/leetcode/2103.java b/src/main/java/com/leetcode/2103.java new file mode 100644 index 0000000..bbcea80 --- /dev/null +++ b/src/main/java/com/leetcode/2103.java @@ -0,0 +1,24 @@ +package com.leetcode; + +import java.util.ArrayList; +import java.util.HashSet; + +class Solution2103 { + public int countPoints(String rings) { + ArrayList> 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 map: maps) { + if (map.size() >= 3) { + res++; + } + } + return res; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2512.java b/src/main/java/com/leetcode/2512.java new file mode 100644 index 0000000..200ca4d --- /dev/null +++ b/src/main/java/com/leetcode/2512.java @@ -0,0 +1,38 @@ +package com.leetcode; + +import java.util.*; +import java.util.stream.Collectors; + +class Solution2512 { + public List topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) { + HashSet pos = new HashSet<>(Arrays.asList(positive_feedback)); + HashSet neg = new HashSet<>(Arrays.asList(negative_feedback)); + HashMap 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 res = Arrays.stream(student_id).boxed().collect(Collectors.toList()); + Collections.sort(res, new Comparator() { + @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); + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2530.java b/src/main/java/com/leetcode/2530.java new file mode 100644 index 0000000..9b4870e --- /dev/null +++ b/src/main/java/com/leetcode/2530.java @@ -0,0 +1,25 @@ +package com.leetcode; + +import java.util.PriorityQueue; + +class Solution2530 { + public long maxKelements(int[] nums, int k) { + PriorityQueue 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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2578.java b/src/main/java/com/leetcode/2578.java new file mode 100644 index 0000000..420bc19 --- /dev/null +++ b/src/main/java/com/leetcode/2578.java @@ -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 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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2678.java b/src/main/java/com/leetcode/2678.java new file mode 100644 index 0000000..b0c2155 --- /dev/null +++ b/src/main/java/com/leetcode/2678.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/2698.java b/src/main/java/com/leetcode/2698.java new file mode 100644 index 0000000..a08ebdb --- /dev/null +++ b/src/main/java/com/leetcode/2698.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/274.java b/src/main/java/com/leetcode/274.java new file mode 100644 index 0000000..3039ff3 --- /dev/null +++ b/src/main/java/com/leetcode/274.java @@ -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; + } +} \ No newline at end of file diff --git a/src/main/java/com/leetcode/275.java b/src/main/java/com/leetcode/275.java new file mode 100644 index 0000000..4509bd6 --- /dev/null +++ b/src/main/java/com/leetcode/275.java @@ -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; + } +} \ No newline at end of file