Skip to content

Commit

Permalink
New Problem C++ Solution - "Squares of a Sorted Array"
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Mar 26, 2019
1 parent 646a155 commit 2ed6107
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LeetCode
|981|[Time Based Key-Value Store](https://leetcode.com/problems/time-based-key-value-store/) | [C++](./algorithms/cpp/timeBasedKeyValueStore/TimeBasedKeyValueStore.cpp)|Medium|
|980|[Unique Paths III](https://leetcode.com/problems/unique-paths-iii/) | [C++](./algorithms/cpp/uniquePaths/UniquePaths.III.cpp)|Hard|
|978|[Longest Turbulent Subarray](https://leetcode.com/problems/longest-turbulent-subarray/) | [C++](./algorithms/cpp/longestTurbulentSubarray/LongestTurbulentSubarray.cpp),[Python](./algorithms/python/LongestTurbulentSubarray/maxTurbulenceSize.py)|Medium|
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [Python](./algorithms/python/SquaresOfSortedArray/sortedSquares.py)|Easy|
|977|[Squares of a Sorted Array](https://leetcode.com/problems/squares-of-a-sorted-array/) | [C++](./algorithms/cpp/squaresOfASortedArray/SquaresOfASortedArray.cpp), [Python](./algorithms/python/SquaresOfSortedArray/sortedSquares.py)|Easy|
|976|[Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) | [Python](./algorithms/python/LargestPerimeterTriangle/largestPerimeter.py)|Easy|
|971|[Flip Binary Tree To Match Preorder Traversal](https://leetcode.com/problems/flip-binary-tree-to-match-preorder-traversal/) | [Python](./algorithms/python/FlipBinaryTreeToMatchPreorderTraversal/flipMatchVoyage.py)|Medium|
|969|[Pancake Sorting](https://leetcode.com/problems/pancake-sorting/) | [Python](./algorithms/python/PancakeSorting/pancakeSort.py)|Medium|
Expand Down
65 changes: 65 additions & 0 deletions algorithms/cpp/squaresOfASortedArray/SquaresOfASortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Source : https://leetcode.com/problems/squares-of-a-sorted-array/
// Author : Hao Chen
// Date : 2019-03-26

/*****************************************************************************************************
*
* Given an array of integers A sorted in non-decreasing order, return an array of the squares of each
* number, also in sorted non-decreasing order.
*
* Example 1:
*
* Input: [-4,-1,0,3,10]
* Output: [0,1,9,16,100]
*
* Example 2:
*
* Input: [-7,-3,2,3,11]
* Output: [4,9,9,49,121]
*
* Note:
*
* 1 <= A.length <= 10000
* -10000 <= A[i] <= 10000
* A is sorted in non-decreasing order.
*
******************************************************************************************************/

class Solution {
public:
vector<int> sortedSquares(vector<int>& A) {
// find the place, negative numbers are right, positive number are right.
// two pointer, one goes left, another goes right.

//using binary search algorithm
const int len = A.size();
int low = 0, high = len- 1;
int mid =0;
while (low <= high) {
mid = low + (high - low)/2;
if (A[mid] >= 0 ) high = mid - 1;
if (A[mid] < 0 ) low = mid + 1;
}

//TRICKY: make sure A[mid] <= 0 or A[mid] is A[0]
if (A[mid] > 0 && mid > 0 ) mid--;
//cout << mid << " - "<< A[mid]<< endl;

vector<int> result;
low = mid; high = mid+1;
while ( low >=0 && high < len ) {
if ( abs(A[low]) < abs(A[high]) ) {
result.push_back(A[low] * A[low]);
low --;
}else {
result.push_back(A[high] * A[high]);
high++;
}
}

for (;low >= 0; low--) result.push_back(A[low] * A[low]);
for (;high<len; high++) result.push_back(A[high] * A[high] );

return result;
}
};

0 comments on commit 2ed6107

Please sign in to comment.