-
-
Notifications
You must be signed in to change notification settings - Fork 361
[sonshn] WEEK 03 Solutions #2730
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
Changes from all commits
fcb31f4
6c69e25
9d7e093
7766ac3
32eb8b6
a1e4dde
d460b73
f3c4ef9
8d1ab21
63da3f1
1422ccd
aa73ee1
7ed860a
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,20 @@ | ||
| /** | ||
| * dp[i] = dp[i - 1] + dp[i - 2] | ||
| */ | ||
| class Solution { | ||
| public int climbStairs(int n) { | ||
| if (n <= 2) { | ||
| return n; | ||
| } | ||
|
|
||
| int[] dp = new int[n + 1]; | ||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
|
|
||
| for (int i = 3; i <= n; i++) { | ||
| dp[i] = dp[i - 1] + dp[i - 2]; | ||
| } | ||
|
|
||
| return dp[n]; | ||
| } | ||
| } |
|
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,19 @@ | ||
| /** | ||
| * hammingWeight | ||
| * 정수 n의 이진수 표현에서 1의 개수를 반환 | ||
| * | ||
| * 시간 복잡도: O(1) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| public class sonshn { | ||
| public int hammingWeight(int n) { | ||
| int count = 0; | ||
|
|
||
| while (n != 0) { | ||
| count += (n & 1); | ||
| n >>>= 1; | ||
| } | ||
|
|
||
| return count; | ||
| } | ||
| } |
|
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,19 @@ | ||
| import java.util.*; | ||
|
|
||
| /** | ||
| * String to char array, sort, and compare | ||
| * | ||
| * 시간 복잡도: O(nlogn) | ||
| * 공간 복잡도: O(n) | ||
| */ | ||
| class Solution { | ||
| public boolean isAnagram(String s, String t) { | ||
| char[] sArray = s.toCharArray(); | ||
| char[] tArray = t.toCharArray(); | ||
|
|
||
| Arrays.sort(sArray); | ||
| Arrays.sort(tArray); | ||
|
|
||
| return Arrays.equals(sArray, tArray); | ||
| } | ||
| } |
|
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,36 @@ | ||
| /** | ||
| * Palindrome 문자열인지 확인하는 문제 | ||
| * | ||
| * Java에서 제공하는 Character.isLetterOrDigit() 메서드를 사용하여 알파벳과 숫자인지 확인하고, | ||
| * Character.toLowerCase() 메서드를 사용하여 대소문자를 구분하지 않고 비교 | ||
| * | ||
| * 시간 복잡도: O(n) | ||
| * 공간 복잡도: O(1) | ||
| */ | ||
| class Solution { | ||
|
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. 저는 문자열을 파싱하고나서 유효성 검사하는 방법이었는데, 파싱과 유효성을 동시에 검사하면 공간복잡도를 좀 더 줄일 수 있는 것 같아 보여서 좋아보입니다 !! |
||
| public boolean isPalindrome(String s) { | ||
| int left = 0; | ||
| int right = s.length() - 1; | ||
|
|
||
| while (left < right) { | ||
|
|
||
| while (left < right && !Character.isLetterOrDigit(s.charAt(left))) { | ||
| left++; | ||
| } | ||
|
|
||
| while (left < right && !Character.isLetterOrDigit(s.charAt(right))) { | ||
| right--; | ||
| } | ||
|
|
||
| if (Character.toLowerCase(s.charAt(left)) | ||
| != Character.toLowerCase(s.charAt(right))) { | ||
| return false; | ||
| } | ||
|
|
||
| left++; | ||
| right--; | ||
| } | ||
|
|
||
| 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(n)피드백: 선형 시간으로 각 i에 대해 이전 두 값을 더해 계단 수를 구한다. 추가 공간으로 dp 배열이 필요하다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 2:
sonshn.hammingWeight— Time: O( number of bits ) / Space: O(1)피드백: 정수 비트 수만큼 루프를 돌며 각 비트를 확인한다. 공간은 상수이다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 3:
Solution.isAnagram— Time: O(n log n) / Space: O(n)피드백: 정렬을 이용한 비교로 구현되었으며, 두 문자열의 길이를 n이라고 하면 시간은 O(n log n)이다.
개선 제안: 현재 구현이 적절해 보입니다.
풀이 4:
Solution.isPalindrome— Time: O(n) / Space: O(1)피드백: 문자열을 한 번 스캔하며 필요없는 문자는 건너뛰고 소문자로 비교한다.
개선 제안: 현재 구현이 적절해 보입니다.