-
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.
Divide & Conquer, Dynamic Programming
- Loading branch information
1 parent
0d7ccc4
commit 321b6fb
Showing
6 changed files
with
161 additions
and
0 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,14 @@ | ||
class Solution: | ||
def majorityElement(self, nums: List[int]) -> int: | ||
if not nums: | ||
return None | ||
|
||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
|
||
half = len(nums) // 2 | ||
a = self.majorityElement(nums[:half]) | ||
b = self.majorityElement(nums[half:]) | ||
|
||
return [b,a][nums.count(a) > half] |
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,49 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
import bisect | ||
import sys | ||
|
||
class Solution: | ||
def rob(self, nums: List[int]) -> int: | ||
dp = collections.defaultdict(int) | ||
|
||
if not nums: | ||
return 0 | ||
|
||
if len(nums) < 3: | ||
return max(nums) | ||
|
||
result = -sys.maxsize | ||
dp[0] = nums[0] | ||
dp[1] = nums[1] | ||
dp[2] = nums[2] + nums[0] | ||
|
||
|
||
for i in range(3, len(nums)): | ||
dp[i] = max(dp[i-2] + nums[i], dp[i-3] + nums[i]) | ||
result = max(result, dp[i]) | ||
|
||
result = max(result, dp[0], dp[1], dp[2]) | ||
return result | ||
|
||
class Solution2: | ||
def rob(self, nums: List[int]) -> int: | ||
if not nums: | ||
return 0 | ||
|
||
if len(nums) <= 2: | ||
return max(nums) | ||
|
||
dp = collections.OrderedDict() | ||
dp[0] = nums[0] | ||
dp[1] = max(nums[0], nums[1]) | ||
|
||
|
||
for i in range(2, len(nums)): | ||
dp[i] = max(dp[i-1], dp[i-2] + nums[i]) | ||
|
||
return dp.popitem()[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,24 @@ | ||
class Solution: | ||
def diffWaysToCompute(self, input: str) -> List[int]: | ||
|
||
def compute(left, right, op): | ||
results = [] | ||
for l in left: | ||
for r in right: | ||
results.append(eval(str(l) + op + str(r))) | ||
|
||
return results | ||
|
||
if input.isdigit(): | ||
return [int(input)] | ||
|
||
results = [] | ||
|
||
for index, value in enumerate(input): | ||
if value in "-*+": | ||
left = self.diffWaysToCompute(input[:index]) | ||
right = self.diffWaysToCompute(input[index+1:]) | ||
|
||
results.extend(compute(left, right, value)) | ||
|
||
return results |
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,30 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
import bisect | ||
|
||
class Solution: | ||
dp = collections.defaultdict(int) | ||
def fib(self, n: int) -> int: | ||
if n <= 1: | ||
return n | ||
|
||
|
||
if self.dp[n]: | ||
return self.dp[n] | ||
|
||
self.dp[n] = self.fib(n-1) + self.fib(n-2) | ||
|
||
return self.dp[n] | ||
|
||
|
||
class Solution2: | ||
dp = collections.defaultdict(int) | ||
|
||
def fib(self, n: int) -> int: | ||
self.dp[1] = 1 | ||
for i in range(2, n+1): | ||
self.dp[i] = self.dp[i-1] + self.dp[i-2] | ||
|
||
return self.dp[n] |
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,12 @@ | ||
class Solution: | ||
def maxSubArray(self, nums: List[int]) -> int: | ||
if len(nums) == 1: | ||
return nums[0] | ||
|
||
current_sum = -2 | ||
best_sum = nums[0] | ||
for i, num in enumerate(nums, 1): | ||
current_sum = max(current_sum + num, num) | ||
best_sum = max(best_sum, current_sum) | ||
|
||
return best_sum |
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,32 @@ | ||
import re | ||
import collections | ||
import itertools | ||
import heapq | ||
import bisect | ||
|
||
class Solution: | ||
dp = collections.defaultdict(int) | ||
dp[1] = 1 | ||
dp[2] = 2 | ||
def climbStairs(self, n: int) -> int: | ||
if n <= 2: | ||
return n | ||
|
||
|
||
if self.dp[n]: | ||
return self.dp[n] | ||
|
||
|
||
self.dp[n] = self.climbStairs(n-1) + self.climbStairs(n-2) | ||
|
||
return self.dp[n] | ||
|
||
class Solution2: | ||
dp = collections.defaultdict(int) | ||
dp[1] = 1 | ||
dp[2] = 2 | ||
def climbStairs(self, n: int) -> int: | ||
for i in range(3, n+1): | ||
self.dp[i] = self.dp[i-1] + self.dp[i-2] | ||
|
||
return self.dp[n] |