-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdailycodingproblem
133 lines (101 loc) · 4.55 KB
/
dailycodingproblem
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#####################################################################################################################################
Problem 1
#####################################################################################################################################
Given a list of numbers and a number k, return whether any two numbers from the list add up to k.
For example, given [10, 15, 3, 7] and k of 17, return true since 10 + 7 is 17.
Bonus: Can you do this in one pass?
brut force solution: O(N^2)
def two_sum(lst, k):
for i in range(len(lst)):
for j in range(len(lst)):
if i != j and lst[i] + lst[j] == k:
return True
return False
OR
Another way is to use a set to remember the numbers we've seen so far.
Then for a given number, we can check if there is another number that, if added, would sum to k.
This would be O(N) since lookups of sets are O(1) each.
def two_sum(lst, k):
seen = set()
for num in lst:
if k - num in seen:
return True
seen.add(num)
return False
OR
Binary search solution
from bisect import bisect_left
def two_sum(lst, K):
lst.sort()
for i in range(len(lst)):
target = K - lst[i]
j = binary_search(lst, target)
# Check that binary search found the target and that it's not in the same index
# as i. If it is in the same index, we can check lst[i + 1] and lst[i - 1] to see
# if there's another number that's the same value as lst[i].
if j == -1:
continue
elif j != i:
return True
elif j + 1 < len(lst) and lst[j + 1] == target:
return True
elif j - 1 >= 0 and lst[j - 1] == target:
return True
return False
def binary_search(lst, target):
lo = 0
hi = len(lst)
ind = bisect_left(lst, target, lo, hi)
if 0 <= ind < hi and lst[ind] == target:
return ind
return -1
###################################################################################################################################
#problem 2
###################################################################################################################################
This problem was asked by Uber.
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
Follow-up: what if you can't use division?
This problem would be easy with division: an optimal solution could just find the product of all numbers in the array and then divide by each of the numbers.
Without division, another approach would be to first see that the ith element simply needs the product of numbers before i and the product of numbers after i. Then we could multiply those two numbers to get our desired product.
In order to find the product of numbers before i, we can generate a list of prefix products. Specifically, the ith element in the list would be a product of all numbers including i. Similarly, we would generate the list of suffix products.
#This runs in O(N) time and space, since iterating over the input arrays takes O(N) time and creating the prefix and suffix arrays take up O(N) space.
#Solution by division:
def products(lst):
full_multiple = 1
for x in lst:
full_multiple *= x
result = []
for num in lst:
result.append(int(full_multiple/num))
return result
OR
#solution by prefix and suffix multiplication
def products(nums):
# Generate prefix products
prefix_products = []
for num in nums:
if prefix_products:
prefix_products.append(prefix_products[-1] * num)
else:
prefix_products.append(num)
# Generate suffix products
suffix_products = []
for num in reversed(nums):
if suffix_products:
suffix_products.append(suffix_products[-1] * num)
else:
suffix_products.append(num)
suffix_products = list(reversed(suffix_products))
# Generate result
result = []
for i in range(len(nums)):
if i == 0:
result.append(suffix_products[i + 1])
elif i == len(nums) - 1:
result.append(prefix_products[i - 1])
else:
result.append(prefix_products[i - 1] * suffix_products[i + 1])
return result
#input : product([1,2,3,4,5)
#output: [120, 60, 40, 30, 24]