You are given an array A consisting of the integers −1, 0 and 1. A slice of that array is any pair of integers (P, Q) such that 0 ≤ P ≤ Q < N. Your task is to find the longest slice of A whose elements yield a non-negative sum.
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A of length N, consisting only of the values −1, 0, 1, returns the length of the longest slice of A that yields a non-negative sum. If there's no such slice, your function should return 0.
For example, given A = [−1, −1, 1, −1, 1, 0, 1, −1, −1], your function should return 7, as the slice starting at the second position and ending at the eighth is the longest slice with a non-negative sum.
For another example, given A = [1, 1, −1, −1, −1, −1, −1, 1, 1] your function should return 4: both the first four elements and the last four elements of array A are longest valid slices.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [2..100,000];
- each element of array A is an integer within the range [−1..1].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
int maxPossibleLength = A.length;
// Special Case:
if (sum > -1) {
return A.length;
} else {
maxPossibleLength = A.length + sum;
}
int solution = 0;
int currentSolution = 0;
for (int i = 0; i < A.length - solution; i++) {
int s = 0;
currentSolution = 0;
for (int j = i; j < i + maxPossibleLength && j < A.length; j++) {
s += A[j];
currentSolution++;
if (s > -1) {
solution = Math.max(solution, currentSolution);
}
}
}
return solution;
}
Solution.java:9: error: illegal start of type for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: ')' expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: illegal start of type for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: <identifier> expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: ';' expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: > expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: '(' expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: <identifier> expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: illegal start of type for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: <identifier> expected for (int i = 0; i < A.length; i++) { ^ Solution.java:9: error: ';' expected for (int i = 0; i < A.length; i++) { ^ Solution.java:10: error: illegal start of type sum += A[i]; ^ Solution.java:10: error: ']' expected sum += A[i]; ^ Solution.java:10: error: illegal start of type sum += A[i]; ^ Solution.java:10: error: <identifier> expected sum += A[i]; ^ Solution.java:10: error: ';' expected sum += A[i]; ^ Solution.java:15: error: illegal start of type if (sum > -1) { ^ Solution.java:15: error: <identifier> expected if (sum > -1) { ^ Solution.java:15: error: ';' expected if (sum > -1) { ^ Solution.java:15: error: illegal start of type if (sum > -1) { ^ Solution.java:15: error: <identifier> expected if (sum > -1) { ^ Solution.java:15: error: ';' expected if (sum > -1) { ^ Solution.java:16: error: illegal start of type return A.length
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
int maxPossibleLength = A.length;
// Special Case:
if (sum > -1) {
return A.length;
} else {
maxPossibleLength = A.length + sum;
}
int solution = 0;
int currentSolution = 0;
for (int i = 0; i < A.length - solution; i++) {
int s = 0;
currentSolution = 0;
for (int j = i; j < i + maxPossibleLength && j < A.length; j++) {
s += A[j];
currentSolution++;
if (s > -1) {
solution = Math.max(solution, currentSolution);
}
}
}
return solution;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
int maxPossibleLength = A.length;
// Special Case:
if (sum > -1) {
return A.length;
} else {
maxPossibleLength = A.length + sum;
}
int solution = 0;
int currentSolution = 0;
for (int i = 0; i < A.length - solution; i++) {
int s = 0;
currentSolution = 0;
for (int j = i; j < i + maxPossibleLength && j < A.length; j++) {
s += A[j];
currentSolution++;
if (s > -1) {
solution = Math.max(solution, currentSolution);
}
}
}
return solution;
}
}
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int sum = 0;
for (int i = 0; i < A.length; i++) {
sum += A[i];
}
int maxPossibleLength = A.length;
// Special Case:
if (sum > -1) {
return A.length;
} else {
maxPossibleLength = A.length + sum;
}
int solution = 0;
int currentSolution = 0;
for (int i = 0; i < A.length - solution; i++) {
int s = 0;
currentSolution = 0;
for (int j = i; j < i + maxPossibleLength && j < A.length; j++) {
s += A[j];
currentSolution++;
if (s > -1) {
solution = Math.max(solution, currentSolution);
}
}
}
return solution;
}
}
The following issues have been detected: timeout errors.
array of fibonacci words N = 100000
running time: >8.00 sec., time limit: 2.27 sec.