Skip to content

[john9803] WEEK 02 solutions #1234

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 5 commits into from
Apr 15, 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
91 changes: 91 additions & 0 deletions climbing-stairs/john9803.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.math.BigInteger;

// 1, 2 걸음으로 n개의 계단을 올라가는 방법의 가짓수를 계산

// ================================================================================
// * 풀이 1 *
// ================================================================================
// n <= 45 이므로 최대한 2로 나눠서 가짓수 구하기
// n이 커짐에 따라서 계산의 범위가 굉장히 늘어남.
// 계산범위 초과로 풀이 실패.

// ================================================================================
// * 풀이 2 *
// ================================================================================
// 풀이 1을 보완한 풀이
// 단순히 조합 구현말고 다른 해결방안이 필요함 -> BigInteger로 계산범위 늘림.
// 풀이성공 -> 다만 런타임 시간이 평균풀이보다 너무 길고, 메모리 사용이 높아서 새로운 풀이 생각해봄.

// ================================================================================
// * 풀이 3 *
// ================================================================================
// 더 빠른 풀이를 위해서 찾아보던 중. n이 커짐에 따라서 나오는 값들의 규칙이 피보나치 배열임을 발견.
// f(n) = f(n-1) + f(n-2) 임을 이용해서 빠르게 풀이.
// 시간복잡도 = O(n)

class john9803 {
public int climbStairs(int n) {
// return firstApproch(n);
// return bigIntSolve(n);
return piboSolve(n);
}

public int firstApproch(int n){
int result = 0;
// 2걸음 최대한 들어가고 남은데 1로 채워넣는 걸로 가짓수 세기
for(int step=0; 2*step<=n; step++){
// 2걸음과 1은 순서를 가짐
int totalNumCnt = step + (n-(2*step));
// (totalNumCnt)C(step) -> Combination 을 계산해서 result에 더함
int top = 1;
int bottom = 1;
// 처음에는 분자분모를 int로 놓았다가 분자계산에서 int범위를 벗어남.
for(int c =0; c< step; c++){
top *= (totalNumCnt-c);
bottom *= (step-c);
}
result = result + (int)(top/bottom);
// System.out.println("top: "+ top + " bottom: " + bottom + " step is: " + step +" result is: "+ result);
}
return result;
}


// 단순한 재귀를 통한 문제풀이
// 문제가 생겼던 부분 n이 커짐에 따라서 계산범위가 커졌음.
// 따라서 결과적으로 계산이 가능하게끔 메모리를 크게 부여하는 BigInteger를 사용하는 방법으로 풀이
public int bigIntSolve(int n){
int result = 0;

for(int step=0; 2*step<=n; step++){
int totalNumCnt = step + (n-(2*step));
// (totalNumCnt)C(step) -> Combination 을 계산해서 result에 더함
BigInteger top = new BigInteger("1");
BigInteger bottom = new BigInteger("1");

for(int c =0; c< step; c++){
top = top.multiply(new BigInteger(String.valueOf(totalNumCnt-c)));
bottom = bottom.multiply(new BigInteger(String.valueOf(step-c)));
}
result += (top.divide(bottom)).intValue();
// System.out.println("top: "+ top + " bottom: " + bottom + " step is: " + step +" result is: "+ result);
}
return result;
}

// n이 커짐에 따라서 값이 피보나치 수열의 규칙성을 지니는 것을 파악함.
// 풀이를 피보나치 수열을 이용해 풀이 하도록 구현
// f(n) = f(n-1) + f(n-2) 임을 이용.
public int piboSolve(int n){
int prev = 1;
int curr = 1;

for(int i=2; i<=n; i++){
int temp = prev+curr;
prev = curr;
curr = temp;
}
return curr;
}

}
32 changes: 32 additions & 0 deletions two-sum/john9803.java
Copy link
Contributor

Choose a reason for hiding this comment

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

해당 문제의 경우 O(n) 으로 풀이하는 방법도 있습니다!
힌트는 Map 사용이에요ㅎㅎ 자세한 설명이 필요하실 경우 리트코드 - 채팅 에 부담없이 절 태그해주세요!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,32 @@
class john9803 {
public int[] twoSum(int[] nums, int target) {
int[] result = n2Solve(nums, target);
return result;
}

public int[] n2Solve(int[] nums, int target){
// 일반적인 N^2 풀이
int alpha_target = 0;
int beta_target = 0;

int[] truth_arr = new int[2];

gnd: for(int i =0; i<nums.length; i++){
alpha_target = nums[i];
for( int j=i+1; j<nums.length; j++){
beta_target = nums[j];
if(alpha_target+beta_target==target){
truth_arr[0] = i;
truth_arr[1] = j;
break gnd;
}
}
}
return truth_arr;
}

// n2미만으로도 풀이해보기
// public int[] 2nSolve(int[] nums, int target){
// return [];
// }
}
14 changes: 14 additions & 0 deletions valid-anagram/john9803.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.Arrays;

public class john9803{
public boolean isAnagram(String s, String t) {
// Max 시간복잡도 -> 5^2 * 10^8 = 1억 미만, 브루트포스 풀이
char[] charS = s.toCharArray();
char[] charT = t.toCharArray();

Arrays.sort(charS);
Arrays.sort(charT);

if(String.valueOf(charS).equals(String.valueOf(charT))){return true;}
else{return false;}
Comment on lines +12 to +13
Copy link
Contributor

Choose a reason for hiding this comment

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

Arrays.equals(charS, charT) 라는 내장 함수 사용하실수도 있어요 :)
return은 boolean 으로 반환됩니다~!

}