Java answer to Top Coder program fails for test case and I cant figure out why -


im trying solve topcoder problem , solution works example inputs excepts 1 im trying run right now, in main. it outputs 48 when should output 92. know has if (mindefeatable >= bestcase) { return bestcase;} in minimalfatigue function. know because if remove that, gives correct answer particular test case subsequently stops working on others. pulling hair trying figure out, appreciated.

problem statement

magical girls girls have magical powers. fight against evil protect earth. cosmic enemies have attacked earth, , magical girls going fight them. given magicalgirlstrength describes magical girls: each i, magicalgirlstrength[i] strength of 1 of girls. given enemystrength , enemycount describe enemies: each i, there enemycount[i] enemies each have strength enemystrength[i]. each magical girl fight 1 enemy @ time. magical girl defeat enemy if strength greater or equal strength of enemy.

at beginning of fight fatigue of each magical girl 0. each time magical girl defeats enemy, fatigue increases 1. magical girls want defeat enemies. is, each of enemies must defeated 1 of magical girls. additionally, magical girls want minimize maximum fatigue among them. if impossible defeat of enemies, return -1. otherwise, return smallest f following property: magical girls can defeat enemies in such way @ end fatigue of each girl @ f.

attempted solution (w/ test case fails in main)

import java.util.arrays;  public class spacewardiv2 {     public static int minimalfatigue(int[] girlstrength, int[] enemystrength, int[] enemycount) {         arrays.sort(girlstrength);         int girlsleft = girlstrength.length;         int enemiesleft = sumints(enemycount);         int[] candefeat = new int[girlsleft];         calcdefeatable(girlstrength, enemystrength, enemycount, candefeat);          if (candefeat[candefeat.length-1] < enemiesleft) return -1;         int mindefeatable = candefeat[0];          int bestcase;         while (girlsleft > 1) {             bestcase = (int) math.ceil((double)enemiesleft/(double)girlsleft);             if (mindefeatable >= bestcase) {                 return bestcase;             } else {                 candefeat = arrays.copyofrange(candefeat, 1, candefeat.length - 1);                 girlsleft--;                 enemiesleft -= mindefeatable;                 subtractfromints(candefeat, mindefeatable);                 mindefeatable = candefeat[0];             }         }          return candefeat[0];      }      // puts max number of enemies girl can defeat array, candefeat, returns index of     // girl lowest defeatable count     public static void calcdefeatable(int[] girlstrength, int[] enemystrength, int[] enemycount, int[] candefeat) {         int numgirls = girlstrength.length;         int enemygroups = enemystrength.length;          int defeatable = 0;         (int = 0; < numgirls; i++) {             (int j = 0; j < enemygroups; j++) {                 if(girlstrength[i] >= enemystrength[j]) {                     defeatable += enemycount[j];                 }             }             candefeat[i] = defeatable;             defeatable=0;         }            }      public static int sumints(int[] ints) {         int sum = 0;         (int = 0; < ints.length; i++) {             sum += ints[i];         }         return sum;     }      public static void subtractfromints(int[] ints, int value) {         (int = 0; < ints.length; i++) {             ints[i] -= value;         }     }      public static void main(string[] args) {         int[] girlstrength1 = {2, 3, 5};         int[] enemystrength1 = {1, 3, 4};         int[] enemycount1 = {2, 9, 4};          int[] girlstrength2 = {14, 6, 22};         int[] enemystrength2 = {8, 33};         int[] enemycount2 = {9, 1};          int[] girlstrength3 = {17, 10, 29, 48, 92, 60, 80, 100, 15, 69, 36, 43, 70, 14, 88, 12, 14, 29, 9, 40};         int[] enemystrength3 = {93, 59, 27, 68, 48, 82, 15, 95, 61, 49, 68, 15, 16, 26, 64, 82, 7, 8, 92, 15};         int[] enemycount3 = {56, 26, 12, 52, 5, 19, 93, 36, 69, 61, 68, 66, 55, 28, 49, 55, 63, 57, 33, 45};          system.out.println(spacewardiv2.minimalfatigue(girlstrength3, enemystrength3, enemycount3));     } } 

example input/output enter image description here


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -