Skip to content

[PDKhan] WEEK 02 solutions #1208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 3sum/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
vector<vector<int>> result;
sort(nums.begin(), nums.end());

for(int i = 0; i < nums.size(); i++){
if(i > 0 && nums[i] == nums[i-1])
continue;

int left = i + 1;
int right = nums.size() - 1;

while(left < right){
int sum = nums[i] + nums[left] + nums[right];

if(sum == 0){
result.push_back({nums[i], nums[left], nums[right]});
left++;
right--;

while(left < right && nums[left] == nums[left-1])
left++;

while(left < right && nums[right] == nums[right+1])
right--;
Comment on lines +22 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전 set을 사용해서 중복을 제거했는데 이런 방법으로도 할 수 있네요! 한 수 배워갑니다

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰 감사합니다!!!

}else if(sum < 0)
left++;
else
right--;
}
}

return result;
}
};
20 changes: 20 additions & 0 deletions climbing-stairs/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public:
int climbStairs(int n) {
if(n < 2)
return 1;

int curr = 0;
int prev1 = 1;
int prev2 = 1;

for(int i = 2; i <= n; i++){
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}

return curr;
}
};

22 changes: 22 additions & 0 deletions product-of-array-except-self/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Solution {
public:
vector<int> productExceptSelf(vector<int>& nums) {
int n = nums.size();
vector<int> result(n, 1);

int left = 1;

for(int i = 0; i < n; i++){
result[i] = left;
left *= nums[i];
}

int right = 1;
for(int i = n - 1; i >= 0; i--){
result[i] *= right;
right *= nums[i];
}

return result;
}
};
21 changes: 21 additions & 0 deletions valid-anagram/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char, int> map;

if(s.length() != t.length())
return false;

for(int i = 0; i < s.length(); i++)
map[s[i]]++;

for(int i = 0; i < t.length(); i++){
if(map[t[i]] == 0)
return false;

map[t[i]]--;
}

return true;
}
};
16 changes: 16 additions & 0 deletions validate-binary-search-tree/PDKhan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
public:
bool search(TreeNode* root, long min, long max){
if(root == NULL)
return true;

if(root->val <= min || root->val >= max)
return false;

return search(root->left, min, root->val) && search(root->right, root->val, max);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

깔끔한 풀이인 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

처음에는 left 따로 right 따로 리턴 값 체크했는데 하나로 합치는게 더 깔끔해보이더라고요

감사합니다.

}

bool isValidBST(TreeNode* root) {
return search(root, (long)INT_MIN-1, (long)INT_MAX+1);
}
};