diff options
Diffstat (limited to 'top-interview-questions/easy/array')
9 files changed, 199 insertions, 0 deletions
diff --git a/top-interview-questions/easy/array/01_remove_duplicates_from_sorted_array.cc b/top-interview-questions/easy/array/01_remove_duplicates_from_sorted_array.cc new file mode 100644 index 0000000..8f5224b --- /dev/null +++ b/top-interview-questions/easy/array/01_remove_duplicates_from_sorted_array.cc | |||
@@ -0,0 +1,13 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | int removeDuplicates(vector<int>& nums) { | ||
4 | size_t i = nums.size() > 0 ? 1 : 0; | ||
5 | for (size_t j = 1; j < nums.size(); ++j) { | ||
6 | if (nums[j] != nums[i-1]) { | ||
7 | nums[i++] = nums[j]; | ||
8 | } | ||
9 | } | ||
10 | nums.resize(i); | ||
11 | return i; | ||
12 | } | ||
13 | }; | ||
diff --git a/top-interview-questions/easy/array/02_best_time_to_buy_and_sell_stock_2.cc b/top-interview-questions/easy/array/02_best_time_to_buy_and_sell_stock_2.cc new file mode 100644 index 0000000..abc4aa9 --- /dev/null +++ b/top-interview-questions/easy/array/02_best_time_to_buy_and_sell_stock_2.cc | |||
@@ -0,0 +1,24 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | int maxProfit(vector<int>& prices) { | ||
4 | int profit = 0; | ||
5 | int buy = -1; | ||
6 | int sell = -1; | ||
7 | for (size_t day = 0; day < prices.size(); ++day) { | ||
8 | if ((sell == -1) && | ||
9 | ((buy == -1) || (prices[day] < buy))) { | ||
10 | buy = prices[day]; | ||
11 | } else if ((sell == -1) || (prices[day] > sell)) { | ||
12 | sell = prices[day]; | ||
13 | } else { | ||
14 | profit += sell - buy; | ||
15 | buy = prices[day]; | ||
16 | sell = -1; | ||
17 | } | ||
18 | } | ||
19 | if (buy < sell) { | ||
20 | profit += sell - buy; | ||
21 | } | ||
22 | return profit; | ||
23 | } | ||
24 | }; | ||
diff --git a/top-interview-questions/easy/array/03_rotate_array.cc b/top-interview-questions/easy/array/03_rotate_array.cc new file mode 100644 index 0000000..19f66dd --- /dev/null +++ b/top-interview-questions/easy/array/03_rotate_array.cc | |||
@@ -0,0 +1,39 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | // Works, but it's slow. | ||
4 | /*int gcd(int a, int b) { | ||
5 | if (a == 0) return b; | ||
6 | if (b == 0) return a; | ||
7 | if (a == b) return a; | ||
8 | if (a > b) return gcd(a-b, b); | ||
9 | else return gcd(a, b-a); | ||
10 | }*/ | ||
11 | |||
12 | // Much faster than the above implementation. | ||
13 | int gcd(int a, int b) { | ||
14 | if (b == 0) return a; | ||
15 | else return gcd(b, a%b); | ||
16 | } | ||
17 | |||
18 | void rotate(vector<int>& nums, int k) { | ||
19 | const size_t N = nums.size(); | ||
20 | if ((N <= 1) || (k == 0)) { | ||
21 | return; | ||
22 | } | ||
23 | // Original algorithm only works for (N,k) coprime. | ||
24 | // Break the cycles when we complete a loop around the array. | ||
25 | const int gcdNk = gcd(N,k); | ||
26 | const int steps = (gcdNk != 1) ? (N / gcdNk) : N; | ||
27 | int start = -1; | ||
28 | for (size_t n = 0; n < N; n += steps) { | ||
29 | ++start; | ||
30 | int i = start; | ||
31 | int i_val = nums[i]; | ||
32 | for (size_t s = 0; s < steps; ++s) { | ||
33 | const int next = (i+k) % N; | ||
34 | std::swap(i_val, nums[next]); | ||
35 | i = next; | ||
36 | } | ||
37 | } | ||
38 | } | ||
39 | }; | ||
diff --git a/top-interview-questions/easy/array/04_contains_duplicate.cc b/top-interview-questions/easy/array/04_contains_duplicate.cc new file mode 100644 index 0000000..d40d1e4 --- /dev/null +++ b/top-interview-questions/easy/array/04_contains_duplicate.cc | |||
@@ -0,0 +1,14 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | bool containsDuplicate(vector<int>& nums) { | ||
4 | std::sort(nums.begin(), nums.end()); | ||
5 | |||
6 | for (size_t i = 1; i < nums.size(); ++i) { | ||
7 | if (nums[i-1] == nums[i]) { | ||
8 | return true; | ||
9 | } | ||
10 | } | ||
11 | |||
12 | return false; | ||
13 | } | ||
14 | }; | ||
diff --git a/top-interview-questions/easy/array/05_single_number.cc b/top-interview-questions/easy/array/05_single_number.cc new file mode 100644 index 0000000..ad55712 --- /dev/null +++ b/top-interview-questions/easy/array/05_single_number.cc | |||
@@ -0,0 +1,10 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | int singleNumber(vector<int>& nums) { | ||
4 | int x = 0; | ||
5 | for (int n : nums) { | ||
6 | x = x ^ n; | ||
7 | } | ||
8 | return x; | ||
9 | } | ||
10 | }; | ||
diff --git a/top-interview-questions/easy/array/06_intersection_of_two_arrays_II.cc b/top-interview-questions/easy/array/06_intersection_of_two_arrays_II.cc new file mode 100644 index 0000000..0921be8 --- /dev/null +++ b/top-interview-questions/easy/array/06_intersection_of_two_arrays_II.cc | |||
@@ -0,0 +1,27 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | vector<int> intersect(vector<int>& nums1, vector<int>& nums2) { | ||
4 | std::sort(nums1.begin(), nums1.end()); | ||
5 | std::sort(nums2.begin(), nums2.end()); | ||
6 | |||
7 | std::vector<int> result; | ||
8 | result.reserve(std::max(nums1.size(), nums2.size())); | ||
9 | |||
10 | size_t i = 0; | ||
11 | size_t j = 0; | ||
12 | |||
13 | while ((i < nums1.size()) && (j < nums2.size())) { | ||
14 | if (nums1[i] == nums2[j]) { | ||
15 | result.push_back(nums1[i]); | ||
16 | ++i; | ||
17 | ++j; | ||
18 | } else if (nums1[i] < nums2[j]) { | ||
19 | ++i; | ||
20 | } else { | ||
21 | ++j; | ||
22 | } | ||
23 | } | ||
24 | |||
25 | return result; | ||
26 | } | ||
27 | }; | ||
diff --git a/top-interview-questions/easy/array/07_plus_one.cc b/top-interview-questions/easy/array/07_plus_one.cc new file mode 100644 index 0000000..05c7b21 --- /dev/null +++ b/top-interview-questions/easy/array/07_plus_one.cc | |||
@@ -0,0 +1,25 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | vector<int> plusOne(vector<int>& digits) { | ||
4 | int carry = 1; | ||
5 | |||
6 | for (int i = digits.size()-1; (i >= 0) && (carry == 1); --i) { | ||
7 | const int sum = digits[i] + carry; | ||
8 | digits[i] = sum % 10; | ||
9 | carry = (sum >= 10) ? 1 : 0; | ||
10 | } | ||
11 | |||
12 | if (carry == 1) { // Overflow. | ||
13 | digits.resize(digits.size() + 1); | ||
14 | |||
15 | // Shift right. | ||
16 | for (size_t i = digits.size()-1; i > 0; --i) { | ||
17 | digits[i] = digits[i-1]; | ||
18 | } | ||
19 | |||
20 | digits[0] = 1; | ||
21 | } | ||
22 | |||
23 | return digits; | ||
24 | } | ||
25 | }; | ||
diff --git a/top-interview-questions/easy/array/08_move_zeroes.cc b/top-interview-questions/easy/array/08_move_zeroes.cc new file mode 100644 index 0000000..074944c --- /dev/null +++ b/top-interview-questions/easy/array/08_move_zeroes.cc | |||
@@ -0,0 +1,23 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | void moveZeroes(vector<int>& nums) { | ||
4 | // Invariant: left subset of array satisfies requirement. | ||
5 | bool sorted = false; | ||
6 | for (size_t i = 0; (i < nums.size()) && !sorted; ++i) { | ||
7 | if (nums[i] == 0) { | ||
8 | // Look for j>i with which to swap this 0. | ||
9 | // If not found, then we're done. | ||
10 | bool found = false; | ||
11 | for (size_t j = i+1; j < nums.size(); ++j) { | ||
12 | if (nums[j] != 0) { | ||
13 | nums[i] = nums[j]; | ||
14 | nums[j] = 0; | ||
15 | found = true; | ||
16 | break; | ||
17 | } | ||
18 | } | ||
19 | sorted = !found; | ||
20 | } | ||
21 | } | ||
22 | } | ||
23 | }; | ||
diff --git a/top-interview-questions/easy/array/09_two_sum.cc b/top-interview-questions/easy/array/09_two_sum.cc new file mode 100644 index 0000000..72d2014 --- /dev/null +++ b/top-interview-questions/easy/array/09_two_sum.cc | |||
@@ -0,0 +1,24 @@ | |||
1 | class Solution { | ||
2 | public: | ||
3 | vector<int> twoSum(vector<int>& nums, int target) { | ||
4 | // When you find, e.g., 2, map (2 -> idx 0) for the event | ||
5 | // in which you find the 7. | ||
6 | std::vector<int> pair(2); | ||
7 | std::unordered_map<int, int> val_to_idx; | ||
8 | |||
9 | for (size_t i = 0; i < nums.size(); ++i) { | ||
10 | const int other = target - nums[i]; | ||
11 | const auto other_idx = val_to_idx.find(other); | ||
12 | |||
13 | if (other_idx != val_to_idx.end()) { | ||
14 | pair[0] = i; | ||
15 | pair[1] = other_idx->second; | ||
16 | break; | ||
17 | } | ||
18 | |||
19 | val_to_idx[nums[i]] = i; | ||
20 | } | ||
21 | |||
22 | return pair; | ||
23 | } | ||
24 | }; | ||