最大化分区,如果单独排序会使整个数组排序
给定一个数组arr[] 。任务是将arr[]划分为最大数量的分区,这样,如果这些分区单独排序,则使整个排序数组排序。
例子:
Input: arr[] = { 28, 9, 18, 32, 60, 50, 75, 70 }
Output: 4
Explanation: Following are the partitions in which the array is divided.
If divide arr[] into four partitions {28, 9, 18}, {32}, { 60, 50}, and {75, 70} sort then and concatenate.
Sorting all of them indivudually makes the whole array sorted.
Hence, 4 is the answer.
Input: arr[] = { 2, 1, 0, 3, 4, 5 }
Output: 4
方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。
- 创建一个最大数组,计算左边的最大元素,直到数组的索引。
- 创建一个最小数组,计算右边的最小元素,直到数组的索引。
- 遍历数组,每次leftMax[]的所有元素都小于(或等于) rightMax[]的所有元素,这意味着有一个新块,因此将计数增加1 。
- 返回count+1作为最终答案。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find maximum partitions.
int maxPartitions(vector& arr)
{
int N = arr.size();
// To keep track of max
// and min elements at every index
vector leftMax(arr.size());
vector rightMin(arr.size());
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = max(leftMax[i - 1],
arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = min(rightMin[i + 1],
arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
int main()
{
vector arr{ 10, 0, 21, 32, 68 };
cout << maxPartitions(arr);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
public class GFG {
// Function to find maximum partitions.
static int maxPartitions(int[] arr)
{
int N = arr.length;
// To keep track of max
// and min elements at every index
int[] leftMax = new int[arr.length];
int[] rightMin = new int[arr.length];
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = Math.max(leftMax[i - 1], arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = Math.min(rightMin[i + 1], arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
public static void main(String args[])
{
int[] arr = { 10, 0, 21, 32, 68 };
System.out.println(maxPartitions(arr));
}
}
// This code is contributed by Samim Hossain Mondal.
Python
# Python program for above approach
# Function to find maximum partitions.
def maxPartitions(arr):
N = len(arr)
# To keep track of max
# and min elements at every index
leftMax = []
rightMin = []
leftMax.append(arr[0])
for i in range(1, N):
leftMax.append(max(leftMax[i - 1], arr[i]))
rightMin.append(arr[N - 1])
for i in range(1, N):
rightMin.append(min(rightMin[i - 1], arr[N - i - 1]))
rightMin.reverse()
count = 0
for i in range(0, N - 1):
if (leftMax[i] <= rightMin[i + 1]):
count = count + 1
# Return count + 1 as the final answer
return count + 1
# Driver code
arr = [10, 0, 21, 32, 68]
print(maxPartitions(arr))
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for above approach
using System;
class GFG {
// Function to find maximum partitions.
static int maxPartitions(int[] arr)
{
int N = arr.Length;
// To keep track of max
// and min elements at every index
int[] leftMax = new int[arr.Length];
int[] rightMin = new int[arr.Length];
leftMax[0] = arr[0];
for (int i = 1; i < N; i++) {
leftMax[i] = Math.Max(leftMax[i - 1], arr[i]);
}
rightMin[N - 1] = arr[N - 1];
for (int i = N - 2; i >= 0; i--) {
rightMin[i] = Math.Min(rightMin[i + 1], arr[i]);
}
int count = 0;
for (int i = 0; i < N - 1; i++) {
if (leftMax[i] <= rightMin[i + 1]) {
count++;
}
}
// Return count + 1 as the final answer
return count + 1;
}
// Driver code
public static void Main()
{
int[] arr = { 10, 0, 21, 32, 68 };
Console.WriteLine(maxPartitions(arr));
}
}
// This code is contributed by ukasp.
Javascript
输出
4
时间复杂度: O(N)
辅助空间: O(N)