📜  用给定值覆盖最大数组元素

📅  最后修改于: 2021-10-25 11:23:15             🧑  作者: Mango

给定糖果总数 ‘X’、学生人数 ‘N’ 和数组 ‘arr’,数组 ‘arr’ 包含必须给学生的糖果的确切数量的值,以使学生开心,其中 arr[i] 是使学生“我”高兴的确切数量的糖果。任务是以让最大程度的学生开心的方式分发糖果。
例子:

Input : X = 70, arr = {20, 30, 10}
Output: 2
One optimal way of distribution is (20, 40, 10)
So, the number of happy students are 2.

Input: X = 10, arr = {20, 30, 10}
Output: 1
One optimal way of distribution is (0, 0, 10)
Only 1 student can be made happy in this case

方法:我们可以通过开始给对较少糖果感到满意的学生提供糖果来最大化快乐学生的数量。
所以我们根据糖果对学生名单进行升序排序。然后,我们将简单地遍历数组并取学生,直到总和小于或等于总糖果。
下面是上述方法的实现:

C++
// C++ implementation of the approach
# include
using namespace std;
 
// Function to find value for
// covering maximum array elements
int maxArrayCover(vector a, int n, int x){
 
    // sort the students in
    // ascending based on
    // the candies
    sort(a.begin(), a.end());
      
    // To store the number
    // of happy students
    int cc = 0;
      
    // To store the running sum
    int s = 0;
    for (int i = 0; i < n; i++){
        s += a[i];
 
        // If the current student
        // can't be made happy
        if(s > x){
           break;
        }
 
        // increment the count
        // if we can make the
        // ith student happy
        cc += 1;
    }
      
    // If the sum = x
    // then answer is n
    if(accumulate(a.begin(), a.end(), 0) == x){
        return n;
    }
    else{
        // If the count is
        // equal to n then
        // the answer is n-1
        if(cc == n){
            return n-1;
        }
        else{
            return cc;
        }
    }
}
 
// Driver function
int main(){
 
    int n = 3;
    int x = 70;
    vector a = {10, 20, 30};
 
    printf("%d\n",maxArrayCover(a, n, x));
 
    return 0;
}
// This code is contributed
// by Harshit Saini


Java
// Java implementation of the approach
 
import java.util.*;
 
class GFG{
 
    // Function to find value for
    // covering maximum array elements
    public static int maxArrayCover(int[] a, int n, int x){
 
        // sort the students in
        // ascending based on
        // the candies
        Arrays.sort(a);
          
        // To store the number
        // of happy students
        int cc = 0;
          
        // To store the running sum
        int s = 0;
        for (int i = 0; i < n; i++){
            s += a[i];
 
            // If the current student
            // can't be made happy
            if(s > x){
               break;
            }
 
            // increment the count
            // if we can make the
            // ith student happy
            cc += 1;
        }
          
        // If the sum = x
        // then answer is n
        if(Arrays.stream(a).sum() == x){
            return n;
        }
        else{
            // If the count is
            // equal to n then
            // the answer is n-1
            if(cc == n){
                return n-1;
            }
            else{
                return cc;
            }
        }
    }
 
    // Driver function
    public static void main(String []args){
 
        int n   = 3;
        int x   = 70;
        int[] a = new int[]{10, 20, 30};
 
        System.out.println(maxArrayCover(a, n, x));
 
        System.exit(0);
    }
}
 
// This code is contributed
// by Harshit Saini


Python3
# Python implementation of the approach
 
# Function to find value for
# covering maximum array elements
def maxArrayCover(a, n, x):
 
    # sort the students in
    # ascending based on
    # the candies
    a.sort()
      
    # To store the number
    # of happy students
    cc = 0
      
    # To store the running sum
    s = 0
    for i in range(n):
         s+= a[i]
           
         # If the current student
         # can't be made happy
         if(s > x):
             break
      
         # increment the count
         # if we can make the
         # ith student happy
         cc += 1
      
    # If the sum = x
    # then answer is n
    if(sum(a) == x):
         return n
    else:
         # If the count is
         # equal to n then
         # the answer is n-1
         if(cc == n):
            return n-1
         else:
            return cc
 
# Driver function
if __name__ == '__main__':
 
    n, x = 3, 70
    a    = [10, 20, 30]   
 
    print(maxArrayCover(a, n, x))


C#
// C# implementation of the approach
 
using System;
using System.Linq;
 
class GFG{
 
    // Function to find value for
    // covering maximum array elements
    static int maxArrayCover(int[] a, int n, int x){
 
        // sort the students in
        // ascending based on
        // the candies
        Array.Sort(a);
          
        // To store the number
        // of happy students
        int cc = 0;
          
        // To store the running sum
        int s = 0;
        for (int i = 0; i < n; i++){
            s += a[i];
 
            // If the current student
            // can't be made happy
            if(s > x){
               break;
            }
 
            // increment the count
            // if we can make the
            // ith student happy
            cc += 1;
        }
          
        // If the sum = x
        // then answer is n
        if(a.Sum() == x){
            return n;
        }
        else{
            // If the count is
            // equal to n then
            // the answer is n-1
            if(cc == n){
                return n-1;
            }
            else{
                return cc;
            }
        }
    }
 
    // Driver function
    public static void Main(){
 
        int n   = 3;
        int x   = 70;
        int[] a = new int[]{10, 20, 30};
 
        Console.WriteLine(maxArrayCover(a, n, x));
    }
}
 
// This code is contributed
// by Harshit Saini


PHP
 $x){
           break;
        }
 
        // increment the count
        // if we can make the
        // ith student happy
        $cc += 1;
    }
      
    // If the sum = x
    // then answer is n
    if(array_sum($a) == $x){
        return $n;
    }
    else{
        // If the count is
        // equal to n then
        // the answer is n-1
        if($cc == $n){
            return $n-1;
        }
        else{
            return $cc;
        }
    }
}
 
$n = 3;
$x = 70;
$a = array(10, 20, 30);
 
echo maxArrayCover($a, $n, $x);
 
// This code is contributed
// by Harshit Saini
?>


Javascript


输出:
2

时间复杂度: O(N * log(N))
空间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程