Skip to content

[moonhyeok] Week 2 #760

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 3 commits into from
Dec 22, 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
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();
}
Loading