Skip to content

[oyeong011] week2 #730

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 6 commits into from
Dec 22, 2024
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
25 changes: 25 additions & 0 deletions climbing-stairs/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public:
int climbStairs(int n) {
if(n == 0 || n == 1){
return 1;
}
vector<int> dp(n + 1);
dp[0] = dp[1] = 1;
for(int i = 2; i <= n; i++){
dp[i] = dp[i-1] + dp[i - 2];
}
return dp[n];
}
};

// dp 문제로 배열을 생성해준다
// 계단 방식
// 1 1개
// 1 + 1, 2 2개
// 1 + 1 + 1, 2 + 1, 1 + 2 3개
// 1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1, 2 + 2 ---> 5개
// 즉 피보나찌 수열의 형태를 띈다 왜냐면 2칸을 뛰기 때문에 한칸전에 1을 더하고 두칸 전에 경우의 수에 2를 더해주면 되기때문
// 즉 f(n) = f(n - 1) + f(n - 2)
// 만약 계단 방식이 3칸까지이면 즉 f(n) = f(n - 1) + f(n - 2) + f(n - 3)
// 시간복잡도는 n만큼 순회하여 O(n)
Copy link
Contributor

Choose a reason for hiding this comment

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

깔끔한 풀이에 피보나치 수열인 것까지 잘 캐치하신 것 같습니다 ㅎㅎ
누락된 공간 복잡도 분석 추가하시면 더 좋을 것 같고, 분석하시는 동시에 공간 복잡도를 최적화할 수 있을지 여부에 대해서 한 번 더 생각해보시면 좋을 것 같아요

15 changes: 15 additions & 0 deletions valid-anagram/oyeong011.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
bool isAnagram(string s, string t) {
unordered_map<char, int> ss, tt;
if(s.length() != t.length())return false;
Comment on lines +4 to +5
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
unordered_map<char, int> ss, tt;
if(s.length() != t.length())return false;
if(s.length() != t.length())return false;
unordered_map<char, int> ss, tt;

s와 t의 길이가 다르면 unordered_map을 할당할 필요도 없으니까 5번 줄이 더 위에 있는게 나을 것 같아요

for(auto i : s) ss[i]++;
for(auto i : t) tt[i]++;
return ss == tt;
}
};

// 문자열 비교 unordered_map은 해쉬 형태이므로 O(1)
// 각 문자 루프 O(n)
// 마지막 문자 비교 해시멥 알파벳은 26개 이므로 O(n)
// 따라서 O(n)
Loading