Skip to content

[jj7779607] Week2 #725

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
Dec 21, 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
39 changes: 39 additions & 0 deletions 3sum/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const result = []; // 결과값
nums.sort((a, b) => a - b); // 오름차순 정렳

// 배열 순차 탐색
for (let i = 0; i < nums.length - 2; i++) {
// 중복 값 건너뜀
if (i > 0 && nums[i] === nums[i - 1]) continue;

let low = i + 1,
high = nums.length - 1;

// low와 high 포인터 이용해 합이 0인 값 찾기
while (low < high) {
const sum = nums[i] + nums[low] + nums[high];
if (sum < 0) low++;
else if (sum > 0) high--;
else {
result.push([nums[i], nums[low], nums[high]]);
// 중복된 값 건너뛰기
while (low < high && nums[low] === nums[low + 1]) low++;
while (low < high && nums[high] === nums[high - 1]) high--;
Comment on lines +25 to +26
Copy link
Contributor

Choose a reason for hiding this comment

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

맞아요 :) 전 문제의 핵심은 정렬, 투 포인터 를 극한의 효율로 사용하기 위한 25,26 라인의 while문인것 같습니다. 전 이 부분을 고려 안해서 자꾸 실패했었네요 ㅎㅎ

low++;
high--;
}
}
}

return result;
};

// 1시간 정도 문제 풀이를 하다가 정렬까지 한 다음 그 이후 어떻게 해야할지 몰라 풀이 참고
// 문제 풀이에서 핵심은 정렬을 한 다음 두 개의 포인터를 지정해 sum이 0인 값을 찾는 것
// 시간 복잡도: O(n^2) 배열 순차 탐색
// 공간 복잡도: O(1) 결과값 result
29 changes: 29 additions & 0 deletions climbing-stairs/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
// 규칙성 찾기
// n = 1 -> 1 (1)
// n = 2 -> 2 (1+1, 2)
// n = 3 -> 2 + 1 = 3 (1+1+1, 1+2, 2+1)
// n = 4 -> 2 + 3 = 5 (1+1+1, 1+1+2, 1+2+1, 2+1+1, 2+2)
// n = 5 -> 3 + 5 = 8 (1+1+1+1+1, 1+1+1+2, 1+1+2+1, 1+2+1+1, 2+1+1+1, 1+2+2, 2+2+1, 2+1+2)
Comment on lines +6 to +11
Copy link
Contributor

Choose a reason for hiding this comment

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

규칙성을 잘 찾으셔서 무리없이 풀으셨을것 같습니다!
이런 식의 규칙성 문제는 대부분 DP를 사용하는것 같아요!

// :
// :
// 규칙 -> n번째 방법 갯수 = n-1번째 방법 갯수 + n-2번째 방법 갯수

// 1. 객체 생성 (n=1일 때, n=2일 때 기본값 설정)
let dp = { 1: 1, 2: 2 };

// 2. n=3일 때부터 dp값 계산
for (let i = 3; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}

// 3. 결과값 반환
return dp[n];
};

// 시간 복잡도: O(n) <- for문 돌면서 dp값 계산하므로
// 공간 복잡도: O(n) <- dp 객체에 n개의 값 저장하므로
32 changes: 32 additions & 0 deletions valid-anagram/limlimjo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
// 길이가 다르면 false
if (s.length !== t.length) {
return false;
}

// s 빈도수, t 빈도수
const countS = {};
const countT = {};

// 하나씩 비교하기
for (let i = 0; i < s.length; i++) {
countS[s[i]] = (countS[s[i]] || 0) + 1;
countT[t[i]] = (countT[t[i]] || 0) + 1;
}

// 두 객체 동일하면 true, 아니면 false
for (let char in countS) {
if (countS[char] !== countT[char]) {
return false;
}
}
Comment on lines +17 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

같은 방법이긴 합니다만 count배열을 하나만 두고 같은 값이 s에 있으면 +, t에 있으면 -를 해주면 결국 해당 값이 0이 될거에요.
이때 두번째 for문에서 배열의 값이 모두 0이면 anagram이 성립하는 식으로 풀이 방법도 있습니다 :)

return true;
};

// 시간복잡도: for문 순회 비교하므로 O(n)
// 공간복잡도: countS, countT 최대 n개의 키 가질 수 있으므로 O(n)
Loading