Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fceeba

Browse files
committed
977_Squares_of_a_Sorted_Array
1 parent 8e21346 commit 3fceeba

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python & JAVA Solutions for Leetcode (inspired by [haoel's leetcode](https://github.com/haoel/leetcode))
22

3-
Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to [jwasham's coding-interview-university](https://github.com/jwasham/coding-interview-university).
3+
Remember solutions are only solutions to given problems. If you want full study checklist for code & whiteboard interview, please turn to [jwasham's coding-interview-university](https://github.com/jwasham/coding-interview-university).
44

55
Also, there are open source implementations for basic data structs and algorithms, such as [Algorithms in Python](https://github.com/TheAlgorithms/Python) and [Algorithms in Java](https://github.com/TheAlgorithms/Java).
66

@@ -65,9 +65,9 @@ Also, there are open source implementations for basic data structs and algorithm
6565
| 153 | [Find Minimum in Rotated Sorted Array](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/153_Find_Minimum_in_Rotated_Sorted_Array.py) | Binary search with conditions, A[l] > A[r] |
6666
| 154 | [Find Minimum in Rotated Sorted Array II](https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/154_Find_Minimum_in_Rotated_Sorted_Array_II.py) | Binary search with conditions, A[l] > A[r], A[l]=A[mid]=A[r] |
6767
| 155 | [Min Stack](https://leetcode.com/problems/min-stack/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/155_Min_Stack.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/155_Min_Stack.java) | Add another stack for min stack, maintance this stack when the main stack pop or push: 1. Only push min, such that len(minStack)<=len(Stack) 2. Push min again when current top is min, such that len(minStack)=len(Stack) |
68-
| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/156_Binary_Tree_Upside_Down.py) | p.left = parent.right, parent.right = p.right, p.right = parent, parent = p.left, p = left |
68+
| 156 | [Binary Tree Upside Down](https://leetcode.com/problems/binary-tree-upside-down/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/156_Binary_Tree_Upside_Down.py) | p.left = parent.right, parent.right = p.right, p.right = parent, parent = p.left, p = left |
6969
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/157_Read_N_Characters_Given_Read4.py) | Handle the edge case (the end) |
70-
| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/158_Read_N_Characters_Given_Read4_II_Call_multiple_times.py) | Store the pos and offset that is read by last read4 |
70+
| 158 | [Read N Characters Given Read4 II - Call multiple times](https://leetcode.com/problems/read-n-characters-given-read4-ii-call-multiple-times/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/158_Read_N_Characters_Given_Read4_II_Call_multiple_times.py) | Store the pos and offset that is read by last read4 |
7171
| 159 | [Longest Substring with At Most Two Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-two-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/159_Longest_Substring_with_At_Most_Two_Distinct_Characters.py) | Maintain a sliding window that always satisfies such condition |
7272
| 161 | [One Edit Distance](https://leetcode.com/problems/one-edit-distance/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/161_One_Edit_Distance.py) | 1. Check the different position and conditions<br>2. [Edit distance](https://leetcode.com/problems/edit-distance/)|
7373
| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/163_Missing_Ranges.py) | Add -1 to lower for special case, then check if curr - prev >= 2|
@@ -207,7 +207,8 @@ Also, there are open source implementations for basic data structs and algorithm
207207
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
208208
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
209209
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br>2. Binary Search for candicates, O(nlogn) and O(n) |
210-
| 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) |
210+
| 977 | [Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/977_Squares_of_a_Sorted_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/977_Squares_of_a_Sorted_Array.java) | 1. Sort, O(nlogn) and O(n)<br>2. Two point, O(n) and O(n) |
211+
| 1064 | [Fixed Point](https://leetcode.com/problems/fixed-point/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/1064_Fixed_Point.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/1064_Fixed_Point.java) | 1. Go through index and value, until find solution encounter index < value, O(n) and O(1)<br>2. Binary search, O(logn) and O(1) |
211212

212213
| # | To Understand |
213214
|---| ----- |
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
/* public int[] sortedSquares(int[] A) {
3+
int[] res = new int[A.length];
4+
for (int i = 0; i < A.length; ++i)
5+
res[i] = A[i] * A[i];
6+
7+
Arrays.sort(res);
8+
return res;
9+
} */
10+
public int[] sortedSquares(int[] A) {
11+
int pos = 0;
12+
int[] res = new int[A.length];
13+
int curr = 0;
14+
while (pos < A.length && A[pos] < 0) pos++;
15+
int npos = pos - 1;
16+
while (pos < A.length && npos >= 0) {
17+
if (A[pos] * A[pos] < A[npos] * A[npos]) {
18+
res[curr++] = A[pos] * A[pos];
19+
pos++;
20+
} else {
21+
res[curr++] = A[npos] * A[npos];
22+
npos--;
23+
}
24+
}
25+
while (npos >= 0) {
26+
res[curr++] = A[npos] * A[npos];
27+
npos--;
28+
}
29+
while (pos < A.length) {
30+
res[curr++] = A[pos] * A[pos];
31+
pos++;
32+
}
33+
return res;
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution(object):
2+
# def sortedSquares(self, A):
3+
# """
4+
# :type A: List[int]
5+
# :rtype: List[int]
6+
# """
7+
# # Directly sort
8+
# return sorted(x * x for x in A)
9+
10+
def sortedSquares(self, A):
11+
pos = 0
12+
while pos < len(A) and A[pos] < 0:
13+
pos += 1
14+
# pos point to first positve
15+
# npos point to larget negative
16+
npos = pos - 1
17+
res = []
18+
while pos < len(A) and npos >= 0:
19+
if A[npos] ** 2 < A[pos] ** 2:
20+
res.append(A[npos] ** 2)
21+
npos -= 1
22+
else:
23+
res.append(A[pos] ** 2)
24+
pos +=1
25+
while npos >= 0:
26+
res.append(A[npos] ** 2)
27+
npos -= 1
28+
while pos < len(A):
29+
res.append(A[pos] ** 2)
30+
pos += 1
31+
return res

0 commit comments

Comments
 (0)