Skip to content

Commit 8f248e1

Browse files
authored
Merge pull request #2280 from se6816/main
[se6816] WEEK 10 solutions
2 parents 090f8fa + a409297 commit 8f248e1

4 files changed

Lines changed: 122 additions & 0 deletions

File tree

invert-binary-tree/se6816.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public TreeNode invertTree(TreeNode root) {
3+
if(root == null) return root;
4+
Queue<TreeNode> que = new ArrayDeque<>();
5+
que.add(root);
6+
7+
while(!que.isEmpty()) {
8+
TreeNode node = que.poll();
9+
TreeNode tempNode = node.left;
10+
node.left = node.right;
11+
node.right = tempNode;
12+
if(node.left != null) {
13+
que.add(node.left);
14+
}
15+
if(node.right != null) {
16+
que.add(node.right);
17+
}
18+
}
19+
return root;
20+
}
21+
}

jump-game/se6816.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public boolean canJump(int[] nums) {
3+
boolean[] visited= new boolean[nums.length];
4+
dfs(visited, nums, 0);
5+
return visited[nums.length - 1];
6+
}
7+
public void dfs(boolean[] visited, int[] nums, int target) {
8+
if(visited[nums.length -1]) {
9+
return;
10+
}
11+
if(visited[target]) {
12+
return;
13+
}
14+
15+
visited[target] = true;
16+
17+
int maxDist = nums[target];
18+
for(int i = maxDist; i > 0; i--) {
19+
int nextIdx = target + i;
20+
if(nextIdx > nums.length - 1) {
21+
continue;
22+
}
23+
24+
dfs(visited, nums, nextIdx);
25+
}
26+
}
27+
}

merge-k-sorted-lists/se6816.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
class Solution {
3+
public ListNode mergeKLists(ListNode[] lists) {
4+
ListNode list= new ListNode(0); // head
5+
ListNode curr=list;
6+
PriorityQueue<ListNode> pq=new PriorityQueue<ListNode>((l1, l2)->{
7+
return l1.val - l2.val;
8+
});
9+
for(int i=0; i<lists.length;i++){
10+
if(lists[i]!=null){
11+
pq.offer(lists[i]);
12+
}
13+
}
14+
while(!pq.isEmpty()){
15+
ListNode temp = pq.poll();
16+
curr.next=new ListNode(temp.val);
17+
curr=curr.next;
18+
temp=temp.next;
19+
if(temp!=null){
20+
pq.offer(temp);
21+
}
22+
23+
}
24+
return list.next;
25+
26+
27+
}
28+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int firstIdx = binarySearch(nums);
4+
int result = 0;
5+
if (firstIdx == 0) {
6+
return binarySearch(target, nums, 0, nums.length - 1);
7+
} else if (target >= nums[0]) {
8+
return binarySearch(target, nums, 0, firstIdx);
9+
} else {
10+
return binarySearch(target, nums, firstIdx, nums.length - 1);
11+
}
12+
}
13+
14+
public int binarySearch(int[] nums) {
15+
int start = 0;
16+
int end = nums.length -1;
17+
while(start < end) {
18+
int mid = (start + end) / 2;
19+
if(nums[mid] < nums[end]) {
20+
end = mid;
21+
} else {
22+
start = mid + 1;
23+
}
24+
}
25+
return start;
26+
}
27+
28+
public int binarySearch(int target, int[] nums, int start, int end) {
29+
int result = -1;
30+
while(start <= end) {
31+
int mid = (start + end) / 2;
32+
if(nums[mid] > target) {
33+
end = mid - 1;
34+
} else if(nums[mid] < target){
35+
start = mid + 1;
36+
} else {
37+
result = mid;
38+
break;
39+
}
40+
}
41+
42+
return result;
43+
}
44+
45+
46+
}

0 commit comments

Comments
 (0)