diff --git a/OJ notes/pages/Leetcode Rotate-Array.md b/OJ notes/pages/Leetcode Rotate-Array.md new file mode 100644 index 0000000..7a9ecfc --- /dev/null +++ b/OJ notes/pages/Leetcode Rotate-Array.md @@ -0,0 +1,63 @@ +# Leetcode Rotate-Array + +#### 2022-07-10 08:54 + +> ##### Algorithms: +> #algorithm #array_in_place_operation +> ##### Data structures: +> #DS #array +> ##### Difficulty: +> #coding_problem #difficulty-medium +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#array_in_place_operation +``` + + + +##### Links: +- [Link to problem](https://leetcode.com/problems/rotate-array/) +___ +### Problem + +Given an array, rotate the array to the right by `k` steps, where `k` is non-negative. + +#### Examples + +**Example 1:** + +**Input:** nums = [1,2,3,4,5,6,7], k = 3 +**Output:** [5,6,7,1,2,3,4] +**Explanation:** +rotate 1 steps to the right: [7,1,2,3,4,5,6] +rotate 2 steps to the right: [6,7,1,2,3,4,5] +rotate 3 steps to the right: [5,6,7,1,2,3,4] + +**Example 2:** + +**Input:** nums = [-1,-100,3,99], k = 2 +**Output:** [3,99,-1,-100] +**Explanation:** +rotate 1 steps to the right: [99,-1,-100,3] +rotate 2 steps to the right: [3,99,-1,-100] + +#### Constraints + +- `1 <= nums.length <= 105` +- `-231 <= nums[i] <= 231 - 1` +- `0 <= k <= 105` + +### Thoughts + +#### Method 1: Array In-place operation + +#### Method 2: Copy + +#### Method 3: CPP's STL + +### Solution diff --git a/OJ notes/pages/Leetcode Squares-of-a-Sorted-Array.md b/OJ notes/pages/Leetcode Squares-of-a-Sorted-Array.md new file mode 100644 index 0000000..f4ad75f --- /dev/null +++ b/OJ notes/pages/Leetcode Squares-of-a-Sorted-Array.md @@ -0,0 +1,98 @@ +# Leetcode Squares-of-a-Sorted-Array + +#### 2022-07-10 08:50 + +> ##### Algorithms: +> #algorithm #two_pointers +> ##### Data structures: +> #DS #array +> ##### Difficulty: +> #coding_problem #difficulty-easy +> ##### Additional tags: +> #leetcode +> ##### Revisions: +> N/A + +##### Related topics: +```expander +tag:#two_pointers +``` + +- [[Leetcode Intersection-of-Two-Arrays-II]] +- [[Leetcode Merge-Sorted-Array]] +- [[Leetcode Merge-Two-Sorted-Lists]] +- [[Two pointers approach]] + + +##### Links: +- [Link to problem](https://leetcode.com/problems/squares-of-a-sorted-array/) +___ +### Problem + +Given an integer array `nums` sorted in **non-decreasing** order, return _an array of **the squares of each number** sorted in non-decreasing order_. + +Follow up: Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? + +#### Examples + +**Example 1:** + +**Input:** nums = [-4,-1,0,3,10] +**Output:** [0,1,9,16,100] +**Explanation:** After squaring, the array becomes [16,1,0,9,100]. +After sorting, it becomes [0,1,9,16,100]. + +**Example 2:** + +**Input:** nums = [-7,-3,2,3,11] +**Output:** [4,9,9,49,121] + +#### Constraints + +- `1 <= nums.length <= 104` +- `-104 <= nums[i] <= 104` +- `nums` is sorted in **non-decreasing** order. + +### Thoughts + +> [!summary] +> This is a #two_pointers + +Using two pointers. +One from left, one from right end. +**fill the array in backwards order.** + +### Solution + +cpp +```cpp +class Solution { +public: + vector sortedSquares(vector &nums) { + // Double pointers, since array is sorted and access in order. + // O(2n) + + int mid; + int size = nums.size(); + int left = 0, right = size - 1; + int count = size - 1; + vector answer(size); + + for (int i = 0; i < size; i++) { + nums[i] = nums[i] * nums[i]; + } + + while (left <= right) { + if (nums[left] >= nums[right]) { + answer[count--] = nums[left]; + left++; + } else if (nums[right] > nums[left]) { + answer[count--] = nums[right]; + right--; + } + } + + return answer; + } +}; +```