Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Challenge-3/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def maxProfit(self, prices):
"""
:type prices: List[int]
:rtype: int
"""
left = 0
right = 1
maxprofit = 0
while right < len(prices):
if prices[right] > prices[left]:
maxprofit = max(prices[right] - prices[left], maxprofit)
else:
left = right
right += 1
return maxprofit
12 changes: 12 additions & 0 deletions Challenge-4/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
dic = {}
for i, n in enumerate(nums):
if n in dic:
return True
dic[n] = i
return False
14 changes: 14 additions & 0 deletions Challenge-5/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution(object):
def getSum(self, a, b):
"""
:type a: int
:type b: int
:rtype: int
"""
carry = 0
mask = 0xffffffff
while b & mask != 0:
carry = (a & b) << 1
a = a ^ b
b = carry
return a & mask if b > mask else a
16 changes: 16 additions & 0 deletions Challenge-6/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def productExceptSelf(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
res = [1] * len(nums)
prefix = 1
for i in range(1, len(nums)):
prefix = prefix * nums[i-1]
res[i] = prefix
suffix = 1
for j in range(len(nums) - 2, -1, -1):
suffix = suffix * nums[j+1]
res[j] = res[j] * suffix
return res
12 changes: 12 additions & 0 deletions Challenge-7/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution(object):
def maxSubArray(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
maxsum = nums[0]
cursum = nums[0]
for i in range(1, len(nums)):
cursum = max(cursum + nums[i], nums[i])
maxsum = max(maxsum, cursum)
return maxsum
15 changes: 15 additions & 0 deletions Challenge-8/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution(object):
def maxProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = nums[0]
maxsum = 1
minsum = 1
for i in nums:
curpro = maxsum * i
maxsum = max(curpro, minsum * i, i)
minsum = min(curpro, minsum * i, i)
res = max(res, maxsum)
return res
16 changes: 16 additions & 0 deletions Challenge-9/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
lo, hi = 0, len(nums) - 1
while lo < hi:
if nums[lo] < nums[hi]:
return nums[lo]

mid = (lo + hi) // 2
if nums[mid] > nums[hi]:
lo = mid + 1
else:
hi = mid
2 changes: 2 additions & 0 deletions challenge-1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name : Rashad M
Email : rashadbasheer.m@gmail.com
13 changes: 13 additions & 0 deletions challenge-2/two-sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def two_sum(nums, target):
"""
:type nums: List[int]
:type k: int
:rtype: float
"""
num_dict = {}
n = len(nums)
for i in range(n):
dif = target - nums[i]
if dif in num_dict:
return [num_dict[dif], i]
num_dict[nums[i]] = i