求整数随机数构成的数组中找到长度大于=3的最长的等差数列

27 篇文章 0 订阅

转自:http://blog.csdn.net/cwqbuptcwqbupt/article/details/7546674点击打开链接


求整数随机数构成的数组中找到长度大于=3的最长的等差数列

输出等差数列由小到大: 

如果没有符合条件的就输出[0,0]

格式:

输入[1,3,0,5,-1,6]

输出[-1,1,3,5]

要求时间复杂度,空间复杂度尽量小

网上有动态规划的解法,时间复杂度O(N^3):http://openuc.sinaapp.com/?p=286

而本文要提另一种动态规划解法,时间复杂度O(N^2),空间复杂度比较大,一会说。

方法:

第一步都一样,先排序。

第二步,动态规划。在一个已排好序的数列中,等差数列的差d满足 0 <= d <= a[N-1] - a[0]。于是,设一个二维表dp,有a[N-1] - a[0] + 1 行,N列,dp[i][j]记录数列a[j],公差为i下的最长数列长度。那么很明显有:dp[i][j] = dp[i][  index_of( a[j] + i )  ] + 1。其中index_of(num)表示数num在数组a中的索引。上述dp的意思是:如果a[j]能构成等差数列的一份子,公差为i,那么它的下一项就是a[j] + i,这当然要求a[j] + i存在于数组a中啦~而且,a[j]构成的数列的长度就是由 a[j] + i 构成数列长度加1. 依据上述分析,只要对数组a由尾到头遍历,对每个a[j],求出所有公差从0到a[N-1]-a[0]下的最长数列长度,则问题就得解了。

注意几个问题:

1. 上述分析过程中要求求出所有公差从0到a[N-1]-a[0],但实际上并不需要这么一个一个的求,因为以任何a[j],它能构成等差数列,则公差一定是 a[  k ] - a[ j ],这里 j < k < N,因此,求解的范围得到缩小,因此整体的时间复杂度为0(N^2)。

2. 另一个实现问题是,dp只记录了最长数列的长度,而我们为了能回朔并输出等差数列,我们还需要知道构成最长等差数列a[j]的下一个数是什么,因此,需要同时记录下一跳的索引。在代码中,我用pair<int,int>来记录,first记录长度,second记录下一跳索引。

3. 注意处理a[j]与多个数差值相同的情况,比如 1,3,3,对a[0]=1,它和a[1],a[2]的差值相同,所以对于a[0],公差为2而言,即dp[2][0],它只需要更新一次即可。


  1. #include "stdafx.h"  
  2.   
  3. #include <stdio.h>  
  4. #include <iostream>  
  5. using namespace std;  
  6.   
  7. const int N = 10;  
  8. const int INVALID_IDX = -1;  
  9.   
  10. void show(int* a,int n)  
  11. {  
  12.     for (int i=0;i<n;++i)  
  13.     {  
  14.         cout<<a[i]<<",";  
  15.     }  
  16.     cout<<endl;  
  17. }  
  18.   
  19. inline int compare(const void* p1,const void* p2)  
  20. {  
  21.     return *(int*)p1 - *(int*)p2;  
  22. }  
  23.   
  24. void longest_seq(int* a)  
  25. {  
  26.     qsort(a,N,sizeof(int),&compare);  
  27.   
  28.     int R = a[N-1]-a[0]+1;  
  29.     pair<int,int>** dp = new pair<int,int>*[R];  
  30.     for (int i=0;i<R;++i)  
  31.     {  
  32.         pair<int,int>* row = new pair<int,int>[N];   
  33.         for (int j=0;j<N;++j)  
  34.         {  
  35.             row[j].first = 0;       //记录当前最长数列的长度  
  36.             row[j].second = INVALID_IDX;//记录与first相对应的等差数列的下一值在数组a中的索引  
  37.         }  
  38.         dp[i] = row;  
  39.     }  
  40.       
  41.     int maxlen = 0;  
  42.     int rowidx = INVALID_IDX;  
  43.     int colidx = INVALID_IDX;  
  44.   
  45.     for (int i=N-2;i>=0;--i)  
  46.     {  
  47.         for (int j=i+1;j<N;++j)  
  48.         {  
  49.             if (dp[ a[j]-a[i] ][i].first != 0) continue;    //以该“差”为行号的值如果已经存在,就不需要再为相同的差值更新  
  50.               
  51.             dp[ a[j]-a[i] ][i].first = dp[ a[j]-a[i] ][j].first + 1;  
  52.             dp[ a[j]-a[i] ][i].second = j;  
  53.   
  54.             if (dp[ a[j]-a[i] ][i].first > maxlen)  
  55.             {  
  56.                 maxlen = dp[ a[j]-a[i] ][i].first;  
  57.                 rowidx = a[j]-a[i];  
  58.                 colidx = i;  
  59.             }  
  60.         }  
  61.     }  
  62.   
  63.     if( maxlen > 1 )  
  64.     {  
  65.         cout<<"The longest seq is:"<<endl;  
  66.         while( colidx != INVALID_IDX )  
  67.         {  
  68.             cout<<a[colidx]<<",";  
  69.             colidx = dp[rowidx][colidx].second;  
  70.         }  
  71.         cout<<endl;  
  72.     }  
  73.     else  
  74.     {  
  75.         cout<<"0,0"<<endl;  
  76.     }  
  77.   
  78.     for (int i=0;i<R;++i)  
  79.         delete []dp[i];  
  80.   
  81.     delete []dp;  
  82. }  
  83.   
  84.   
  85. int main(void)  
  86. {  
  87.     int a[N] = {8, 8, 7, 4, 1, 3, 3, 1, 8, 4};  
  88.     longest_seq(a);  
  89.   
  90.     return 0;  


在 Java 中,你可以使用 `Random` 类来生成指定范围内的随机数,并结合数组和循环来完成这个任务。以下是具体的步骤: 1. 首先,获取用户输入的数组长度随机数种子,例如: ```java Scanner scanner = new Scanner(System.in); System.out.print("请输入数组长度:"); int length = scanner.nextInt(); System.out.print("请输入随机数种子(建议设置固定的值,如 System.currentTimeMillis()):"); long seed = scanner.nextLong(); // 使用当前时间作为默认值,也可以手动输入 ``` 2. 创建一个大小为输入长度的整数数组,并用 `Random` 类生成随机数: ```java int[] numbers = new int[length]; Random random = new Random(seed); for (int i = 0; i < length; i++) { numbers[i] = random.nextInt(20); // 生成0到19之间的随机整数 } ``` 3. 计算每个随机数出现的次数,可以使用哈希表(HashMap)存储计数: ```java Map<Integer, Integer> countMap = new HashMap<>(); for (int number : numbers) { countMap.put(number, countMap.getOrDefault(number, 0) + 1); } ``` 4. 输出出现次数大于0的随机数及其出现次数: ```java countMap.entrySet().stream() .filter(entry -> entry.getValue() > 0) .forEach(entry -> System.out.println("数字 " + entry.getKey() + " 出现了 " + entry.getValue() + " 次")); ``` 5. 对数组进行排序(这里假设你想要按照自然顺序排序): ```java Arrays.sort(numbers); ``` 6. 最后,遍历已排序的数组输出: ```java System.out.println("按升序排列后的数组:"); for (int num : numbers) { System.out.print(num + " "); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值