Skip to content

Commit 09407f3

Browse files
committed
240523
1 parent 265a0cb commit 09407f3

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

src/Main.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import leetcode.editor.en.Q639.DecodeWaysIi;
2323
import leetcode.editor.en.Q649.Dota2Senate;
2424
import leetcode.editor.en.Q651.FourKeysKeyboard;
25+
import leetcode.editor.en.Q703.KthLargestElementInAStream;
2526
import leetcode.editor.en.Q785.IsGraphBipartite;
2627
import leetcode.editor.en.Q91.DecodeWays;
2728
import leetcode.editor.en.Q934.ShortestBridge;
@@ -39,7 +40,12 @@
3940
public class Main {
4041
public static void main(String[] args) throws IOException {
4142

42-
System.out.println(new FrogJump().canCross(toIntArray("[0,1,3,6,10,15,19,21,24,26,30,33]\n")));
43+
KthLargestElementInAStream kthLargest = new KthLargestElementInAStream(3, new int[]{4, 5, 8, 2});
44+
kthLargest.add(3); // return 4
45+
kthLargest.add(5); // return 5
46+
kthLargest.add(10); // return 5
47+
kthLargest.add(9); // return 8
48+
kthLargest.add(4); // return 8
4349

4450
}
4551

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode.editor.en.Q1134;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class Solution {
9+
public boolean isArmstrong(int n) {
10+
String s = String.valueOf(n);
11+
int power = s.length();
12+
int sum = 0;
13+
for (int i = 0; i < s.length(); i++) {
14+
sum += Math.pow(Character.getNumericValue(s.charAt(i)), power);
15+
}
16+
return sum == n;
17+
18+
}
19+
}
20+
//leetcode submit region end(Prohibit modification and deletion)
21+
22+
23+
public class ArmstrongNumber extends Solution {
24+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package leetcode.editor.en.Q703;
2+
3+
import java.util.*;
4+
5+
import javafx.util.Pair;
6+
7+
//leetcode submit region begin(Prohibit modification and deletion)
8+
class KthLargest {
9+
10+
LinkedList<Integer> nums;
11+
int k;
12+
13+
public KthLargest(int k, int[] nums) {
14+
this.k = k;
15+
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
16+
for (int num : nums) {
17+
pq.add(num);
18+
}
19+
this.nums = new LinkedList<>();
20+
for (int i = 0; i < k && !pq.isEmpty(); i++) {
21+
this.nums.addFirst(pq.poll());
22+
}
23+
24+
}
25+
26+
27+
public int add(int val) {
28+
int indexToInsert = binarySearch(val);
29+
if (indexToInsert < this.nums.size()) {
30+
this.nums.add(indexToInsert, val);
31+
} else {
32+
this.nums.add(val);
33+
}
34+
if (this.nums.size() > k) this.nums.pollFirst();
35+
int ans = this.nums.get(this.nums.size() - k);
36+
return ans;
37+
}
38+
39+
private int binarySearch(int target) {
40+
int left = 0;
41+
int right = this.nums.size();
42+
while (left < right) {
43+
int mid = (left + right) / 2;
44+
if (this.nums.get(mid) < target) {
45+
left = mid + 1;
46+
} else {
47+
right = mid;
48+
}
49+
}
50+
return left;
51+
52+
}
53+
}
54+
55+
/**
56+
* Your KthLargest object will be instantiated and called as such:
57+
* KthLargest obj = new KthLargest(k, nums);
58+
* int param_1 = obj.add(val);
59+
*/
60+
//leetcode submit region end(Prohibit modification and deletion)
61+
62+
63+
public class KthLargestElementInAStream extends KthLargest {
64+
65+
public KthLargestElementInAStream(int k, int[] nums) {
66+
super(k, nums);
67+
}
68+
}

0 commit comments

Comments
 (0)