Skip to content

Commit 55c004d

Browse files
authored
Merge pull request DaleStudy#741 from taewanseoul/main
2 parents 82d87ff + e0b014b commit 55c004d

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

3sum/taewanseoul.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 15. 3Sum
3+
* 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.
4+
* Notice that the solution set must not contain duplicate triplets.
5+
*
6+
* https://leetcode.com/problems/3sum/description/
7+
*/
8+
function threeSum(nums: number[]): number[][] {
9+
nums.sort((a, b) => a - b);
10+
const triplets: number[][] = [];
11+
12+
for (let i = 0; i < nums.length - 2; i++) {
13+
if (nums[i] > 0 || nums[i] === nums[i - 1]) {
14+
continue;
15+
}
16+
17+
let low = i + 1;
18+
let high = nums.length - 1;
19+
20+
while (low < high) {
21+
const sum = nums[i] + nums[low] + nums[high];
22+
if (sum < 0) {
23+
low++;
24+
} else if (sum > 0) {
25+
high--;
26+
} else {
27+
triplets.push([nums[i], nums[low], nums[high]]);
28+
29+
while (low < high && nums[low] === nums[low + 1]) {
30+
low++;
31+
}
32+
while (low < high && nums[high] === nums[high - 1]) {
33+
high--;
34+
}
35+
low++;
36+
high--;
37+
}
38+
}
39+
}
40+
41+
return triplets;
42+
}
43+
44+
// O(n^2) time
45+
// O(n) space

climbing-stairs/taewanseoul.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* 70. Climbing Stairs
3+
* You are climbing a staircase. It takes n steps to reach the top.
4+
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
5+
*
6+
* https://leetcode.com/problems/climbing-stairs/description/
7+
*/
8+
function climbStairs(n: number): number {
9+
if (n <= 2) {
10+
return n;
11+
}
12+
13+
let prev = 1;
14+
let cur = 2;
15+
16+
for (let i = 3; i < n + 1; i++) {
17+
const next = prev + cur;
18+
prev = cur;
19+
cur = next;
20+
}
21+
22+
return cur;
23+
}
24+
25+
// O(n) time
26+
// O(1) space

valid-anagram/taewanseoul.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* 242. Valid Anagram
3+
* Given two strings s and t, return true if t is an anagram of s, and false otherwise.
4+
*
5+
* https://leetcode.com/problems/valid-anagram/description/
6+
*/
7+
function isAnagram(s: string, t: string): boolean {
8+
if (s.length !== t.length) {
9+
return false;
10+
}
11+
12+
const charMap = new Map<string, number>();
13+
14+
for (const char of s) {
15+
const count = charMap.get(char);
16+
if (count) {
17+
charMap.set(char, count + 1);
18+
} else {
19+
charMap.set(char, 1);
20+
}
21+
}
22+
23+
for (const char of t) {
24+
const count = charMap.get(char);
25+
if (count) {
26+
charMap.set(char, count - 1);
27+
} else {
28+
return false;
29+
}
30+
}
31+
32+
return true;
33+
}
34+
35+
// O(n) time
36+
// O(n) space

0 commit comments

Comments
 (0)