📜  通过连接给定的绳索,最大限度地增加连续长度的绳索

📅  最后修改于: 2021-09-06 11:23:55             🧑  作者: Mango

给定一个大小为N的数组A[ ] ,其中每个数组元素表示绳索的长度,任务是找到可以通过连接从长度1开始的给定绳索来创建的连续长度的绳索数量。

例子 :

方法:这个问题可以通过使用以下事实来解决:如果我们能够创建范围为[1, K]长度的绳索,并且长度为L的绳索剩余,使得(L <= K+1)那么我们可以创建范围[1, K+L]的绳索通过将L长度的绳索添加到范围[max(1, K-L+1), K] 的每条绳索。所以要解决这个问题,首先对数组进行排序,然后遍历数组,每次检查当前元素是否小于等于我们得到的最大连续长度+1。如果发现为真,则将该元素添加到最大连续长度。否则,返回答案。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find maximized count
// of ropes of consecutive length
int maxConsecutiveRopes(int ropes[], int N)
{
    // Stores the maximum count
    // of ropes of consecutive length
    int curSize = 0;
 
    // Sort the ropes by their length
    sort(ropes, ropes + N);
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
 
        // If size of the current rope is less
        // than or equal to current maximum
        // possible size + 1, update the
        // range to curSize + ropes[i]
 
        if (ropes[i] <= curSize + 1) {
            curSize = curSize + ropes[i];
        }
 
        // If a rope of size (curSize + 1)
        // cannot be obtained
        else
            break;
    }
    return curSize;
}
 
// Driver Code
int main()
{
    // Input
    int N = 5;
    int ropes[] = { 1, 2, 7, 1, 1 };
 
    // Function Call
    cout << maxConsecutiveRopes(ropes, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG {
 
    // Function to find maximized count
    // of ropes of consecutive length
    static int maxConsecutiveRope(int ropes[], int N)
    {
        // Stores the maximum count
        // of ropes of consecutive length
        int curSize = 0;
 
        // Sort the ropes by their length
        Arrays.sort(ropes);
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
 
            // If size of the current rope is less
            // than or equal to current maximum
            // possible size + 1, update the
            // range to curSize + ropes[i]
 
            if (ropes[i] <= curSize + 1) {
                curSize = curSize + ropes[i];
            }
 
            // If a rope of size (curSize + 1)
            // cannot be obtained
            else
                break;
        }
        return curSize;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Input
        int N = 5;
        int ropes[] = { 1, 2, 7, 1, 1 };
 
        // Function Call
        System.out.println(
            maxConsecutiveRope(ropes, N));
    }
}


Python3
# Python3 program for the above approach
 
# Function to find maximized count
# of ropes of consecutive length
def maxConsecutiveRopes(ropes, N):
 
    # Stores the maximum count
    # of ropes of consecutive length
    curSize = 0
 
    # Sort the ropes by their length
    ropes = sorted(ropes)
 
    # Traverse the array
    for i in range(N):
         
        # If size of the current rope is less
        # than or equal to current maximum
        # possible size + 1, update the
        # range to curSize + ropes[i]
        if (ropes[i] <= curSize + 1):
            curSize = curSize + ropes[i]
 
        # If a rope of size (curSize + 1)
        # cannot be obtained
        else:
            break
 
    return curSize
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    N = 5
    ropes = [ 1, 2, 7, 1, 1 ]
 
    # Function Call
    print (maxConsecutiveRopes(ropes, N))
     
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find maximized count
// of ropes of consecutive length
static int maxConsecutiveRope(int[] ropes, int N)
{
     
    // Stores the maximum count
    // of ropes of consecutive length
    int curSize = 0;
 
    // Sort the ropes by their length
    Array.Sort(ropes);
 
    // Traverse the array
    for(int i = 0; i < N; i++)
    {
         
        // If size of the current rope is less
        // than or equal to current maximum
        // possible size + 1, update the
        // range to curSize + ropes[i]
        if (ropes[i] <= curSize + 1)
        {
            curSize = curSize + ropes[i];
        }
 
        // If a rope of size (curSize + 1)
        // cannot be obtained
        else
            break;
    }
    return curSize;
}
 
// Driver code
public static void Main ()
{
     
    // Input
    int N = 5;
    int[] ropes = { 1, 2, 7, 1, 1 };
 
    // Function Call
    Console.WriteLine(maxConsecutiveRope(ropes, N));
}
}
 
// This code is contributed by souravghosh0416


Javascript


输出:
5

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live