Skip to content

Commit

Permalink
Merge pull request #760 from mike2ox/main
Browse files Browse the repository at this point in the history
[moonhyeok] Week 2
  • Loading branch information
SamTheKorean authored Dec 22, 2024
2 parents b98effe + 5823714 commit 4c8b68f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 3sum/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function threeSum(nums: number[]): number[][] {
if (nums.length < 3) return [];
const result: number[][] = [];
const checked = new Set<string>();
const numMap = new Map<number, number>();

// 중복 결과 방지
nums.sort((a, b) => a - b);
// Map에 모든 값과 인덱스 저장
nums.forEach((num, index) => numMap.set(num, index));

for (let i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) break; // 양수면 존재 X
// 중복된 첫 번째 수 건너뛰기
if (i > 0 && nums[i] === nums[i - 1]) continue;

for (let j = i + 1; j < nums.length - 1; j++) {
// 중복된 두 번째 수 건너뛰기
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
// 세 번째 수 계산
const target = -(nums[i] + nums[j]);

// Map을 사용하여 세 번째 수 검색
if (numMap.has(target)) {
const k = numMap.get(target)!;
if (k > j) {
// k가 j보다 커야 중복 방지
const triplet = [nums[i], nums[j], nums[k]];
const key = triplet.join(",");

// Set을 사용하여 중복 결과 방지
if (!checked.has(key)) {
checked.add(key);
result.push(triplet);
}
}
}
}
}

return result;
}
12 changes: 12 additions & 0 deletions climbing-stairs/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function climbStairs(n: number): number {
let result = 0;
let step1 = 1;
let step2 = 0;

for (let i = 0; i < n; i++) {
result = step1 + step2;
step2 = step1;
step1 = result;
}
return result;
}
4 changes: 4 additions & 0 deletions valid-anagram/mike2ox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false;
return s.split("").sort().join() === t.split("").sort().join();
}

0 comments on commit 4c8b68f

Please sign in to comment.