-
-
Notifications
You must be signed in to change notification settings - Fork 362
[Hoonjichoi] WEEK 02 solutions #2696
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||
| f(45) = f(44) + f(43) | ||||||||||||||||||||||||||||||||||
| f(44) = f(43) + f(42) | ||||||||||||||||||||||||||||||||||
| . | ||||||||||||||||||||||||||||||||||
| . | ||||||||||||||||||||||||||||||||||
| . | ||||||||||||||||||||||||||||||||||
| f(3) = f(2) + f(1) | ||||||||||||||||||||||||||||||||||
| f(2) = 2 | ||||||||||||||||||||||||||||||||||
| f(1) = 1 | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| class Solution { | ||||||||||||||||||||||||||||||||||
| public int climbStairs(int n) { | ||||||||||||||||||||||||||||||||||
| if (n <= 2) return n; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| int prev = 1; | ||||||||||||||||||||||||||||||||||
| int result = 2; | ||||||||||||||||||||||||||||||||||
| int temp = 0; | ||||||||||||||||||||||||||||||||||
| for (int i = 2; i < n ; i++) { | ||||||||||||||||||||||||||||||||||
| temp = result; | ||||||||||||||||||||||||||||||||||
| result += prev; | ||||||||||||||||||||||||||||||||||
| prev = temp; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+16
to
+24
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 몇 가지 생각나는 것들 적어봅니다 :)
코드는 요런 스타일을 생각했습니다!
Suggested change
|
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,39 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class hoonjichoi1 { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public boolean isAnagram(String s, String t) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (s.equals(t)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (s.length() != t.length()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| HashMap<Character, Integer> map = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < s.length(); i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Character c = s.charAt(i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (map.containsKey(c)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int value = map.get(c) + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| map.put(c, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| map.put(c, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int j = 0; j < t.length(); j++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Character c2 = t.charAt(j); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (map.containsKey(c2)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int value = map.get(c2) - 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| map.put(c2, value); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+12
to
+30
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개선해보면 좋을 거 같은 포인트 적어보았어요.
코드는 이런 식으로 될 거 같습니다 :)
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (Integer i : map.values()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (i < 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+32
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 반복문은 2번째 반복문에서 처리해줘도 좋을 듯 싶습니다..! |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
풀이 1:
Solution.climbStairs— Time: O(n) / Space: O(1)피드백: 상수 공간으로 이전 두 값만 유지하며 반복문으로 계산한다. 루프는 n-2회 실행되므로 선형 시간 복잡도다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
hoonjichoi1.isAnagram— Time: O(n) / Space: O(k)피드백: 해시맵을 사용해 문자의 등장 횟수를 세고, 두 문자열의 차이를 확인한다. 최악의 경우 모든 문자를 한 번씩 처리한다.
개선 제안: 현재 구현이 적절해 보입니다.