Cat A
.Cat B
.Mouse C
as the two cats fight and mouse escapes.function catAndMouse(x, y, z) {
const distanceOfA = Math.abs(z - x);
const distanceOfB = Math.abs(z - y);
if (distanceOfA < distanceOfB) {
return 'Cat A';
} else if (distanceOfA > distanceOfB) {
return 'Cat B';
} else {
return 'Mouse C';
}
}
-1
.b = 60
keyboards = [40, 50, 60]
drives = [5, 8, 12]
function getMoneySpent(keyboards, drives, b) {
let costCases = [];
for (let i = keyboards.length - 1; i >= 0; i--) {
let keyboardCount = keyboards[i];
let restBudget = b - keyboardCount;
if (restBudget <= 0) continue;
let buyableDrives = drives.filter(drive => drive <= restBudget).sort((a,b) => b - a);
if (buyableDrives.length > 0) {
const cost = keyboardCount + buyableDrives[0];
costCases.push(cost);
continue;
}
}
if (costCases.length === 0) return -1;
costCases.sort((a,b) => b - a);
return costCases[0];
}
You are given two strings of the same length s
and t
. In one step you can choose any character of t and replace it with another character.
Return the minimum number of steps to make t
an anagram of s
.
An Anagram of a string is a string that contains the same characters with a different (or the same) ordering.
Example
var minSteps = function(s, t) {
let replacedCount = 0;
for (let i = 0; i < t.length; i++) {
const strT = t[i];
const indexS = s.indexOf(strT);
if (indexS !== -1) {
s = s.replace(strT, '');
} else {
replacedCount++;
}
}
return replacedCount;
};
You are given the root
of a full binary tree with the following properties
0
or 1
, where 0 represents False
and 1 represents True
.2
or 3
, where 2 represents the boolean OR
and 3 represents the boolean AND
The evaluation of a node is as follows
True
or False
.Return the boolean result of evaluating the root
node.
0
or 2
children.Example
/**
* 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 {boolean}
*/
var evaluateTree = function(root) {
if (!root.left && !root.right) {
return root.val ? true : false;
}
let left = evaluateTree(root.left);
let right = evaluateTree(root.right);
return root.val === 2 ? left || right : left && right;
};
root
node of a binary search tree and two integers low
and high
, return the sum of values of all nodes with a value in the inclusive range [low, high]
./**
* 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
* @param {number} low
* @param {number} high
* @return {number}
*/
var rangeSumBST = function(root, low, high) {
let result = 0;
function traverse(node){
if (node.left) traverse(node.left);
if (node.val >= low && node.val <= high) result += node.val;
if (node.right) traverse(node.right);
}
traverse(root);
return result;
};
root
of a binary search tree (BST) and an integer val
.val
and return the subtree rooted with that node. If such a node does not exist, return null
./**
* 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
* @param {number} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
let queue = [root];
let result = null;
while(queue.length) {
let node = queue.shift();
if (node.val === val) {
result = node;
break;
}
if (node.left) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
return result;
};