divide&conquer:find max array
package max_subarrayy;
import java.lang.Math;
public class max_subarrayy {
private static int[] array;
public static class mark{
private int lom = 100;
private int him;
private int value;
public mark(int a,int b,int c){
lom = a;him = b;value = c;
}
}
public static mark merge(int low,int high){ //return max line
int N = high + low ; // should plus when to get the mid number *high-low,high
double mid = (double)N/2;
if(low == high){return new mark(low,high,array[low]);}
mark leftmax = merge(low,(int)Math.floor(mid));
mark rightmax = merge((int)Math.floor(mid)+1,high);//even number error *Math ceil
mark midmax = findmidmax(low,high,(int)Math.floor((float)N/2));
int k = (Math.max(leftmax.value, rightmax.value) > midmax.value) ? Math.max(leftmax.value, rightmax.value) : midmax.value;
if(k == leftmax.value){return leftmax;}
else if(k == rightmax.value){return rightmax;}
else{return midmax;}
}
public static mark findmidmax(int low,int high,int mid){
int max = array[mid] + array[mid + 1];
int temp = max;
int lom = mid;
int him = mid +1;
for(int i = mid -1;i >= low;i--){
temp = temp + array[i];
if(temp > max){
max = temp;
lom = i;
}
}
temp = max;
for(int i = mid +2;i <= high;i++){// plus 2 *mid +1
temp = temp + array[i];
if(temp > max){
max = temp;
him = i;
}
}
return new mark(lom,him,max);
}
public static void main(String[] args){
int N = args.length;
array = new int[N];
for(int i = 0;i < N;i++){
array[i] = Integer.parseInt(args[i]);
}
mark a = merge(0,N-1);
System.out.println(a.lom + " " + a.him +" " + a.value);
}
}
divide&conquer:find max array的更多相关文章
- 九章算法系列(#3 Binary Tree & Divide Conquer)-课堂笔记
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode ...
- leetcode Ch4-Binary Tree & BFS & Divide/Conquer
一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, ...
- 分治法(divide & conquer)与动态规划(dynamic programming)应用举例
动态规划三大重要概念:最优子结构,边界,状态转移公式(问题规模降低,如问题由 n 的规模降低为 n−1 或 n−2 及二者之间的关系): 0. 爬台阶 F(n)⇒F(n−1)+F(n−2) F(n−1 ...
- 【Lintcode】122.Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- Divide and Conquer.(Merge Sort) by sixleaves
algo-C1-Introductionhtml, body {overflow-x: initial !important;}html { font-size: 14px; }body { marg ...
- [geeksforgeeks] Count the number of occurrences in a sorted array
Count the number of occurrences in a sorted array Given a sorted array arr[] and a number x, write a ...
- JS中Array详细用法
1.数组的创建 var name= new Array(); //创建一个数组 name[0]="zhangsan"; //给数组赋值 name[1]="lisi&q ...
- Scala入门之Array
/** * 大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: * 1,数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储现在比较经典的是使用Hadoop,也有很多情况使用Kaf ...
- Get the largest sum of contiguous subarray in an int array
When I finished reading this problem,I thought I could solve it by scanning every single subarray in ...
随机推荐
- Seagull License Server 9.4 SR3 2781 完美激活(解决不能打印问题)
BarTender 9.4 SR3完美激活方法 网上下载的BarTender 9.4大部分不能正常打印,已经测试过了,完美解决无法打印,界面停留在“无法打印,出现正在试图连接到seagull lice ...
- 『TensorFlow』SSD源码学习_其四:数据介绍及TFR文件生成
Fork版本项目地址:SSD 一.数据格式介绍 数据文件夹命名为VOC2012,内部有5个子文件夹,如下, 我们的检测任务中使用JPEGImages文件夹和Annotations文件夹. JPEGIm ...
- 5月13 PDO数据访问抽象层
方法1:较简单的 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- win php安装 oracle11 g
1.下载plsql和oracle11g plsql安装比较简单,就是普通的安装.oracle11 g不用安装, 下面我讲解一下win 64位的系统配置oracle: (1).首先我使用的是warpse ...
- Notes for 'Making elephants fly'
1. 技术陷阱:应是需求导向, 而不是技术导向. 2. 时机最重要:而不是创造力,团队,客户,产品,或技术. 3. 模仿:能模仿就模仿,不能模仿就创新.巧匠摹形,大师窃意. good artists ...
- java 中多播、广播编程
在 IP 协议层,有多播.广播的概念.IP 地址分为网络地址和主机地址,主机地址全部为1,就是广播地址.多播使用 D 类地址,从 224.0.0.0 到 239.255.255.255.IP 多播需要 ...
- Qt_自定义菜单
一.右键菜单 右键菜单实现:通过重写contextMenuEvent(QContextMenuEvent *event)事件,QMenu+QAction即可完美实现! 重写voidcontextMen ...
- windows7安装教程(vmware)
这步是正确安装windows的关键,如果不设置那么安装时将不能识别出磁盘,造成安装不成功. 选择No进行自定义修饰,主要是保证C盘大小合适,其他盘可在安装完成之后再调整. 后续安装步骤全自动,完全不用 ...
- cpu占用过高排查
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器 内容解释: PID:进程的ID USER:进程所有者 PR:进程的优先级别,越小 ...
- 解决libvlc_media_player_stop时死锁的方法
转自:http://www.jiazi.cn/blog/?id=56 扩散该解决方法 最近需要使用VLC控件来做一个简单的流媒体播放器,在实施过程中,发现在调用libvlc_media_player_ ...