📌  相关文章
📜  水平放置的最大杆数,以便没有两杆在 X 坐标上重叠

📅  最后修改于: 2021-10-23 08:18:07             🧑  作者: Mango

给定两个大小为N 的数组X[]H[] ,其中X[i]表示 X 坐标 i垂直杆的高度, H[i]表示该杆的高度,并且还假设i杆也可以水平放置,方法是将杆放在线段[X[i]-H[i] 上, X[i]][X[i], X[i]+H[i]]在 X 坐标上,任务是找到可以水平放置的最大杆数,使得没有两杆重叠X 坐标。

例子:

方法:该问题可以使用贪心算法解决。请按照以下步骤解决问题:

  • 初始化两个变量,也就是说,ANS0存储答案和prevINT_MIN存储X上最后占用的点坐标。
  • 使用变量i在范围[0, N] 上迭代并执行以下步骤:  
    • 如果当前杆可以放在左边,即如果X[i]-H[i]大于prev ,则将ans的值增加1并将prev更新为X[i]。
    • 否则,如果当前杆可以放在右边,即如果X[i]+H[i]小于X[i+1] ,则将ans的值增加1并将prev更新为X[i] +H[i]。
    • 否则,将prev更新为X[i]
  • 最后,完成上述步骤后,打印ans.h中得到的答案

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
 
using namespace std;
 
// Function to find the maximum number
// of rods that can be put horizontally
int findMaximumPoints(int N, int X[], int H[])
{
 
    // Stores the result
    int ans = 0;
 
    // Stores the last occupied point
    int prev = INT_MIN;
 
    // Traverse the array arr[]
    for (int i = 0; i < N; ++i) {
 
        // If the current point can be put on
        // the left side
        if (prev < (X[i] - H[i])) {
 
            // Increment the ans by 1
            ++ans;
 
            // Update prev
            prev = X[i];
        }
 
        // Else if the given point can be put
        // on the right side
        else if (i == N - 1
                 || (X[i] + H[i]) < X[i + 1]) {
 
            // Increment the ans by 1
            ++ans;
 
            // Update prev
            prev = X[i] + H[i];
        }
 
        // Otherwise,
        else {
            // Update prev
            prev = X[i];
        }
    }
 
    // Return the ans
    return ans;
}
 
// Driver Code
int main()
{
 
    int X[] = { 1, 2, 3 };
    int H[] = { 2, 5, 5 };
    int N = sizeof(X) / sizeof(X[0]);
 
    cout << findMaximumPoints(N, X, H);
}


Java
// Java program for the above approach
 
class GFG {
 
    // Function to find the maximum number
    // of rods that can be put horizontally
    public static int findMaximumPoints(int N, int X[], int H[]) {
 
        // Stores the result
        int ans = 0;
 
        // Stores the last occupied point
        int prev = Integer.MIN_VALUE;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; ++i) {
 
            // If the current point can be put on
            // the left side
            if (prev < (X[i] - H[i])) {
 
                // Increment the ans by 1
                ++ans;
 
                // Update prev
                prev = X[i];
            }
 
            // Else if the given point can be put
            // on the right side
            else if (i == N - 1 || (X[i] + H[i]) < X[i + 1]) {
 
                // Increment the ans by 1
                ++ans;
 
                // Update prev
                prev = X[i] + H[i];
            }
 
            // Otherwise,
            else {
                // Update prev
                prev = X[i];
            }
        }
 
        // Return the ans
        return ans;
    }
 
    // Driver Code
    public static void main(String args[]) {
 
        int X[] = { 1, 2, 3 };
        int H[] = { 2, 5, 5 };
        int N = X.length;
 
        System.out.println(findMaximumPoints(N, X, H));
    }
}
 
// This code is contributed by _saurabh_jaiswal.


Python3
# Python 3 program for the above approach
import sys
 
# Function to find the maximum number
# of rods that can be put horizontally
def findMaximumPoints(N, X, H):
    # Stores the result
    ans = 0
 
    # Stores the last occupied point
    prev = -sys.maxsize-1
 
    # Traverse the array arr[]
    for i in range(N):
       
        # If the current point can be put on
        # the left side
        if (prev < (X[i] - H[i])):
 
            # Increment the ans by 1
            ans += 1
 
            # Update prev
            prev = X[i]
 
        # Else if the given point can be put
        # on the right side
        elif(i == N - 1 or (X[i] + H[i]) < X[i + 1]):
 
            # Increment the ans by 1
            ans += 1
 
            # Update prev
            prev = X[i] + H[i]
 
        # Otherwise,
        else:
            # Update prev
            prev = X[i]
 
    # Return the ans
    return ans
 
# Driver Code
if __name__ == '__main__':
    X = [1, 2, 3]
    H = [2, 5, 5]
    N = len(X)
 
    print(findMaximumPoints(N, X, H))
     
    # This code is contributed by SURENDRA_GANGWAR.


C#
// C# program for the above approach
using System;
 
class GFG{
     
    // Function to find the maximum number
    // of rods that can be put horizontally
    public static int findMaximumPoints(int N, int[] X, int[] H) {
 
        // Stores the result
        int ans = 0;
 
        // Stores the last occupied point
        int prev = Int32.MinValue;
 
        // Traverse the array arr[]
        for (int i = 0; i < N; ++i) {
 
            // If the current point can be put on
            // the left side
            if (prev < (X[i] - H[i])) {
 
                // Increment the ans by 1
                ++ans;
 
                // Update prev
                prev = X[i];
            }
 
            // Else if the given point can be put
            // on the right side
            else if (i == N - 1 || (X[i] + H[i]) < X[i + 1]) {
 
                // Increment the ans by 1
                ++ans;
 
                // Update prev
                prev = X[i] + H[i];
            }
 
            // Otherwise,
            else {
                // Update prev
                prev = X[i];
            }
        }
 
        // Return the ans
        return ans;
    }
 
 
// Driver code
static public void Main()
{
    int[] X = { 1, 2, 3 };
        int[] H = { 2, 5, 5 };
        int N = X.Length;
 
        Console.WriteLine(findMaximumPoints(N, X, H));
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出
2

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

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