Skip to content

Commit 3780d9f

Browse files
authored
Merge pull request #306 from Sunjae95/main
[선재] Week1 문제 풀이
2 parents bd6c112 + 43c7341 commit 3780d9f

File tree

5 files changed

+196
-0
lines changed

5 files changed

+196
-0
lines changed

contains-duplicate/sunjae95.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* time complexity: O(n)
4+
* space complexity: O(n)
5+
* approach/strategy:
6+
* 1. brute force + hash table
7+
*/
8+
9+
/**
10+
* @param {number[]} nums
11+
* @return {boolean}
12+
*/
13+
var containsDuplicate = function (nums) {
14+
const hashTable = new Set();
15+
16+
for (const num of nums) {
17+
if (hashTable.has(num)) return true;
18+
hashTable.add(num);
19+
}
20+
21+
return false;
22+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @description
3+
* time complexity: O(N)
4+
* space complexity: O(N)
5+
*
6+
* brainstorming:
7+
* 1. BFS, DFS
8+
* 2. Brute force
9+
*
10+
* strategy:
11+
* inOrder search
12+
*
13+
* reason:
14+
* tree features
15+
*/
16+
var kthSmallest = function (root, k) {
17+
let answer = 0;
18+
19+
inOrder(root, (value) => {
20+
k -= 1;
21+
if (k > 0) return false;
22+
if (k === 0) answer = value;
23+
return true;
24+
});
25+
26+
return answer;
27+
};
28+
29+
function inOrder(tree, isEnd) {
30+
if (tree.left) inOrder(tree.left, isEnd);
31+
if (isEnd(tree.val)) return;
32+
if (tree.right) inOrder(tree.right, isEnd);
33+
}

number-of-1-bits/sunjae95.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @description
3+
* time complexity: O(logN)
4+
* space complexity: O(1)
5+
* approach/strategy:
6+
* 1. decimal to binary
7+
*/
8+
9+
/**
10+
* @param {number} n
11+
* @return {number}
12+
*/
13+
var hammingWeight = function (n) {
14+
let answer = 0;
15+
16+
while (n > 0) {
17+
answer += n % 2;
18+
n = Math.floor(n / 2);
19+
}
20+
21+
return answer;
22+
};

palindromic-substrings/sunjae95.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* @description
3+
* time complexity: O(N^3)
4+
* space complexity: O(N)
5+
*
6+
* brainstorming:
7+
* 1. stack, permutation
8+
* 2. Brute force
9+
*
10+
* strategy:
11+
* Brute force, calculate
12+
*
13+
* reason:
14+
* intuitive way
15+
*
16+
* @param {string} s
17+
* @return {number}
18+
*/
19+
var countSubstrings = function (s) {
20+
let answer = 0;
21+
const len = s.length;
22+
23+
for (let i = 0; i < len; i++) {
24+
for (let j = i + 1; j < len + 1; j++) {
25+
const subStr = s.slice(i, j);
26+
if (isPalindrome(subStr)) answer++;
27+
}
28+
}
29+
30+
return answer;
31+
};
32+
33+
function isPalindrome(str) {
34+
const len = str.length;
35+
const middleIndex = Math.floor(len / 2);
36+
37+
for (let i = 0; i < middleIndex; i++) {
38+
if (str[i] !== str[len - 1 - i]) return false;
39+
}
40+
41+
return true;
42+
}
43+
44+
/**
45+
* @description
46+
* time complexity: O(N^2)
47+
* space complexity: O(N^2)
48+
*
49+
* brainstorming:
50+
* 1. https://sbslc.tistory.com/56
51+
*
52+
* strategy:
53+
* dynamic programming
54+
*
55+
* reason:
56+
* to challenge dp
57+
*
58+
* @param {string} s
59+
* @return {number}
60+
*/
61+
var countSubstrings = function (s) {
62+
const answer = [];
63+
const MAX_LENGTH = s.length;
64+
const dp = Array.from({ length: MAX_LENGTH }, (_, i) =>
65+
Array.from({ length: MAX_LENGTH }, (_, j) => {
66+
if (i === j) answer.push(s[i]);
67+
return i === j;
68+
})
69+
);
70+
// Check next step ex) aa, bb, cc
71+
for (let i = 0; i < MAX_LENGTH - 1; i++) {
72+
const nextIndex = i + 1;
73+
if (s[i] === s[nextIndex]) {
74+
dp[i][nextIndex] = true;
75+
answer.push(s.slice(i, nextIndex + 1));
76+
}
77+
}
78+
// Check against values calculated in the previous step
79+
for (let len = 2; len <= MAX_LENGTH; len++) {
80+
for (let i = 0; i < MAX_LENGTH - len; i++) {
81+
const lastIndex = len + i;
82+
if (s[i] === s[lastIndex] && dp[i + 1][lastIndex - 1]) {
83+
dp[i][lastIndex] = true;
84+
answer.push(s.slice(i, lastIndex + 1));
85+
}
86+
}
87+
}
88+
89+
return answer.length;
90+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @description
3+
* time complexity: O(N logN)
4+
* space complexity: O(N)
5+
*
6+
* brainstorming:
7+
* 1. javascript sort method
8+
* 2. priority queue
9+
*
10+
* strategy:
11+
* javascript sort method
12+
*
13+
* reason:
14+
* javascript sort method is easier to implement.
15+
*/
16+
17+
var topKFrequent = function (nums, k) {
18+
const answer = [];
19+
const hashTable = new Map();
20+
21+
nums.forEach((num) => hashTable.set(num, (hashTable.get(num) ?? 0) + 1));
22+
23+
hashTable.forEach((count, number) => answer.push({ number, count }));
24+
25+
return answer
26+
.sort((a, b) => b.count - a.count)
27+
.slice(0, k)
28+
.map(({ number }) => number);
29+
};

0 commit comments

Comments
 (0)