Skip to content

Commit

Permalink
Merge pull request liuyubobobo#16 from penpenps/master
Browse files Browse the repository at this point in the history
Add py solutions for #0152 and #0169
  • Loading branch information
liuyubobobo authored Jul 22, 2019
2 parents 3a6a701 + 434e9a8 commit 371d7a3
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 2 deletions.
31 changes: 31 additions & 0 deletions 0152-Maximum-Product-Subarray/py-0152/Solution1.py
Original file line number Diff line number Diff line change
@@ -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)
27 changes: 27 additions & 0 deletions 0152-Maximum-Product-Subarray/py-0152/Solution2.py
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions 0169-Majority-Element/py-0169/Solution.py
Original file line number Diff line number Diff line change
@@ -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]
18 changes: 18 additions & 0 deletions 0169-Majority-Element/py-0169/Solution2.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions 0169-Majority-Element/py-0169/Solution3.py
Original file line number Diff line number Diff line change
@@ -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]
28 changes: 28 additions & 0 deletions 0169-Majority-Element/py-0169/Solution4.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions 0169-Majority-Element/py-0169/Solution5.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ email: [[email protected]](mailto:[email protected])
| 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/) | | |
Expand All @@ -196,7 +196,7 @@ email: [[email protected]](mailto:[email protected])
| | | | | | |
| 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/) | | |
| | | | | | |
Expand Down

0 comments on commit 371d7a3

Please sign in to comment.