diff --git a/challenge-1/two_sum.py b/challenge-1/two_sum.py new file mode 100644 index 0000000..734c948 --- /dev/null +++ b/challenge-1/two_sum.py @@ -0,0 +1,13 @@ +class Two_sum: + def twosum(self,target,nums): + prev_map=dict() + for i,num in enumerate(nums): + if target - num in prev_map: + return[i,prev_map[target-num]] + prev_map[num] = i + +two_sum = Two_sum() +print(two_sum.twosum(9,[2,7,11,15,9])) + + + \ No newline at end of file diff --git a/challenge-2/Best_time_to_buyandsell_stock.py b/challenge-2/Best_time_to_buyandsell_stock.py new file mode 100644 index 0000000..e7dbe64 --- /dev/null +++ b/challenge-2/Best_time_to_buyandsell_stock.py @@ -0,0 +1,29 @@ +""" +Best Time to Buy and Sell Stock + +You are given an array prices where prices[i] is the price of a given stock on the ith day. + +You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. + +Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. +""" + + +class Solution: + def maxProfit(self, prices): + #Two-pointer + left,right=0,1 + profit=0 + + while right < len(prices): + #profitable + if prices[right] > prices[left]: + profit=max(profit,prices[right] - prices[left]) + else: + #nonprofitable + left=right + right += 1 + return profit + +stock= Solution() +print(stock.maxProfit([7,1,5,3,6,4])) diff --git a/challenge-3/Contains_Duplicate.py b/challenge-3/Contains_Duplicate.py new file mode 100644 index 0000000..636fed8 --- /dev/null +++ b/challenge-3/Contains_Duplicate.py @@ -0,0 +1,29 @@ +""" +Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. +Example 1: + +Input: nums = [1,2,3,1] + +Output: true + +Explanation: + +The element 1 occurs at the indices 0 and 3. +""" + +class Solution: + def containsDuplicate(self, nums): + hash_set=set() + for num in nums: + if num in hash_set: + return True + else: + hash_set.add(num) + return False + +duplicate = Solution() +print(duplicate.containsDuplicate([1,2,3,1])) +print(duplicate.containsDuplicate([1,2,3,4])) + + + diff --git a/challenge-4/Sum_of_Two_Integers.py b/challenge-4/Sum_of_Two_Integers.py new file mode 100644 index 0000000..cc8b750 --- /dev/null +++ b/challenge-4/Sum_of_Two_Integers.py @@ -0,0 +1,18 @@ +# Given two integers a and b, return the sum of the two integers without using the operators + and -. + +# Example 1: +# Input: a = 1, b = 2 +# Output: 3 + +class Solution: + def getSum(self, a, b) : + bitshortner = 0xFFFFFFFF + + while (b & bitshortner) > 0: + carry = (a & b) << 1 + a = a ^ b + b = carry + return (a & bitshortner) if b > 0 else a + +sum = Solution() +print(sum.getSum(1,2)) diff --git a/challenge-5/Product_of_Array_Except_Self.py b/challenge-5/Product_of_Array_Except_Self.py new file mode 100644 index 0000000..a1f403f --- /dev/null +++ b/challenge-5/Product_of_Array_Except_Self.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/product-of-array-except-self/ + +# Product of Array Except Self + + +class Solution: + def productExceptSelf(self, nums): + res=[1] * (len(nums)) + prefix=1 + for i in range(len(nums)): + res[i] = prefix + prefix *= nums[i] + postfix=1 + for i in range(len(nums)-1, -1, -1): + res[i] *= postfix + postfix *= nums[i] + return res + + +product = Solution() +print(product.productExceptSelf([1,2,3,4]))