█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6608

Problema 0xcb - Contar maneras de formar una cantidad con monedas

  • Autor: rpedrazacoello
  • Fecha: 2022-09-03 21:30:47 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#2
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#3
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#4
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#5
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#6
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#7
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#8
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#9
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#10
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#11
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#12
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#13
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#14
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
#15
Error de compilación
                      main.c:1:1: error: unknown type name 'import'
import java.util.HashMap;
^
main.c:1:12: error: expected ';' after top level declarator
import java.util.HashMap;
           ^
           ;
main.c:2:1: error: unknown type name 'import'
import java.util.Scanner;
^
main.c:2:12: error: expected ';' after top level declarator
import java.util.Scanner;
           ^
           ;
main.c:4:1: error: unknown type name 'public'
public class Main {
^
main.c:4:13: error: expected ';' after top level declarator
public class Main {
            ^
            ;
6 errors generated.

                    
Puntos totales: 0 / 100

Código

import java.util.HashMap;
import java.util.Scanner;

public class Main {

    public static HashMap<String, Integer> memoization = new HashMap<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] coins = new int[n];

        for (int i = 0; i < n; i++) {
            coins[i] = scanner.nextInt();
        }

        int noConsultas = scanner.nextInt();
        int[] consultas = new int[noConsultas];
        for (int i = 0; i < noConsultas; i++) {
            consultas[i] = scanner.nextInt();
        }

        for (int i = 0; i < noConsultas; i++) {
            System.out.println(dpCountCoins(-1, coins, consultas[i]));
        }

    }

    public static int dpCountCoins(int currentCoin, int[] coins, int totalAmount) {
        String key = currentCoin + "-" + totalAmount;
        if (memoization.containsKey(key)) {
            return memoization.get(key);
        }

        if (totalAmount == 0) {
            return 1;
        }

        if (totalAmount < 0) {
            return 0;
        }

        if (currentCoin >= coins.length) {
            return 0;
        }

        int count = 0;

        int coinValue = currentCoin >=0 ? coins[currentCoin] : 0;
        int localAmount = totalAmount - coinValue;
        while (localAmount > 0) {
            for (int i = currentCoin + 1; i <= coins.length; i++) {
                count += dpCountCoins(i, coins, localAmount);
            }
            localAmount = currentCoin >=0 ? localAmount - coinValue : -1;
        }

        if(localAmount == 0){
            count++;
        }

        memoization.put(key, count);
        return count;
    }
}