diff --git a/121 Leetcode/121 leetcode.py b/121 Leetcode/121 leetcode.py index ea6114e9..cccd16b8 100644 --- a/121 Leetcode/121 leetcode.py +++ b/121 Leetcode/121 leetcode.py @@ -1,5 +1,5 @@ class Solution: - def maxProfit(self, prices: List[int]) -> int: + def maxProfit(self, prices: list[int]) -> int: maxProfit = 0 currentMax = 0 diff --git a/Python/Dynamic_Programming/CountMinSteps_Iterative.py b/Python/Dynamic_Programming/CountMinSteps_Iterative.py index 30b35469..ed540552 100644 --- a/Python/Dynamic_Programming/CountMinSteps_Iterative.py +++ b/Python/Dynamic_Programming/CountMinSteps_Iterative.py @@ -5,7 +5,7 @@ # 2.) If n is divisible by 2, divide by 2.( if n % 2 == 0, then n = n / 2 ) , # 3.) If n is divisible by 3, divide by 3. (if n % 3 == 0, then n = n / 3 ). -# Now solve this iteratively : +# Now solve this iteratively from sys import stdin from sys import maxsize as MAX_VALUE diff --git a/Python/Leetcode_with_Python/Leetcode_#1_Two_sum.py b/Python/Leetcode_with_Python/Leetcode_#1_Two_sum.py new file mode 100644 index 00000000..5fbda3bd --- /dev/null +++ b/Python/Leetcode_with_Python/Leetcode_#1_Two_sum.py @@ -0,0 +1,79 @@ +# Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + +# You may assume that each input would have exactly one solution, and you may not use the same element twice. + +# You can return the answer in any order. + +# Example 1: + +# Input: nums = [2,7,11,15], target = 9 +# Output: [0,1] +# Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. + + +class Solution: + def twoSum(self, num:list[int], target: int)-> list[int]: + for i in range(len(num)): + for j in range(i+1, len(num)): + if num[i] + num[j] == target: + return [i, j] + +# general_solution = Solution() +# print(general_solution.twoSum([2,7,1,11], 9)) + +# This code taking way too much runtime because of O(n^2) + +# We will try another approch which will be Hashmap + + +class Solution2: + def Two_Sum(self, num: list[int], target:int)->list[int]: + saw = {} + + for i, num in enumerate(num): + if target - num in saw: + return [saw[target-num], i] + elif num not in saw: + saw[num] = i + + +# hashmap = Solution2() +# print(hashmap.Two_Sum([2,1,11,23,7], 9)) + + +# Used binary search Algorithm + + + + +class Solution3: + def Twosum(self, num: list[int], target: int) -> list[int]: + # Create a list of tuples (original number, index) + num_with_indices = [(num[i], i) for i in range(len(num))] + print(num_with_indices) + # Sort the list of tuples based on the numbers + num_with_indices.sort() + # num_with_indices.sort() + + # print(num_with_indices) + low = 0 + high = len(num) - 1 + + while low <= high: + current_sum = num_with_indices[low][0] + num_with_indices[high][0] + + if current_sum < target: + low += 1 + elif current_sum > target: + high -= 1 + else: + return [num_with_indices[low][1], num_with_indices[high][1]] + + return [-1, -1] # Return [-1, -1] if no solution is found + + + + + +hashmap = Solution3() +print(hashmap.Twosum([2, 3, 23, 7], 9)) \ No newline at end of file diff --git a/Python/Searching-Algorithms/binary_search.py b/Python/Searching-Algorithms/binary_search.py index ea000683..b52f4de2 100644 --- a/Python/Searching-Algorithms/binary_search.py +++ b/Python/Searching-Algorithms/binary_search.py @@ -1,5 +1,5 @@ class Solution: - def search(self, nums: List[int], target: int) -> int: + def search(self, nums: list[int], target: int) -> int: low = 0 high = len(nums) - 1 mid = 0