-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1_29_2025.js
81 lines (63 loc) · 2.05 KB
/
1_29_2025.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* @param {string[]} words
* @param {string} order
* @return {boolean}
*/
const getLongestWordLength = function(words) {
let longestLength = 0;
words.forEach(word => {
if (word.length > longestLength) longestLength = word.length;
});
return longestLength;
}
const buildOrderIndicesHash = (order) => {
const letterToIndex = {};
for (let idx in order) {
let letter = order[idx];
letterToIndex[letter] = idx;
}
return letterToIndex;
}
const isAlienSorted = function(words, order) {
const longestWordLength = getLongestWordLength(words);
const letterToIndexHash = buildOrderIndicesHash(order);
let letterIdx = 0;
console.log({letterToIndexHash})
while (letterIdx < longestWordLength) {
for (let wordNum = 1; wordNum< words.length; wordNum++) {
let firstLetter = words[wordNum - 1]?.[letterIdx];
let secondLetter = words[wordNum]?.[letterIdx];
console.log(`Comparing 1:${firstLetter} to 2: ${secondLetter}`)
if (firstLetter === undefined) continue;
if (secondLetter === undefined) return false;
if (letterToIndexHash[secondLetter] < letterToIndexHash[firstLetter]) {
console.log("OUT OF ALIEN ORDER!")
return false
}
}
letterIdx++
}
return true;
};
/**
* 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 {TreeNode} root
* @return {number[]}
*/
var rightSideView = function(root) {
let rightNodeValues = [];
let nodeQueue = [root];
while(nodeQueue.length > 0) {
let currNode = nodeQueue.shift();
rightNodeValues.push(currNode.val);
if (currNode.right) nodeQueue.push(currNode.right)
}
return rightNodeValues;
};