Skip to content

[나리] WEEK 01 Solutions #316

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 6 commits into from
Aug 18, 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
24 changes: 24 additions & 0 deletions contains-duplicate/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @param {number[]} nums
* @return {boolean}
*/

/**
* Runtime: 89ms, Memory: 63MB
*
* Time complexity: O(n)
* - To find the length of an array: O(n)
* - Array to Set: O(n)
* - To find the size of a set: O(n)
* Space complexity: O(n)
* - arrToSet: O(n)
*
* **/

var containsDuplicate = function (nums) {
const arrLength = nums.length;
const arrToSet = new Set(nums);
const setLength = arrToSet.size;

return arrLength !== setLength;
};
29 changes: 29 additions & 0 deletions number-of-1-bits/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @param {number} n
* @return {number}
*/

/**
* Runtime: 59ms, Memory: 50.76MB
*
* Time complexity: O(logN)
* -> While
* Space complexity: O(logN)
* -> To save binary.split()
*
* **/

var hammingWeight = function (n) {
let binary = "";

while (n > 1) {
let left = n % 2;
binary += left.toString();
n = Math.floor(n / 2);
}
binary = binary + n.toString();

const count = binary.split("1").length - 1;

return count;
};
39 changes: 39 additions & 0 deletions palindromic-substrings/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @param {string} s
* @return {number}
*/

/**
* Runtime: 1521ms, Memory: 56.61MB
*
* Time complexity: O(N^2)
* Space complexity: O(N^2)
*
* Note: necessary to think of an alternative approach
* **/

function isPalindrome(subString) {
const len = subString.length;
for (let i = 0; i < len / 2; i++) {
if (subString[i] !== subString[len - 1 - i]) {
return false;
}
return true;
}
}

var countSubstrings = function (s) {
const n = s.length;
let answer = n;

for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
let subString = s.slice(i, j + 1);
if (isPalindrome(subString)) {
answer += 1;
}
}
}

return answer;
};
30 changes: 30 additions & 0 deletions top-k-frequent-elements/naringst.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/

/**
* Runtime: 64ms, Memory: 53.31MB
*
* Time complexity: O(NlogN)
* - frquentEntries.sort: NlogN
* Space complexity: O(n)
*
* **/

var topKFrequent = function (nums, k) {
let answer = [];

const frequent = {};
for (const num of nums) {
frequent[num] = frequent[num] ? frequent[num] + 1 : 1;
}

const frequentEntries = Object.entries(frequent);
frequentEntries.sort((a, b) => b[1] - a[1]);

const topK = frequentEntries.slice(0, k).map((i) => i[0]);

return topK;
};