You are not logged in. Please login at www.codechef.com to post your questions!

×

in problem, "COINS" find what is wrong in my code in memorization in dynmic implementation

problem Coin link
my below code is giving wrong answer
i am new to dynamic progrming... what is problem with my written follwing code while doing memorisation

    //package PracticeMedium;

//package PracticeMedium;

/*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
 */
//package PracticeMedium;
import java.util.HashMap;
import java.util.Scanner;

/**
 *
 * @author Hemant Dhanuka
 */
//without memorisation 
class COINS4 {

    static HashMap<Integer, Long> memorisationMap = new HashMap<>();

    public static void main(String[] args) throws Exception {

        Scanner s = new Scanner(System.in);
        while (s.hasNextInt()) {
            int numberWrittenOnCoin = s.nextInt();
            calculateMaxforEachNo(numberWrittenOnCoin);
            if (numberWrittenOnCoin < 12) {
                System.out.println(numberWrittenOnCoin);
            } else {
                System.out.println(memorisationMap.get(numberWrittenOnCoin));
            }
        }
    }

    private static long calculateMaxforEachNo(int num) {

        if (num < 12) {
            return num;
        }

        if (memorisationMap.get(num) != null) {
            return memorisationMap.get(num);
        }
        memorisationMap.put(num, (calculateMaxforEachNo(num / 2) + calculateMaxforEachNo(num / 3) + calculateMaxforEachNo(num / 4)));
        return 0;

    }
}

please tell me what modification required to get Correct answer in my above code, and other alternate solution of these problem to approch and perform memorisation

asked 11 Mar '17, 12:01

hemant_dhanuka's gravatar image

5★hemant_dhanuka
533112
accept rate: 3%

edited 11 Mar '17, 12:02

2

@hemant_dhanuka I see that you fixed the bug and got AC, good job :)

(11 Mar '17, 19:15) meooow ♦6★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,657
×2,474
×2,212
×1,313
×558
×432
×107
×65

question asked: 11 Mar '17, 12:01

question was seen: 344 times

last updated: 11 Mar '17, 19:15