Data Structure Array: Maximum circular subarray sum
http://www.geeksforgeeks.org/maximum-contiguous-circular-sum/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; bool allneg(int arr[], int n) {
for (int i = ; i < n; i++) {
if (arr[i] > ) return false;
}
return true;
} int maxsum(int arr[], int n) {
int tmp = ;
int ans = INT_MIN;
for (int i = ; i < n; i++) {
tmp += arr[i];
ans = max(ans, tmp);
tmp = max(tmp, );
}
return ans;
} int maxcircular(int arr[], int n) {
int ans = maxsum(arr, n);
if (allneg(arr, n)) return ans;
int sum = ;
for (int i = ; i < n; i++) {
sum += arr[i];
arr[i] = -arr[i];
}
return max(ans, sum + maxsum(arr, n));
} int main() {
int arr[] = {, , -, , -, -, , -, };
cout << maxcircular(arr, ) << endl;
return ;
}
Data Structure Array: Maximum circular subarray sum的更多相关文章
- Data Structure Array: Maximum sum such that no two elements are adjacent
http://www.geeksforgeeks.org/maximum-sum-such-that-no-two-elements-are-adjacent/ #include <iostre ...
- Data Structure Array: Maximum of all subarrays of size k
http://www.geeksforgeeks.org/maximum-of-all-subarrays-of-size-k/ #include <iostream> #include ...
- Subarray Sum & Maximum Size Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- Subarray Sum & Maximum Size Subarray Sum Equals K && Subarray Sum Equals K
Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...
- leetcode 560. Subarray Sum Equals K 、523. Continuous Subarray Sum、 325.Maximum Size Subarray Sum Equals k(lintcode 911)
整体上3个题都是求subarray,都是同一个思想,通过累加,然后判断和目标k值之间的关系,然后查看之前子数组的累加和. map的存储:560题是存储的当前的累加和与个数 561题是存储的当前累加和的 ...
- [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- Maximum Size Subarray Sum Equals k LT325
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组
Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...
- Data Structure Array: Find if there is a subarray with 0 sum
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #incl ...
随机推荐
- 【转】 IntelliJ IDEA 详细图解最常用的配置 ,适合刚刚用的新人
本文转载于:https://blog.csdn.net/qq_27093465/article/details/52918873 刚刚使用IntelliJ IDEA 编辑器的时候,会有很多设置,会方便 ...
- git个人使用总结(命令版)
一.基础命令 快照类操作:add.status.diff.commit.reset.rm.mv 分支类基本操作:branch.checkout.log.stash 分享及更新项目基本操作:pull.p ...
- mongodb在Windows安装配置及遇到的问题、java连接测试
一.安装 1.访问mongodb的官网http://www.mongodb.org/downloads下载64bit的包,我下载的是mongodb-win32-x86_64-2008plus-ssl- ...
- mongoDB 高级查询语法
http://www.cnblogs.com/ITAres/articles/2084794.html本文参考自官方的手册:http://www.mongodb.org/display/DOCS/Ad ...
- block知识点
1.block引用局部变量的时候,该变量会作为常量编码到block中,在block中不能被修改. 2.使用 __block修饰的局部变量,不会作为常量被编码到block中,故而在block中可以被修改 ...
- 转 SQL行转列汇总
1.PIVOT 用于将列值旋转为列名(即行转列) PIVOT 的一般语法是:PIVOT(聚合函数(列名) FOR 列名 in (列值1,…) )AS P select * from TB pivot ...
- zabbix监控xenserver
xenserver是基于redhat的,可以在zabbix官网下载对应的redhat zabbix安装包,直接安装即可 http://repo.zabbix.com/zabbix/3.0/rhel/5 ...
- linux下 apache启动、停止、重启命令
假设你的apahce安装目录为/usr/local/apache2,这些方法适合任何情况 apahce启动命令:推荐/usr/local/apache2/bin/apachectl start apa ...
- c# 获取毫秒值,时间戳
获取时间戳(秒) (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000 获取时间戳(毫秒) (DateTime. ...
- smarty、smarty格式化、smarty整数、smarty float、smarty各种转换方式、smarty日期转换等等 (转)
<? require("setup.php"); define('PAGETITLE','pagtitle'); function insert_top($lid,$sid) ...