Skip to content
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

[forest000014] Week 2 #719

Merged
merged 3 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
30 changes: 30 additions & 0 deletions climbing-stairs/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
i번째 칸에 가는 방법은 (1) i-2번째 칸에서 2칸을 점프하거나 (2) i-1번째 칸에서 1칸을 점프하는 2가지 방법 뿐입니다. (MECE함)
따라서, (i번째 칸에 가는 경우의 수) = (i-2번째 칸에 가는 경우의 수) + (i-1번째 칸에 가는 경우의 수)

Runtime: 0 ms (Beats: 100.00%)
Time Complexity: O(n)

Memory: 40.47 MB (Beats: 36.79%)
Space Complexity: O(1)
*/

class Solution {
public int climbStairs(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 2;
} else {
int prev2 = 1;
int prev1 = 2;
int cur = 0;
for (int i = 3; i <= n; i++) {
cur = prev2 + prev1;
prev2 = prev1;
prev1 = cur;
}
return cur;
}
Comment on lines +14 to +28
Copy link
Contributor

Choose a reason for hiding this comment

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

로직은 좋다고 생각합니다! 개인적으로는 else if 문을 줄이는 방향으로 하면 가독성 면에서 좀 더 올라가지 않을까 싶어요!

}
}
32 changes: 32 additions & 0 deletions valid-anagram/forest000014.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
s와 t는 알파벳 소문자로만 이루어지므로, 카운팅을 위해 26개의 고정된 key를 사용하면 충분하고, 배열이 가장 간단하고 적합하다고 생각함

Runtime: 4 ms (Beats: 76.59%)
Time Complexity: O(n)

Memory: 43.04 MB (Beats: 78.65%)
Space Complexity: O(1)
*/

class Solution {
public boolean isAnagram(String s, String t) {
if (s.length() != t.length())
return false;

int[] cnt = new int[26];

for (int i = 0; i < s.length(); i++) {
cnt[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
cnt[t.charAt(i) - 'a']--;
}
Comment on lines +18 to +23
Copy link
Contributor

Choose a reason for hiding this comment

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

위에 If문에서 이미 s와 t의 길이를 비교한 이후라면 둘의 길이가 같을테니, 하나의 for문으로 합칠 수 있지 않을까요? :)


for (int i = 0; i < 26; i++) {
if (cnt[i] != 0)
return false;
}

return true;
}
}
Loading