Skip to content

[Wan] Week 2 #741

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 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
45 changes: 45 additions & 0 deletions 3sum/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 15. 3Sum
* Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
* Notice that the solution set must not contain duplicate triplets.
*
* https://leetcode.com/problems/3sum/description/
*/
function threeSum(nums: number[]): number[][] {
nums.sort((a, b) => a - b);
const triplets: number[][] = [];

for (let i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0 || nums[i] === nums[i - 1]) {
continue;
}

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

while (low < high) {
const sum = nums[i] + nums[low] + nums[high];
if (sum < 0) {
low++;
} else if (sum > 0) {
high--;
} else {
triplets.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--;
}
low++;
high--;
}
}
}

return triplets;
}

// O(n^2) time
// O(n) space
26 changes: 26 additions & 0 deletions climbing-stairs/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 70. Climbing Stairs
* You are climbing a staircase. It takes n steps to reach the top.
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
*
* https://leetcode.com/problems/climbing-stairs/description/
*/
function climbStairs(n: number): number {
if (n <= 2) {
return n;
}

let prev = 1;
let cur = 2;

for (let i = 3; i < n + 1; i++) {
Comment on lines +9 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

n을 기준으로 prev는 1, cur는 2로 하신 것 같습니다. (다른 풀이나 chatgpt도 위와 같았던 것으로 기억합니다.)

결과는 사실 상 같으나 약간 생각을 해봤을 때 n을 1부터 보는게 아니라 0부터 봐서
prev 1, cur 1로 설정하여 for문을 2 ~ n 까지 구할 수 있는 방법으로도 생각할 수 있을 것 같습니다 :)

let prev = 1, cur = 1;
for (let i = 2; i <= n; i++) {
    const next = cur;
    cur = prev + cur;
    prev = next;
}

return cur;

const next = prev + cur;
prev = cur;
cur = next;
}

return cur;
}

// O(n) time
// O(1) space
36 changes: 36 additions & 0 deletions valid-anagram/taewanseoul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 242. Valid Anagram
* Given two strings s and t, return true if t is an anagram of s, and false otherwise.
*
* https://leetcode.com/problems/valid-anagram/description/
*/
function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) {
return false;
}

const charMap = new Map<string, number>();

for (const char of s) {
const count = charMap.get(char);
if (count) {
charMap.set(char, count + 1);
} else {
charMap.set(char, 1);
}
}

for (const char of t) {
const count = charMap.get(char);
if (count) {
charMap.set(char, count - 1);
} else {
return false;
}
}
Comment on lines +12 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

map 풀이 방법이군요 :)

저는 문자열을 정렬해서 비교하는 방법을 떠올렸는데 javascript가 코드가 훨씬 깔끔하게 나오네요.
(효율성은 떨어집니다!)

return s.split('').sort().join('') === t.split('').sort().join('');

Copy link
Contributor Author

Choose a reason for hiding this comment

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

그렇게도 풀이할 수 있겠군요!


return true;
}

// O(n) time
// O(n) space
Loading