forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request liuyubobobo#16 from penpenps/master
Add py solutions for #0152 and #0169
- Loading branch information
Showing
8 changed files
with
163 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/) | | | | ||
|
@@ -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/) | | | | ||
| | | | | | | | ||
|