๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ๋ฒ ์ŠคํŠธ ์•จ๋ฒ” - ํ•ด์‹œ

by ynzu๐Ÿค 2022. 2. 20.
๋ฐ˜์‘ํ˜•

 

๋จผ์ € ์ƒ๊ฐํ–ˆ๋˜ ํ’€์ด ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  • ๊ฐ ์žฅ๋ฅด์˜ ๊ฐœ์ˆ˜๋ฅผ HashMap์— ๋‹ด๊ธฐ
classic : 3
pop : 2

 

  • ๊ฐ ์žฅ๋ฅด์˜ ์žฌ์ƒ ํšŸ์ˆ˜ ํ•ฉ๊ณ„๋ฅผ HashMap์— ๋‹ด๊ณ , ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ  sortingํ•˜๊ธฐ
pop : 3100
classic : 1450

pop ์žฅ๋ฅด์˜ ์žฌ์ƒ ํšŸ์ˆ˜ ํ•ฉ๊ณ„๊ฐ€ ๊ฐ€์žฅ ๋งŽ์œผ๋ฏ€๋กœ pop ์žฅ๋ฅด ๋…ธ๋ž˜์˜ index๊ฐ€ ๋จผ์ € ์ˆ˜๋ก๋˜์–ด์•ผ ํ•œ๋‹ค.  ex) 4, 1

๊ทธ ๋‹ค์Œ์œผ๋ก  classic ์žฅ๋ฅด ๋…ธ๋ž˜์˜ index๊ฐ€ ์ˆ˜๋ก๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ ๊ฐ ์žฅ๋ฅด๋ณ„ 2๊ฐœ์”ฉ๋งŒ ์ˆ˜๋ก๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ 3, 0์ด ์ฐจ๋ก€๋กœ ์ˆ˜๋ก๋˜๊ณ  2๋Š” ์ˆ˜๋ก๋˜์ง€ ๋ชปํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์•จ๋ฒ”์—” 4, 1, 3, 0 index ์ˆœ์œผ๋กœ ์ˆ˜๋ก๋œ๋‹ค.

 

์ด๋ฅผ ์œ„ํ•˜์—ฌ HashMap<String, HashMap<Integer, Integer>> hm = new HashMap<String, HashMap<Integer, Integer>>(); ๋ฅผ ์ƒ์„ฑํ•˜์˜€๋‹ค. 

key๋Š” ์žฅ๋ฅด์ด๋ฉฐ, value๊ฐ€ ๋˜ HashMap์ธ๋ฐ ์ด HashMap์˜ key๋Š” ์žฌ์ƒํšŸ์ˆ˜์ด๋ฉฐ, value๋Š” ํ•ด๋‹น ์žฌ์ƒํšŸ์ˆ˜์˜ index์ด๋‹ค.

์žฌ์ƒํšŸ์ˆ˜ ํ•ฉ๊ณ„๊ฐ€ ๋†’์€ ์žฅ๋ฅด์˜ ๋ชจ๋“  ์žฌ์ƒํšŸ์ˆ˜์™€ ๊ฐ ์žฌ์ƒํšŸ์ˆ˜์˜ index๋ฅผ ๋„ฃ๊ธฐ ์œ„ํ•จ์ด์˜€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด ํ›„์— inner? HashMap์„ ๋‚ด๋ฆผ์ฐจ์ˆœ sortingํ•ด์ฃผ๋ฉฐ, ์ƒ์œ„ 2๊ฐœ์˜ index(value)๋งŒ resultList์— ๋„ฃ์–ด์ฃผ์—ˆ๋‹ค. (1๊ฐœ๋งŒ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” 1๊ฐœ๋งŒ ๋„ฃ์–ด์คŒ)

 

public int[] solution(String[] genres, int[] plays) {
        
    	//๊ฐ ์žฅ๋ฅด์˜ ๊ฐœ์ˆ˜
        HashMap<String, Integer> genreCntMap = new HashMap<String, Integer>();
        for (int i=0; i<genres.length; i++) {
        	genreCntMap.put(genres[i], genreCntMap.getOrDefault(genres[i], 0)+1);
        }
        
        //๊ฐ ์žฅ๋ฅด์˜ ์žฌ์ƒ ํšŸ์ˆ˜ ํ•ฉ๊ณ„
        HashMap<String, Integer> genreSumMap = new HashMap<String, Integer>();
        for (int i=0; i<genres.length; i++) {
        	genreSumMap.put(genres[i], genreSumMap.getOrDefault(genres[i], 0)+plays[i]);
        }
        
       
        List<Entry<String, Integer>> listEntries = new ArrayList<Entry<String, Integer>>(genreSumMap.entrySet());

        Collections.sort(listEntries, new Comparator<Entry<String, Integer>>() {
			
			@Override
			public int compare(Entry<String, Integer> obj1, Entry<String, Integer> obj2) {
				// ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
				return obj2.getValue().compareTo(obj1.getValue());
			}
		});
		
        
		HashMap<String, HashMap<Integer, Integer>> hm = new HashMap<String, HashMap<Integer, Integer>>();
		
		for (int i=0; i<genres.length; i++) {
			
			HashMap<Integer, Integer> value = null;
			if(hm.containsKey(genres[i])) {
				value = hm.get(genres[i]);
			}else {
				value = new HashMap<Integer, Integer>();
			}
			value.put(plays[i], i);
			hm.put(genres[i], value);
        	
        }
        
		
		List<Integer> result = new ArrayList<>();
		for(Entry<String, Integer> entry : listEntries) {
			
			
			List<Entry<Integer, Integer>> order = new ArrayList<Entry<Integer, Integer>>(hm.get(entry.getKey()).entrySet());

			Collections.sort(order, new Comparator<Entry<Integer, Integer>>() {
				
				@Override
				public int compare(Entry<Integer, Integer> obj1, Entry<Integer, Integer> obj2) {
					// ๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ
					return obj2.getKey().compareTo(obj1.getKey());
				}
			});
			
			result.add(order.get(0).getValue());
			if(genreCntMap.get(entry.getKey()) != 1) {
				result.add(order.get(1).getValue());
			}
			
		}
		
		int[] i = result.stream().mapToInt(Integer::intValue).toArray();
    
        return  i;
    }

 

์˜ˆ์ œ๋Š” ์›ํ•˜๋Š” ๊ฒฐ๊ณผ๋Œ€๋กœ ๋‚˜์™”์ง€๋งŒ ๋‹ค๋ฅธ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์—์„œ ์‹คํŒจํ•˜์˜€๋‹ค....

๊ทธ๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ๊ฐ€ ๋ชจ๋‘ ์„ฑ๊ณตํ•ด๋„ ํšจ์œจ์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ๋ง‰ํž๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐ์€ ํ•˜๊ณ  ์žˆ์—ˆ๋‹ค. ๋‚ด๊ฐ€ ์ƒ๊ฐํ•ด๋„ ๋ณต์žกํ–ˆ๊ธฐ์—!


 ๊ฐ ์žฅ๋ฅด์˜ ๊ฐœ์ˆ˜๋ฅผ HashMap์— ๋‹ด์„ ํ•„์š”๊ฐ€ ์—†์„ ๊ฒƒ ๊ฐ™์•„ ์ œ์™ธ์‹œ์ผฐ๊ณ , HashMap<String, HashMap<Integer, Integer>> ๋ถ€๋ถ„์ด ๋„ˆ๋ฌด ๋ณต์žกํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ Music์ด๋ผ๋Š” class๋ฅผ ์ƒ์„ฑํ•˜๊ณ  List<Music> resultList๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.

class Music{
	String genres;
	int plays;
	int index;
	
	public Music(String genres, int plays, int index) {
		this.genres=genres;
		this.plays=plays;
		this.index=index;
	}
}

 

์ตœ์ข… ์†Œ์Šค๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

public int[] solution(String[] genres, int[] plays) {
    	int[] answer = {};
    	 
        //๊ฐ ์žฅ๋ฅด์˜ ์žฌ์ƒ ํšŸ์ˆ˜ ํ•ฉ๊ณ„
        HashMap<String, Integer> genreSumMap = new HashMap<String, Integer>();
        for (int i=0; i<genres.length; i++) {
        	genreSumMap.put(genres[i], genreSumMap.getOrDefault(genres[i], 0)+plays[i]);
        }
        
       
        List<Entry<String, Integer>> listEntries = new ArrayList<Entry<String, Integer>>(genreSumMap.entrySet());
        
        Collections.sort(listEntries, new Comparator<Entry<String, Integer>>() {
	
			@Override
			public int compare(Entry<String, Integer> obj1, Entry<String, Integer> obj2) {
				//๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ 
				return obj2.getValue().compareTo(obj1.getValue()); // ๋‚ด๋ฆผ์ฐจ์ˆœ
			}
		});
		
		List<Music> resultList = new ArrayList<Music>();
		for (int i=0; i<listEntries.size(); i++) {
			List<Music> tempList = new ArrayList<Music>();
			
			
			for(int j=0; j<genres.length; j++) {
				if(listEntries.get(i).getKey().equals(genres[j])) {
					tempList.add(new Music(genres[j], plays[j], j));
				}
			}
			
			Collections.sort(tempList, new Comparator<Music>() {

				@Override
				public int compare(Music o1, Music o2) {
					//๋‚ด๋ฆผ์ฐจ์ˆœ ์ •๋ ฌ 
					return o2.plays - o1.plays;
				}
			});
			
			resultList.add(tempList.get(0));
			if(tempList.size() != 1 ) {
				resultList.add(tempList.get(1));
			}
		}
		
		answer = new int[resultList.size()];
		for(int i=0; i<resultList.size(); i++) {
			answer[i] = resultList.get(i).index;
		}
		return answer;
    }
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€