HackerRank The Maximum Subarray
2016-07-22 06:56
375 查看
原题网址:https://www.hackerrank.com/challenges/maxsubarray
Given an array of elements,
find the maximum possible sum of a
Contiguous subarray
Non-contiguous (not necessarily contiguous) subarray.
Empty subarrays/subsequences should not be considered.
Input Format
First line of the input has an integer . cases
follow.
Each test case begins with an integer .
In the next line, integers
follow representing the elements of array .
Constraints:
The subarray and subsequences you consider
should have at least one element.
Output Format
Two, space separated, integers denoting the maximum contiguous and non-contiguous subarray. At least one integer should be selected and put into the subarrays (this may be required in cases where all elements are negative).
Sample Input
Sample Output
Explanation
In the first case:
The max sum for both contiguous and non-contiguous elements is the sum of ALL the elements (as they are all positive).
In the second case:
[2 -1 2 3 4] --> This forms the contiguous sub-array with the maximum sum.
For the max sum of a not-necessarily-contiguous group of elements, simply add all the positive elements.
方法:动态规划。
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for(int i = 0; i < t; i++) {
int n = scanner.nextInt();
int num = scanner.nextInt();
int maxContiguous = num;
int maxNonContiguous = num;
int sumContiguous = num;
sumContiguous = Math.max(sumContiguous, 0);
for(int j = 1; j < n; j++) {
num = scanner.nextInt();
maxNonContiguous = Math.max(maxNonContiguous, Math.max(maxNonContiguous + num, num));
sumContiguous += num;
maxContiguous = Math.max(maxContiguous, sumContiguous);
sumContiguous = Math.max(sumContiguous, 0);
}
System.out.println(maxContiguous + " " + maxNonContiguous);
}
}
}
Given an array of elements,
find the maximum possible sum of a
Contiguous subarray
Non-contiguous (not necessarily contiguous) subarray.
Empty subarrays/subsequences should not be considered.
Input Format
First line of the input has an integer . cases
follow.
Each test case begins with an integer .
In the next line, integers
follow representing the elements of array .
Constraints:
The subarray and subsequences you consider
should have at least one element.
Output Format
Two, space separated, integers denoting the maximum contiguous and non-contiguous subarray. At least one integer should be selected and put into the subarrays (this may be required in cases where all elements are negative).
Sample Input
2 4 1 2 3 4 6 2 -1 2 3 4 -5
Sample Output
10 10 10 11
Explanation
In the first case:
The max sum for both contiguous and non-contiguous elements is the sum of ALL the elements (as they are all positive).
In the second case:
[2 -1 2 3 4] --> This forms the contiguous sub-array with the maximum sum.
For the max sum of a not-necessarily-contiguous group of elements, simply add all the positive elements.
方法:动态规划。
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for(int i = 0; i < t; i++) {
int n = scanner.nextInt();
int num = scanner.nextInt();
int maxContiguous = num;
int maxNonContiguous = num;
int sumContiguous = num;
sumContiguous = Math.max(sumContiguous, 0);
for(int j = 1; j < n; j++) {
num = scanner.nextInt();
maxNonContiguous = Math.max(maxNonContiguous, Math.max(maxNonContiguous + num, num));
sumContiguous += num;
maxContiguous = Math.max(maxContiguous, sumContiguous);
sumContiguous = Math.max(sumContiguous, 0);
}
System.out.println(maxContiguous + " " + maxNonContiguous);
}
}
}
相关文章推荐
- [hackerrank]Weekly Challenges - Week 11
- HackerRank Weekly Challenges - Week 6: Minimum Average Waiting Time 停不下来的厨师
- HackerRank Weekly Challenges - Week 6: Consecutive Subsequences 你有余我也有余
- HackerRank Largest Rectangle
- HackerRank Find Median(中位数)
- HackerRank Huffman Decoding(Huffman解码)
- HackerRank Truck Tour
- HackerRank Self Balancing Tree(AVL树)
- hackerrank_Permutation game
- hackerrank_Chocolate Game
- HackerRank - Stock Maximize
- HackerRank Fibonacci Modified
- HackerRank Breadth First Search: Shortest Reach
- HackerRank Service Lane
- HackRank Cut the sticks
- HackerRank [Algo] Matrix Rotation
- HackRank Two Two
- HackRank Stacks: Balanced Brackets;/Leetcode valid-parentheses; 括号匹配
- Leetcode 合并有序数组 Merge 2 arrays in 1 array
- HackerRank Find first repeating letter in a string