Skip to content

Commit df078a5

Browse files
authored
Merge pull request #316 from naringst/main
[나리] WEEK 01 Solutions
2 parents f08dc73 + 2fe91c1 commit df078a5

File tree

4 files changed

+122
-0
lines changed

4 files changed

+122
-0
lines changed

contains-duplicate/naringst.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {boolean}
4+
*/
5+
6+
/**
7+
* Runtime: 89ms, Memory: 63MB
8+
*
9+
* Time complexity: O(n)
10+
* - To find the length of an array: O(n)
11+
* - Array to Set: O(n)
12+
* - To find the size of a set: O(n)
13+
* Space complexity: O(n)
14+
* - arrToSet: O(n)
15+
*
16+
* **/
17+
18+
var containsDuplicate = function (nums) {
19+
const arrLength = nums.length;
20+
const arrToSet = new Set(nums);
21+
const setLength = arrToSet.size;
22+
23+
return arrLength !== setLength;
24+
};

number-of-1-bits/naringst.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
6+
/**
7+
* Runtime: 59ms, Memory: 50.76MB
8+
*
9+
* Time complexity: O(logN)
10+
* -> While
11+
* Space complexity: O(logN)
12+
* -> To save binary.split()
13+
*
14+
* **/
15+
16+
var hammingWeight = function (n) {
17+
let binary = "";
18+
19+
while (n > 1) {
20+
let left = n % 2;
21+
binary += left.toString();
22+
n = Math.floor(n / 2);
23+
}
24+
binary = binary + n.toString();
25+
26+
const count = binary.split("1").length - 1;
27+
28+
return count;
29+
};

palindromic-substrings/naringst.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
6+
/**
7+
* Runtime: 1521ms, Memory: 56.61MB
8+
*
9+
* Time complexity: O(N^2)
10+
* Space complexity: O(N^2)
11+
*
12+
* Note: necessary to think of an alternative approach
13+
* **/
14+
15+
function isPalindrome(subString) {
16+
const len = subString.length;
17+
for (let i = 0; i < len / 2; i++) {
18+
if (subString[i] !== subString[len - 1 - i]) {
19+
return false;
20+
}
21+
return true;
22+
}
23+
}
24+
25+
var countSubstrings = function (s) {
26+
const n = s.length;
27+
let answer = n;
28+
29+
for (let i = 0; i < n; i++) {
30+
for (let j = i + 1; j < n; j++) {
31+
let subString = s.slice(i, j + 1);
32+
if (isPalindrome(subString)) {
33+
answer += 1;
34+
}
35+
}
36+
}
37+
38+
return answer;
39+
};

top-k-frequent-elements/naringst.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number[]}
5+
*/
6+
7+
/**
8+
* Runtime: 64ms, Memory: 53.31MB
9+
*
10+
* Time complexity: O(NlogN)
11+
* - frquentEntries.sort: NlogN
12+
* Space complexity: O(n)
13+
*
14+
* **/
15+
16+
var topKFrequent = function (nums, k) {
17+
let answer = [];
18+
19+
const frequent = {};
20+
for (const num of nums) {
21+
frequent[num] = frequent[num] ? frequent[num] + 1 : 1;
22+
}
23+
24+
const frequentEntries = Object.entries(frequent);
25+
frequentEntries.sort((a, b) => b[1] - a[1]);
26+
27+
const topK = frequentEntries.slice(0, k).map((i) => i[0]);
28+
29+
return topK;
30+
};

0 commit comments

Comments
 (0)