Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 497241b

Browse files
committed
Added 1 solution & modified 2 solutions
1 parent f62b89a commit 497241b

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed
Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
class Solution {
22
public int[] sortByBits(int[] arr) {
3-
PriorityQueue<Integer> pq = new PriorityQueue<>(new Comparator<Integer>(){
4-
public int compare(Integer o1, Integer o2) {
5-
int c = Integer.bitCount(o1) - Integer.bitCount(o2);
6-
if (c != 0) {
7-
return c;
8-
}
9-
return o1 - o2;
10-
}
11-
});
12-
for (int num : arr) {
13-
pq.add(num);
14-
}
15-
int[] ans = new int[arr.length];
16-
int idx = 0;
17-
while (!pq.isEmpty()) {
18-
ans[idx++] = pq.poll();
19-
}
20-
return ans;
3+
return Arrays.stream(arr).boxed()
4+
.sorted(Comparator.comparingInt(Integer::bitCount).thenComparingInt(o -> o))
5+
.collect(Collectors.toList())
6+
.stream()
7+
.mapToInt(Integer::intValue)
8+
.toArray();
219
}
2210
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public int shipWithinDays(int[] weights, int D) {
3+
int maxWeight = weights[0];
4+
int totalWeight = 0;
5+
for (int weight : weights) {
6+
maxWeight = Math.max(maxWeight, weight);
7+
totalWeight += weight;
8+
}
9+
while (maxWeight < totalWeight) {
10+
int midWeight = (maxWeight + totalWeight) / 2;
11+
int need = 1;
12+
int curr = 0;
13+
for (int weight : weights) {
14+
if (curr + weight > midWeight) {
15+
need += 1;
16+
curr = 0;
17+
}
18+
curr += weight;
19+
}
20+
if (need > D) {
21+
maxWeight = midWeight + 1;
22+
}
23+
else {
24+
totalWeight = midWeight;
25+
}
26+
}
27+
return maxWeight;
28+
}
29+
}

Medium/Insert into a Binary Search Tree.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
* int val;
55
* TreeNode left;
66
* TreeNode right;
7-
* TreeNode(int x) { val = x; }
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
814
* }
915
*/
1016
class Solution {
11-
public TreeNode insertIntoBST(TreeNode root, int val) {
12-
if (root == null) {
13-
root = new TreeNode(val);
14-
return root;
15-
}
16-
17-
if (root.val < val) {
18-
root.right = insertIntoBST(root.right, val);
19-
}
20-
21-
if (root.val > val) {
22-
root.left = insertIntoBST(root.left, val);
23-
}
24-
25-
return root;
17+
public TreeNode insertIntoBST(TreeNode root, int val) {
18+
if (root == null) {
19+
return new TreeNode(val);
2620
}
21+
if (root.val < val) {
22+
root.right = insertIntoBST(root.right, val);
23+
}
24+
else {
25+
root.left = insertIntoBST(root.left, val);
26+
}
27+
return root;
28+
}
2729
}

0 commit comments

Comments
 (0)