📜  一行中不重叠的最大元素数

📅  最后修改于: 2021-10-27 03:19:04             🧑  作者: Mango

给定两个相同大小N 的数组XLX i代表无限线上的位置。 L I表示至多其第i个元素的两面都可以覆盖的范围内。任务是选择最大数量的元素,这样如果两个所选元素覆盖右侧或左侧部分,则它们不会重叠。
注意:数组X已排序。
例子:

方法:
这个问题可以贪婪地解决。我们总是可以让第一个元素覆盖左段,最后一个元素覆盖右段。对于其他元素,如果可能,请尝试给出左段,否则尝试给出右段。如果它们都不可能,则离开该元素。
下面是上述方法的实现:

C++
// CPP program to find maximum number of
// elements without overlapping in a line
#include 
using namespace std;
 
// Function to find maximum number of
// elements without overlapping in a line
int Segment(int x[], int l[], int n)
{
    // If n = 1, then answer is one
    if (n == 1)
        return 1;
     
    // We can always make 1st element to cover
    // left segment and nth the right segment
    int ans = 2;
         
         
    for (int i = 1; i < n - 1; i++)
    {
        // If left segment for ith element doesnt overlap
        // with i - 1 th element then do left
        if (x[i] - l[i] > x[i - 1])
            ans++;
 
        // else try towards right if possible
        else if (x[i] + l[i] < x[i + 1])
        {
            // update x[i] to right endpoint of
            // segment covered by it
            x[i] = x[i] + l[i];
            ans++;
        }
    }
     
    // Return the required answer
    return ans;
}
 
// Driver code
int main()
{
    int x[] = {1, 3, 4, 5, 8}, l[] = {10, 1, 2, 2, 5};
     
    int n = sizeof(x) / sizeof(x[0]);
 
    // Function call
    cout << Segment(x, l, n);
 
    return 0;
}


Java
// Java program to find maximum number of
// elements without overlapping in a line
import java.util.*;
 
class GFG
{
 
// Function to find maximum number of
// elements without overlapping in a line
static int Segment(int x[], int l[], int n)
{
    // If n = 1, then answer is one
    if (n == 1)
        return 1;
     
    // We can always make 1st element to cover
    // left segment and nth the right segment
    int ans = 2;
         
    for (int i = 1; i < n - 1; i++)
    {
        // If left segment for ith element
        // doesn't overlap with i - 1 th
        // element then do left
        if (x[i] - l[i] > x[i - 1])
            ans++;
 
        // else try towards right if possible
        else if (x[i] + l[i] < x[i + 1])
        {
            // update x[i] to right endpoint of
            // segment covered by it
            x[i] = x[i] + l[i];
            ans++;
        }
    }
     
    // Return the required answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int x[] = {1, 3, 4, 5, 8},
        l[] = {10, 1, 2, 2, 5};
     
    int n = x.length;
 
    // Function call
    System.out.println(Segment(x, l, n));
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find maximum number of
# elements without overlapping in a line
 
# Function to find maximum number of
# elements without overlapping in a line
def Segment(x, l, n):
     
    # If n = 1, then answer is one
    if (n == 1):
        return 1
 
    # We can always make 1st element to cover
    # left segment and nth the right segment
    ans = 2
     
    for i in range(1, n - 1):
         
        # If left segment for ith element doesnt overlap
        # with i - 1 th element then do left
        if (x[i] - l[i] > x[i - 1]):
            ans += 1
 
        # else try towards right if possible
        elif (x[i] + l[i] < x[i + 1]):
             
            # update x[i] to right endpoof
            # segment covered by it
            x[i] = x[i] + l[i]
            ans += 1
 
    # Return the required answer
    return ans
 
# Driver code
x = [1, 3, 4, 5, 8]
l = [10, 1, 2, 2, 5]
 
n = len(x)
 
# Function call
print(Segment(x, l, n))
 
# This code is contributed
# by Mohit Kumar


C#
// C# program to find maximum number of
// elements without overlapping in a line
using System;
     
class GFG
{
 
// Function to find maximum number of
// elements without overlapping in a line
static int Segment(int []x, int []l, int n)
{
    // If n = 1, then answer is one
    if (n == 1)
        return 1;
     
    // We can always make 1st element to cover
    // left segment and nth the right segment
    int ans = 2;
         
    for (int i = 1; i < n - 1; i++)
    {
        // If left segment for ith element
        // doesn't overlap with i - 1 th
        // element then do left
        if (x[i] - l[i] > x[i - 1])
            ans++;
 
        // else try towards right if possible
        else if (x[i] + l[i] < x[i + 1])
        {
            // update x[i] to right endpoint of
            // segment covered by it
            x[i] = x[i] + l[i];
            ans++;
        }
    }
     
    // Return the required answer
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []x = {1, 3, 4, 5, 8};
    int []l = {10, 1, 2, 2, 5};
     
    int n = x.Length;
 
    // Function call
    Console.WriteLine(Segment(x, l, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:

4 

时间复杂度: O(N)

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