내가 너무 어렵게 푸나? 다른 사람 코드는 쉽던데. 이 문제가 java Collection framework를 아는지 물어본다. 물론 난 잘 모르지만.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 시작..
// String[] operations = { "I16", "I10", "I20", "D1" };
String[] genres = { "classic", "pop", "classic", "classic", "pop" };
int[] plays = new int[] { 500, 600, 150, 800, 2500 };
Solution sol = new Solution();
int[] answer = new int[10];
answer = sol.solution(genres, plays);
System.out.println("답은"+answer);
}
}
class Solution {
public int[] solution(String[] genres, int[] plays) {
int[] answer;
TreeMap<String, Integer> genresSelcetion = new TreeMap<>();
TreeMap<Integer, Integer> genresIndex = new TreeMap<>();
ArrayList<Integer> bestAlbum = new ArrayList<>();
for (int i = 0; i < genres.length; i++) {
int count = 0;
// count = genresSelcetion.get(genres[i]);
if (genresSelcetion.get(genres[i]) == null) {
//genres가 없음.
genresSelcetion.put(genres[i], plays[i]);
// System.out.println("String은" + genres[i]);
} else { //genres가 있음.
count = genresSelcetion.get(genres[i]);
genresSelcetion.put(genres[i], count + plays[i]);
}
System.out.println("카운트" + count);
}
//TreeMap 왼쪽 정렬.
Comparator<String> comparator = new ValueComparator<String, Integer>(genresSelcetion);
TreeMap<String, Integer> sortedGenresSelection = new TreeMap<>(comparator);
sortedGenresSelection.putAll(genresSelcetion);
Iterator<String> itrGenres = sortedGenresSelection.keySet().iterator();
//입력된 String[] genres - key, 곡 index - value로 만듦.
while (itrGenres.hasNext()) {
String key = itrGenres.next();
System.out.println("Key=" + key);
for (int i = 0; i < genres.length; i++) {
if (genres[i].equals(key)) {
genresIndex.put(i, plays[i]);
System.out.println("No" + i + " is genres=" + genres[i] + ", plays =" + plays[i]);
//넣을때 정렬.
}
System.out.println("전체 value은" + genresIndex.values());
} //for
//한 장르 모두 끝난 후, 값으로 정렬
//다시 사용하기 위해 loop 안에서 선언
Comparator<Integer> comparator2 = new ValueComparator<Integer, Integer>(genresIndex){
@Override
public int compare(Integer arg0, Integer arg1) {
// TODO Auto-generated method stub
return map.get(arg0)> map.get(arg1)?-1:1;
}
};
TreeMap<Integer, Integer> sortedGenresIndex = new TreeMap<>(comparator2);
sortedGenresIndex.putAll(genresIndex);
Iterator<Integer> itrGenresIndex = sortedGenresIndex.keySet().iterator();
int j = 0;
while (itrGenresIndex.hasNext()) {
//정리한 순서를 Array List에 삽입
Integer playIndex = itrGenresIndex.next();
j++;
if (j > 2)
break;
bestAlbum.add(playIndex);
}
System.out.println("여기 확인!! value은" + sortedGenresIndex.values());
for (int k = 0; k < bestAlbum.size(); k++)
System.out.println("여기는 ArrayList" + bestAlbum.get(k));
//clear TreeMap..
sortedGenresIndex.clear();
genresIndex.clear();
} // while
/*
* while (itrGenres.hasNext()) {
*
* Object key = itrGenres.next(); System.out.println("key는" + key);
* System.out.println("value는" + sortedGenresSelection.get(key));
*
* }
*
*/
// System.out.println("전체 value은" + sortedGenresSelection.values());
answer = new int[bestAlbum.size()];
for (int l = 0; l < bestAlbum.size(); l++) {
answer[l] = bestAlbum.get(l);
}
return answer;
}
}//Solution
class ValueComparator<K, V extends Comparable<V>> implements Comparator<K> {
TreeMap<K, V> map = new TreeMap<K, V>();
public ValueComparator(TreeMap<K, V> map) {
this.map.putAll(map);
}
@Override
public int compare(K arg0, K arg1) {
// TODO Auto-generated method stub
return -map.get(arg0).compareTo(map.get(arg1));
}
} //class ValueComparator