File tree 2 files changed +44
-3
lines changed
2 files changed +44
-3
lines changed Original file line number Diff line number Diff line change 16
16
17
17
### Problems Solved
18
18
19
- | Total | 40 |
19
+ | Total | 41 |
20
20
| :---:| :---:|
21
21
22
22
#### Search By Topic
40
40
| Sliding Window | 1 |
41
41
| Stack | 2 |
42
42
| Tries | 0 |
43
- | Two Pointers | 3 |
43
+ | Two Pointers | 4 |
44
44
45
45
#### Search By Difficulty
46
46
47
47
| Difficulty | Number |
48
48
| :---| ---:|
49
- | Easy | 39 |
49
+ | Easy | 40 |
50
50
| Medium | 1 |
51
51
| Hard | 0 |
52
52
Original file line number Diff line number Diff line change
1
+ /*
2
+ * Problem: 977
3
+ * Name: Squares Of A Sorted Array
4
+ * Difficulty: Easy
5
+ * Topic: Two Pointers
6
+ * Link: https://leetcode.com/problems/squares-of-a-sorted-array/
7
+ */
8
+
9
+ #include < bits/stdc++.h>
10
+ using namespace std ;
11
+
12
+ // Naive Approach
13
+ // Time Complexity: O(n log n)
14
+ // Space Complexity: O(1)
15
+ vector<int > sortedSquaresSort (vector<int >& nums) {
16
+ for (int idx = 0 ; idx < nums.size (); idx++){
17
+ nums[idx] = nums[idx] * nums[idx];
18
+ }
19
+ sort (nums.begin (), nums.end ());
20
+ return nums;
21
+ }
22
+
23
+ // Two Pointer Approach
24
+ // Time Complexity: O(n)
25
+ // Space Complexity: O(n)
26
+ vector<int > sortedSquaresTwoPointers (vector<int >& nums) {
27
+ vector<int > result (nums.size ());
28
+ int left = 0 ;
29
+ int right = nums.size ()-1 ;
30
+ while (left <= right){
31
+ if (abs (nums[left]) > abs (nums[right])){
32
+ result[right - left] = nums[left] * nums[left];
33
+ left++;
34
+ }
35
+ else {
36
+ result[right - left] = nums[right] * nums[right];
37
+ right--;
38
+ }
39
+ }
40
+ return result;
41
+ }
You can’t perform that action at this time.
0 commit comments