forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🎨 Format files (🛠️ from Github Actions)
- Loading branch information
Showing
512 changed files
with
8,250 additions
and
7,926 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
class Solution { | ||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> prevMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int num = nums[i]; | ||
int diff = target - num; | ||
|
||
if (prevMap.containsKey(nums[i])) { | ||
return new int[]{prevMap.get(num), i}; | ||
} | ||
|
||
prevMap.put(target - num, i); | ||
} | ||
|
||
return new int[]{}; | ||
|
||
public int[] twoSum(int[] nums, int target) { | ||
HashMap<Integer, Integer> prevMap = new HashMap<>(); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
int num = nums[i]; | ||
int diff = target - num; | ||
|
||
if (prevMap.containsKey(nums[i])) { | ||
return new int[] { prevMap.get(num), i }; | ||
} | ||
|
||
prevMap.put(target - num, i); | ||
} | ||
} | ||
|
||
return new int[] {}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
class Solution { | ||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
return 1+Math.max(maxDepth(root.left), maxDepth(root.right)); | ||
} | ||
|
||
public int maxDepth(TreeNode root) { | ||
if (root == null) return 0; | ||
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,13 @@ | ||
class Solution { | ||
public int lastStoneWeight(int[] stones) { | ||
PriorityQueue<Integer> maxHeap = new PriorityQueue(); | ||
for (int stone: stones) | ||
maxHeap.add(-stone); | ||
while (maxHeap.size()>1) { | ||
int stone1 = maxHeap.remove(); | ||
int stone2 = maxHeap.remove(); | ||
if (stone1!=stone2) | ||
maxHeap.add(stone1-stone2); | ||
} | ||
return maxHeap.size()!=0?(-maxHeap.remove()):0; | ||
|
||
public int lastStoneWeight(int[] stones) { | ||
PriorityQueue<Integer> maxHeap = new PriorityQueue(); | ||
for (int stone : stones) maxHeap.add(-stone); | ||
while (maxHeap.size() > 1) { | ||
int stone1 = maxHeap.remove(); | ||
int stone2 = maxHeap.remove(); | ||
if (stone1 != stone2) maxHeap.add(stone1 - stone2); | ||
} | ||
return maxHeap.size() != 0 ? (-maxHeap.remove()) : 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 52 additions & 18 deletions
70
java/106-Construct-Binary-Tree-from-Inorder-and-Postorder-Traversal.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,58 @@ | ||
//This video helped https://www.youtube.com/watch?v=LgLRTaEMRVc&ab_channel=takeUforward | ||
|
||
class Solution { | ||
public TreeNode buildTree(int[] inorder, int[] postorder) { | ||
HashMap<Integer, Integer> inMap = new HashMap<>(); | ||
for (int i = 0; i<inorder.length; i++) { | ||
inMap.put(inorder[i],i); | ||
} | ||
return helper(inorder, 0, inorder.length-1, postorder,0, postorder.length-1, inMap); | ||
|
||
public TreeNode buildTree(int[] inorder, int[] postorder) { | ||
HashMap<Integer, Integer> inMap = new HashMap<>(); | ||
for (int i = 0; i < inorder.length; i++) { | ||
inMap.put(inorder[i], i); | ||
} | ||
|
||
public TreeNode helper(int[] inorder, int iStart, int iEnd, int[] postorder, int pStart, int pEnd, HashMap<Integer, Integer> inMap) { | ||
if (pStart>pEnd || iStart>iEnd) { | ||
return null; | ||
} | ||
TreeNode root = new TreeNode(postorder[pEnd]); | ||
int index = inMap.get(postorder[pEnd]); | ||
int numsLeft = index-iStart; | ||
root.left = helper(inorder, iStart, index-1, postorder, pStart, pStart+numsLeft-1, inMap); | ||
root.right = helper(inorder, index+1, iEnd, postorder, pStart+numsLeft, pEnd-1, inMap); | ||
return root; | ||
return helper( | ||
inorder, | ||
0, | ||
inorder.length - 1, | ||
postorder, | ||
0, | ||
postorder.length - 1, | ||
inMap | ||
); | ||
} | ||
|
||
public TreeNode helper( | ||
int[] inorder, | ||
int iStart, | ||
int iEnd, | ||
int[] postorder, | ||
int pStart, | ||
int pEnd, | ||
HashMap<Integer, Integer> inMap | ||
) { | ||
if (pStart > pEnd || iStart > iEnd) { | ||
return null; | ||
} | ||
|
||
TreeNode root = new TreeNode(postorder[pEnd]); | ||
int index = inMap.get(postorder[pEnd]); | ||
int numsLeft = index - iStart; | ||
root.left = | ||
helper( | ||
inorder, | ||
iStart, | ||
index - 1, | ||
postorder, | ||
pStart, | ||
pStart + numsLeft - 1, | ||
inMap | ||
); | ||
root.right = | ||
helper( | ||
inorder, | ||
index + 1, | ||
iEnd, | ||
postorder, | ||
pStart + numsLeft, | ||
pEnd - 1, | ||
inMap | ||
); | ||
return root; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
//Same just reverse in the end. | ||
//Same just reverse in the end. | ||
//Reversing in the end is better than using add(0, E) in case of arraylist as it's an O(1) operation. | ||
|
||
class Solution { | ||
public List<List<Integer>> levelOrderBottom(TreeNode root) { | ||
List<List<Integer>> ans = new ArrayList<>(); | ||
if (root == null) return ans; | ||
Queue<TreeNode> q = new LinkedList<>(); | ||
q.offer(root); | ||
while (!q.isEmpty()) { | ||
List<Integer> level = new ArrayList<>(); | ||
int size = q.size(); | ||
for (int i = 0; i<size; i++) { | ||
TreeNode cur = q.poll(); | ||
level.add(cur.val); | ||
if (cur.left!=null) q.offer(cur.left); | ||
if (cur.right!=null) q.offer(cur.right); | ||
} | ||
ans.add(level); | ||
} | ||
int i = 0 , j = ans.size()-1; | ||
//reverse the list | ||
Collections.reverse(ans); | ||
return ans; | ||
|
||
public List<List<Integer>> levelOrderBottom(TreeNode root) { | ||
List<List<Integer>> ans = new ArrayList<>(); | ||
if (root == null) return ans; | ||
Queue<TreeNode> q = new LinkedList<>(); | ||
q.offer(root); | ||
while (!q.isEmpty()) { | ||
List<Integer> level = new ArrayList<>(); | ||
int size = q.size(); | ||
for (int i = 0; i < size; i++) { | ||
TreeNode cur = q.poll(); | ||
level.add(cur.val); | ||
if (cur.left != null) q.offer(cur.left); | ||
if (cur.right != null) q.offer(cur.right); | ||
} | ||
ans.add(level); | ||
} | ||
int i = 0, j = ans.size() - 1; | ||
//reverse the list | ||
Collections.reverse(ans); | ||
return ans; | ||
} | ||
} |
Oops, something went wrong.