Ferris Wheel

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 nint kvector<int&arr)
{

    sort(arr.begin(), arr.end(), greater<int>());
    int ans = 0;
    for (int i = 0j = n - 1i <= jans++)
    {
        if (i == j)
            i++;
        else if (arr[i] + arr[j] <= k)
            i++, j--;
        else
            i++;
    }
    cout << ans << "\n";
}

int main()
{
    int n = 4k = 10;

    vector<intarr = {7239};

    ferrisWheel(nkarr);

    return 0;
}


No comments:

Post a Comment