-
-
Notifications
You must be signed in to change notification settings - Fork 195
[yeoju] Week 02 #1257
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
[yeoju] Week 02 #1257
Changes from all commits
Commits
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,62 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(n^2) | ||
- sort: O(nlogn) | ||
- 바깥루프 * 안쪽루프: O(n^2) | ||
SC: O(1) | ||
|
||
풀이 방법: | ||
- nums 배열을 오름차순으로 정렬 | ||
- 첫번째 숫자를 기준으로, 나머지 두 숫자는 투 포인터로 탐색 | ||
- 세 수의 합이 0이면 ans에 추가 | ||
- 중복되는 값들은 건너뛰면서 ans배열에 중복되는 벡터가 들어가지 않도록 한다 | ||
*/ | ||
class Solution { | ||
public: | ||
vector<vector<int>> threeSum(vector<int>& nums) { | ||
vector<vector<int>> ans; | ||
|
||
sort(nums.begin(), nums.end()); | ||
|
||
for (int idx = 0; idx < nums.size() - 2; idx++) | ||
{ | ||
// 중복되는 숫자 건너뛰기 | ||
if (idx > 0 && nums[idx] == nums[idx - 1]) | ||
continue ; | ||
|
||
int left = idx + 1; | ||
int right = nums.size() - 1; | ||
|
||
while (left < right) | ||
{ | ||
int sum = nums[idx] + nums[left] + nums[right]; | ||
|
||
if (sum < 0) | ||
left++; | ||
else if (sum > 0) | ||
right--; | ||
else | ||
{ | ||
// 합이 0인 경우 벡터 ans에 삽입 | ||
ans.push_back({nums[idx], nums[right], nums[left]}); | ||
|
||
// 중복되는 숫자 건너뛰기 | ||
while (left < right && nums[left] == nums[left + 1]) | ||
left++; | ||
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,34 @@ | ||
#include <iostream> | ||
|
||
/* | ||
TC:O(n) | ||
SC:O(1) | ||
|
||
풀이방법: | ||
- 점화식: f(n) = f(n-1) + f(n-2) | ||
- tmp: f(n-2) | ||
- tmp2: f(n-1) | ||
- tmp3: 임시 저장용 | ||
- 반복문을 통해 f(n)까지 bottom up 방식으로 계산 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
int climbStairs(int n) { | ||
int tmp = 1; | ||
int tmp2 = 2; | ||
int tmp3; | ||
|
||
if (n == 1) | ||
return (1); | ||
if (n == 2) | ||
return (2); | ||
for (int i = 2; i < n; i++) | ||
{ | ||
tmp3 = tmp; | ||
tmp = tmp2; | ||
tmp2 = tmp2 + tmp3; | ||
} | ||
return (tmp2); | ||
} | ||
}; |
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,35 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC:O(n) | ||
SC:O(1) | ||
- 정답 배열 ans를 제외한 추가 공간 없음 | ||
|
||
풀이 방법: | ||
- 첫번째 반복문: 각 인덱스 기준 왼쪽 요소들의 누적 곱 저장 | ||
- 두번째 반복문: 각 인덱스 기준 오른쪽 요소들의 누적 곱과 곱 | ||
|
||
*/ | ||
|
||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& nums) { | ||
vector<int> ans(nums.size()); | ||
|
||
ans[0] = 1; | ||
for (int i = 1; i < nums.size(); i++) | ||
ans[i] = ans[i - 1] * nums[i - 1]; | ||
|
||
int tmp = 1; | ||
for (int i = nums.size() - 1; i >= 0; i--) | ||
{ | ||
ans[i] *= tmp; | ||
tmp *= 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,39 @@ | ||
#include <iostream> | ||
#include <string> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC:O(n) | ||
SC:O(1) | ||
|
||
풀이방법: | ||
- ascii 기준으로 문자 빈도를 저장할 cnt 배열 선언 | ||
- s의 각 문자 등장 횟수를 cnt에 +1 | ||
- t의 각 문자는 cnt에서 -1 | ||
- cnt 배열의 모든 값이 0이면 두 문자열은 아나그램이다 | ||
*/ | ||
|
||
class Solution { | ||
public: | ||
bool isAnagram(string s, string t) { | ||
char cnt[256]; | ||
|
||
// cnt 배열 0으로 초기화 | ||
for (char &value : cnt) | ||
value = 0; | ||
|
||
for (char ch : s) | ||
cnt[ch]++; | ||
|
||
for (char ch : t) | ||
cnt[ch]--; | ||
|
||
for (int i = 0; i < 256; i++) | ||
{ | ||
if (cnt[i] != 0) | ||
return (false); | ||
} | ||
return (true); | ||
} | ||
}; |
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,44 @@ | ||
#include <iostream> | ||
#include <climits> | ||
|
||
using namespace std; | ||
|
||
/* | ||
TC: O(n) | ||
중복되는 방문 없이 모든 노드를 한번씩 방문하므로 O(n) | ||
SC: O(h) | ||
재귀 호출의 깊이는 트리의 높이와 같다 | ||
최악의 경우 O(N)이고 최선의 경우엔 O(logn)이다 | ||
|
||
풀이 방법: | ||
전위순회를 하면서 왼쪽 자식노드와 오른쪽 자식노드에 대한 깊이탐색을 수행한다 | ||
INT_MIN 혹은 INT_MAX에 대한 값을 처리하기 위해 초기 min과 max값을 long형으로 둔다 | ||
*/ | ||
|
||
// 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) { | ||
return (dfs(root, LONG_MIN, LONG_MAX)); | ||
} | ||
bool dfs(TreeNode *node, long min, long max) | ||
{ | ||
if (!node) | ||
return (true); | ||
if (node->val <= min || node->val >= max) | ||
return (false); | ||
if (dfs(node->left, min, node->val) && dfs(node->right, node->val, max)) | ||
return (true); | ||
else | ||
return (false); | ||
} | ||
}; |
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.
풀이에는 어떤 역할을 하는지 잘 설명되어있지만 변수명 자체에도 각 변수의 용도를 담는 이름이면 더 좋을것 같습니다!