数组可以有多少个不同的分区?

时间:2019-03-27 11:34:58

标签: arrays

为您提供了一个大小为'n'的数组以及两个整数'l'和'r'。您必须找到数组的分区数,以使每个分区中的元素之和介于'l'和'r'(包括两端)之间。

Input: 

n = 5, l = 3, r = 12

a: [3, 5, 1, 2, 6]

Output: 

8

这些不同的分区如下:-

1){3512},{6}

2){351},{26}

3){35},{126}

4){35},{12},{6}

5){3},{512},{6}

6){3},{51},{26}

7){3},{5},{126}

8){3},{5},{12},{6}

4 个答案:

答案 0 :(得分:0)

据我了解

所以您需要对数组元素进行分组,而不是对其进行分组,并且我认为这是一个问题解决问题,我将在算法中回答,该算法来自o(n ^ 2)

您只需要检查l,r是否为排他性

   if n[x] + n[y] > min && n[x] + n[y] < max:   

您只需检查l,r是否包含在内的条件:

   if n[x] + n[y] => min && n[x] + n[y] <= max:

具有数组长度= n,最小值= l,最大值= r

 partition = 0
   for x = 0 to n.length - 1:
     for y = 1 to n.length - 1: 
       if n[x] + n[y] > min && n[x] + n[y] < max:
         partition++;   
       else:
         break;

在这里我做了JS表示

let n = [3, 5, 1, 2, 6]
let minN = 3, maxN = 12

let n1 = [1, 1]
let minN1 = 1, maxN1 = 2
       
console.log(getPartitions(n, minN, maxN))
console.log(getPartitions(n1, minN1, maxN1))
          
function getPartitions(n, minN, maxN) {
    let  partitions = 0;
    if (n.length > 2) {
        for (let i = 0; i < n.length - 1; i++) {
            for (let j = 1; j < n.length - 1; j++) {
                if (n[i] + n[j] > minN && n[i] + n[j] < maxN) {
                    partitions++;       
                } else {
                    break;
                }
            }
        }
    } else if (n.length == 2) {
        if (n[0] + n[1] >= minN && n[0] + n[1] <= maxN) {
            partitions = 1;
        }            
    }

    return partitions;
}

答案 1 :(得分:0)

您可以计算前缀总和,然后执行两次循环以检查每个分区(i,j)

这是一个代码:

psum[0] = 0
res = 0
for i in 1..n 
    psum[i] = psum[i-1] + a[i]
for i in 1..n
    for j in i..n
        if(psum[j] - psum[i-1] <= r && psum[j] - psum[i-1] >= l)
            res++;
print(res)

这是Java代码

int n = 5, l = 3, r = 12, ans = 0;
int a[] = {3, 5, 1, 2, 6};
int psum[] = new int[n+1];
for(int i = 1; i <= n; i++) psum[i] = psum[i-1] + a[i-1];
for(int i=1; i <= n; i++)
  for(int j=i; j <= n; j++)
     if(psum[j] - psum[i-1] <= r && psum[j] - psum[i-1] >= l)
        ans++;
System.out.println(ans);

答案 2 :(得分:0)

static int getPartitions(int[] arr, int minN, int maxN) {
        int n=arr.length;
        int[] ans=new int[n];

        int[]  parSum=new int[n];
        parSum[0]=arr[0];
        for (int i=1;i<n;i++){
            parSum[i]=parSum[i-1]+arr[i];
        }

        if (arr[n-1]<=maxN&&arr[n-1]>=minN) ans[n-1]=1;

        for (int i=n-2;i>=0;i--){
            int sum=0;
            for (int j=i;j<n;j++){
                sum+=arr[j];
                if (sum<=maxN&&sum>=minN){
                    if (j==n-1){
                        ans[i]+=1;
                    }
                    else ans[i]+=ans[j+1];
                }
            }
        }
        return ans[0];
    }

这可以肯定地工作。

答案 3 :(得分:0)

对数组进行分区的c ++代码

 //int n=5, l = 3, r = 12;
//int arr[5] = {3, 5, 1, 2, 6};
complete traverse this using given algo
it will be very help to understand an code given below 

ll a[n];

memset(a,0,sizeof(a));
        if (arr[n-1]<=r&&arr[n-1]>=l) a[n-1]=1;

      for (ll i=n-2;i>=0;i--){
            ll sum=0;
            for (ll j=i;j<n;j++){
                sum+=arr[j];
                if (sum<=r&&sum>=l){
                    if (j==n-1){
                        a[i]=a[i]%1000000007+1;
                    }
                    else a[i]=(a[i]%1000000007+a[j+1]%1000000007)%1000000007;
                }
            }
        }
print(a[0])