📌  相关文章
📜  使得总失窃价值小于K的最大利润以获得奖金

📅  最后修改于: 2021-04-27 21:00:24             🧑  作者: Mango

给定一个整数K和一个数组arr [] ,该数组表示可以被盗的金额,任务是选择一个项目子集,使它们的总值小于K,以获取奖励金额。

例子:

方法:想法是使用置换和组合来选择元素,使它们的总和小于K。因此,考虑所有可能的因素将获得最大可能的利润。

下面是上述方法的实现:

C++
// C++ implementation to find the
// maximum stolen value such that
// total stolen value is less than K
 
#include 
 
using namespace std;
 
// Function to find the maximum
// profit from the given values
int maxProfit(vector value,
                         int N, int K)
{
    sort(value.begin(), value.end());
    int maxval = value[N - 1];
    int maxProfit = 0;
    int curr_val;
     
    // Iterating over every
    // possible permutation
    do {
        curr_val = 0;
        for (int i = 0; i < N; i++) {
            curr_val += value[i];
            if (curr_val <= K) {
                maxProfit = max(curr_val +
                  maxval * (i + 1), maxProfit);
            }
        }
    } while (next_permutation(
        value.begin(), value.end()));
    return maxProfit;
}
 
// Driver Code
int main()
{
    int N = 4, K = 6;
    vector values{5, 2, 7, 3};
     
    // Function Call
    cout << maxProfit(values, N, K);
}


Java
// Java implementation to find the
// maximum stolen value such that
// total stolen value is less than K
import java.util.*;
class GFG{
  
// Function to find the maximum
// profit from the given values
static int maxProfit(int []value,
                     int N, int K)
{
    Arrays.sort(value);
    int maxval = value[N - 1];
    int maxProfit = 0;
    int curr_val;
      
    // Iterating over every
    // possible permutation
    do {
        curr_val = 0;
        for (int i = 0; i < N; i++) {
            curr_val += value[i];
            if (curr_val <= K) {
                maxProfit = Math.max(curr_val +
                                     maxval * (i + 1),
                                     maxProfit);
            }
        }
    } while (next_permutation(value));
    return maxProfit;
}
static boolean next_permutation(int[] p) {
      for (int a = p.length - 2; a >= 0; --a)
        if (p[a] < p[a + 1])
          for (int b = p.length - 1;; --b)
            if (p[b] > p[a]) {
              int t = p[a];
              p[a] = p[b];
              p[b] = t;
              for (++a, b = p.length - 1; a < b; ++a, --b) {
                t = p[a];
                p[a] = p[b];
                p[b] = t;
              }
              return true;
            }
      return false;
    }
   
// Driver Code
public static void main(String[] args)
{
    int N = 4, K = 6;
    int []values = {5, 2, 7, 3};
      
    // Function Call
    System.out.print(maxProfit(values, N, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 implementation to find the
# maximum stolen value such that
# total stolen value is less than K
 
# Function to find the maximum
# profit from the given values
def maxProfit(value, N, K):
     
    value.sort()
    maxval = value[N - 1]
    maxProfit = 0
     
    # Iterating over every
    # possible permutation
    while True:
        curr_val = 0
        for i in range(N):
            curr_val += value[i]
             
            if (curr_val <= K):
                maxProfit = max(curr_val + maxval *
                                      (i + 1), maxProfit)
                                       
        if not next_permutation(value):
            break
     
    return maxProfit
 
def next_permutation(p):
     
    for a in range(len(p) - 2, -1, -1):
        if p[a] < p[a + 1]:
            b = len(p) - 1
             
            while True:
                if p[b] > p[a]:
                    t = p[a]
                    p[a] = p[b]
                    p[b] = t
                     
                    a += 1
                    b = len(p) - 1
                     
                    while a < b:
                        t = p[a]
                        p[a] = p[b]
                        p[b] = t
                         
                        a += 1
                        b -= 1
                         
                    return True
                     
                b -= 1
     
    return False
     
# Driver Code 
N, K = 4, 6
values = [ 5, 2, 7, 3 ]
 
# Function Call
print(maxProfit(values, N, K))
 
# This code is contributed by divyesh072019


C#
// C# implementation to find the
// maximum stolen value such that
// total stolen value is less than K
using System;
 
class GFG{
     
// Function to find the maximum
// profit from the given values
static int maxProfit(int[] value,
                     int N, int K)
{
    Array.Sort(value);
    int maxval = value[N - 1];
    int maxProfit = 0;
    int curr_val;
       
    // Iterating over every
    // possible permutation
    do
    {
        curr_val = 0;
        for(int i = 0; i < N; i++)
        {
            curr_val += value[i];
            if (curr_val <= K)
            {
                maxProfit = Math.Max(curr_val +
                                     maxval * (i + 1),
                                     maxProfit);
            }
        }
    } while (next_permutation(value));
    return maxProfit;
}
 
static bool next_permutation(int[] p)
{
    for(int a = p.Length - 2; a >= 0; --a)
        if (p[a] < p[a + 1])
            for(int b = p.Length - 1;; --b)
                if (p[b] > p[a])
                {
                    int t = p[a];
                    p[a] = p[b];
                    p[b] = t;
                       
                    for(++a, b = p.Length - 1;
                            a < b; ++a, --b)
                    {
                        t = p[a];
                        p[a] = p[b];
                        p[b] = t;
                    }
                    return true;
                }
    return false;
}
 
// Driver code  
static void Main()
{
    int N = 4, K = 6;
    int[] values = { 5, 2, 7, 3 };
   
    // Function call
    Console.WriteLine(maxProfit(values, N, K));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
19

时间复杂度: O(N 2 )
辅助空间: O(N)