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 ...
随机推荐
- leetcode-algorithms-14 Longest Common Prefix
leetcode-algorithms-14 Longest Common Prefix Write a function to find the longest common prefix stri ...
- git push 失败出现error: src refspec master does not match any.解决方案
今天写好一个demo往GitHub上传时报错 错误提示: error: src refspec master does not match any. error: failed to push som ...
- 兼容IE8的video写法
<video width="100%" height="515px" controls preload> <source src=" ...
- LeetCode 958. 二叉树的完全性检验
958. 二叉树的完全性检验 显示英文描述 我的提交返回竞赛 用户通过次数119 用户尝试次数157 通过次数123 提交次数378 题目难度Medium 给定一个二叉树,确定它是否是一个完全二 ...
- git忽略未被跟踪和已被跟踪的文件
git的文件操作本质上来讲是基于文件索引来做追踪的. 至于忽略未跟踪(untrack)文件文件,git提供了三种方式 1 .gitignore 2 git config --global core ...
- win10 中redis client提示 ERR Client sent AUTH,but no password is set
[问题原因]Redis服务器没有设置密码,但客户端向其发送了AUTH(authentication,身份验证)请求. [解决办法] 确定Redis启动时指定是哪个配置文件 如上图是 redis.win ...
- ActiveMQ broker 非持久化queue消息的入队、出队和应答
消息入队:Queue.doMessageSend 消息分发:Queue.doActualDispatch 消息发送:TransportConnection.dispatch broker收到consu ...
- SSL证书读取
证书内容: MIIDhDCCAmygAwIBAgIFAV0Imw0wDQYJKoZIhvcNAQELBQAwXDEnMCUGA1UEAwweczUwLTYyLTEzNS0xNS5zZWN1cmVzZX ...
- Linux基线合规检查中各文件的作用及配置脚本
1./etc/motd 操作:echo " Authorized users only. All activity may be monitored and reported " ...
- python 数字格式化
第二种办法比较常用: %02d print '%02d' % 11