-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path013-TwoSum-II-Input-Array-Is-Sorted.py
50 lines (39 loc) · 1.74 KB
/
013-TwoSum-II-Input-Array-Is-Sorted.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Link: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description/
#Brute Force solution:
def twoSumII(nums, target):
n = len(nums)
for i in range(n):
for j in range(i, n):
if nums[i] + nums[j] == target: # Time Complexity: O(n^2)
return [i+1, j+1] # Space Complexity: O(1)
nums1 = [2,7,11,15]
target = 9
print(twoSumII(nums1,target))
# -------------------------------------------------------------------------------------------------------------------------
# Better Solution:
def twoSum2(nums, target):
num_map = {}
for i, n in enumerate(nums):
x = target - n
if x in num_map:
return [num_map[x], i+1]
num_map[n] = i+1 # Time Complexity: O(n)
return[] # Space Complexity: O(n)
nums1 = [2,7,11,15]
target = 9
print(twoSum2(nums1,target))
# -------------------------------------------------------------------------------------------------------------------------
def twoSum3(nums,target):
l = 0
r = len(nums)-1
while l < r:
res = nums[l] + nums[r]
if res == target:
return [l+1,r+1]
if res > target:
r -= 1 # Time Complexity: O(n)
else: # Space Complexity: O(1)
l += 1
nums1 = [1,2,3,5,7,11,15]
target = 8
print(twoSum3(nums1,target))