最大子序列的四种算法

简介:
None.gif package Chapter1;
None.gif
ExpandedBlockStart.gif public  class MaxSubSum  {
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Cubic maximun contiguous susequence sum algorithm.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public static int MaxSubSum1(int[] a) {
InBlock.gif        int maxSum = 0;
ExpandedSubBlockStart.gif        for (int i = 0; i < a.length; i++) {
ExpandedSubBlockStart.gif            for (int j = i; j < a.length; j++) {
InBlock.gif                int thisSum = 0;
InBlock.gif
InBlock.gif                for (int k = i; k <= j; k++)
InBlock.gif                    thisSum += a[k];
InBlock.gif
InBlock.gif                if (thisSum > maxSum)
InBlock.gif                    maxSum = thisSum;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        return maxSum;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Quadratic maximum contiguous subsequence sum algorithm.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public static int maxSubSum2(int[] a) {
InBlock.gif        int maxSum = 0;
InBlock.gif
ExpandedSubBlockStart.gif        for (int i = 0; i < a.length; i++) {
InBlock.gif            int thisSum = 0;
ExpandedSubBlockStart.gif            for (int j = i; j < a.length; j++) {
InBlock.gif                thisSum += a[j];
InBlock.gif                if (thisSum > maxSum)
InBlock.gif                    maxSum = thisSum;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        return maxSum;
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Resursive maximum contiguous subsequence sum algorithm. Finds maximum sum
InBlock.gif     * in subarray spanning a[leftdot.gifright]. Does not attempt to maintain actual
InBlock.gif     * est sequence.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    private static int maxSumRec(int[] a, int left, int right) {
InBlock.gif        if (left == right) // Base case
InBlock.gif
            if (a[left] > 0)
InBlock.gif                return a[left];
InBlock.gif            else
InBlock.gif                return 0;
InBlock.gif
InBlock.gif        int center = (left + right) / 2;
InBlock.gif        int maxLeftSum = maxSumRec(a, left, center);
InBlock.gif        int maxRightSum = maxSumRec(a, center + 1, right);
InBlock.gif
InBlock.gif        int maxLeftBorderSum = 0, leftBorderSum = 0;
ExpandedSubBlockStart.gif        for (int i = center; i >= left; i--) {
InBlock.gif            leftBorderSum += a[i];
InBlock.gif            if (leftBorderSum > maxLeftBorderSum)
InBlock.gif                maxLeftBorderSum = leftBorderSum;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        int maxRightBorderSum = 0, rightBorderSum = 0;
ExpandedSubBlockStart.gif        for (int i = center + 1; i < right; i++) {
InBlock.gif            leftBorderSum += a[i];
InBlock.gif            if (rightBorderSum > maxRightBorderSum)
InBlock.gif                maxRightBorderSum = rightBorderSum;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        return max3(maxLeftSum, maxRightSum, maxLeftBorderSum
InBlock.gif                + maxRightBorderSum);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Return the max of the three number.
InBlock.gif     * 
InBlock.gif     * 
@param a
InBlock.gif     *            the first number.
InBlock.gif     * 
@param b
InBlock.gif     *            the second number.
InBlock.gif     * 
@param c
InBlock.gif     *            the thrid number.
InBlock.gif     * 
@return the max of the three number.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    private static int max3(int a, int b, int c) {
ExpandedSubBlockStart.gif        if (a > b) {
InBlock.gif            if (a > c)
InBlock.gif                return a;
InBlock.gif            else
InBlock.gif                return c;
ExpandedSubBlockStart.gif        }
 else {
InBlock.gif            if (b > c)
InBlock.gif                return b;
InBlock.gif            else
InBlock.gif                return c;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Driver for divide-and-conquer maximum contiguous subsequence sum
InBlock.gif     * algorithm
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public static int maxSubSum3(int[] a) {
InBlock.gif        return maxSumRec(a, 0, a.length - 1);
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gif    /**
InBlock.gif     * Linear-time maximum contiguous subsequence sum algorithm.
ExpandedSubBlockEnd.gif     
*/

ExpandedSubBlockStart.gif    public static int maxSubSum4(int[] a) {
InBlock.gif        int maxSum = 0, thisSum = 0;
InBlock.gif
ExpandedSubBlockStart.gif        for (int j = 0; j < a.length; j++) {
InBlock.gif            thisSum += a[j];
InBlock.gif            if (thisSum > maxSum)
InBlock.gif                maxSum = thisSum;
InBlock.gif            else if (thisSum < 0)
InBlock.gif                thisSum = 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        return maxSum;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
时间复杂度分别是:O(N 3),O(N 2),O(NlogN),O(N).
本文转自冬冬博客园博客,原文链接:http://www.cnblogs.com/yuandong/archive/2006/08/17/479690.html ,如需转载请自行联系原作者
相关文章
|
2月前
|
存储 算法 索引
模拟算法题练习(二)(DNA序列修正、无尽的石头)
模拟算法题练习(二)(DNA序列修正、无尽的石头)
|
2月前
|
设计模式 算法 Java
【数据结构和算法】递增的三元子序列
给你一个整数数组nums,判断这个数组中是否存在长度为3的递增子序列。 如果存在这样的三元组下标(i, j, k)且满足i < j < k,使得nums[i] < nums[j] < nums[k],返回true;否则,返回false。
62 3
|
2月前
|
编解码 算法 定位技术
GEE时序——利用sentinel-2(哨兵-2)数据进行地表物候学分析(时间序列平滑法估算和非平滑算法代码)
GEE时序——利用sentinel-2(哨兵-2)数据进行地表物候学分析(时间序列平滑法估算和非平滑算法代码)
179 3
|
2月前
|
算法
class072 最长递增子序列问题与扩展【算法】
class072 最长递增子序列问题与扩展【算法】
28 0
|
1天前
|
自然语言处理 算法 搜索推荐
字符串相似度算法完全指南:编辑、令牌与序列三类算法的全面解析与深入分析
在自然语言处理领域,人们经常需要比较字符串,这些字符串可能是单词、句子、段落甚至是整个文档。如何快速判断两个单词或句子是否相似,或者相似度是好还是差。这类似于我们使用手机打错一个词,但手机会建议正确的词来修正它,那么这种如何判断字符串相似度呢?本文将详细介绍这个问题。
139 1
|
16天前
|
机器学习/深度学习 人工智能 算法
【机器学习】Q-Learning算法:在序列决策问题中的实践与探索
【机器学习】Q-Learning算法:在序列决策问题中的实践与探索
22 0
【机器学习】Q-Learning算法:在序列决策问题中的实践与探索
|
29天前
|
算法 Shell C语言
数据结构与算法——希尔排序(引例、希尔增量序列、原始希尔排序、代码、时间复杂度、Hibbard增量序列、Sedgewick增量序列)
数据结构与算法——希尔排序(引例、希尔增量序列、原始希尔排序、代码、时间复杂度、Hibbard增量序列、Sedgewick增量序列)
15 0
|
29天前
|
算法
数据结构和算法学习记录——层序遍历(层次遍历)、二叉树遍历的应用(输出二叉树中的叶节点、求二叉树的高度、二元运算表达式树及其遍历、由两种遍历序列确定二叉树)
数据结构和算法学习记录——层序遍历(层次遍历)、二叉树遍历的应用(输出二叉树中的叶节点、求二叉树的高度、二元运算表达式树及其遍历、由两种遍历序列确定二叉树)
18 0
|
2月前
|
机器学习/深度学习 人工智能 运维
人工智能平台PAI 操作报错合集之请问Alink的算法中的序列异常检测组件,是对数据进行分组后分别在每个组中执行异常检测,而不是将数据看作时序数据进行异常检测吧
阿里云人工智能平台PAI (Platform for Artificial Intelligence) 是阿里云推出的一套全面、易用的机器学习和深度学习平台,旨在帮助企业、开发者和数据科学家快速构建、训练、部署和管理人工智能模型。在使用阿里云人工智能平台PAI进行操作时,可能会遇到各种类型的错误。以下列举了一些常见的报错情况及其可能的原因和解决方法。
|
2月前
|
算法 数据安全/隐私保护 数据格式
基于混沌序列的图像加解密算法matlab仿真,并输出加解密之后的直方图
该内容是一个关于混沌系统理论及其在图像加解密算法中的应用摘要。介绍了使用matlab2022a运行的算法,重点阐述了混沌系统的特性,如确定性、非线性、初值敏感性等,并以Logistic映射为例展示混沌序列生成。图像加解密流程包括预处理、混沌序列生成、数据混淆和扩散,以及密钥管理。提供了部分核心程序,涉及混沌序列用于图像像素的混淆和扩散过程,通过位操作实现加密。

热门文章

最新文章