LeetCode刷题 Day35

171 阅读3分钟

LeetCode刷题 Day35

860. Lemonade Change

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 do not have any change in hand at first.

Given an integer array bills where bills[i] is the bill the ith customer pays, return true if you can provide every customer with the correct change, or false otherwise.

Example 1:

Input: bills = [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.

Example 2:

Input: bills = [5,5,10,10,20]
Output: false
Explanation: 
From the first two customers in order, we collect two $5 bills.
For the next two customers in order, we collect a $10 bill and give back a $5 bill.
For the last customer, we can not give the change of $15 back because we only have two $10 bills.
Since not every customer received the correct change, the answer is false.

思路:

  • 这道题是很直接的计算5,10,20个数,当遇到10,20的时候要看手中的货币组合是否满足找零条件 10: 5 >= 1 20: 5 >= 3 || 10 >= 1 && 5 >= 1

代码:

var lemonadeChange = function(bills) {
    if (bills[0] !== 5) return false;
    let fiveSum = 0;
    let tenSum = 0;
    let fifteenSum = 0;
    for (let bill of bills) {
        if (bill === 5) {
            fiveSum++;
        } else if (bill === 10) {
            if (fiveSum >= 1) {
                fiveSum--;
                tenSum++;
            }
            else return false;
        } else {
            if (fiveSum >= 1 && tenSum >= 1) {
                fiveSum--;
                tenSum--;
            } else if (fiveSum >= 3) {
                fiveSum -= 3;
            } else {
                return false;
            }
        }
    }

    return true;
};

406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each people[i] = [hi, ki] represents the ith person of height hi with exactly ki other people in front who have a height greater than or equal to hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where queue[j] = [hj, kj] is the attributes of the jth person in the queue (queue[0] is the person at the front of the queue).

Example 1:

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.

Example 2:

Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]

思路: 需要两步

  • 第一步按照由高到低的顺序排列,当高度相同的时候,k小的在前面
  • init一个空数组,将k值作为index插入的数组中

代码:

var reconstructQueue = function(people) {
    const compare = ([h1, k1], [h2, k2]) => {
        if (h1 === h2) return k1 - k2;
        return h2 - h1;
    }

    people.sort(compare);

    let res = [];
   for (let item of people) {
        const pos = item[1];
        
        res.splice(item, 0, pos);
    }

    return res;
};

时间复杂度: O(n2), 空间复杂度: O(n)


452. Minimum Number of Arrows to Burst Balloons

There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.

Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.

Given the array points, return the minimum number of arrows that must be shot to burst all balloons.

Example 1:

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
- Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].

Example 2:

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4
Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.

Example 3:

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation: The balloons can be burst by 2 arrows:
- Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
- Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].

思路:

  • 初始化的次数为一, 因为至少要发射1次
  • 排序
  • 当相邻的数组arr[i - 1][1] < arr[i]][0]时, res++
  • 不断取最小值更新当前右边界,通过 arr[i][1] = Math.min(arr[i][1], arr[i - 1][1])

代码:

var findMinArrowShots = function(points) {
    points.sort((a, b) => a[1] - b[1]);
    let times = 1;
    for (let i = 1; i < points.length; i++) {
        if (points[i][0] > points[i - 1][1]) {
            times++;
        } else {
            points[i][1] = Math.min(points[i][1], points[i - 1][1]);
        }
    }

    return times;
};

时间复杂度: O(nlogn) 空间复杂度: O(1)