Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public int Il deck raw can find the best possibte score for the starting player.

ID: 3596258 • Letter: P

Question


public int Il deck raw can find the best possibte score for the starting player. Assume that our : public T int II deck) t this.deck deck; Finds the best score, public int findBestScoret int i, int j assuming our maximizer is going first ) elsear ((j-1)2-deck.length2) { ) else t return 0; return Math.mar(findBestScore( i: + 1, j) + decklil, tindbestScore(1, j: j-1) + deckl"); return Moth.minlfindBestScoret i i1. j). findBestScoreli, -1) Test cases fer y public static void main Stringll args) t int I1 exampleDeck1 new int11 (1, 3, 45, 6, 7, 8, 9, 9h intI exampleDeck2 new int1 (1, 3, 45, 6, 7, 8, 9, 9, 2h raw tbp1-new T raw tbp2new T exanpleDeck1); exampleDeck2): System. out.printf"findBestScore returned d, should be 631n", tbp1. findBestScorel :, exampleDeck1.length-1)) System. out, printf("findbestScore returned %d, should be 27V', tbp2.tineest re( i: e, j: exampleeck2·Length-1)); /Home/t objc(517221: Class Jav elper is implemented in both /Library/3ava/JavaVirtua LMachines/jdk1.8.0 144.jdk/Contents tscore returned 22, should be 63 findBestScore returned 63, should be 27

Explanation / Answer

public class TopBottomDraw

{

public int deck[];

public TopBottomDraw(int [] deck)

{

this.deck = deck;

}

public int findBestScore(int i, int j)

{

if(i == j)

return 0;

else if((j - i) % 2 == deck.length % 2)

{

return Math.max(findBestScore(i+1, j) + deck[i], findBestScore(i, j-1) + deck[j]);

}

else

return Math.min(findBestScore(i+1, j), findBestScore(i, j-1));

}

public static void main(String[] args)

{

int [] exampleDeck1 = new int[]{1,3,45,6,7,8,9,9};

int [] exampleDeck2 = new int[]{1,3,45,6,7,8,9,9,2};

TopBottomDraw tbp1 = new TopBottomDraw(exampleDeck1);

TopBottomDraw tbp2 = new TopBottomDraw(exampleDeck2);

//Make the change in the bold mark

System.out.printf("findBestSocre returned %d, should be 63 ", tbp2.findBestScore(0, exampleDeck2.length-1));

System.out.printf("findBestSocre returned %d, should be 27 ", tbp1.findBestScore(0, exampleDeck1.length-1));

}

}

Sample Run:

findBestSocre returned 63, should be 63
findBestSocre returned 22, should be 27