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 ...
随机推荐
- PTA L2-001 紧急救援
题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805073643683840 输入: 输入第一行给出4个正整数N ...
- HTML(简介及常用标签)
一.HTML简介 1.1 html是什么? 超文本标记语言(Hypertext Markup Language,HTML)通过标签语言来标记要显示的网页中的各个部分.一套规则,浏览器认识的规则. 浏览 ...
- PAT 1011 World Cup Betting
1011 World Cup Betting (20 分) With the 2010 FIFA World Cup running, football fans the world over w ...
- mybatis调用存储过程的两种方式
先总结和说明一下注意点: 1.如果传入的某个参数可能为空,必须指定jdbcType 2.当传入map作为参数时,必须指定JavaType 3.如果做动态查询(参数为表名,sql关键词),可以使用${} ...
- Oracle 11.2.0.4.0 Dataguard部署和日常维护(5)-Datauard 主备切换和故障转移篇
1. dataguard主备切换 1.1. 查看当前主备库是否具备切换条件 on slave select sequence#,first_time,next_time,archived,appl ...
- 九、持久层框架(MyBatis)
一.基于MyBatis的对象关系配置(基于XML方式的配置) 注: MyBatis不能像Hibernate那样,在实体类上配置上注解或者配置xml映射文件,系统启动后就可以自动创建表.因为MyBati ...
- java旅程(二) 基本语法
java变量: 数值型 整数类型(byte,short,int,long)浮点类型(float,double) 字符型 char 基本数据类型 ...
- html 巧用data-for藏自定义属性
<div class="form-ele"> <label for="week" class="label col-1"& ...
- 【转】你必须知道的EF知识和经验
注意:以下内容如果没有特别申明,默认使用的EF6.0版本,code first模式. 推荐MiniProfiler插件 工欲善其事,必先利其器. 我们使用EF和在很大程度提高了开发速度,不过随之带来的 ...
- java 类变量初始化顺序
假定有一个类定义如下: package com.zhang; public final class Girl { // static代码块1 private static String sex = & ...