给定两个整数M和N ,任务是找到N长度的数组的数量,该数组可能具有位于[1, M]范围内的不相等相邻元素,并且其第一个和最后一个索引的元素相等。
例子:
Input: N = 3, M = 3
Output: 6
Explanation:
The possible arrays are {1, 2, 1}, {1, 3, 1}, {2, 1, 2}, {2, 3, 2}, {3, 1, 3}, {3, 2, 3}.
Input: N = 5, M = 4
Output: 84
处理方法:按照以下步骤解决问题:
- 首先修正arr[0]和arr[N-1]等于 1。
- 现在找到大小为i以1结尾的可能数组的数量(即arr[i] = 1 )。将此结果存储到end_with_one[i] 中。
- 现在,找到大小为i且不以1结尾的数组的数量( arr[i] ≠ 1 )。将此结果存储到end_not_with_one[i] 中。
- 因为,直到第i个索引且arr[i] = 1为止形成数组的方法数与使用arr[i-1] 的第(i – 1)个索引为止形成数组的方法数相同≠ 1 ,设置end_with_one[i] = end_not_with_one[i – 1] 。
- 现在,直到第i个索引且arr[i] ≠ 1的数组形成方式如下:
- 如果 arr[i – 1]= 1,则有(M – 1) 个数字要放置在第i个索引处。
- 如果 arr[i – 1] ≠ 1,则(M – 2) 个数字可以放在索引i 处,因为arr[i]不能为 1 并且arr[i]不能等于arr[i – 1] 。
- 因此,设置end_not_with_one[i] = end_with_one[i-1] * (M – 1) + end_not_with_one[i-1]* (M – 2) 。
- 因此,形成大小为N且arr[0]和arr[N – 1]等于1 的数组的方法数是end_with_one[N – 1] 。
- 类似地, arr[0]和arr[N – 1]可以设置为从1到M 的任何元素。
- 因此,可能的数组总数为M * end_with_one[N-1] 。
下面是上述方法的实现:
C++14
// C++ program for the above approach
#include
using namespace std;
// Function to print the count of
// arrays satisfying given condition
int totalArrays(int N, int M)
{
int end_with_one[N + 1];
int end_not_with_one[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++) {
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver Code
int main()
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
cout << ans << "\n";
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to print the count of
// arrays satisfying given condition
static int totalArrays(int N, int M)
{
int []end_with_one = new int[N + 1];
int []end_not_with_one = new int[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++)
{
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver Code
public static void main(String[] args)
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
System.out.print(ans+ "\n");
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program for the above approach
# Function to prthe count of
# arrays satisfying given condition
def totalArrays(N, M):
end_with_one = [0] * (N + 1);
end_not_with_one = [0] * (N + 1);
# First element of
# array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
# Since the first element
# of arr is 1, the
# second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
# Traverse the remaining indices
for i in range(2, N):
# If arr[i] = 1
end_with_one[i] = end_not_with_one[i - 1];
# If arr[i] ≠ 1
end_not_with_one[i] = end_with_one[i - 1] * (M - 1) + end_not_with_one[i - 1] * (M - 2);
# Since last element needs to be 1
return end_with_one[N - 1];
# Driver Code
if __name__ == '__main__':
N = 3;
M = 3;
# Stores the count of arrays
# where arr[0] = arr[N - 1] = 1
temp = totalArrays(N, M);
# Since arr[0] and arr[N - 1]
# can be any number from 1 to M
ans = M * temp;
# Pranswer
print(ans);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to print the count of
// arrays satisfying given condition
static int totalArrays(int N, int M)
{
int[] end_with_one = new int[N + 1];
int[] end_not_with_one = new int[N + 1];
// First element of
// array is set as 1
end_with_one[0] = 1;
end_not_with_one[0] = 0;
// Since the first element
// of arr[] is 1, the
// second element can't be 1
end_with_one[1] = 0;
end_not_with_one[1] = M - 1;
// Traverse the remaining indices
for (int i = 2; i < N; i++) {
// If arr[i] = 1
end_with_one[i]
= end_not_with_one[i - 1];
// If arr[i] ≠ 1
end_not_with_one[i]
= end_with_one[i - 1] * (M - 1)
+ end_not_with_one[i - 1] * (M - 2);
}
// Since last element needs to be 1
return end_with_one[N - 1];
}
// Driver code
static void Main()
{
int N = 3, M = 3;
// Stores the count of arrays
// where arr[0] = arr[N - 1] = 1
int temp = totalArrays(N, M);
// Since arr[0] and arr[N - 1]
// can be any number from 1 to M
int ans = M * temp;
// Print answer
Console.WriteLine(ans);
}
}
// This code is contributed by divyeshrabadiya07
Javascript
输出:
6
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live