For a given array A of N integers and a sequence S of N integers from the set {−1, 1}, we define val(A, S) as follows:
val(A, S) = |sum{ A[i]*S[i] for i = 0..N−1 }|
(Assume that the sum of zero elements equals zero.)
For a given array A, we are looking for such a sequence S that minimizes val(A,S).
Write a function:
class Solution { public int solution(int[] A); }
that, given an array A of N integers, computes the minimum value of val(A,S) from all possible values of val(A,S) for all possible sequences S of N integers from the set {−1, 1}.
For example, given array:
A[0] = 1 A[1] = 5 A[2] = 2 A[3] = -2your function should return 0, since for S = [−1, 1, −1, 1], val(A, S) = 0, which is the minimum possible value.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..20,000];
- each element of array A is an integer within the range [−100..100].
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=-1;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<max; i++){
if(hash[j]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-can<<1);
}
}
Solution.java:31: error: cannot find symbol if(hash[j]>0){ ^ symbol: variable j location: class Solution 1 error
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=-1;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-can<<1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-can<<1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
//if(can>=half) break;
}
return Math.abs(sum-can<<1);
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
//if(can>=half) break;
}
return Math.abs(sum-(can<<1));
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<=max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-(can<<1));
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<=max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-(can<<1));
}
}
// you can also use imports, for example:
// import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
int[] hash = new int[101];
int max = 0;
int sum = 0;
for(int i=0; i<A.length; i++){
int a=Math.abs(A[i]);
hash[a]++;
sum+=a;
max = Math.max(max,a);
}
//
int half = sum/2+1;
int[] dp = new int[half];
for(int i=0; i<dp.length; i++){
dp[i]=-1;
}
dp[0]=0;
int can=0;
// dp save the left number of item i to achive the sum j
// -1 means sum 0-i < j
for(int i=1; i<=max; i++){
if(hash[i]>0){
for(int j=0; j<dp.length; j++){
if(dp[j]>=0){
dp[j]=hash[i];
if(j>can){
can = j;
}
}
else{
if(j-i>=0 && dp[j-i]>0){
dp[j] = dp[j-i]-1;
if(j>can){
can = j;
}
}
}
}
}
if(can>=half) break;
}
return Math.abs(sum-(can<<1));
}
}
The solution obtained perfect score.