Simple Problems Flashcards

1
Q

For multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]
A
/**
 * @param {number} n
 * @return {string[]}
 */
var fizzBuzz = function(n) {
    const res = [];
    const FIZZ = 'Fizz';
    const BUZZ = 'Buzz';
    for (let i = 1; i <= n; i++) {
        if (i % 3 === 0 &amp;&amp; i % 5 === 0) {
            res.push(FIZZ + BUZZ);
        } else if (i % 3 === 0) {
            res.push(FIZZ);
        } else if (i % 5 === 0) {
            res.push(BUZZ);
        } else {
            res.push(String(i));
        }
    }
return res; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word (last word means the last appearing word if we loop from left to right) in the string.

If the last word does not exist, return 0.

Note: A word is defined as a maximal substring consisting of non-space characters only.

Example:

Input: “Hello World”
Output: 5

A
/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLastWord = function(s) {
    const arr = s.split(' ').filter(word => word.length);
    const len = arr.length;
    if (len) {
        return arr[len - 1].length;
    }
    return 0;
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

You’re given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so “a” is considered a different type of stone from “A”.

Example 1:

Input: J = “aA”, S = “aAAbbbb”
Output: 3
Example 2:

Input: J = “z”, S = “ZZ”
Output: 0

A
/**
 * @param {string} J
 * @param {string} S
 * @return {number}
 */
var numJewelsInStones = function(J, S) {
    let counter = 0;
    for (let i = 0; i < S.length; i++) {
        if (J.includes(S[i])) {
            counter += 1;
        }
    }
return counter; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bottom-left corner, and (x2, y2) are the coordinates of its top-right corner.

Two rectangles overlap if the area of their intersection is positive. To be clear, two rectangles that only touch at the corner or edges do not overlap.

Given two (axis-aligned) rectangles, return whether they overlap.

Example 1:

Input: rec1 = [0,0,2,2], rec2 = [1,1,3,3]
Output: true
Example 2:

Input: rec1 = [0,0,1,1], rec2 = [1,0,2,1]
Output: false

A
/**
 * @param {number[]} rec1
 * @param {number[]} rec2
 * @return {boolean}
 */
var isRectangleOverlap = function(rec1, rec2) {
    // Минимальный из x2 - top right > Максимальный из x1
    // Минимальный из y2 - top right ? Максимальный из y2
    return Math.min(rec1[2], rec2[2]) > Math.max(rec1[0], rec2[0]) &amp;&amp;
           Math.min(rec1[3], rec2[3]) > Math.max(rec1[1], rec2[1])
};
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

At a lemonade stand, each lemonade costs $5.

Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).

Each customer will only buy one lemonade and pay with either a $5, $10, or $20 bill. You must provide the correct change to each customer, so that the net transaction is that the customer pays $5.

Note that you don’t have any change in hand at first.

Return true if and only if you can provide every customer with correct change.

Example 1:

Input: [5,5,5,10,20]
Output: true
Explanation:
From the first 3 customers, we collect three $5 bills in order.
From the fourth customer, we collect a $10 bill and give back a $5.
From the fifth customer, we give a $10 bill and a $5 bill.
Since all customers got correct change, we output true.

A
/**
 * @param {number[]} bills
 * @return {boolean}
 */
var lemonadeChange = function(bills) {
    const cashier = {
        5: 0,
        10: 0,
        20: 0,
    };
    for (let i = 0; i < bills.length; i++) {
        const bill = bills[i];
        if (bills[i] === 5) {
            cashier[5] += 1;
        } else if (bills[i] === 10) {
            if (cashier[5] > 0) {
                cashier[5]  -= 1;
                cashier[10] += 1; 
            } else {
                return false;
            }
        } else if (bills[i] === 20) {
            if (cashier[5] >= 1 &amp;&amp; cashier[10] > 0) {
                cashier[5]  -= 1;
                cashier[10] -= 1;
            } else if (cashier[5] >= 3) {
                cashier[5]  -= 3;
            } else {
                return false;
            }
        }
    }
return true; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly