From 434e9a8c9fc7f7f152f7e14136f0c9c0ec2413d0 Mon Sep 17 00:00:00 2001 From: penpenps Date: Mon, 22 Jul 2019 23:08:37 +0800 Subject: [PATCH] Add py solutions for #0152 and #0169 --- .../py-0152/Solution1.py | 31 +++++++++++++++++++ .../py-0152/Solution2.py | 27 ++++++++++++++++ 0169-Majority-Element/py-0169/Solution.py | 13 ++++++++ 0169-Majority-Element/py-0169/Solution2.py | 18 +++++++++++ 0169-Majority-Element/py-0169/Solution3.py | 21 +++++++++++++ 0169-Majority-Element/py-0169/Solution4.py | 28 +++++++++++++++++ 0169-Majority-Element/py-0169/Solution5.py | 23 ++++++++++++++ readme.md | 4 +-- 8 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 0152-Maximum-Product-Subarray/py-0152/Solution1.py create mode 100644 0152-Maximum-Product-Subarray/py-0152/Solution2.py create mode 100644 0169-Majority-Element/py-0169/Solution.py create mode 100644 0169-Majority-Element/py-0169/Solution2.py create mode 100644 0169-Majority-Element/py-0169/Solution3.py create mode 100644 0169-Majority-Element/py-0169/Solution4.py create mode 100644 0169-Majority-Element/py-0169/Solution5.py diff --git a/0152-Maximum-Product-Subarray/py-0152/Solution1.py b/0152-Maximum-Product-Subarray/py-0152/Solution1.py new file mode 100644 index 00000000..641ff8a9 --- /dev/null +++ b/0152-Maximum-Product-Subarray/py-0152/Solution1.py @@ -0,0 +1,31 @@ +# Source : https://leetcode.com/problems/maximum-product-subarray/ +# Author : penpenps +# Time : 2019-07-21 + +from typing import List + +# Travesal nums, use maxVal and minVal to keep max and min product end with current element +# Time Complexity: O(n) +# Space Complexity: O(1) + +class Solution: + def maxProduct(self, nums: List[int]) -> int: + n = len(nums) + minVal, maxVal = nums[0], nums[0] + ans = nums[0] + for i in range(1, n): + # If negative number, it should swap min and max for sign switching + if nums[i] < 0: + minVal, maxVal = maxVal, minVal + maxVal = max(nums[i]*maxVal, nums[i]) + minVal = min(nums[i]*minVal, nums[i]) + ans = max(maxVal, ans) + + return ans + + +if __name__ == '__main__': + s = Solution() + nums = [2,3,-2,4] + answer = s.maxProduct(nums) + print(answer) diff --git a/0152-Maximum-Product-Subarray/py-0152/Solution2.py b/0152-Maximum-Product-Subarray/py-0152/Solution2.py new file mode 100644 index 00000000..a5935d13 --- /dev/null +++ b/0152-Maximum-Product-Subarray/py-0152/Solution2.py @@ -0,0 +1,27 @@ +# Source : https://leetcode.com/problems/maximum-product-subarray/ +# Author : penpenps +# Time : 2019-07-21 + +from typing import List + +# If even number of elements, the max product is multiple all nums together +# If odd number, it should be divided into to part from one of the negative numbers and the answer is one of two seperated parts to multiple together +# Time Complexity: O(n) +# Space Complexity: O(1) + +class Solution: + def maxProduct(self, nums: List[int]) -> int: + n = len(nums) + rnums = nums[::-1] + for i in range(1, n): + nums[i] *= nums[i-1] or 1 + rnums[i] *= rnums[i-1] or 1 + + return max(nums + rnums) + + +if __name__ == '__main__': + s = Solution() + nums = [2,3,-2,4] + answer = s.maxProduct(nums) + print(answer) diff --git a/0169-Majority-Element/py-0169/Solution.py b/0169-Majority-Element/py-0169/Solution.py new file mode 100644 index 00000000..91ef31b4 --- /dev/null +++ b/0169-Majority-Element/py-0169/Solution.py @@ -0,0 +1,13 @@ +# Source : https://leetcode.com/problems/majority-element/ +# Author : penpenps +# Time : 2019-07-22 + +from typing import List + +# Sort nums and pick the (n/2)-th number +# Time Complexity: O(nlogn) +# Space Complexity: O(n) + +class Solution: + def majorityElement(self, nums: List[int]) -> int: + return sorted(nums)[len(nums)//2] diff --git a/0169-Majority-Element/py-0169/Solution2.py b/0169-Majority-Element/py-0169/Solution2.py new file mode 100644 index 00000000..134f5203 --- /dev/null +++ b/0169-Majority-Element/py-0169/Solution2.py @@ -0,0 +1,18 @@ +# Source : https://leetcode.com/problems/majority-element/ +# Author : penpenps +# Time : 2019-07-22 + +from typing import List + +# Count occurance for each number and find out the one whose occurance more than n/2 times +# Time Complexity: O(n) +# Space Complexity: O(n) + +import collections +class Solution: + def majorityElement(self, nums: List[int]) -> int: + m = collections.Counter(nums) + n = len(nums) + for k,v in m.items(): + if v >= n/2: + return k diff --git a/0169-Majority-Element/py-0169/Solution3.py b/0169-Majority-Element/py-0169/Solution3.py new file mode 100644 index 00000000..991edcf6 --- /dev/null +++ b/0169-Majority-Element/py-0169/Solution3.py @@ -0,0 +1,21 @@ +# Source : https://leetcode.com/problems/majority-element/ +# Author : penpenps +# Time : 2019-07-22 + +from typing import List + +# Randomly pick one to check whether the answer +# Time Complexity: Worst: O(infinity) Average: O(n) +# Space Complexity: O(1) + +import random +class Solution: + def majorityElement(self, nums: List[int]) -> int: + def occurance(nums, target): + return sum(1 if x == target else 0 for x in nums) + + n = len(nums) + while True: + index = random.randint(0, n-1) + if occurance(nums, nums[index]) >= n / 2: + return nums[index] diff --git a/0169-Majority-Element/py-0169/Solution4.py b/0169-Majority-Element/py-0169/Solution4.py new file mode 100644 index 00000000..beb73d5f --- /dev/null +++ b/0169-Majority-Element/py-0169/Solution4.py @@ -0,0 +1,28 @@ +# Source : https://leetcode.com/problems/majority-element/ +# Author : penpenps +# Time : 2019-07-22 + +from typing import List + +# Recursivly divide two half parts and search most occurance number +# Time Complexity: O(nlogn) +# Space Complexity: O(logn) + +class Solution: + def majorityElement(self, nums: List[int]) -> int: + def occurance(nums, left, right, target): + return sum(1 if x == target else 0 for x in nums[left:right+1]) + def helper(nums, left, right): + length = right - left + 1 + if length <= 2: + return nums[left] + mid = (left+right) // 2 + leftMaj = helper(nums, left, mid) + rightMaj = helper(nums, mid+1, right) + if leftMaj == rightMaj: + return leftMaj + leftMajCnt = occurance(nums, left, mid, leftMaj) + rightMajCnt = occurance(nums, mid+1, right, rightMaj) + + return leftMaj if leftMajCnt > rightMajCnt else rightMaj + return helper(nums, 0, len(nums)-1) diff --git a/0169-Majority-Element/py-0169/Solution5.py b/0169-Majority-Element/py-0169/Solution5.py new file mode 100644 index 00000000..5267e498 --- /dev/null +++ b/0169-Majority-Element/py-0169/Solution5.py @@ -0,0 +1,23 @@ +# Source : https://leetcode.com/problems/majority-element/ +# Author : penpenps +# Time : 2019-07-22 + +from typing import List + +# Boyer-Moore Voting Algorithm, count for one number, if not equal to current number, count minus 1 until ot 0 then try another number +# Time Complexity: O(n) +# Space Complexity: O(1) + +class Solution: + def majorityElement(self, nums: List[int]) -> int: + ans = nums[0] + count = 0 + for n in nums: + if count == 0: + ans = n + count += 1 + elif n == ans: + count += 1 + else: + count -= 1 + return ans diff --git a/readme.md b/readme.md index 67dcf20f..62fac3b1 100644 --- a/readme.md +++ b/readme.md @@ -185,7 +185,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | 149 | [Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/description/) | [solution](https://leetcode.com/problems/max-points-on-a-line/solution/) | [C++](0149-Max-Points-on-a-Line/cpp-0149/) | | | | 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/description/) | [无] | [C++](0150-Evaluate-Reverse-Polish-Notation/cpp-0150/) | | | | 151 | [Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/) | [无] | [C++](151-Reverse-Words-in-a-String/cpp-0151/) | | | -| | | | | | | +| 152 | [Maximum Product Subarray](https://leetcode.com/problems/maximum-product-subarray/) | [无] | | | [Python](0152-Maximum-Product-Subarray/py-0152/) | | 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/) | [solution](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/solution/) | [C++](0153-Find-Minimum-in-Rotated-Sorted-Array/cpp-0153/) | | | | | | | | | | | 155 | [Min Stack](https://leetcode.com/problems/min-stack/description/) | [无] | [C++](0155-Min-Stack/cpp-0155/) | | | @@ -196,7 +196,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com) | | | | | | | | 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/) | [solution](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/solution/) | [C++](0167-Two-Sum-II-Input-array-is-sorted/cpp-0167/) | [Java](0167-Two-Sum-II-Input-array-is-sorted/java-0167/src/) | | | | | | | | | -| 169 | [Majority Element](https://leetcode.com/problems/majority-element/description/) | [solution](https://leetcode.com/problems/majority-element/solution/) | [C++](0169-Majority-Element/cpp-0169/) | | | +| 169 | [Majority Element](https://leetcode.com/problems/majority-element/description/) | [solution](https://leetcode.com/problems/majority-element/solution/) | [C++](0169-Majority-Element/cpp-0169/) | | [Python](0169-Majority-Element/py-0169/) | | 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/description/) | [无] | [C++](0170-Two-Sum-III-Data-structure-design/cpp-0170/) | | | | 171 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/description/) | [无] | [C++](0171-Excel-Sheet-Column-Number/cpp-0171/) | | | | | | | | | |