0% found this document useful (0 votes)
6 views2 pages

Algorithmssssasasasasaas

The document contains implementations of four algorithms in Python: Two Sum, Climbing Stairs, Binary Search, and Search Insert Position. Each algorithm is accompanied by its time and space complexity analysis, along with test cases demonstrating its functionality. The solutions utilize efficient data structures and methods to achieve optimal performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Algorithmssssasasasasaas

The document contains implementations of four algorithms in Python: Two Sum, Climbing Stairs, Binary Search, and Search Insert Position. Each algorithm is accompanied by its time and space complexity analysis, along with test cases demonstrating its functionality. The solutions utilize efficient data structures and methods to achieve optimal performance.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#1 Two Sum

class Solution:
def twoSum(self, nums, target):
hash_map = {}
for i, num in enumerate(nums):
complement = target - num
if complement in hash_map:
return [hash_map[complement], i]
hash_map[num] = i
solution = Solution()
# Test cases
print(solution.twoSum([2, 7, 11, 15], 9))
print(solution.twoSum([3, 2, 4], 6))
print(solution.twoSum([3, 3], 6))
### Time complexity: O(n), Space complexity: O(n)

#2 Climbing Stairs
class Solution:
def climbStairs(self, n):
if n == 1:
return 1
if n == 2:
return 2
first, second = 1, 2
for i in range(3, n + 1):
current = first + second
first = second
second = current
return second
solution = Solution()
# Test cases
print(solution.climbStairs(2))
print(solution.climbStairs(3))
print(solution.climbStairs(4))
###Time complexity: O(n), Space complexity: O(1)

#3 Binary Search
class Solution:
def search(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2

if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
solution = Solution()
# Test cases
print(solution.search([-1, 0, 3, 5, 9, 12], 9))
print(solution.search([-1, 0, 3, 5, 9, 12], 2))
print(solution.search([1, 2, 3, 4, 5, 6], 4))
### Time complexity: O(log n), Space complexity: O(1)

#4 Search Insert Position


class Solution:
def searchInsert(self, nums, target):
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2

if nums[mid] == target:
return mid
elif nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left

solution = Solution()
# Test cases
print(solution.searchInsert([1, 3, 5, 6], 5))
print(solution.searchInsert([1, 3, 5, 6], 2))
print(solution.searchInsert([1, 3, 5, 6], 7))
print(solution.searchInsert([1, 3, 5, 6], 0))

### Time complexity: O(log n), Space complexity: O(1)

You might also like