Kotlin | Rust
\nhttps://youtu.be/ZJSjkMMBSkA\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/592\n\n#### Problem TLDR\n\nMinimum total boats with at most
![2024-05-04_08-54.webp](https://assets.leetcode.com/users/images/87a4b6ea-ecf1-4f8e-b2f8-6efff11cc7e5_1714802074.5178337.webp)\nhttps://youtu.be/ZJSjkMMBSkA\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/592\n\n#### Problem TLDR\n\nMinimum total boats with at most `2` people & `limit` weight #medium #two_pointers #greedy\n\n#### Intuition\n\nFirst idea as to try to take as much people as possible in a single boat: if we start with light first, then heavier people might not give a space for a `limit`. By intuition, we need to try put most heavy and most light people in pairs together:\n\n```j\n    // 6654321   limit = 6\n    // i     j\n    // i         +1\n    //  i        +1\n    //   i   j   +1\n    //    i j    +1\n    //     i     +1\n```\n\n#### Approach\n\nThe interesting part is how some conditions are not relevant: we can skip `i < j` check when moving `j--`.\n\n#### Complexity\n\n- Time complexity:\n$$O(nlog(n))$$\n\n- Space complexity:\n$$O(1)$$\n\n#### Code\n\n```kotlin []\n\n    fun numRescueBoats(people: IntArray, limit: Int): Int {\n        people.sortDescending(); var j = people.lastIndex\n        for ((i, p) in people.withIndex())\n            if (i > j) return i\n            else if (p + people[j] <= limit) j--\n        return people.size\n    }\n\n```\n```rust []\n\n    pub fn num_rescue_boats(mut people: Vec<i32>, limit: i32) -> i32 {\n        people.sort_unstable_by(|a, b| b.cmp(a)); \n        let mut j = people.len() - 1;\n        for (i, p) in people.iter().enumerate() {\n            if i > j { return i as _ }\n            else if p + people[j] <= limit { j -= 1 }\n        }; people.len() as _\n    }\n\n```
['Two Pointers', 'Greedy', 'Rust', 'Kotlin']