Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 longest-consecutive-sequence/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var longestConsecutive = function (nums) { // sorting approach
if (!nums.length) return 0;
let set = new Set(nums);
let sorted = [...set].sort((a, b) => a - b);
let longestSeq = 0;
let currSeq = 1;

for (let i = 1; i < sorted.length; i++) { // loop through sorted list to find sequence
if (sorted[i - 1] + 1 === sorted[i]) {
currSeq++;
} else {
longestSeq = Math.max(longestSeq, currSeq); // compare sequence to figure out the longest
currSeq = 1;
}
}

return Math.max(longestSeq, currSeq);
};

// time - O(nlong) using sort
// space - O(n) store nums in set


// TODO - try O(n) TC approach
30 changes: 30 additions & 0 deletions missing-number/kimyoung.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여러가지 패턴으로 푸는거 좋네요 👍

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var missingNumber = function (nums) { // brute force approach
let missingNumber;
for (let i = 0; i < nums.length; i++) {
if (nums.indexOf(i) === -1) missingNumber = i;
}
return missingNumber === undefined ? nums.length : missingNumber;
};

// time - O(n^2) finding the index of i while iterating through nums
// splace - O(1) no extra space needed other than missingNumber which just stores the result

var missingNumber = function (nums) { // set approach
let set = new Set(nums);
for (let i = 0; i < nums.length + 1; i++) {
if (!set.has(i)) return i;
}
};

// time - O(n) looping through nums to find the missing number
// splace - O(n) creating a set of nums

var missingNumber = function (nums) { // mathematical approach
const len = nums.length
const expectedSum = len * (len + 1) / 2;
const actualSum = nums.reduce((acc, el) => acc += el, 0);
return expectedSum - actualSum;
};

// time - O(n) reduce method on actualSum
// space - O(1) extra space irrelevant to input
23 changes: 23 additions & 0 deletions valid-palindrome/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var isPalindrome = function (s) {
let left = 0, right = s.length - 1;
while (left < right) {
while (left < right && !isAlphaNumeric(s[left])) { // increment the left index until it's an alphanumeric character
left++;
}
while (left < right && !isAlphaNumeric(s[right])) { // decrement the right index until it's an alphanumeric character
right--;
}
if (s[left++].toLowerCase() !== s[right--].toLowerCase()) return false; // compare the two string values, if different return false;
}
return true;
};

function isAlphaNumeric(char) { // use ASCII code to findout if char is alphanumeric or not
const asciiCode = char.charCodeAt(0);
return (asciiCode >= 65 && asciiCode <= 90) ||
(asciiCode >= 97 && asciiCode <= 122) ||
(asciiCode >= 48 && asciiCode <= 57)
}

// time - O(n) iterate through the string once
// space - O(1) no extra space created