This is the 26th day of my participation in the August Text Challenge.More challenges in August

⭐ August more text challenge day 26 ⭐, review and consolidate Java😁 with friends

Code shrimp is a sand carving and funny boy who likes listening to music, playing games and writing as well as most of his friends. The days are still very long, let’s refuel our efforts together 🌈

Welcome to follow my public account: JavaCodes, the name is with Java but the scope can be more than Java field oh 😁, will share blog posts or welfare for a long time, looking forward to your attention ❤

😉 offer yourself

Self-introduction, to everyone recommend their column 😁, welcome small partners to collect attention 😊

The small white to learn Java

MybatisPlus column

App crawler Column

PC side crawler column

Big factory interview question column

The title


Their thinking

  1. Sort an array

  2. Greed + double pointer


Case on

Take case 3 for example

Sort the array by:3, 5-tetrafluorobenzoic.limit = 5

Maintaining dual PointersLeft = 0, right = people. Length - 1

Cyclic conditions:left <= rightBecause, whenleft == rightWhen, there was still one person who needed to make the boat, so it could not be usedleft < right


Case 3 might not be obvious, so let’s do case 2 again

Sort the array by:1,2,2,3.limit = 3

Why is it left++, because they say a boat can’t do more than two people


Code implementation

class Solution {
    public int numRescueBoats(int[] people, int limit) {
    
        Arrays.sort(people);
        int len = people.length;
        int left = 0, right = len - 1;

        int res = 0;

        while (left <= right) {
            if (people[left] + people[right] <= limit) {
                ++left;
            }
            --right;
            ++res;
        }
        returnres; }}Copy the code

❤ finally

I am aCode pipi shrimp, a prawns lover who loves to share knowledge, will update useful blog posts in the future, looking forward to your attention!!

Creation is not easy, if this blog is helpful to you, I hope you can == one key three even oh! Thank you for your support. See you next time