Skip to content

[sora0319] WEEK 02 solutions #1764

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 3 commits into from
Aug 3, 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
28 changes: 28 additions & 0 deletions climbing-stairs/sora0319.java
Copy link
Contributor

Choose a reason for hiding this comment

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

back, front 라는 2개의 변수를 사용해서 답을 구할 수도 있군요. 2개의 풀이로 보여주셔서 감사합니다.

Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
// 공간 복잡도를 줄인 2번째 버전
class Solution {
public int climbStairs(int n) {
int back = 1;
int front = 2;
int target = 0;

if (n == 1) return back;
if (n == 2) return front;

for(int i = 1; i < n-1; i++){
target = front + back;
back = front;
front = target;
}

return target;
}
}


// 초기 버전
class Solution {
public int climbStairs(int n) {
int[] stairs = new int[n+1];
Expand All @@ -12,3 +34,9 @@ public int climbStairs(int n) {
}
}

// 1 : 1
// 2 : [1] + 1, 2 1+1 2 2개
// 3 : [2-1] + 1, [2-2] + 1, [1] + 2 1+1+1 2+1 1+2 3개
// 4 : [3-1] + 1, [3-2] + 1, [3-3] + 1 1+1+1+1 2+1+1 1+2+1 1+1+2 2+2 5개
// 5 : 1+1+1+1+1 2+1+1+1 1+2+1+1 1+1+2+1 1+1+1+2 2+2+1 2+1+2 1+2+2 8개

34 changes: 33 additions & 1 deletion valid-anagram/sora0319.java
Copy link
Contributor

Choose a reason for hiding this comment

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

Map과 배열을 활용한 2개의 풀이 잘 봤습니다. 저는 Map을 활용해서 풀이를 했는데 배열로도 풀이할 수 있는 문제였네요! 저는 2개의 Map을 사용해서 풀이를 했는데 1개의 Map으로 풀이할 수 있군요. 다음에는 Map을 2개 사용하는 풀이가 떠오른다면 1개로는 풀이할 수 없는지 고민해봐야 겠습니다.

Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
// Map을 사용한 버전
class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;

Map <Character, Integer> alphabet = new HashMap<>();

for(char c : s.toCharArray()){
if(!alphabet.containsKey(c)){
alphabet.put(c, 0);
}
alphabet.put(c, alphabet.get(c) + 1);
}

for(char c : t.toCharArray()){
if(!alphabet.containsKey(c)) return false;
if(alphabet.get(c) == 0) return false;

alphabet.put(c, alphabet.get(c)-1);
}

return true;
}
}


// 초기 버전
class Solution {
public boolean isAnagram(String s, String t) {
int[] character = new int[26];
Expand All @@ -17,4 +44,9 @@ public boolean isAnagram(String s, String t) {
}
}


/*
Map, 배열 모두 평균시간복잡도는 O(1)이지만,
배열이 직접 접근 방식이고, Map은 Hash를 사용하여서 배열 보다는 시간이 더 걸린다
배열 사용시 4ms
Map 사용 시 17ms
*/