Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode_with_Python #746

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion 121 Leetcode/121 leetcode.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
def maxProfit(self, prices: list[int]) -> int:
maxProfit = 0
currentMax = 0

Expand Down
2 changes: 1 addition & 1 deletion Python/Dynamic_Programming/CountMinSteps_Iterative.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
79 changes: 79 additions & 0 deletions Python/Leetcode_with_Python/Leetcode_#1_Two_sum.py
Original file line number Diff line number Diff line change
@@ -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))
2 changes: 1 addition & 1 deletion Python/Searching-Algorithms/binary_search.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down