Skip to content

Commit

Permalink
Merge pull request #1 from Sunjae95/week2
Browse files Browse the repository at this point in the history
Week2
  • Loading branch information
Sunjae95 authored Aug 23, 2024
2 parents 81d4e12 + 81aefb5 commit 84b3daf
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @description
* time complexity: O(n^2)
* space complexity: O(n)
*
* brainstorming:
* stack, Drawing a graph
*
* strategy:
* discover the rules
* leftStack = left create , rightStack = right create
*/
var buildTree = function (preorder, inorder) {
let answer = null;
let pointer = 0;

const leftStack = [];
const rightStack = [];

preorder.forEach((val, i) => {
const node = new TreeNode(val);

if (i === 0) answer = node;

const leftLen = leftStack.length;
const rightLen = rightStack.length;

if (leftLen && rightLen) {
if (leftStack[leftLen - 1].left) rightStack[rightLen - 1].right = node;
else leftStack[leftLen - 1].left = node;
}
if (leftLen && !rightLen) leftStack[leftLen - 1].left = node;
if (!leftLen && rightLen) rightStack[rightLen - 1].right = node;

leftStack.push(node);

while (leftStack.length && pointer < inorder.length) {
if (leftStack[leftStack.length - 1].val !== inorder[pointer]) break;
rightStack.push(leftStack.pop());
pointer++;
}
});

return answer;
};
24 changes: 24 additions & 0 deletions counting-bits/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @description
* time complexity: O(n log n)
* space complexity: O(N)
*
* brainstorming:
* convert integer to bit
* for loop
*
* strategy:
* string change to hash table
*/
var countBits = function (n) {
return Array.from({ length: n + 1 }, (_, i) => convertBitCount(i));
};

const convertBitCount = (n) => {
let count = 0;
while (n > 0) {
count += n % 2;
n = Math.floor(n / 2);
}
return count;
};
76 changes: 76 additions & 0 deletions decode-ways/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @description
* brainstorming:
* 1. dp -> dp[i] = dp[i-1] + count
* 2. recursive function
*
* strategy:
* https://www.algodale.com/problems/decode-ways/
*
* result:
* 1. couldn't think of the conditions
* true: 1~9, 10~27
* false: 0, 0N, 28↑
* 2. persist solution that is top down
*/

// https://www.algodale.com/problems/decode-ways/ Solve 1
/**
* time complexity: O(2^n)
* space complexity: O(n)
*/
var numDecodings = function (s) {
const search = (start) => {
if (start === s.length) return 1;
if (s[start] === "0") return 0;
if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27) {
return search(start + 1) + search(start + 2);
}
return search(start + 1);
};

return search(0);
};

// https://www.algodale.com/problems/decode-ways/ Solve 2
/**
* time complexity: O(2^n)
* space complexity: O(n)
*/
var numDecodings = function (s) {
const memo = new Map();
memo.set(s.length, 1);

const search = (start) => {
if (!!memo.get(start)) return memo.get(start);

if (s[start] === "0") memo.set(start, 0);
else if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27)
memo.set(start, search(start + 1) + search(start + 2));
else memo.set(start, search(start + 1));

return memo.get(start);
};

return search(0);
};

// https://www.algodale.com/problems/decode-ways/ Solve 3
/**
* time complexity: O(n)
* space complexity: O(n)
*/
var numDecodings = function (s) {
const dp = Array.from({ length: s.length + 1 }, (_, i) =>
i === s.length ? 1 : 0
);

for (let i = s.length - 1; i >= 0; i--) {
if (s[i] === "0") dp[i] = 0;
else if (i + 1 < s.length && Number(`${s[i]}${s[i + 1]}`) < 27)
dp[i] = dp[i + 1] + dp[i + 2];
else dp[i] = dp[i + 1];
}

return dp[0];
};
74 changes: 74 additions & 0 deletions palindromic-substrings/sunjae95.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,77 @@ var countSubstrings = function (s) {

return answer.length;
};

/**
* @description
* WEEK 01 Feedback
* 1. remove slice and change to endpoint
* 2. kadane's algorithm
*
* time complexity: O(N^3)
* space complexity: O(1)
*
* result
* solution 1 apply
*/
var countSubstrings = function (s) {
let answer = 0;
const len = s.length;

for (let startIndex = 0; startIndex < len; startIndex++) {
let subStr = "";
let isSubPalindromic = true;
answer++;

for (let endIndex = startIndex + 1; endIndex < len; endIndex++) {
if (isSubPalindromic && s[startIndex] === s[endIndex]) answer++;
subStr += s[endIndex];
isSubPalindromic = isPalindromic(subStr);
}
}

return answer;
};

function isPalindromic(str) {
const len = str.length;
const middleIndex = Math.floor(len / 2);

for (let i = 0; i < middleIndex; i++) {
if (str[i] !== str[len - 1 - i]) return false;
}

return true;
}
/**
* @description
* WEEK 01 Feedback
* 1. remove slice and change to endpoint
* 2. kadane's algorithm
*
* time complexity: O(N^2)
* space complexity: O(1)
*
* result
* solve 3(https://www.algodale.com/problems/palindromic-substrings/) include 1,2 solution
*/
var countSubstrings = function (s) {
let count = 0;
const len = s.length;

for (let i = 0; i < len; i++) {
let [start, end] = [i, i];

while (s[start] === s[end] && start >= 0 && end < len) {
count++;
[start, end] = [start - 1, end + 1];
}
[start, end] = [i, i + 1];
while (s[start] === s[end] && start >= 0 && end < len) {
count++;
[start, end] = [start - 1, end + 1];
}
}

return count;
};
31 changes: 31 additions & 0 deletions valid-anagram/sunjae95.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @description
* time complexity: O(N)
* space complexity: O(N)
*
* brainstorming:
* 1. hash table value compare to count
*
* strategy:
* string change to hash table
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) return false;

let answer = true;
const tableS = convertHashTable(s);
const tableT = convertHashTable(t);

tableS.forEach((_, key) => {
if (tableT.get(key) && tableT.get(key) === tableS.get(key)) return;
answer = false;
});

return answer;
};

const convertHashTable = (str) =>
str.split("").reduce((map, s) => {
map.set(s, (map.get(s) ?? 0) + 1);
return map;
}, new Map());

0 comments on commit 84b3daf

Please sign in to comment.