diff --git a/1-Two-Sum/README.md b/1-Two-Sum/README.md new file mode 100644 index 0000000..4896c62 --- /dev/null +++ b/1-Two-Sum/README.md @@ -0,0 +1,4 @@ + +Name: Rifana E +Email: rifaaaah88@gmail.com + diff --git a/1-Two-Sum/solution.py b/1-Two-Sum/solution.py new file mode 100644 index 0000000..d88ba6b --- /dev/null +++ b/1-Two-Sum/solution.py @@ -0,0 +1,10 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + num_dict = {} + for i, first_num in enumerate(nums): + second_num =target - first_num + if second_num in num_dict: + return [i, nums.index(second_num)] + else: + num_dict[first_num] = i + return -1 diff --git a/Best-Time-To-Buy-and-Sell-Stock/solution.py b/Best-Time-To-Buy-and-Sell-Stock/solution.py new file mode 100644 index 0000000..e9ba1e4 --- /dev/null +++ b/Best-Time-To-Buy-and-Sell-Stock/solution.py @@ -0,0 +1,13 @@ +class Solution: + def maxProfit(self, prices: List[int]) -> int: + left, right = 0, 1 + profit = 0 + while right < len(prices): + if prices[right] > prices[left]: + profit = max(profit, prices[right] - prices[left]) + else: + left = right + + right+=1 + + return profit diff --git a/Contains-Duplicate/solution.py b/Contains-Duplicate/solution.py new file mode 100644 index 0000000..e008691 --- /dev/null +++ b/Contains-Duplicate/solution.py @@ -0,0 +1,9 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + hash_set = set() + for num in nums: + if num not in hash_set: + hash_set.add(num) + else: + return True + return False diff --git a/Find-Minimum-in-Rotated-Sorted-Array/solution.py b/Find-Minimum-in-Rotated-Sorted-Array/solution.py new file mode 100644 index 0000000..c950d6b --- /dev/null +++ b/Find-Minimum-in-Rotated-Sorted-Array/solution.py @@ -0,0 +1,20 @@ +class Solution: + def findMin(self, nums: List[int]) -> int: + res = nums[0] + + l = 0 + r =len(nums) - 1 + + while l <= r: + if nums[l] < nums[r]: + res = min(res, nums[l]) + break + + m = (l+r)//2 + res = min(res, nums[m]) + + if nums[l] <= nums[m]: + l = m + 1 + else: + r = m - 1 + return res diff --git a/Maximum-Product-Subarray/solution.py b/Maximum-Product-Subarray/solution.py new file mode 100644 index 0000000..ad91add --- /dev/null +++ b/Maximum-Product-Subarray/solution.py @@ -0,0 +1,11 @@ +class Solution: + def maxProduct(self, nums: List[int]) -> int: + max_product = nums[0] + calc_max = 1 + calc_min = 1 + for n in nums: + temp = calc_max * n + calc_max = max(temp, calc_min*n, n) + calc_min = min(temp, calc_min*n, n) + max_product = max(max_product, calc_max) + return max_product diff --git a/Maximum-Subarray/solution.py b/Maximum-Subarray/solution.py new file mode 100644 index 0000000..e6e8804 --- /dev/null +++ b/Maximum-Subarray/solution.py @@ -0,0 +1,12 @@ +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_sum = nums[0] + calc_sum = 0 + + for n in nums: + if calc_sum < 0: + calc_sum = 0 + + calc_sum += n + max_sum = max(max_sum, calc_sum) + return max_sum diff --git a/Product-Of-Array-Except-Self/solution.py b/Product-Of-Array-Except-Self/solution.py new file mode 100644 index 0000000..685cdc1 --- /dev/null +++ b/Product-Of-Array-Except-Self/solution.py @@ -0,0 +1,20 @@ +import math + +class Solution: + def productExceptSelf(self, nums: List[int]) -> List[int]: + n = len(nums) + answer = [1] * n + + prefix = 1 + for i in range(n): + answer[i] = prefix + prefix *= nums[i] + + suffix = 1 + for i in range(n - 1, -1, -1): + answer[i] *= suffix + suffix *= nums[i] + + return answer + + diff --git a/Search-in-Rotated-Sorted-Array/solution.py b/Search-in-Rotated-Sorted-Array/solution.py new file mode 100644 index 0000000..3fd0337 --- /dev/null +++ b/Search-in-Rotated-Sorted-Array/solution.py @@ -0,0 +1,22 @@ +class Solution: + def search(self, nums: List[int], target: int) -> int: + + l = 0 + r =len(nums) - 1 + + while l <= r: + mid = (l+r)//2 + if nums[mid] == target: + return mid + if nums[l] <= nums[mid]: + if nums[l] <= target < nums[mid]: + r = mid -1 + else: + l = mid+1 + else: + if nums[mid]< target <= nums[r]: + l = mid+1 + else: + r = mid-1 + + return -1 diff --git a/Sum-of-Two-Integers/solution.py b/Sum-of-Two-Integers/solution.py new file mode 100644 index 0000000..b6301c4 --- /dev/null +++ b/Sum-of-Two-Integers/solution.py @@ -0,0 +1,11 @@ +# while b != 0: +# sum = a ^ b +# carry = (a & b) << 1 +# a = sum +# b = carry +# return a + +class Solution: + def getSum(self, a: int, b: int) -> int: + sum_int = add(a, b) + return sum_int