Skip to content

Commit 2180589

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent fbfdd14 commit 2180589

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

problems/squares-of-a-sorted-array.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
If all the numbers in the array is larger or equal than 0, all the square value will automatically be sorted.
3+
4+
If there are negatives in the array, we need to seperate the negatives and the others.
5+
To do that, we need to find the index of 0 or index of first positive element. We call the index `m`.
6+
So for the non-negative part, it is sorted already: `numbers[m:]`.
7+
The negative part sorted: `[-1*n for n in reversed(numbers[:m])]`
8+
9+
And now we apply the merge method in the merge sort.
10+
The method takes two sorted array and turn it into one.
11+
If you are not familiar it take a look at [this](https://leetcode.com/problems/merge-sorted-array/discuss/208832/Python-O(M%2BN)-Solution-Explained).
12+
After we got the sorted array, we compute all the squares and return.
13+
14+
The time complexity is O(N).
15+
The space complexity is O(N).
16+
"""
17+
class Solution(object):
18+
def sortedSquares(self, numbers):
19+
if not numbers: return numbers
20+
21+
if numbers[0]>=0:
22+
return [n**2 for n in numbers]
23+
24+
m = 0
25+
for i, n in enumerate(numbers):
26+
if n>=0:
27+
m = i
28+
break
29+
30+
A, B = numbers[m:], [-1*n for n in reversed(numbers[:m])]
31+
return [n**2 for n in self.merge(A, B)]
32+
33+
def merge(self, A, B):
34+
a = b = 0
35+
opt = []
36+
while a<len(A) and b<len(B):
37+
if A[a]<B[b]:
38+
opt.append(A[a])
39+
a+=1
40+
else:
41+
opt.append(B[b])
42+
b+=1
43+
if a<len(A): opt.extend(A[a:])
44+
if b<len(B): opt.extend(B[b:])
45+
return opt
46+
47+
"""
48+
How to approach problem like this?
49+
50+
**Mindset 1, think of the time complexity of your brute force.**
51+
In this case, simply square all the element and sort them.
52+
That is O(NLogN).
53+
So we are going to try to reduce it into O(N) or O(LogN) or O(1).
54+
55+
**Mindset 2, think of the time complexity that you are not able to reduce.**
56+
For example, I could not think of a way to square all the sorted element without O(N).
57+
So all other operation less or equal to O(N) does not effect the time complexity.
58+
When I am trying to find `m`, I can use binary search to search for it, the time complexity is O(LogN).
59+
But O(N) is fine, so I use this simpler approach.
60+
Sure, we can optimize it and make it faster. But it is not the bottle neck here.
61+
**I think what the interviwer wanted to see is you realizing the bottle neck and solve it.**
62+
"""

problems/two-sum-ii-input-array-is-sorted.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
class Solution(object):
2-
def twoSum(self, numbers, target):
3-
memo = {}
4-
for i, n in enumerate(numbers):
5-
if target-n in memo:
6-
return [memo[target-n]+1, i+1]
7-
memo[n] = i
8-
91
"""
102
We put `l` and `r` at the beginning and at the end of the array.
113
Everytime with new pair of `l` and `r`, we check if its sum is target. If true, return the answer.
@@ -26,3 +18,16 @@ def twoSum(self, numbers, target):
2618
else:
2719
l = l+1
2820
return []
21+
22+
"""
23+
We use a hashmap to keep track of all the number we went through.
24+
Since we exactly know what we are looking for (`target-n`), we can check if the counter part is in the hashmap or not.
25+
If true, return the answer.
26+
"""
27+
class Solution(object):
28+
def twoSum(self, numbers, target):
29+
memo = {}
30+
for i, n in enumerate(numbers):
31+
if target-n in memo:
32+
return [memo[target-n]+1, i+1]
33+
memo[n] = i

0 commit comments

Comments
 (0)