给定一个二维数组arr[][] ,其中每一行的形式为{start, end} ,表示X轴上每条线段的起点和终点。在一个步骤中,在X轴上选择一个点并删除所有通过该点的线段。任务是找到需要选择的最小数量的点来删除数组的所有线段。
例子:
Input: arr[][]= { {9, 15}, {3, 8}, {1, 6}, {7, 12}, {5,10} }
Output : 2
Explanation:
Select the point arr[2][1](= (6, 0) on the X-axis and delete the second(= arr[1]), third(= arr[2]), and fifth(= arr[4]) line segments.
Select the point arr[3][1](= (12, 0)) on the X-axis and delete the first(=arr[0]) and the fourth(=arr[3]) line segments.
Therefore, the required count is 2.
Input: arr[][]={ {1, 4}, {5, 7}, {9, 13} }
Output: 3
方法:该问题可以使用贪心技术解决。请按照以下步骤解决问题:
- 初始化一个变量,比如cntSteps来计算删除所有线段所需的总步数。
- 根据线段的端点对数组arr[][] 进行排序。
- 初始化一个变量,比如Points = arr[0][1]来存储X轴的点。
- 遍历数组并检查arr[i][0] 的值是否大于Points 。如果发现为真,则将值cntSteps增加1并更新Points = arr[i][1] 的值。
- 最后,打印cntSteps的值。
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Comparator function
bool comp(vector &x, vector y)
{
return x[1] < y[1];
}
// Function to count the minimum number of
// steps required to delete all the segments
int cntMinSteps(vector > &arr,
int N)
{
// Stores count of steps required
// to delete all the line segments
int cntSteps = 1;
// Sort the array based on end points
// of line segments
sort(arr.begin(), arr.end(), comp);
// Stores point on X-axis
int Points = arr[0][1];
// Traverse the array
for(int i = 0; i < N; i++) {
// If arr[1][0] is
// greater than Points
if(arr[i][0] > Points) {
// Update cntSteps
cntSteps++;
// Update Points
Points = arr[i][1];
}
}
return cntSteps;
}
// Driver Code
int main() {
vector > arr
= { { 9, 15 }, { 3, 8 },
{ 1, 6 }, { 7, 12 },
{ 5, 10 } };
int N = arr.size();
cout<< cntMinSteps(arr, N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
class GFG{
// Function to sort by column
public static void sortbyColumn(int arr[][],
int col)
{
// Using built-in sort function Arrays.sort
Arrays.sort(arr, new Comparator()
{
@Override
// Compare values according to columns
public int compare(final int[] entry1,
final int[] entry2)
{
// To sort in descending order revert
// the '>' Operator
if (entry1[col] > entry2[col])
return 1;
else
return -1;
}
}); // End of function call sort().
}
// Function to count the minimum number of
// steps required to delete all the segments
static int cntMinSteps(int[][] arr, int N)
{
// Stores count of steps required
// to delete all the line segments
int cntSteps = 1;
// Sort the array based on end points
// of line segments
sortbyColumn(arr, 1);
// Stores point on X-axis
int Points = arr[0][1];
// Traverse the array
for(int i = 0; i < N; i++)
{
// If arr[1][0] is
// greater than Points
if(arr[i][0] > Points)
{
// Update cntSteps
cntSteps++;
// Update Points
Points = arr[i][1];
}
}
return cntSteps;
}
// Driver Code
public static void main(String[] args)
{
int[][] arr = { { 9, 15 }, { 3, 8 },
{ 1, 6 }, { 7, 12 },
{ 5, 10 } };
int N = arr.length;
System.out.print(cntMinSteps(arr, N));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program to implement
# the above approach
# Comparator function
def comp(x, y):
return x[1] < y[1]
# Function to count the
# minimum number of steps
# required to delete all
# the segments
def cntMinSteps(arr, N):
# Stores count of steps
# required to delete all
# the line segments
cntSteps = 1
# Sort the array based
# on end points of line
# segments
arr.sort(reverse = False)
# Stores point on X-axis
Points = arr[0][1]
# Traverse the array
for i in range(N):
# If arr[1][0] is
# greater than Points
if(arr[i][0] > Points):
# Update cntSteps
cntSteps += 1
# Update Points
Points = arr[i][1]
return cntSteps
# Driver Code
if __name__ == '__main__':
arr = [[9, 15], [3, 8],
[1, 6], [7, 12],
[5, 10]]
N = len(arr)
print(cntMinSteps(arr, N))
# This code is contributed by bgangwar59
2
时间复杂度: O(N * log(N))
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live