You are viewing a single comment's thread. Return to all comments →

Likewise did a 'brute force' approach, after trying several times to find a better 'dynamic programming' method. Is this really dynamic programming?

Here's my Java solution:

Scanner scan = new Scanner(System.in); int length = scan.nextInt(); int[] children = new int[length]; int[] candies = new int[length]; // seed children[0] = scan.nextInt(); candies[0] = 1; // search forward sequences for (int i = 1; i < length; i++){ children[i] = scan.nextInt(); candies[i] = 1; if (children[i] > children[i - 1]) candies[i] = candies[i - 1] + 1; } // search reverse sequences for (int i = length - 1; i > 0; i--){ if (children[i] < children[i - 1]) candies[i - 1] = Math.max(candies[i - 1], candies[i] + 1); } long sum = 0; for (int i= 0; i < candies.length; i++){ sum += candies[i]; } System.out.println(sum);

Thanks. Helped a lot to find the bug :)

Why is it that I get the wrong answer for test cases 11 and 12? do you get that as well?

You should consider the case where you get the maximum result.

Declare your sum variable as type long and solved.

What is the need of reverse sequence? Please explain.

To handle cases like the following:

5 4 3 2 1

Somehow, the algorithm must be 'smart' enough to know that the leftmost child will have five pieces of candy.

You must be a programmer for pokemon go with all of those candies

## Candies

You are viewing a single comment's thread. Return to all comments →

Likewise did a 'brute force' approach, after trying several times to find a better 'dynamic programming' method. Is this really dynamic programming?

Here's my Java solution:

Thanks. Helped a lot to find the bug :)

Why is it that I get the wrong answer for test cases 11 and 12? do you get that as well?

You should consider the case where you get the maximum result.

Declare your sum variable as type long and solved.

What is the need of reverse sequence? Please explain.

To handle cases like the following:

5 4 3 2 1

Somehow, the algorithm must be 'smart' enough to know that the leftmost child will have five pieces of candy.

You must be a programmer for pokemon go with all of those candies