-
-
Notifications
You must be signed in to change notification settings - Fork 245
[sungjinwi] Week 02 solution #1258
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
20d4573
#218 valid-anagram solution
sungjinwi 3549a8c
#230 climbing-stairs solution
sungjinwi c4240c8
#239 product-of-array-except-self
sungjinwi 9a1aa25
#241 3sum solution
sungjinwi 3dc6c7d
#251 validate-binary-search-tree solution
sungjinwi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
풀이 : | ||
nums 배열을 정렬시킨 후 반복되는 값을 건너뛰며 두 포인터 기법을 사용한다 | ||
i포인터와 left, right 포인터의 값의 합이 0보다 작으면 left++, 크면 right-- | ||
0이면 ans에 저장하고 left++, right--하는 로직을 left < right인 동안 반복한다 | ||
|
||
nums의 길이 N | ||
|
||
TC : O(N^2) | ||
외부 반복문 N * 내부 반복문 N | ||
|
||
SC : O(1) (ans 제외) | ||
left, right, threeSum 3개의 변수만 사용한다 | ||
*/ | ||
|
||
#include <vector> | ||
#include <algorithm> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
vector<vector<int>> ans; | ||
int left; | ||
int right; | ||
int threeSum; | ||
|
||
sort(nums.begin(), nums.end()); | ||
for (int i = 0; i < nums.size() - 2; i++) | ||
{ | ||
// i포인터 중복 제거 | ||
if (i > 0 && nums[i] == nums[i - 1]) | ||
continue ; | ||
|
||
left = i + 1; | ||
right = nums.size() - 1; | ||
while (left < right) | ||
{ | ||
threeSum = nums[i] + nums[left] + nums[right]; | ||
if (threeSum < 0) | ||
left++; | ||
else if(threeSum > 0) | ||
right--; | ||
else | ||
{ | ||
ans.push_back({nums[i], nums[left], nums[right]}); | ||
// left포인터 중복 제거 | ||
while (left < right && nums[left] == nums[left + 1]) | ||
left++; | ||
// right 포인터 중복 제거 | ||
while (left < right && nums[right] == nums[right - 1]) | ||
right--; | ||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
풀이 : | ||
n이 1과 2일 떄는 따로 처리, 그 외에 n번째는 prv(n - 2번째) + cur(n -1번째)로 값을 업데이트 하며 n까지 더해나감 | ||
|
||
TC : O(N) | ||
n의 크기에 반복문이 비례한다 | ||
|
||
SC : O(1) | ||
n의 크기와 상관없이 3개의 변수 사용 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
if (n == 1) | ||
return 1; | ||
if (n == 2) | ||
return 2; | ||
|
||
int prv = 1; | ||
int cur = 2; | ||
int tmp; | ||
for (int i = 3; i <= n; i++) | ||
{ | ||
tmp = cur; | ||
cur = cur + prv; | ||
prv = tmp; | ||
} | ||
return cur; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
풀이 : | ||
해당 인덱스 이전 값들의 곱을 before에 저장(인덱스 0 이전에는 값이 없으므로 1로 초기화) | ||
해당 인덱스 이후 값들의 곱을 after에 저장(인덱스 n - 1이후에는 값이 없으므로 1로 초기화) | ||
인덱스 0부터 반복문을 돌며 ans에 인덱스 이전값들의 곱 before를 곱함 & before값에 현재 num을 곱함 | ||
인덱스 끝부터 반복문을 돌며 after를 이용해 동일 작업 | ||
|
||
nums의 길이 : N | ||
|
||
TC : O(N) | ||
반복문 2회 O(N + N) | ||
|
||
SC : O(1) (ans배열 제외) | ||
N에 관계없이 before와 after 변수만 사용 | ||
*/ | ||
|
||
#include <vector> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
int before = 1; | ||
int after = 1; | ||
vector<int> ans(nums.size(), 1); | ||
|
||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
ans[i] *= before; | ||
before *= nums[i]; | ||
} | ||
for (int i = nums.size() - 1; i >= 0; i--) | ||
{ | ||
ans[i] *= after; | ||
after *= nums[i]; | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
풀이 : | ||
해시테이블에 string을 순회하며 문자 : 빈도로 값 저장 후 두 데이터가 같은 지 비교 | ||
|
||
TC : O(S + T) | ||
SC : O(S + T) | ||
*/ | ||
|
||
#include <unordered_map> | ||
#include <string> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
unordered_map<char, int> s_map; | ||
unordered_map<char, int> t_map; | ||
|
||
for (char c : s) | ||
s_map[c]++; | ||
for (char c : t) | ||
t_map[c]++; | ||
|
||
return (s_map == t_map); | ||
} | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
풀이 : | ||
중위순회로 BST를 탐색하면 오름차순으로 탐색 | ||
->중위순회 했을 떄 오름차순이 아니면 BST가 아니다 | ||
현재 node의 val이 last_val보다 더 큰지 확인하면서 탐색한다 | ||
|
||
초기값은 INT_MIN이 있을경우를 생각해서 그보다 작은 값과 비교하기 위해 LONG_MIN사용 | ||
|
||
node 갯수 N | ||
|
||
TC : O(N) | ||
노드 전체 순회 | ||
|
||
SC : O(N) | ||
재귀 호출스택이 N에 비례 | ||
*/ | ||
|
||
#include <limits.h> | ||
|
||
// Definition for a binary tree node. | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode() : val(0), left(nullptr), right(nullptr) {} | ||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} | ||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} | ||
}; | ||
|
||
class Solution { | ||
public: | ||
bool isValidBST(TreeNode* root) { | ||
long min = LONG_MIN; | ||
return dfs(root, &min); | ||
} | ||
|
||
bool dfs(TreeNode* node, long* last_val){ | ||
if (!node) | ||
return true; | ||
|
||
if (!dfs(node->left, last_val)) | ||
return false; | ||
|
||
if (*last_val >= node->val) | ||
return false; | ||
*last_val = node->val; | ||
|
||
if (!dfs(node->right, last_val)) | ||
return false; | ||
|
||
return true; | ||
} | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
깔끔한 풀이와 복잡도 정리까지 한눈에 볼 수 있어 좋았습니다! 고생하셨어요!! 화이팅✨