diff --git a/Challenge-1/two-sum.py b/Challenge-1/two-sum.py new file mode 100644 index 0000000..47f8d35 --- /dev/null +++ b/Challenge-1/two-sum.py @@ -0,0 +1,15 @@ +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hash_map={} + + for index in range(len(nums)): + if nums[index] in hash_map: + return [hash_map[nums[index]],index] + else: + hash_map[target-nums[index]]=index + diff --git a/Challenge-2/Best-Time-to-Buy-and-Sell-Stock.py b/Challenge-2/Best-Time-to-Buy-and-Sell-Stock.py new file mode 100644 index 0000000..8161211 --- /dev/null +++ b/Challenge-2/Best-Time-to-Buy-and-Sell-Stock.py @@ -0,0 +1,16 @@ +class Solution(object): + def maxProfit(self, prices): + """ + :type prices: List[int] + :rtype: int + """ + max_profit=0 + minimum=prices[0] + for i in range(1,len(prices)): + if prices[i]>minimum: + max_profit=max(max_profit,prices[i]-minimum) + else: + minimum=prices[i] + return max_profit + + diff --git a/challenge-1/two-sum.py b/challenge-1/two-sum.py new file mode 100644 index 0000000..47f8d35 --- /dev/null +++ b/challenge-1/two-sum.py @@ -0,0 +1,15 @@ +class Solution(object): + def twoSum(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: List[int] + """ + hash_map={} + + for index in range(len(nums)): + if nums[index] in hash_map: + return [hash_map[nums[index]],index] + else: + hash_map[target-nums[index]]=index +