Skip to content

Commit 4180ac1

Browse files
authored
Merge pull request DaleStudy#80 from nhistory/week3
[Sehwan] Week3 solutions with javascript
2 parents 14c0fa3 + 481418b commit 4180ac1

File tree

5 files changed

+161
-0
lines changed

5 files changed

+161
-0
lines changed

climbing-stairs/nhistory.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var climbStairs = function (n) {
6+
// Make an array to store each number of ways
7+
let steps = new Array(n);
8+
// When stairs is 1 and 2 has exact number 1 and 2
9+
steps[1] = 1;
10+
steps[2] = 2;
11+
// Iterate to get ways of 3 more steps stairs
12+
// ((n-1) + (n-2))
13+
for (let i = 3; i <= n; i++) {
14+
steps[i] = steps[i - 1] + steps[i - 2];
15+
}
16+
return steps[n];
17+
};
18+
19+
// TC: O(n)
20+
// SC: O(n)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function (root) {
14+
// Check root is null or not
15+
if (root === null) {
16+
return 0;
17+
}
18+
// Make queue array to store root and depth variable
19+
let queue = [root];
20+
let depth = 0;
21+
// Iterate until there is an element inside of queue
22+
while (queue.length > 0) {
23+
// Record level size to avoid fault size measuring
24+
let levelSize = queue.length;
25+
26+
for (let i = 0; i < levelSize; i++) {
27+
// Grasp first element from the queue
28+
const node = queue.shift();
29+
// Push the left node into the queue
30+
if (node.left) {
31+
queue.push(node.left);
32+
}
33+
// Push the right node into the queue
34+
if (node.right) {
35+
queue.push(node.right);
36+
}
37+
}
38+
// Increase depth value
39+
depth++;
40+
}
41+
42+
return depth;
43+
};
44+
45+
// TC: O(n)
46+
// SC: O(n)

meeting-rooms/nhistory.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition of Interval:
3+
* class Interval {
4+
* constructor(start, end) {
5+
* this.start = start;
6+
* this.end = end;
7+
* }
8+
* }
9+
*/
10+
11+
class Solution {
12+
/**
13+
* @param {Interval[]} intervals
14+
* @returns {boolean}
15+
*/
16+
canAttendMeetings(intervals) {
17+
// Sort the intervals based on their start times
18+
intervals.sort((a, b) => a.start - b.start);
19+
20+
for (let i = 0; i < intervals.length - 1; i++) {
21+
// Check if the current interval overlaps with the next interval
22+
if (intervals[i].end > intervals[i + 1].start) {
23+
return false;
24+
}
25+
}
26+
27+
return true;
28+
}
29+
}
30+
31+
// TC: O(nlogn)
32+
// SC: O(1)

same-tree/nhistory.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} p
11+
* @param {TreeNode} q
12+
* @return {boolean}
13+
*/
14+
var isSameTree = function (p, q) {
15+
// If p and q is null, return true
16+
if (!p && !q) return true;
17+
// Compare root and length between p and q
18+
if (!p || !q || p.val !== q.val) return false;
19+
// Execute recursive function to search each tree
20+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
21+
};
22+
23+
// TC: O(n)
24+
// SC: O(n)

subtree-of-another-tree/nhistory.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @param {TreeNode} subRoot
12+
* @return {boolean}
13+
*/
14+
var isSubtree = function (root, subRoot) {
15+
// Make function to check two triangle is same or not
16+
const isSame = (root1, root2) => {
17+
// If both of root1 and root2 is null, return true
18+
if (!root1 && !root2) return true;
19+
// If one of root1 and root2 is null or root1.val is not equal to root2.val, return false
20+
if (!root1 || !root2 || root1.val !== root2.val) return false;
21+
// Compare each left and right value with recursive
22+
return isSame(root1.left, root2.left) && isSame(root1.right, root2.right);
23+
};
24+
25+
// Make dfs function to check nodes inside of root tree
26+
const dfs = (node) => {
27+
// if node is null, return false
28+
if (!node) return false;
29+
// Check triangle is equal to subRoot
30+
if (isSame(node, subRoot)) return true;
31+
// Check one of the left or right node is same with triangle
32+
return dfs(node.left) || dfs(node.right);
33+
};
34+
// Execute dfs function
35+
return dfs(root);
36+
};
37+
38+
// TC: O(n*m)
39+
// SC: O(max(m,n))

0 commit comments

Comments
 (0)