Skip to content

Commit de94efd

Browse files
committed
Add C++ solutions for the entire NeetCode 150 list
1 parent 51bcb14 commit de94efd

File tree

151 files changed

+6883
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+6883
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
Given int array, return true if any value appears at least twice
3+
Ex. nums = [1,2,3,1] -> true, nums = [1,2,3,4] -> false
4+
5+
If seen num previously then has dupe, else insert into hash set
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
class Solution {
12+
public:
13+
bool containsDuplicate(vector<int>& nums) {
14+
unordered_set<int> s;
15+
16+
for (int i = 0; i < nums.size(); i++) {
17+
if (s.find(nums[i]) != s.end()) {
18+
return true;
19+
}
20+
s.insert(nums[i]);
21+
}
22+
23+
return false;
24+
}
25+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Design algorithm to encode/decode: list of strings <-> string
3+
4+
Encode/decode w/ non-ASCII delimiter: {len of str, "#", str}
5+
6+
Time: O(n)
7+
Space: O(1)
8+
*/
9+
10+
class Codec {
11+
public:
12+
13+
// Encodes a list of strings to a single string.
14+
string encode(vector<string>& strs) {
15+
string result = "";
16+
17+
for (int i = 0; i < strs.size(); i++) {
18+
string str = strs[i];
19+
result += to_string(str.size()) + "#" + str;
20+
}
21+
22+
return result;
23+
}
24+
25+
// Decodes a single string to a list of strings.
26+
vector<string> decode(string s) {
27+
vector<string> result;
28+
29+
int i = 0;
30+
while (i < s.size()) {
31+
int j = i;
32+
while (s[j] != '#') {
33+
j++;
34+
}
35+
int length = stoi(s.substr(i, j - i));
36+
string str = s.substr(j + 1, length);
37+
result.push_back(str);
38+
i = j + 1 + length;
39+
}
40+
41+
return result;
42+
}
43+
private:
44+
};
45+
46+
// Your Codec object will be instantiated and called as such:
47+
// Codec codec;
48+
// codec.decode(codec.encode(strs));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Given array of strings, group anagrams together (same letters diff order)
3+
Ex. strs = ["eat","tea","tan","ate","nat","bat"] -> [["bat"],["nat","tan"],["ate","eat","tea"]]
4+
5+
Count chars, for each string use total char counts (naturally sorted) as key
6+
7+
Time: O(n x l) -> n = length of strs, l = max length of a string in strs
8+
Space: O(n x l)
9+
*/
10+
11+
class Solution {
12+
public:
13+
vector<vector<string>> groupAnagrams(vector<string>& strs) {
14+
unordered_map<string, vector<string>> m;
15+
for (int i = 0; i < strs.size(); i++) {
16+
string key = getKey(strs[i]);
17+
m[key].push_back(strs[i]);
18+
}
19+
20+
vector<vector<string>> result;
21+
for (auto it = m.begin(); it != m.end(); it++) {
22+
result.push_back(it->second);
23+
}
24+
return result;
25+
}
26+
private:
27+
string getKey(string str) {
28+
vector<int> count(26);
29+
for (int j = 0; j < str.size(); j++) {
30+
count[str[j] - 'a']++;
31+
}
32+
33+
string key = "";
34+
for (int i = 0; i < 26; i++) {
35+
key.append(to_string(count[i] + 'a'));
36+
}
37+
return key;
38+
}
39+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Given unsorted array, return length of longest consecutive sequence
3+
Ex. nums = [100,4,200,1,3,2] -> 4, longest is [1,2,3,4]
4+
5+
Store in hash set, only check for longer seq if it's the beginning
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
class Solution {
12+
public:
13+
int longestConsecutive(vector<int>& nums) {
14+
unordered_set<int> s;
15+
for (int i = 0; i < nums.size(); i++) {
16+
s.insert(nums[i]);
17+
}
18+
19+
int result = 0;
20+
21+
for (auto it = s.begin(); it != s.end(); it++) {
22+
int currNum = *it;
23+
if (s.find(currNum - 1) != s.end()) {
24+
continue;
25+
}
26+
int currLength = 1;
27+
while (s.find(currNum + 1) != s.end()) {
28+
currLength++;
29+
currNum++;
30+
}
31+
result = max(result, currLength);
32+
}
33+
34+
return result;
35+
}
36+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Given an integer array nums, return an array such that:
3+
answer[i] is equal to the product of all elements of nums except nums[i]
4+
Ex. nums = [1,2,3,4] -> [24,12,8,6], nums = [-1,1,0,-3,3] -> [0,0,9,0,0]
5+
6+
Calculate prefix products forward, then postfix backwards in a 2nd pass
7+
8+
Time: O(n)
9+
Space: O(1)
10+
*/
11+
12+
class Solution {
13+
public:
14+
vector<int> productExceptSelf(vector<int>& nums) {
15+
int n = nums.size();
16+
vector<int> result(n, 1);
17+
18+
int prefix = 1;
19+
for (int i = 0; i < n; i++) {
20+
result[i] = prefix;
21+
prefix = prefix * nums[i];
22+
}
23+
24+
int postfix = 1;
25+
for (int i = n - 1; i >= 0; i--) {
26+
result[i] = result[i] * postfix;
27+
postfix = postfix * nums[i];
28+
}
29+
30+
return result;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Given an integer array nums & an integer k, return the k most frequent elements
3+
Ex. nums = [1,1,1,2,2,3] k = 2 -> [1,2], nums = [1] k = 1 -> [1]
4+
5+
Heap -> optimize w/ freq map & bucket sort (no freq can be > n), get results from end
6+
*/
7+
8+
// Time: O(n log k)
9+
// Space: O(n + k)
10+
11+
// class Solution {
12+
// public:
13+
// vector<int> topKFrequent(vector<int>& nums, int k) {
14+
// unordered_map<int, int> m;
15+
// for (int i = 0; i < nums.size(); i++) {
16+
// m[nums[i]]++;
17+
// }
18+
// priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
19+
// for (auto it = m.begin(); it != m.end(); it++) {
20+
// pq.push({it->second, it->first});
21+
// if (pq.size() > k) {
22+
// pq.pop();
23+
// }
24+
// }
25+
// vector<int> result;
26+
// while (!pq.empty()) {
27+
// result.push_back(pq.top().second);
28+
// pq.pop();
29+
// }
30+
// return result;
31+
// }
32+
// };
33+
34+
// Time: O(n)
35+
// Space: O(n)
36+
37+
class Solution {
38+
public:
39+
vector<int> topKFrequent(vector<int>& nums, int k) {
40+
int n = nums.size();
41+
42+
unordered_map<int, int> m;
43+
for (int i = 0; i < n; i++) {
44+
m[nums[i]]++;
45+
}
46+
47+
vector<vector<int>> buckets(n + 1);
48+
for (auto it = m.begin(); it != m.end(); it++) {
49+
buckets[it->second].push_back(it->first);
50+
}
51+
52+
vector<int> result;
53+
54+
for (int i = n; i >= 0; i--) {
55+
if (result.size() >= k) {
56+
break;
57+
}
58+
if (!buckets[i].empty()) {
59+
result.insert(result.end(), buckets[i].begin(), buckets[i].end());
60+
}
61+
}
62+
63+
return result;
64+
}
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Given int array & target, return indices of 2 nums that add to target
3+
Ex. nums = [2,7,11,15] & target = 9 -> [0,1], 2 + 7 = 9
4+
5+
At each num, calculate complement, if exists in hash map then return
6+
7+
Time: O(n)
8+
Space: O(n)
9+
*/
10+
11+
class Solution {
12+
public:
13+
vector<int> twoSum(vector<int>& nums, int target) {
14+
unordered_map<int, int> m;
15+
vector<int> result;
16+
17+
for (int i = 0; i < nums.size(); i++) {
18+
int complement = target - nums[i];
19+
if (m.find(complement) != m.end()) {
20+
result.push_back(m[complement]);
21+
result.push_back(i);
22+
break;
23+
} else {
24+
m.insert({nums[i], i});
25+
}
26+
}
27+
28+
return result;
29+
}
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Given 2 strings, return true if anagrams (same letters diff order)
3+
Ex. s = "anagram" & t = "nagaram" -> true, s = "rat" & t = "car" -> false
4+
5+
Count chars, strings should have same # of chars if anagram
6+
7+
Time: O(n)
8+
Space: O(26)
9+
*/
10+
11+
class Solution {
12+
public:
13+
bool isAnagram(string s, string t) {
14+
if (s.size() != t.size()) {
15+
return false;
16+
}
17+
18+
vector<int> count(26);
19+
20+
for (int i = 0; i < s.size(); i++) {
21+
count[s[i] - 'a']++;
22+
}
23+
24+
for (int j = 0; j < t.size(); j++) {
25+
count[t[j] - 'a']--;
26+
if (count[t[j] - 'a'] < 0) {
27+
return false;
28+
}
29+
}
30+
return true;
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Determine if a 9x9 Sudoku board is valid (no repeats)
3+
4+
Hash set to store seen values, check rows, cols, blocks
5+
6+
Time: O(n^2)
7+
Space: O(n^2)
8+
*/
9+
10+
class Solution {
11+
public:
12+
bool isValidSudoku(vector<vector<char>>& board) {
13+
unordered_set<char> s;
14+
15+
for (int i = 0; i < 9; i++) {
16+
for (int j = 0; j < 9; j++) {
17+
if (board[i][j] == '.') {
18+
continue;
19+
}
20+
auto it = s.find(board[i][j]);
21+
if (it != s.end()) {
22+
return false;
23+
} else {
24+
s.insert(board[i][j]);
25+
}
26+
}
27+
s.clear();
28+
}
29+
30+
for (int i = 0; i < 9; i++) {
31+
for (int j = 0; j < 9; j++) {
32+
if (board[j][i] == '.') {
33+
continue;
34+
}
35+
auto it = s.find(board[j][i]);
36+
if (it != s.end()) {
37+
return false;
38+
} else {
39+
s.insert(board[j][i]);
40+
}
41+
}
42+
s.clear();
43+
}
44+
45+
for (int iCount = 0; iCount < 9; iCount += 3) {
46+
for (int jCount = 0; jCount < 9; jCount += 3) {
47+
for (int i = iCount; i < iCount + 3; i++) {
48+
for (int j = jCount; j < jCount + 3; j++) {
49+
if (board[i][j] == '.') {
50+
continue;
51+
}
52+
auto it = s.find(board[i][j]);
53+
if (it != s.end()) {
54+
return false;
55+
} else {
56+
s.insert(board[i][j]);
57+
}
58+
}
59+
}
60+
s.clear();
61+
}
62+
}
63+
64+
return true;
65+
}
66+
};

0 commit comments

Comments
 (0)