Skip to content

Commit 5a23c83

Browse files
authored
Merge pull request #724 from Jeehay28/main
[Jeehay Park(박지혜)] Week 2
2 parents 9c2b45f + a69b246 commit 5a23c83

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

3sum/Jeehay28.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
7+
// Time Complexity: O(n^2)
8+
// Space Complexity: O(n^2)
9+
10+
var threeSum = function (nums) {
11+
12+
const sorted = [...nums].sort((a, b) => a - b);
13+
14+
let result = [];
15+
16+
// Loop through the array and pick each number as the first number for the triplet
17+
for (let i = 0; i < sorted.length - 2; i++) {
18+
19+
// skip duplicate values for sorted[middle]
20+
if(i > 0 && sorted[i - 1] === sorted[i]) {
21+
continue;
22+
}
23+
24+
let left = i + 1; // Left pointer starts right after the current middle
25+
let right = sorted.length - 1; // Right pointer starts at the last element
26+
27+
while (left < right) {
28+
const sum = sorted[i] + sorted[left] + sorted[right];
29+
30+
if (sum === 0) {
31+
result.push([sorted[left], sorted[i], sorted[right]]);
32+
33+
// skip duplicates for sorted[left] and sorted[right]
34+
while(left < right && sorted[left] === sorted[left + 1]){
35+
left += 1; // Move left pointer to the right to skip duplicate values
36+
}
37+
38+
while(left < right && sorted[right] === sorted[right - 1]) {
39+
right -= 1; // Move right pointer to the left to skip duplicate values
40+
}
41+
42+
left += 1;
43+
right -= 1;
44+
45+
} else if (sum > 0) {
46+
right -= 1;
47+
48+
} else {
49+
left += 1
50+
}
51+
}
52+
}
53+
54+
return result;
55+
56+
};
57+
58+
59+
// var threeSum = function (nums) {
60+
61+
// i != j, i != k, and j != k can be interpreted as i < j < k
62+
63+
// three nested loops
64+
// time complexity of O(n³)
65+
// Time Limit Exceeded
66+
67+
// let result = [];
68+
69+
// for (let i = 0; i < nums.length - 2; i++) {
70+
// for (let j = i + 1; j < nums.length - 1; j++) {
71+
// for (let k = j + 1; k < nums.length; k++) {
72+
// if (nums[i] + nums[j] + nums[k] === 0) {
73+
// const str = [nums[i], nums[j], nums[k]].sort((a, b) => a - b).join(",")
74+
// result.push(str)
75+
// }
76+
// }
77+
// }
78+
79+
// }
80+
81+
// result = [... new Set(result)].map(str => str.split(",").map(str => +str))
82+
83+
// return result;
84+
85+
// }
86+

climbing-stairs/Jeehay28.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
6+
// Time Complexity: O(n^2)
7+
// First O(n): From the loop that runs up to n times.
8+
// Second O(n): From the factorial calculations in each iteration.
9+
// overall time complexity is O(n^2).
10+
11+
// Space Complexity: O(n)
12+
// the factorial function's recursion has a space complexity of O(n)
13+
// O(1) means constant space complexity. It implies that the amount of memory used does not grow with the size of the input.
14+
// The other variables in the function only use a constant amount of space, resulting in an O(1) space usage.
15+
16+
var climbStairs = function (n) {
17+
let ways = 0;
18+
19+
let maxStepTwo = Math.floor(n / 2);
20+
21+
const factorial = (num) => {
22+
if (num === 0 || num === 1) {
23+
return 1;
24+
}
25+
26+
for (let i = 2; i <= num; i++) {
27+
return num * factorial(num - 1);
28+
}
29+
};
30+
31+
for (let i = 0; i <= maxStepTwo; i++) {
32+
const stepTwo = i;
33+
const stepOne = n - 2 * i;
34+
const steps = stepTwo + stepOne;
35+
36+
ways += factorial(steps) / (factorial(stepTwo) * factorial(stepOne));
37+
}
38+
39+
return ways;
40+
};
41+

valid-anagram/Jeehay28.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
// 시간 복잡도: O(n)
8+
// 공간 복잡도: O(n)
9+
10+
var isAnagram = function (s, t) {
11+
12+
if (s.length !== t.length) {
13+
return false;
14+
}
15+
16+
let obj = {};
17+
18+
for (let k of s) {
19+
obj[k] = (obj[k] || 0) + 1;
20+
21+
}
22+
23+
for (let k of t) {
24+
if (obj[k] === undefined || obj[k] === 0) {
25+
return false;
26+
}
27+
obj[k]--;
28+
}
29+
30+
return true;
31+
32+
};
33+
34+
// 시간 복잡도: O(n log n)
35+
// 공간 복잡도: O(n)
36+
37+
// var isAnagram = function (s, t) {
38+
39+
// if (s.length !== t.length) {
40+
// return false;
41+
// }
42+
43+
// let sArr = s.split("").sort();
44+
// let tArr = t.split("").sort();
45+
46+
// for (let i = 0; i < sArr.length; i++) {
47+
// if (sArr[i] !== tArr[i]) {
48+
// return false;
49+
// }
50+
// }
51+
52+
// return true;
53+
54+
// };
55+

0 commit comments

Comments
 (0)