diff --git a/README.md b/README.md index 3fa9fa245..2efbbc100 100644 --- a/README.md +++ b/README.md @@ -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| diff --git a/algorithms/cpp/squaresOfASortedArray/SquaresOfASortedArray.cpp b/algorithms/cpp/squaresOfASortedArray/SquaresOfASortedArray.cpp new file mode 100644 index 000000000..ec9edf742 --- /dev/null +++ b/algorithms/cpp/squaresOfASortedArray/SquaresOfASortedArray.cpp @@ -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 sortedSquares(vector& 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 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