📌  相关文章
📜  从给定的权重和 N 项的成本中最大化权重为 K 的段的成本

📅  最后修改于: 2022-05-13 01:56:05.317000             🧑  作者: Mango

从给定的权重和 N 项的成本中最大化权重为 K 的段的成本

给定两个数组 W[] 和 C[] 分别包含 N(1 到 N)个项目的权重和成本,以及一个整数 K,找到一个从 1 到 N 的段,使得该段的总权重最多为 K 并且总成本最高。打印该段的成本。

例子:

朴素方法:该方法是找到权重最多为 k 的所有段,并跟踪最大成本。对于每个元素,找到从该元素开始的段。

下面是上述方法的实现。

C++
// C++ code to find maximum cost of
// a segment whose weight is at most K.
#include 
using namespace std;
 
// Function to find the maximum cost of
// a segment whose weight is at most k.
int findMaxCost(int W[], int C[],
                int N, int K)
{
    // Variable to keep track of
    // current weight.
    int weight = 0;
 
    // Variable to keep track
    // of current cost.
    int cost = 0;
 
    // variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
 
    // Loop to get segment
    // having weight at most K
    for (int l = 0; l < N; l++) {
        weight = 0;
        cost = 0;
        for (int r = l; r < N; r++) {
            weight += W[r];
            cost += C[r];
            if (weight <= K)
                maxCost = max(maxCost, cost);
        }
    }
    return maxCost;
}
 
// Driver code
int main()
{
    int W[] = { 9, 7, 6, 5, 8, 4 };
    int C[] = { 7, 1, 3, 6, 8, 3 };
    int N = sizeof(W) / sizeof(W[0]);
    int K = 20;
 
    cout << findMaxCost(W, C, N, K);
    return 0;
}


Java
// Java code to find maximum cost of
// a segment whose weight is at most K.
class GFG{
 
// Function to find the maximum cost of
// a segment whose weight is at most k.
static int findMaxCost(int W[], int C[],
                int N, int K)
{
   
    // Variable to keep track of
    // current weight.
    int weight = 0;
 
    // Variable to keep track
    // of current cost.
    int cost = 0;
 
    // variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
 
    // Loop to get segment
    // having weight at most K
    for (int l = 0; l < N; l++) {
        weight = 0;
        cost = 0;
        for (int r = l; r < N; r++) {
            weight += W[r];
            cost += C[r];
            if (weight <= K)
                maxCost = Math.max(maxCost, cost);
        }
    }
    return maxCost;
}
 
// Driver code
public static void main(String[] args)
{
    int W[] = { 9, 7, 6, 5, 8, 4 };
    int C[] = { 7, 1, 3, 6, 8, 3 };
    int N = W.length;
    int K = 20;
 
    System.out.print(findMaxCost(W, C, N, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python code to find maximum cost of
# a segment whose weight is at most K.
 
# Function to find the maximum cost of
# a segment whose weight is at most k.
def findMaxCost(W, C, N, K) :
     
    # Variable to keep track of
    # current weight.
    weight = 0;
 
    # Variable to keep track
    # of current cost.
    cost = 0;
 
    # variable to keep track of
    # maximum cost of a segment
    # whose weight is at most K.
    maxCost = 0;
 
    # Loop to get segment
    # having weight at most K
    for l in range(N):
        weight = 0;
        cost = 0;
         
        for r in range(l, N):
            weight += W[r];
            cost += C[r];
            if (weight <= K):
                maxCost = max(maxCost, cost);
    return maxCost;
 
# Driver code
W = [ 9, 7, 6, 5, 8, 4 ];
C = [ 7, 1, 3, 6, 8, 3 ];
N = len(W);
K = 20;
 
print(findMaxCost(W, C, N, K));
 
# This code is contributed by Saurabh Jaiswal


C#
// C# code to find maximum cost of
// a segment whose weight is at most K.
using System;
 
class GFG
{
   
    // Function to find the maximum cost of
    // a segment whose weight is at most k.
    static int findMaxCost(int[] W, int[] C, int N, int K)
    {
       
        // Variable to keep track of
        // current weight.
        int weight = 0;
 
        // Variable to keep track
        // of current cost.
        int cost = 0;
 
        // variable to keep track of
        // maximum cost of a segment
        // whose weight is at most K.
        int maxCost = 0;
 
        // Loop to get segment
        // having weight at most K
        for (int l = 0; l < N; l++) {
            weight = 0;
            cost = 0;
            for (int r = l; r < N; r++) {
                weight += W[r];
                cost += C[r];
                if (weight <= K)
                    maxCost = Math.Max(maxCost, cost);
            }
        }
        return maxCost;
    }
 
    // Driver code
    public static void Main()
    {
        int[] W = { 9, 7, 6, 5, 8, 4 };
        int[] C = { 7, 1, 3, 6, 8, 3 };
        int N = W.Length;
        int K = 20;
 
        Console.WriteLine(findMaxCost(W, C, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


C++
// C++ code to find maximum cost of
// a segment whose weight is at most K.
#include 
using namespace std;
 
// Function to find the maximum cost of
// a segment whose weight is at most K.
int findMaxCost(int W[], int C[],
                int N, int K)
{   
    // Variable to keep track
    // of current weight.
    int weight = 0;
     
    // Variable to keep track
    // of current cost.
    int cost = 0;
   
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
     
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
         
        // Add current element to the window.
        weight += W[r];
          cost += C[r];
       
        // Keep removing elements
        // from the start of current window
        // while weight is greater than K
        while(weight > K)
        {
            weight -= W[l];
            cost -= C[l];
              l++;
        }
 
          // Update maxCost
          maxCost = max(maxCost, cost);
    }
    return maxCost;
}
 
// Driver code
int main()
{
    int W[] = {9, 7, 6, 5, 8, 4};
    int C[] = {7, 1, 3, 6, 8, 3};
    int N = sizeof(W) / sizeof(W[0]);
    int K = 20;
 
    cout << findMaxCost(W, C, N, K);
    return 0;
}


Java
// Java code to find maximum cost of
// a segment whose weight is at most K.
class GFG{
 
// Function to find the maximum cost of
// a segment whose weight is at most K.
static int findMaxCost(int W[], int C[],
                int N, int K)
{   
   
    // Variable to keep track
    // of current weight.
    int weight = 0;
     
    // Variable to keep track
    // of current cost.
    int cost = 0;
   
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
     
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
         
        // Add current element to the window.
        weight += W[r];
          cost += C[r];
       
        // Keep removing elements
        // from the start of current window
        // while weight is greater than K
        while(weight > K)
        {
            weight -= W[l];
            cost -= C[l];
              l++;
        }
 
          // Update maxCost
          maxCost = Math.max(maxCost, cost);
    }
    return maxCost;
}
 
// Driver code
public static void main(String[] args)
{
    int W[] = {9, 7, 6, 5, 8, 4};
    int C[] = {7, 1, 3, 6, 8, 3};
    int N = W.length;
    int K = 20;
 
    System.out.print(findMaxCost(W, C, N, K));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python 3 code to find maximum cost of
# a segment whose weight is at most K.
 
# Function to find the maximum cost of
# a segment whose weight is at most K.
def findMaxCost(W, C, N, K):
 
    # Variable to keep track
    # of current weight.
    weight = 0
 
    # Variable to keep track
    # of current cost.
    cost = 0
 
    # Variable to keep track of
    # maximum cost of a segment
    # whose weight is at most K.
    maxCost = 0
 
    # Loop to implement
    # sliding window technique
    l = 0
    for r in range(N):
 
        # Add current element to the window.
        weight += W[r]
        cost += C[r]
 
        # Keep removing elements
        # from the start of current window
        # while weight is greater than K
        while(weight > K):
 
            weight -= W[l]
            cost -= C[l]
            l += 1
 
        # Update maxCost
        maxCost = max(maxCost, cost)
    return maxCost
 
# Driver code
if __name__ == "__main__":
 
    W = [9, 7, 6, 5, 8, 4]
    C = [7, 1, 3, 6, 8, 3]
    N = len(W)
    K = 20
 
    print(findMaxCost(W, C, N, K))
 
    # This code is contributed by gaurav01.


C#
// C# code to find maximum cost of
// a segment whose weight is at most K.
using System;
using System.Collections.Generic;
public class GFG{
 
  // Function to find the maximum cost of
  // a segment whose weight is at most K.
  static int findMaxCost(int []W, int []C,
                         int N, int K)
  {   
 
    // Variable to keep track
    // of current weight.
    int weight = 0;
 
    // Variable to keep track
    // of current cost.
    int cost = 0;
 
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
 
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
 
      // Add current element to the window.
      weight += W[r];
      cost += C[r];
 
      // Keep removing elements
      // from the start of current window
      // while weight is greater than K
      while(weight > K)
      {
        weight -= W[l];
        cost -= C[l];
        l++;
      }
 
      // Update maxCost
      maxCost = Math.Max(maxCost, cost);
    }
    return maxCost;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []W = {9, 7, 6, 5, 8, 4};
    int []C = {7, 1, 3, 6, 8, 3};
    int N = W.Length;
    int K = 20;
 
    Console.Write(findMaxCost(W, C, N, K));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript


输出
17

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

有效方法:一种有效的方法是使用滑动窗口技术。

  • 令 l 和 r 分别表示当前窗口中第一个和最后一个元素的索引。
  • 开始遍历数组并跟踪当前窗口中元素的总权重和总成本以及到目前为止找到的最大总成本。
  • 当窗口的权重大于 k 时,继续从窗口的开头删除元素。
  • 现在更新最大成本。
  • 遍历所有项目后返回最大成本。

下面是上述方法的实现。

C++

// C++ code to find maximum cost of
// a segment whose weight is at most K.
#include 
using namespace std;
 
// Function to find the maximum cost of
// a segment whose weight is at most K.
int findMaxCost(int W[], int C[],
                int N, int K)
{   
    // Variable to keep track
    // of current weight.
    int weight = 0;
     
    // Variable to keep track
    // of current cost.
    int cost = 0;
   
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
     
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
         
        // Add current element to the window.
        weight += W[r];
          cost += C[r];
       
        // Keep removing elements
        // from the start of current window
        // while weight is greater than K
        while(weight > K)
        {
            weight -= W[l];
            cost -= C[l];
              l++;
        }
 
          // Update maxCost
          maxCost = max(maxCost, cost);
    }
    return maxCost;
}
 
// Driver code
int main()
{
    int W[] = {9, 7, 6, 5, 8, 4};
    int C[] = {7, 1, 3, 6, 8, 3};
    int N = sizeof(W) / sizeof(W[0]);
    int K = 20;
 
    cout << findMaxCost(W, C, N, K);
    return 0;
}

Java

// Java code to find maximum cost of
// a segment whose weight is at most K.
class GFG{
 
// Function to find the maximum cost of
// a segment whose weight is at most K.
static int findMaxCost(int W[], int C[],
                int N, int K)
{   
   
    // Variable to keep track
    // of current weight.
    int weight = 0;
     
    // Variable to keep track
    // of current cost.
    int cost = 0;
   
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
     
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
         
        // Add current element to the window.
        weight += W[r];
          cost += C[r];
       
        // Keep removing elements
        // from the start of current window
        // while weight is greater than K
        while(weight > K)
        {
            weight -= W[l];
            cost -= C[l];
              l++;
        }
 
          // Update maxCost
          maxCost = Math.max(maxCost, cost);
    }
    return maxCost;
}
 
// Driver code
public static void main(String[] args)
{
    int W[] = {9, 7, 6, 5, 8, 4};
    int C[] = {7, 1, 3, 6, 8, 3};
    int N = W.length;
    int K = 20;
 
    System.out.print(findMaxCost(W, C, N, K));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 code to find maximum cost of
# a segment whose weight is at most K.
 
# Function to find the maximum cost of
# a segment whose weight is at most K.
def findMaxCost(W, C, N, K):
 
    # Variable to keep track
    # of current weight.
    weight = 0
 
    # Variable to keep track
    # of current cost.
    cost = 0
 
    # Variable to keep track of
    # maximum cost of a segment
    # whose weight is at most K.
    maxCost = 0
 
    # Loop to implement
    # sliding window technique
    l = 0
    for r in range(N):
 
        # Add current element to the window.
        weight += W[r]
        cost += C[r]
 
        # Keep removing elements
        # from the start of current window
        # while weight is greater than K
        while(weight > K):
 
            weight -= W[l]
            cost -= C[l]
            l += 1
 
        # Update maxCost
        maxCost = max(maxCost, cost)
    return maxCost
 
# Driver code
if __name__ == "__main__":
 
    W = [9, 7, 6, 5, 8, 4]
    C = [7, 1, 3, 6, 8, 3]
    N = len(W)
    K = 20
 
    print(findMaxCost(W, C, N, K))
 
    # This code is contributed by gaurav01.

C#

// C# code to find maximum cost of
// a segment whose weight is at most K.
using System;
using System.Collections.Generic;
public class GFG{
 
  // Function to find the maximum cost of
  // a segment whose weight is at most K.
  static int findMaxCost(int []W, int []C,
                         int N, int K)
  {   
 
    // Variable to keep track
    // of current weight.
    int weight = 0;
 
    // Variable to keep track
    // of current cost.
    int cost = 0;
 
    // Variable to keep track of
    // maximum cost of a segment
    // whose weight is at most K.
    int maxCost = 0;
 
    // Loop to implement
    // sliding window technique
    int l = 0;
    for (int r = 0; r < N; r++) {
 
      // Add current element to the window.
      weight += W[r];
      cost += C[r];
 
      // Keep removing elements
      // from the start of current window
      // while weight is greater than K
      while(weight > K)
      {
        weight -= W[l];
        cost -= C[l];
        l++;
      }
 
      // Update maxCost
      maxCost = Math.Max(maxCost, cost);
    }
    return maxCost;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
    int []W = {9, 7, 6, 5, 8, 4};
    int []C = {7, 1, 3, 6, 8, 3};
    int N = W.Length;
    int K = 20;
 
    Console.Write(findMaxCost(W, C, N, K));
  }
}
 
// This code is contributed by shikhasingrajput

Javascript


输出
17

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