Skip to content

Commit

Permalink
add 654 java, py, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 3, 2023
1 parent 035ef08 commit bb2c104
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@
637 |[Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree/)| [Python](./leetcode_python/Tree/average-of-levels-in-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/AverageOfLevelsInBinaryTree.java) | _O(n)_ | _O(h)_ | Easy |`bfs`, `dfs`, `good basic`, `fb`| OK*** (4)
652 |[Find Duplicate Subtrees](https://leetcode.com/problems/find-duplicate-subtrees/)| [Python](./leetcode_python/Tree/find-duplicate-subtrees.py) | _O(n)_ | _O(n)_ | Medium | AGAIN, good basic,dfs, Hash, `amazon`| AGAIN************** (6)
653 |[Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/)| [Python](./leetcode_python/Tree/two-sum-iv-input-is-a-bst.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/TwoSumIV.java)| _O(n)_ | _O(h)_ | Easy | Two Pointers,`2 sum`,`bfs`, `dfs`,`amazon`,`fb`| OK******* (4)
654 |[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)| [Python](./leetcode_python/Tree/maximum-binary-tree.py)| _O(n)_ | _O(n)_ | Medium |LintCode, Descending Stack, `good basic` | AGAIN* (2)
654 |[Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/)| [Python](./leetcode_python/Tree/maximum-binary-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java)| _O(n)_ | _O(n)_ | Medium |LintCode, Descending Stack, `good basic` | AGAIN* (2)
655 | [Print Binary Tree](https://leetcode.com/problems/print-binary-tree/) | [Python](./leetcode_python/Tree/print-binary-tree.py) | _O(n)_ | _O(h)_ | Medium | | AGAIN* (2) (not start)
662 | [Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Python](./leetcode_python/Tree/maximum-width-of-binary-tree.py) | _O(n)_ | _O(h)_ | Medium | width of tree, bfs, dfs, `trick`, `amazon` | AGAIN********* (5)
663 | [Equal Tree Partition](https://leetcode.com/problems/equal-tree-partition/) | [Python](./leetcode_python/Tree/equal-tree-partition.py) | _O(n)_ | _O(n)_ | Medium |`# LC 508`, AGAIN, 🔒 , Hash, tree, dfs ,`trick`, `good trick`, `amazon`| OK******** (8)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20230903: 654
20230830: 200
20230827: 131,17
20230822: 79
Expand Down
15 changes: 9 additions & 6 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
2023-10-28 -> ['654']
2023-10-24 -> ['200']
2023-10-21 -> ['131,17']
2023-10-16 -> ['79']
2023-10-15 -> ['40']
2023-10-14 -> ['90']
2023-10-13 -> ['46']
2023-10-07 -> ['78,39']
2023-10-07 -> ['654', '78,39']
2023-10-06 -> ['355']
2023-10-05 -> ['621']
2023-10-03 -> ['200', '973,215']
2023-09-30 -> ['131,17']
2023-09-25 -> ['79']
2023-09-24 -> ['40']
2023-09-24 -> ['654', '40']
2023-09-23 -> ['90', '208,211']
2023-09-22 -> ['46', '230,105']
2023-09-21 -> ['098']
2023-09-20 -> ['200']
2023-09-19 -> ['1448']
2023-09-18 -> ['199']
2023-09-17 -> ['131,17']
2023-09-16 -> ['78,39', '572,235']
2023-09-16 -> ['654', '78,39', '572,235']
2023-09-15 -> ['355', '2,146']
2023-09-14 -> ['621', '138']
2023-09-13 -> ['981,143']
2023-09-12 -> ['200', '79', '973,215']
2023-09-11 -> ['40', '452']
2023-09-11 -> ['654', '40', '452']
2023-09-10 -> ['90', '33']
2023-09-09 -> ['131,17', '46', '875,153']
2023-09-08 -> ['853,74']
2023-09-08 -> ['654', '853,74']
2023-09-07 -> ['200', '739,150,22']
2023-09-04 -> ['200', '131,17', '79', '567,155']
2023-09-06 -> ['654']
2023-09-05 -> ['654']
2023-09-04 -> ['654', '200', '131,17', '79', '567,155']
2023-09-03 -> ['40', '78,39', '19']
2023-09-02 -> ['200', '90', '355', '208,211', '128,167,3,424']
2023-09-01 -> ['200', '131,17', '46', '621', '230,105', '36,271']
Expand Down
127 changes: 127 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package LeetCodeJava.Tree;

import LeetCodeJava.DataStructure.TreeNode;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Optional;
import java.util.OptionalInt;

// https://leetcode.com/problems/maximum-binary-tree/

public class MaximumBinaryTree {

TreeNode root = new TreeNode();

// TODO : fix below
// V0
// public TreeNode constructMaximumBinaryTree(int[] nums) {
//
// if (nums.length == 1){
// return new TreeNode(nums[0]);
// }
//
// // get max val in nums
//// int max_val = Arrays.stream(nums).max().getAsInt();
//// System.out.println("max_val = " + max_val);
////
//// int idx = Arrays.asList(nums).indexOf(max_val);
//
// // recursive
// return _help(nums);
// }
//
// private TreeNode _help(int[] nums){
//
// if (nums.length == 0){
// return null;
// }
//
// // ??
// if (nums.length == 1){
// return new TreeNode(nums[0]);
// }
//
// Integer[] _nums = toConvertInteger(nums);
// // get max val in nums
// //Optional<Integer> max_val = Arrays.stream(_nums).max(Comparator.comparing(x, y));
// Integer max_val = getMax(_nums);
// // get idx of max val in nums
// Integer idx = Arrays.asList(_nums).indexOf(max_val);
// System.out.println("max_val = " + max_val + " idx = " + idx);
//
// this.root.val = max_val;
// this.root.left = _help(Arrays.copyOfRange(nums, 0, idx+1));
// this.root.right = _help(Arrays.copyOfRange(nums, idx+1, nums.length+1));
//
// System.out.println("root.left = " + root.left.val + " root.right = " + root.right.val);
//
// return this.root;
// }
//
// private int getMax(Integer[] input){
// int res = -1;
// for(Integer x : input){
// if(x > res){
// res = x;
// }
// }
// return res;
// }
//
// public static Integer[] toConvertInteger(int[] ids) {
//
// Integer[] newArray = new Integer[ids.length];
// for (int i = 0; i < ids.length; i++) {
// newArray[i] = Integer.valueOf(ids[i]);
// }
// return newArray;
// }

// V1
// IDEA : Recursive Solution
// https://leetcode.com/problems/maximum-binary-tree/editorial/
public TreeNode constructMaximumBinaryTree_1(int[] nums) {
return construct(nums, 0, nums.length);
}

/** NOTE !!! : parameters : l, r */
public TreeNode construct(int[] nums, int l, int r) {
if (l == r)
return null;
int max_i = max(nums, l, r);
TreeNode root = new TreeNode(nums[max_i]);
root.left = construct(nums, l, max_i);
root.right = construct(nums, max_i + 1, r);
return root;
}
public int max(int[] nums, int l, int r) {
int max_i = l;
for (int i = l; i < r; i++) {
if (nums[max_i] < nums[i])
max_i = i;
}
return max_i;
}


// public static void main(String[] args) {
//
// Integer[] _nums = new Integer[]{1,2,3};
// Integer[] sub = Arrays.copyOfRange(_nums, 0, 2);
//
// System.out.println(_nums);
// System.out.println(Arrays.asList(_nums).indexOf(3));
//
// Integer[] array1 = {2, 4, 6, 8, 10};
// int index = Arrays.asList(array1).indexOf(8);
// System.out.println("Found element at location at index:"+index);
//
//
//// for(int x :sub){
//// System.out.println(x);
//// }
// //System.out.println(Arrays.copyOfRange(_nums, 0,3));
// }

}
44 changes: 44 additions & 0 deletions leetcode_python/Tree/maximum-binary-tree.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
"""
654. Maximum Binary Tree
Medium
You are given an integer array nums with no duplicates. A maximum binary tree can be built recursively from nums using the following algorithm:
Create a root node whose value is the maximum value in nums.
Recursively build the left subtree on the subarray prefix to the left of the maximum value.
Recursively build the right subtree on the subarray suffix to the right of the maximum value.
Return the maximum binary tree built from nums.
Example 1:
Input: nums = [3,2,1,6,0,5]
Output: [6,3,5,null,2,0,null,null,1]
Explanation: The recursive calls are as follow:
- The largest value in [3,2,1,6,0,5] is 6. Left prefix is [3,2,1] and right suffix is [0,5].
- The largest value in [3,2,1] is 3. Left prefix is [] and right suffix is [2,1].
- Empty array, so no child.
- The largest value in [2,1] is 2. Left prefix is [] and right suffix is [1].
- Empty array, so no child.
- Only one element, so child is a node with value 1.
- The largest value in [0,5] is 5. Left prefix is [0] and right suffix is [].
- Only one element, so child is a node with value 0.
- Empty array, so no child.
Example 2:
Input: nums = [3,2,1]
Output: [3,null,2,null,1]
Constraints:
1 <= nums.length <= 1000
0 <= nums[i] <= 1000
All integers in nums are unique.
"""

# V0

# V1
Expand Down

0 comments on commit bb2c104

Please sign in to comment.