Skip to content

[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 1 commit into from
Apr 12, 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
62 changes: 62 additions & 0 deletions 3sum/crumbs22.cpp
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);
}
};
34 changes: 34 additions & 0 deletions climbing-stairs/crumbs22.cpp
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: 임시 저장용
Comment on lines +9 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

풀이에는 어떤 역할을 하는지 잘 설명되어있지만 변수명 자체에도 각 변수의 용도를 담는 이름이면 더 좋을것 같습니다!

- 반복문을 통해 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);
}
};
35 changes: 35 additions & 0 deletions product-of-array-except-self/crumbs22.cpp
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);
}
};
39 changes: 39 additions & 0 deletions valid-anagram/crumbs22.cpp
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);
}
};
44 changes: 44 additions & 0 deletions validate-binary-search-tree/crumbs22.cpp
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);
}
};