Skip to content

Commit

Permalink
chore: Pad problem code to 4 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
euchangxian committed Aug 1, 2024
1 parent 1b03a65 commit b82d9c7
Show file tree
Hide file tree
Showing 144 changed files with 6,013 additions and 23 deletions.
22 changes: 22 additions & 0 deletions C++/0003-LongestSubstringWithoutRepeatingCharacters/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string>
#include <unordered_set>

using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_set<char> unique;
int result = 0;

int l = 0;
for (int r = 0; r < s.length(); ++r) {
while (unique.count(s[r])) {
unique.erase(s[l++]);
}
unique.insert(s[r]);
result = max(result, r - l + 1);
}

return result;
}
};
15 changes: 15 additions & 0 deletions C++/0005-LongestPalindromicSubstring/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
class Solution {
public:
string longestPalindrome(string s) {}
};
23 changes: 23 additions & 0 deletions C++/0011-ContainerWithMostWater/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <vector>

using namespace std;
class Solution {
public:
int maxArea(vector<int> &height) {
int left = 0;
int right = height.size() - 1;

int result = 0;
while (left < right) {
int currentArea = min(height[left], height[right]) * (right - left);
result = max(result, currentArea);

if (height[left] < height[right]) {
++left;
} else {
--right;
}
}
return result;
}
};
28 changes: 28 additions & 0 deletions C++/0014-LongestCommonPrefix/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <string>
#include <vector>

using namespace std;

class Solution {
public:
string longestCommonPrefix(vector<string> &strs) {
if (strs.empty()) {
return "";
}

string prefix = strs[0];
for (int i = 1; i < strs.size(); ++i) {
int j = 0;
while (j < min(prefix.size(), strs[i].size()) &&
prefix[j] == strs[i][j]) {
++j;
}
prefix = prefix.substr(0, j); // Adjust the prefix
if (prefix.empty()) {
break; // Early exit if there's no common prefix
}
}
return prefix;
}
};

52 changes: 52 additions & 0 deletions C++/0015-ThreeSum/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <algorithm>
#include <vector>

using namespace std;
class Solution {
public:
vector<vector<int>> threeSum(vector<int> &nums) {
sort(nums.begin(), nums.end());

vector<vector<int>> result;
for (int i = 0; i < nums.size() - 2; ++i) {
if (nums[i] > 0) {
// Considering the way we only look forward for matches,
// and the array is sorted, if the beginning number
// is positive, we can terminate the algorithm because
// there is no way to sum to zero.
break;
}

if (i > 0 && nums[i] == nums[i - 1]) {
// Considering the array is sorted, if the value
// of the current is same as previous, then skip
// duplicates.
continue;
}

// reduce to 2-sum
int target = -nums[i];

int j = i + 1;
int k = nums.size() - 1;
while (j < k) {
int sum = nums[j] + nums[k];
if (sum < target) {
++j;
} else if (sum > target) {
--k;
} else {
result.push_back({nums[i], nums[j++], nums[k--]});

while (j < k && nums[j] == nums[j - 1]) {
++j;
}
while (j < k && nums[k] == nums[k + 1]) {
--k;
}
}
}
}
return result;
}
};
44 changes: 44 additions & 0 deletions C++/0020-ValidParentheses/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <algorithm>
#include <climits>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;
class Solution {
public:
bool isValid(string s) {
stack<char> expected;

for (char const &c : s) {
if (c == '(') {
expected.push(')');
continue;
}
if (c == '{') {
expected.push('}');
continue;
}
if (c == '[') {
expected.push(']');
continue;
}

if (expected.empty()) {
return false;
}

char prev = expected.top();
expected.pop();
if (prev != c) {
return false;
}
}

return expected.empty();
}
};
77 changes: 77 additions & 0 deletions C++/0036-ValidSudoku/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <vector>

using namespace std;
class Solution {
private:
bool isRowValid(vector<vector<char>> const &board, int row) {
vector<bool> present = vector<bool>(9, false);
for (char num : board[row]) {
if (num == '.') {
continue;
}
if (present[num - '0']) {
return false;
}
present[num - '0'] = true;
}
return true;
}

bool isColValid(vector<vector<char>> const &board, int col) {
vector<bool> present = vector<bool>(9, false);
for (auto row : board) {
if (row[col] == '.') {
continue;
}
if (present[row[col] - '0']) {
return false;
}
present[row[col] - '0'] = true;
}
return true;
}

bool isBoxValid(vector<vector<char>> const &board, int row, int col) {
// no bounds check. assume inputs are correct.
vector<bool> present = vector<bool>(9, false);
for (int i = row; i < row + 3; ++i) {
for (int j = col; j < col + 3; ++j) {
if (board[i][j] == '.') {
continue;
}
if (present[board[i][j] - '0']) {
return false;
}
present[board[i][j] - '0'] = true;
}
}
return true;
}

public:
bool isValidSudoku(vector<vector<char>> &board) {
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
bool boxResult = isBoxValid(board, i, j);
if (!boxResult) {
return false;
}

for (int row = i; row < i + 3; ++row) {
bool rowResult = isRowValid(board, row);
if (!rowResult) {
return false;
}
}

for (int col = j; col < j + 3; ++col) {
bool colResult = isColValid(board, col);
if (!colResult) {
return false;
}
}
}
}
return true;
}
};
81 changes: 81 additions & 0 deletions C++/0042-TrappingRainWater/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>
#include <stack>
#include <vector>

using namespace std;
class Solution {
public:
// Water cannot be trapped at boundaries
int trap(vector<int> const &height) {
if (height.size() < 3) {
return 0;
}

int l = 0;
int r = height.size() - 1;

int leftBoundary = height[l];
int rightBoundary = height[r];
int result = 0;
while (l < r) {
// if-checks guarantee somewhat of a monotonic property.
if (leftBoundary < rightBoundary) {
leftBoundary = max(leftBoundary, height[++l]);
result += leftBoundary - height[l];
} else {
rightBoundary = max(rightBoundary, height[--r]);
result += rightBoundary - height[r];
}
}
return result;
}

int trapStack(vector<int> &height) {
if (height.size() < 3) {
return 0; // Water cannot be trapped
}

// Monotonically non-increasing stack (same or decreasing)
stack<int> indices;
int result = 0;
for (int i = 0; i < height.size(); ++i) {
if (indices.empty() || height[i] <= height[indices.top()]) {
indices.push(i);
continue;
}

while (!indices.empty() && height[i] > height[indices.top()]) {
int popIndex = indices.top();
indices.pop();

// Handle boundaries (literally edge cases) and adjacent bars.
if (indices.empty()) {
break;
}

int leftBoundaryIndex = indices.top();
// Take the min of the leftBoundary and rightBoundary (height[i]),
// minus the depth of the poppedIndex, then multiply by the distance
// between the left and right bounds. Visualize it as adding horizontal
// layers of water instead of vertical layers.
result +=
(min(height[leftBoundaryIndex], height[i]) - height[popIndex]) *
(i - leftBoundaryIndex - 1);
}

indices.push(i);
}

return result;
}
};

int main(int argc, char *argv[]) {
vector<int> heights = {0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1};
cout << "want 6, got " << Solution().trap(heights) << endl;

heights = {4, 2, 0, 3, 2, 5};
cout << "want 9, got " << Solution().trap(heights) << endl;

return 0;
}
28 changes: 28 additions & 0 deletions C++/0046-Permutations/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <utility>
#include <vector>

using namespace std;
class Solution {
private:
void generatePermutations(vector<int> &nums,
vector<vector<int>> &permutations, int index) {
if (index >= nums.size()) {
permutations.push_back(nums);
return;
}

for (int i = index; i < nums.size(); ++i) {
swap(nums[index], nums[i]);
generatePermutations(nums, permutations, index + 1);
swap(nums[index], nums[i]);
}
return;
}

public:
vector<vector<int>> permute(vector<int> &nums) {
vector<vector<int>> permutations;
generatePermutations(nums, permutations, 0);
return permutations;
}
};
Loading

0 comments on commit b82d9c7

Please sign in to comment.