| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.BoatsToSavePeople; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class BoatsToSavePeopleTwoPointers : IBoatsToSavePeople |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n log n) |
| | 19 | | /// Space complexity - O(1) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="people"></param> |
| | 22 | | /// <param name="limit"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int NumRescueBoats(int[] people, int limit) |
| 6 | 25 | | { |
| 6 | 26 | | var result = 0; |
| | 27 | |
|
| 6 | 28 | | Array.Sort(people); |
| | 29 | |
|
| 6 | 30 | | var left = 0; |
| 6 | 31 | | var right = people.Length - 1; |
| | 32 | |
|
| 23 | 33 | | while (left <= right) |
| 17 | 34 | | { |
| 17 | 35 | | var leftValue = people[left]; |
| 17 | 36 | | var rightValue = people[right]; |
| | 37 | |
|
| 17 | 38 | | if (leftValue + rightValue <= limit) |
| 9 | 39 | | { |
| 9 | 40 | | left++; |
| 9 | 41 | | } |
| | 42 | |
|
| 17 | 43 | | right--; |
| | 44 | |
|
| 17 | 45 | | result++; |
| 17 | 46 | | } |
| | 47 | |
|
| 6 | 48 | | return result; |
| 6 | 49 | | } |
| | 50 | | } |