Skip to content

Commit

Permalink
Merge pull request #734 from gwbaik9717/main
Browse files Browse the repository at this point in the history
[ganu] Week 2
  • Loading branch information
SamTheKorean authored Dec 22, 2024
2 parents cc98766 + 3168fa4 commit 8e95fdc
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
38 changes: 38 additions & 0 deletions 3sum/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time complexity: O(n^2)
// Space complexity: O(n)

/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
const sumsDict = new Set();
const sortedNums = nums.toSorted((a, b) => a - b);

const n = nums.length;

for (let i = 0; i < n - 2; i++) {
let left = i + 1;
let right = n - 1;
const fixed = sortedNums[i];

const targetSum = 0 - fixed;

while (left < right) {
const currentSum = sortedNums[left] + sortedNums[right];

if (currentSum < targetSum) {
left++;
} else if (currentSum > targetSum) {
right--;
} else {
const key = [fixed, sortedNums[left], sortedNums[right]];
sumsDict.add(key.join(","));
left++;
right--;
}
}
}

return Array.from(sumsDict).map((nums) => nums.split(",").map(Number));
};
16 changes: 16 additions & 0 deletions climbing-stairs/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Time complexity: O(n)
// Space complexity: O(1)

/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
const dp = [1, 1];

for (let i = 2; i <= n; i++) {
dp[i % 2] = dp[0] + dp[1];
}

return dp[n % 2];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Time complexity: O(n^2)
// Space complexity: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {number[]} preorder
* @param {number[]} inorder
* @return {TreeNode}
*/
var buildTree = function (preorder, inorder) {
if (!preorder.length || !inorder.length) return null;

const root = new TreeNode(preorder[0]);
const mid = inorder.indexOf(root.val);

root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));

return root;
};
38 changes: 38 additions & 0 deletions decode-ways/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {string} s
* @return {number}
*/
var numDecodings = function (s) {
const n = s.length;
const dp = Array.from({ length: n + 2 }, () => 0);
dp[1] = 1;

for (let i = 2; i < n + 2; i++) {
// 한자리
const charCode = Number(s[i - 2]);

if (charCode > 0) {
dp[i] += dp[i - 1];
}

// 두자리
if (i <= 2) {
continue;
}

if (Number(s[i - 3]) == 0) {
continue;
}

const strCode = Number(s.slice(i - 3, i - 1));

if (strCode > 0 && strCode <= 26) {
dp[i] += dp[i - 2];
}
}

return dp.at(-1);
};
39 changes: 39 additions & 0 deletions valid-anagram/gwbaik9717.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time complexity: O(n)
// Space complexity: O(n)

/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) {
return false;
}

const createDictFromString = (str) => {
const dict = new Map();

for (const chr of str) {
if (dict.has(chr)) {
dict.set(chr, dict.get(chr) + 1);
continue;
}

dict.set(chr, 1);
}

return dict;
};

const dictS = createDictFromString(s);
const dictT = createDictFromString(t);

for (const [key, value] of dictS) {
if (dictT.get(key) !== value) {
return false;
}
}

return true;
};

0 comments on commit 8e95fdc

Please sign in to comment.