Skip to content

Commit 087ddb0

Browse files
committed
Added 2 solutions & refactored 2 solutions
1 parent 30597d5 commit 087ddb0

4 files changed

+107
-62
lines changed
Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class Solution {
2-
public int maxNumberOfApples(int[] arr) {
3-
Arrays.sort(arr);
4-
int total = 0;
5-
int count = 0;
6-
for (int i = 0; i < arr.length; i++) {
7-
total += arr[i];
8-
if (total > 5000) {
9-
break;
10-
}
11-
count++;
12-
}
13-
return count;
2+
public int maxNumberOfApples(int[] arr) {
3+
int weight = 0;
4+
int currCount = 0;
5+
Arrays.sort(arr);
6+
for (int i = 0; i < arr.length && weight + arr[i] <= 5000; i++) {
7+
weight += arr[i];
8+
currCount++;
149
}
10+
return currCount;
11+
}
1512
}

Easy/Sort Array By Parity.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] sortArrayByParity(int[] A) {
3+
int start = 0;
4+
int end = A.length - 1;
5+
while (start < end) {
6+
if (A[start] % 2 > A[end] % 2) {
7+
int temp = A[start];
8+
A[start] = A[end];
9+
A[end] = temp;
10+
}
11+
if (A[start] % 2 == 0) {
12+
start++;
13+
}
14+
if (A[end] % 2 != 0) {
15+
end--;
16+
}
17+
}
18+
return A;
19+
}
20+
}

Medium/Boundary of Binary Tree.java

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,50 @@
88
* }
99
*/
1010
class Solution {
11-
List<Integer> list;
12-
int backIdx;
13-
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
14-
list = new ArrayList<>();
15-
backIdx = 0;
16-
17-
if (root == null) {
18-
return list;
19-
}
20-
21-
list.add(root.val);
22-
23-
addLeft(root.left);
24-
addLeaves(root.left);
25-
addLeaves(root.right);
26-
addRight(root.right);
27-
28-
return list;
11+
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
12+
List<Integer> list = new ArrayList<>();
13+
List<Integer> right = new ArrayList<>();
14+
if (root == null) {
15+
return list;
2916
}
30-
31-
private void addLeft(TreeNode node) {
32-
if (node == null || (node.left == null && node.right == null)) {
33-
return;
34-
}
35-
36-
list.add(node.val);
37-
38-
addLeft(node.left == null ? node.right : node.left);
17+
list.add(root.val);
18+
addLeft(root.left, list);
19+
addLeaves(root.left, list);
20+
addLeaves(root.right, list);
21+
addRight(root.right, right);
22+
for (int i = right.size() - 1; i >= 0; i--) {
23+
list.add(right.get(i));
3924
}
40-
41-
private void addRight(TreeNode node) {
42-
if (node == null || (node.right == null && node.left == null)) {
43-
return;
44-
}
45-
46-
list.add(list.size() - backIdx, node.val);
47-
backIdx++;
48-
49-
addRight(node.right == null ? node.left : node.right);
25+
return list;
26+
}
27+
28+
private void addLeft(TreeNode root, List<Integer> list) {
29+
if (root == null || (root.left == null && root.right == null)) {
30+
return;
5031
}
51-
52-
private void addLeaves(TreeNode node) {
53-
if (node == null) {
54-
return;
55-
}
56-
57-
if (node.left == null && node.right == null) {
58-
list.add(node.val);
59-
return;
60-
}
61-
62-
addLeaves(node.left);
63-
addLeaves(node.right);
32+
list.add(root.val);
33+
addLeft(root.left == null ? root.right : root.left, list);
34+
}
35+
36+
private void addRight(TreeNode root, List<Integer> list) {
37+
if (root == null || (root.left == null && root.right == null)) {
38+
return;
6439
}
40+
list.add(root.val);
41+
TreeNode next = root.right == null ? root.left : root.right;
42+
addRight(root.right == null ? root.left : root.right, list);
43+
}
44+
45+
private void addLeaves(TreeNode root, List<Integer> list) {
46+
if (root == null) {
47+
return;
48+
}
49+
if (root.left == null && root.right == null) {
50+
list.add(root.val);
51+
}
52+
else {
53+
addLeaves(root.left, list);
54+
addLeaves(root.right, list);
55+
}
56+
}
6557
}

Medium/Search Suggestions System.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public List<List<String>> suggestedProducts(String[] products, String searchWord) {
3+
List<List<String>> ans = new ArrayList<>();
4+
List<String> prev = new ArrayList<>();
5+
for (int i = 0; i < searchWord.length(); i++) {
6+
List<String> curr = new ArrayList<>();
7+
List<String> temp = new ArrayList<>();
8+
PriorityQueue<String> pq = new PriorityQueue<>(Comparator.reverseOrder());
9+
if (i == 0) {
10+
helper(Arrays.asList(products), searchWord, i, curr, pq);
11+
}
12+
else {
13+
helper(prev, searchWord, i, curr, pq);
14+
}
15+
while (!pq.isEmpty()) {
16+
temp.add(pq.poll());
17+
}
18+
Collections.sort(temp);
19+
ans.add(temp);
20+
prev = curr;
21+
}
22+
return ans;
23+
}
24+
25+
private void helper(List<String> products, String searchWord, int i, List<String> curr, PriorityQueue<String> pq) {
26+
for (String word : products) {
27+
if (i < word.length() && word.charAt(i) == searchWord.charAt(i)) {
28+
curr.add(word);
29+
pq.add(word);
30+
if (pq.size() > 3) {
31+
pq.poll();
32+
}
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)