func numRescueBoats(people []int, limit int) int {
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
}
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
// The heaviest remaining person (right) must board no matter what. If the
// lightest (left) also fits under the limit, seat them together.
}
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
// The heaviest remaining person (right) must board no matter what. If the
// lightest (left) also fits under the limit, seat them together.
if people[left]+people[right] <= limit {
left++
}
}
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
// The heaviest remaining person (right) must board no matter what. If the
// lightest (left) also fits under the limit, seat them together.
if people[left]+people[right] <= limit {
left++
}
// Either way the heaviest person is now seated and one boat is used.
}
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
// The heaviest remaining person (right) must board no matter what. If the
// lightest (left) also fits under the limit, seat them together.
if people[left]+people[right] <= limit {
left++
}
// Either way the heaviest person is now seated and one boat is used.
right--
boats++
}
}func numRescueBoats(people []int, limit int) int {
// A boat holds at most two people. Sorting lets us try the greedy pairing:
// put the heaviest person with the lightest one who can share their boat.
sort.Ints(people)
left, right := 0, len(people)-1
boats := 0
// <= (not <) so a lone person left in the middle still gets a boat.
for left <= right {
// The heaviest remaining person (right) must board no matter what. If the
// lightest (left) also fits under the limit, seat them together.
if people[left]+people[right] <= limit {
left++
}
// Either way the heaviest person is now seated and one boat is used.
right--
boats++
}
return boats // O(n log n) for the sort, then an O(n) scan
}Tap the left half to go back, the right half to go forward.
Arrow keys or space to navigate · Esc to exit.