Блог пользователя dimriXD

Автор dimriXD, история, 5 лет назад, По-английски

I am trying to create a HashMap of Integer and another HashMap(Chracter, Integer)

The code is below:

Your code here...

public static void createHM(HashMap<Integer, HashMap<Character, Integer>> hm, char s[], int n) {
    	HashMap<Character, Integer> chm = new HashMap<>();
        for(int i = 0; i < n; i++) {
                if(chm.containsKey(s[i])) {
                    chm.put(s[i], chm.get(s[i])+1);
                } else {
                    chm.put(s[i], 1);
                }
            hm.put(i+1, chm);
        }
    }

Description: chm stands for current HashMap, I am trying to add hashmap created while iterating over char array "s" to be included in the the hm i.e. main HashMap as values while key to be the current iteration (i+1)

Problem: hm the main HashMap contains same hashMap as value for all keys and the hashMap value is equal to what supposed to be only for the final key i.e. n while iterating over char array "s" but all keys from 1 till n has same value

I hope I clarified the problem, please help what is the issue with this approach

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

Автор dimriXD, история, 7 лет назад, По-английски

Hello guys!

I am new to codeforces, I stumbled upon 230A problem and my code is here. My basic idea was to create a list of list containing three things first->strength of dragon, second->bonus points, third->flag if this dragon is killed or not. Then I sorted according to bonus points and started incrementing kills if a dragon could be killed based on condition.

Please help if there is a flaw in my logic or I missed something in the code. I shall be really thankful to you. Please help.

Полный текст и комментарии »

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится