Skip to content

Commit e8b91bd

Browse files
2 parents df7a5b4 + 9f4a684 commit e8b91bd

File tree

4 files changed

+122
-3
lines changed

4 files changed

+122
-3
lines changed

concepts/Promises.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,11 @@ const delay = () => {
6060
});
6161
}
6262

63-
delay().then(sayHello);
64-
6563
const sayHello = (value) => {
6664
console.log('Hello');
6765
}
66+
67+
delay().then(sayHello);
6868
```
6969

7070
> This challenge we'll chain promises together using ".then" Create two variables: firstPromise and secondPromise Set secondPromise to be a promise that resolves to "Second!" Set firstPromise to be a promise that resolves to secondPromise Call the firstPromise with a ".then", which will return the secondPromise> promise. Then print the contents of the promise after it has been resolved by passing console.log to .then
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
3+
4+
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters
5+
6+
Example:
7+
8+
Input: "23"
9+
Output: ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].
10+
*/
11+
12+
let output = [];
13+
const phoneNumberMap = new Map();
14+
phoneNumberMap.set("2", "abc");
15+
phoneNumberMap.set("3", "def");
16+
phoneNumberMap.set("4", "ghi");
17+
phoneNumberMap.set("5", "jkl");
18+
phoneNumberMap.set("6", "mno");
19+
phoneNumberMap.set("7", "pqrs");
20+
phoneNumberMap.set("8", "tuv");
21+
phoneNumberMap.set("9", "wxyz");
22+
23+
const backtrack = (combination, nextDigits) => {
24+
if (nextDigits.length === 0) {
25+
output.push(combination);
26+
} else {
27+
let digit = nextDigits.substring(0, 1);
28+
let letters = phoneNumberMap.get(digit);
29+
30+
for (let i = 0; i < letters.length; i++) {
31+
let letter = phoneNumberMap.get(digit).substring(i, i+1);
32+
backtrack(combination + letter, nextDigits.substring(1));
33+
}
34+
}
35+
}
36+
37+
const letterCombinations = digits => {
38+
output = [];
39+
if (digits.length !== 0) {
40+
backtrack("", digits);
41+
}
42+
43+
return output;
44+
}
45+
46+
/**
47+
* Time Complexity: 3 ^ N x 4 ^ M
48+
* Space Complexity: 3 ^ N x 4 ^ M
49+
*/

exercises/leetcode/populateNextRightPointer-II.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,9 @@ const connect = root => {
6262
}
6363
}
6464
return root;
65-
}
65+
}
66+
67+
/**
68+
* Time Complexity: O(N)
69+
* Space Complexity: O(1)
70+
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* You are given a perfect binary tree where all leaves are on the same level, and every parent has two children.
3+
* The binary tree has the following definition:
4+
5+
struct Node {
6+
int val;
7+
Node *left;
8+
Node *right;
9+
Node *next;
10+
}
11+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
12+
13+
Initially, all next pointers are set to NULL.
14+
*/
15+
16+
class Queue {
17+
constructor() {
18+
this.data = [];
19+
}
20+
add(val) {
21+
this.data.push(val);
22+
}
23+
remove() {
24+
return this.data.shift();
25+
}
26+
peek() {
27+
return this.data[0];
28+
}
29+
size() {
30+
this.data.length;
31+
}
32+
}
33+
34+
const connect = root => {
35+
if (root === null) {
36+
return root;
37+
}
38+
39+
// create a queue to push nodes from each level
40+
const queue = new Queue();
41+
42+
queue.add(root);
43+
while (queue.size() > 0) {
44+
// we want to do BFS so we take the size of each level
45+
let size = queue.size();
46+
47+
for (let i = 0; i < size; i++) {
48+
let node = queue.remove();
49+
50+
if (i < size - 1) {
51+
node.next = queue.peek();
52+
}
53+
54+
if (node.left !== null) {
55+
queue.add(node.left);
56+
}
57+
58+
if (node.right !== null) {
59+
queue.add(node.right);
60+
}
61+
}
62+
}
63+
64+
return root;
65+
}

0 commit comments

Comments
 (0)