GeezerVison is showing github.com — 628_Maximum_Product_of_Three_Numbers · Taogal/leetcode-python@ede4a59
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -152,6 +152,7 @@ Also, there are open source implementations for basic data structs and algorithm | 152 | 152 | | 572 | [Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/572_Subtree_of_Another_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/572_Subtree_of_Another_Tree.java) | 1. Tree traverse and compare<br>2. Tree to string and compare | | 153 | 153 | | 581 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/subtree-of-another-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/581_Shortest_Unsorted_Continuous_Subarray.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/581_Shortest_Unsorted_Continuous_Subarray.java) | 1. Sort and find the difference (min and max), O(nlgn)<br>2. Using stack to find boundaries (push when correct order, pop when not correct), O(n) and O(n)<br>3. Find min and max of unordered array, O(n) and O(1)| | 154 | 154 | | 617 | [Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/617_Merge_Two_Binary_Trees.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/617_Merge_Two_Binary_Trees.java) | Traverse both trees Recursion & Iterative (stack) | | | 155 | +| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/628_Maximum_Product_of_Three_Numbers.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/628_Maximum_Product_of_Three_Numbers.java) | Actually, we should only care about min1, min2 and max1-max3, to find these five elements, we can use 1. Brute force, O(n^3) and O(1)<br>2. Sort, O(nlogn) and O(1)<br>3. Single scan with 5 elements keep, O(n) and O(1) | | 155 | 156 | | 654 | [Maximum Binary Tree](https://leetcode.com/problems/maximum-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/654_Maximum_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/654_Maximum_Binary_Tree.java) | 1. Divide and conquer, recursive, O(n^2)<br>2. Monotonic stack, O(n) | | 156 | 157 | | 680 | [Valid Palindrome II](https://leetcode.com/problems/valid-palindrome-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/680_Valid_Palindrome_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/680_Valid_Palindrome_II.java) | Recursively check s[left == end, when not equal delete left or right. | | 157 | 158 | | 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) | | |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +public class Solution { | | 2 | + /*public int maximumProduct(int[] nums) { | | 3 | + Arrays.sort(nums); | | 4 | + return Math.max(nums[0] * nums[1] * nums[nums.length - 1], nums[nums.length - 1] * nums[nums.length - 2] * nums[nums.length - 3]); | | 5 | + }*/ | | 6 | + | | 7 | + public int maximumProduct(int[] nums) { | | 8 | + int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE; | | 9 | + int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE, max3 = Integer.MIN_VALUE; | | 10 | + for (int n: nums) { | | 11 | + if (n <= min1) { | | 12 | + min2 = min1; | | 13 | + min1 = n; | | 14 | + } else if (n <= min2) { // n lies between min1 and min2 | | 15 | + min2 = n; | | 16 | + } | | 17 | + if (n >= max1) { // n is greater than max1, max2 and max3 | | 18 | + max3 = max2; | | 19 | + max2 = max1; | | 20 | + max1 = n; | | 21 | + } else if (n >= max2) { // n lies betweeen max1 and max2 | | 22 | + max3 = max2; | | 23 | + max2 = n; | | 24 | + } else if (n >= max3) { // n lies betwen max2 and max3 | | 25 | + max3 = n; | | 26 | + } | | 27 | + } | | 28 | + return Math.max(min1 * min2 * max1, max1 * max2 * max3); | | 29 | + } | | 30 | +} | | 31 | + |
Collapse file Copy file name to clipboard +30Lines changed: 30 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +class Solution(object): | | 2 | + # def maximumProduct(self, nums): | | 3 | + # """ | | 4 | + # :type nums: List[int] | | 5 | + # :rtype: int | | 6 | + # """ | | 7 | + # nums.sort() | | 8 | + # # Check min1*min2*max1 and max1*max2*max3 | | 9 | + # return max(reduce(lambda x, y: x * y, nums[:2]) * nums[-1], | | 10 | + # reduce(lambda x, y: x * y, nums[-3:])) | | 11 | + | | 12 | + def maximumProduct(self, nums): | | 13 | + min1 = min2 = float('inf') | | 14 | + max1 = max2 = max3 = float('-inf') | | 15 | + for num in nums: | | 16 | + if num <= min1: | | 17 | + min2 = min1 | | 18 | + min1 = num | | 19 | + elif num <= min2: | | 20 | + min2 = num | | 21 | + if num >= max1: | | 22 | + max3 = max2 | | 23 | + max2 = max1 | | 24 | + max1 = num | | 25 | + elif num >= max2: | | 26 | + max3 = max2 | | 27 | + max2 = num | | 28 | + elif num >= max3: | | 29 | + max3 = num | | 30 | + return max(min1 * min2 * max1, max1 * max2 * max3) |
|