Skip to content
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
49 changes: 49 additions & 0 deletions 3sum/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @param nums - 정수 배열
* @returns - 세 요소를 합해 0이 되는 값의 배열
* @description
* - 투 포인터 방식
* - 결국 유니크한 조합을 찾으며 중복을 제외하는 방향
* - 무조건 적인 반복 줄이기가 아닌 효율적 범위 반복을 학습해야함
* - 시간 복잡도 O(N^2)
* - 공간 복잡도 O(log N)
*/
function threeSum(nums: number[]): number[][] {
const answer: number[][] = [];
nums.sort((a, b) => a - b);

for (let i = 0; i < nums.length - 2; i++) {
if (i && nums[i] === nums[i - 1]) {
Copy link
Contributor

Choose a reason for hiding this comment

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

i가 true라는건 0보다 클 때를 칭하는것이죠?!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

넵 0은 falsy니까 명시하지 않고 i만 적었습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

리뷰남겨 주셔서 감사합니다!!!!

continue;
}

let left = i + 1;
let right = nums.length - 1;

while (left < right) {
const sum = nums[i] + nums[left] + nums[right];

if (sum === 0) {
answer.push([nums[i], nums[left], nums[right]]);
while (left < right && nums[left] === nums[left + 1]) {
left++;
}
while (left < right && nums[right] === nums[right - 1]) {
right--;
}
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}

return answer;
}

const nums = [-1, 0, 1, 2, -1, -4];
threeSum(nums);

Copy link
Contributor

Choose a reason for hiding this comment

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

저는 set으로 해서 연산이 오래걸린던데 투포인터로 변경해봐야겠네요! 👍

45 changes: 45 additions & 0 deletions climbing-stairs/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 1 or 2 스텝 가능
* @param n - 꼭대기
* @returns - 꼭대기 까지 도달할 수 있는 방법 수
* @description
* - 결국 패턴은 피보나치
* - 시간 복잡도 O(n)
* - 공간 복잡도 O(1)
*/

// function climbStairs(n: number): number {
// if (n <= 2) {
// return n;
// }

// const dp = Array.from({ length: n + 1 }, () => 0);
// dp[1] = 1;
// dp[2] = 2;

// for (let i = 3; i <= n; i++) {
// dp[i] = dp[i - 1] + dp[i - 2];
// }
// return dp[n];
// }

function climbStairs(n: number): number {
if (n <= 2) {
return n;
}

let prevTwo = 1;
let prevOne = 2;

for (let i = 2; i <= n; i++) {
const current = prevTwo + prevOne;
prevTwo = prevOne;
prevOne = current;
}

return prevOne;
}

const n = 3;
climbStairs(n);

36 changes: 36 additions & 0 deletions product-of-array-except-self/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @param nums - 정수 배열
* @returns - nums[i]를 제외한 요소들의 곱셈 값 배열
* @description
* - 나누기를 통한 풀이는 막힘
* - 왼쪽, 오른쪽으로 순회하며 곱을 누적하기
* - 첫번째 반복 - 처음 값은 자신을 제외한 누적의 전체이므로 1, 이후 부터 누적
* - 두번째 반복 - 마지막 값 또한 자신을 제외한 첫번째 반복의 누적, 이후 부터 미리 누적한 값과 자신을 곱
*
* @description
* - 시간 복잡도 O(n)
* - 공간 복잡도 O(n)
* - 해당 방법을 떠올리지 못해 AI의 도움을 받았는데 수학적 사고가 더 필요하다.
*/
function productExceptSelf(nums: number[]): number[] {
let prev = 1;
let next = 1;
const arr: number[] = [];

for (let i = 0; i < nums.length; i++) {
arr.push(prev);
prev = prev * nums[i];
}

for (let j = nums.length - 1; j >= 0; j--) {
arr[j] = arr[j] * next;
next = next * nums[j];
}

return arr;
}

const nums = [1, 2, 3, 4];
productExceptSelf(nums);


47 changes: 47 additions & 0 deletions valid-anagram/Blossssom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @param s 문자열 1
* @param t 문자열 2
* @returns 두 문자열의 나열을 바꿔 동일한 문자열이 나올 수 있는지 반환
* @description
* - 1. 시간 복잡도: O(n), 공간 복잡도 O(1)
* - 2. 시간, 공간 복잡도는 같지만 1번 보다는 빠름 -> 동시처리,
*/

// function isAnagram(s: string, t: string): boolean {
// if(s.length !== t.length) {
// return false;
// }
// const sMap = new Map();
// for(let i = 0; i < s.length; i++) {
// sMap.has(s[i]) ? sMap.set(s[i], sMap.get(s[i]) + 1) : sMap.set(s[i], 1);
// sMap.has(t[i]) ? sMap.set(t[i], sMap.get(t[i]) - 1) : sMap.set(t[i], -1);
// }

// for(const v of sMap.values()) {
// if(v) {
// return false;
// }
// }
// return true;
// }

function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;

const hash: Record<string, number> = {};

for (const letter of s) {
hash[letter] = (hash[letter] || 0) + 1;
}

for (const letter of t) {
if (hash[letter] > 0) {
hash[letter] = hash[letter] - 1;
} else {
return false;
}
}

return true;
}