Skip to content

Commit 7c6941b

Browse files
authored
Merge pull request neetcode-gh#1223 from Vicen-te/main
Create: 4 Solutions - 278-First-Bad-Version.cpp, 35-Search-Insert-Position.cpp, 977-Squares-of-a-Sorted-Array.cpp, 189-Rotate-Array.cpp
2 parents 7c5b92e + 31bc146 commit 7c6941b

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

cpp/189-Rotate-Array.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Given an array, rotate the array to the right by k steps, where k is non-negative.
3+
Ex.
4+
Input: nums = [1,2,3,4,5,6,7], k = 3
5+
Output: [5,6,7,1,2,3,4]
6+
7+
1.- To avoid problems with the size of the vector we use the remainder of a division.
8+
2.- Reverse the entire vector.
9+
3.- Reverse the parts you want to obtain the result.
10+
11+
Time: O(1)
12+
Space: O(1)
13+
*/
14+
15+
class Solution {
16+
public:
17+
void rotate(vector<int>& nums, int k) {
18+
k %= nums.size();
19+
reverse(nums.begin(), nums.end());
20+
reverse(nums.begin(), nums.begin() + k);
21+
reverse(nums.begin() + k, nums.end());
22+
}
23+
};

cpp/278-First-Bad-Version.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Suppose you have versions and you want to find out the first bad one, which causes all the following ones to be bad.n[1, 2, ..., n]
3+
You are given an API which returns whether is bad. Implement a function to find the first bad version.
4+
5+
Ex.
6+
Input: n = 5, bad = 4
7+
Output: 4
8+
9+
Explanation:
10+
call isBadVersion(3) -> false
11+
call isBadVersion(5) -> true
12+
call isBadVersion(4) -> true
13+
14+
1.- Find the number in the middle of the vector.
15+
2.- Takes a part (first or second), depending on whether or not the target is greater than the middel.
16+
3.- Change the current left or right part.
17+
3.- Do this process until the left reaches the right.
18+
19+
Time: O(log n)
20+
Space: O(1)
21+
*/
22+
23+
class Solution {
24+
public:
25+
int firstBadVersion(int n) {
26+
int left = 1;
27+
int right = n;
28+
29+
while (right > left) {
30+
int mid = left + (right - left) / 2;
31+
32+
if (isBadVersion(mid))
33+
right = mid;
34+
else
35+
left = mid + 1;
36+
}
37+
return left;
38+
}
39+
};

cpp/35-Search-Insert-Position.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Given a sorted array of distinct integers and a target value, return the index if the target is found.
3+
If not, return the index where it would be if it were inserted in order.
4+
5+
Ex.
6+
Input: nums = [1,3,5,6], target = 5
7+
Output: 2
8+
9+
1.- Find the number in the middle of the vector.
10+
2.- Takes a part (first or second), depending on whether or not the target is greater than the middel.
11+
3.- Change the current left or right part.
12+
3.- Do this process until the left exceeds the right.
13+
14+
Time: O(log n)
15+
Space: O(1)
16+
*/
17+
18+
class Solution {
19+
public:
20+
int searchInsert(vector<int>& nums, int target) {
21+
int left = 0;
22+
int right = nums.size() - 1;
23+
24+
while (left <= right) {
25+
int mid = left + (right - left) / 2;
26+
27+
if (nums[mid] < target)
28+
left = mid + 1;
29+
else
30+
right = mid - 1;
31+
}
32+
return left;
33+
}
34+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Given an integer array nums sorted in non-decreasing order, return
3+
an array of the squares of each number sorted in non-decreasing order.
4+
5+
Ex.
6+
Input: nums = [-4,-1,0,3,10]
7+
Output: [0,1,9,16,100]
8+
9+
1.- Multiply each number of the nums by themselves.
10+
2.- Use the sort function to sort the vector.
11+
12+
Time: O(NlogN)
13+
Space: O(N)
14+
*/
15+
16+
class Solution {
17+
public:
18+
vector<int> sortedSquares(vector<int>& nums) {
19+
for (int& i : nums)
20+
i *= i;
21+
sort(nums.begin(), nums.end());
22+
return nums;
23+
}
24+
};

0 commit comments

Comments
 (0)