There are n children who want to go to a Ferris wheel, and your task is to find a gondola for each child.
Each gondola may have one or two children in it, and in addition, the total weight in a gondola may not exceed x. You know the weight of every child.
What is the minimum number of gondolas needed for the children?
Example:
Input: n = 4, k = 10, arr = {7, 2, 3, 9}
Output: 3
Approach
C++
#include <bits/stdc++.h>using namespace std;void ferrisWheel(int n, int k, vector<int> &arr){sort(arr.begin(), arr.end(), greater<int>());int ans = 0;for (int i = 0, j = n - 1; i <= j; ans++){if (i == j)i++;else if (arr[i] + arr[j] <= k)i++, j--;elsei++;}cout << ans << "\n";}int main(){int n = 4, k = 10;vector<int> arr = {7, 2, 3, 9};ferrisWheel(n, k, arr);return 0;}
No comments:
Post a Comment