-
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.
- Loading branch information
1 parent
248c757
commit 05e5eb7
Showing
8 changed files
with
147 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,10 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
num_map = {} | ||
for i, num in enumerate(nums): | ||
if target - num in num_map and i != num_map[target-num]: | ||
return i, num_map[target-num] | ||
num_map[num] = i; | ||
|
||
|
||
|
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,10 @@ | ||
class Solution: | ||
def maxProfit(self, prices: List[int]) -> int: | ||
profit = 0 | ||
min_price = sys.maxsize | ||
|
||
for price in prices: | ||
min_price = min(min_price, price) | ||
profit = max(price-min_price, profit) | ||
|
||
return profit |
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 @@ | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
result = [] | ||
nums.sort() | ||
|
||
|
||
for i in range(len(nums) - 2): | ||
if i > 0 and nums[i] == nums[i-1]: | ||
continue | ||
|
||
left, right = i + 1, len(nums) - 1 | ||
while left < right: | ||
three_sum = nums[i] + nums[left] + nums[right] | ||
|
||
if three_sum < 0: | ||
left += 1 | ||
elif three_sum > 0: | ||
right -= 1 | ||
else: | ||
result.append([nums[i], nums[left], nums[right]]) | ||
|
||
while left < right and nums[left] == nums[left + 1]: | ||
left += 1 | ||
while left < right and nums[right] == nums[right - 1]: | ||
right -= 1 | ||
left += 1 | ||
right -= 1 | ||
return result |
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 @@ | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
a = [1] | ||
b = [1] | ||
res = [] | ||
for i in range(len(nums) - 1): | ||
a.append(a[i] * nums[i]) | ||
b.append(b[i] * nums[-i-1]) | ||
|
||
b.reverse() | ||
|
||
for i in range(len(nums)): | ||
res.append(a[i] * b[i]) | ||
|
||
return res | ||
|
||
|
||
class Solution2: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
out = [] | ||
p = 1 | ||
|
||
for i in range(0, len(nums)): | ||
out.append(p) | ||
p = p * nums[i] | ||
p = 1 | ||
|
||
for i in range(len(nums)-1, 0 - 1, -1): | ||
out[i] = out[i] * p | ||
p = p * nums[i] | ||
|
||
return out |
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,45 @@ | ||
# Stack | ||
class Solution: | ||
def trap(self, height: List[int]) -> int: | ||
stck = [] | ||
result = 0 | ||
|
||
for i in range(len(height)): | ||
while stck and height[i] > height[stck[-1]]: | ||
|
||
top = stck.pop() | ||
|
||
if not len(stck): | ||
break | ||
|
||
dist = i - stck[-1] - 1 | ||
water = min(height[i], height[stck[-1]]) - height[top] | ||
|
||
result += dist * water | ||
|
||
stck.append(i) | ||
|
||
return result | ||
|
||
# Two pointers | ||
class Solution2: | ||
def trap(self, height: List[int]) -> int: | ||
if not height: | ||
return 0 | ||
|
||
volume = 0 | ||
left, right = 0, len(height) - 1 | ||
left_max, right_max = height[left], height[right] | ||
|
||
|
||
while left < right: | ||
left_max, right_max = max(height[left], left_max), max(height[right], right_max) | ||
|
||
if left_max <= right_max: | ||
volume += left_max - height[left] | ||
left += 1 | ||
else: | ||
volume += right_max - height[right] | ||
right -= 1 | ||
return volume | ||
|
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,15 @@ | ||
# normal | ||
class Solution: | ||
def arrayPairSum(self, nums: List[int]) -> int: | ||
result = 0 | ||
nums.sort() | ||
|
||
for i in range(0, len(nums)-1, 2): | ||
result += nums[i] | ||
|
||
return result | ||
|
||
# Pythonic | ||
class Solution2: | ||
def arrayPairSum(self, nums: List[int]) -> int: | ||
return sum(sorted(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
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