From bb2c104093b4dd6881e7be0dc7202f2042fd6cf9 Mon Sep 17 00:00:00 2001 From: yennanliu Date: Sun, 3 Sep 2023 15:27:46 +0100 Subject: [PATCH] add 654 java, py, update progress --- README.md | 2 +- data/progress.txt | 1 + data/to_review.txt | 15 ++- .../LeetCodeJava/Tree/MaximumBinaryTree.java | 127 ++++++++++++++++++ leetcode_python/Tree/maximum-binary-tree.py | 44 ++++++ 5 files changed, 182 insertions(+), 7 deletions(-) create mode 100644 leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java diff --git a/README.md b/README.md index 5a361da21..3c38b94b6 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/data/progress.txt b/data/progress.txt index 592f9e9e7..ebccf29da 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -1,3 +1,4 @@ +20230903: 654 20230830: 200 20230827: 131,17 20230822: 79 diff --git a/data/to_review.txt b/data/to_review.txt index f5f56f278..e0a78ad46 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -1,16 +1,17 @@ +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'] @@ -18,17 +19,19 @@ 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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java b/leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java new file mode 100644 index 000000000..13add66d0 --- /dev/null +++ b/leetcode_java/src/main/java/LeetCodeJava/Tree/MaximumBinaryTree.java @@ -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 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)); +// } + +} diff --git a/leetcode_python/Tree/maximum-binary-tree.py b/leetcode_python/Tree/maximum-binary-tree.py index 34c0fe995..b9c33f598 100644 --- a/leetcode_python/Tree/maximum-binary-tree.py +++ b/leetcode_python/Tree/maximum-binary-tree.py @@ -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