on
[프로그래머스] [Level2] 순위 검색 JAVA
[프로그래머스] [Level2] 순위 검색 JAVA
import java.util. * ;
import java.io. * ;
import java.util.stream. * ;
public class Main {
public static void main( String [] args) throws IOException {
BufferedReader br = new BufferedReader( new InputStreamReader( System . in ));
Solution sol = new Solution();
String [] info = { "java backend junior pizza 150" , "python frontend senior chicken 210" , "python frontend senior chicken 150" , "cpp backend senior pizza 260" , "java backend junior chicken 80" , "python backend senior chicken 50" };
String [] query = { "java and backend and junior and pizza 100" , "python and frontend and senior and chicken 200" , "cpp and - and senior and pizza 250" , "- and backend and senior and - 150" , "- and - and - and chicken 100" , "- and - and - and - 150" };
sol.solution(info,query);
}
}
class Solution {
HashMap < String ,ArrayList < Integer > > infoMap = new HashMap < > ();
ArrayList < Integer > ans = new ArrayList < > ();
public int [] solution( String [] info, String [] query) {
for ( String s : info) combination( "" , 0 , s. split ( " " ));
infoMap.values().forEach(Collections::sort);
for ( String q : query) {
String [] split = q.replaceAll( " and " , "" ). split ( " " );
String key = split [ 0 ];
int score = Integer. parseInt ( split [ 1 ]);
ans. add (counting(key, score));
}
return ans.stream().mapToInt(Integer::intValue).toArray();
}
int counting( String str, int score){
if ( ! infoMap.containsKey(str)) return 0 ;
List < Integer > list = infoMap.get(str);
int start = 0 ,end = list.size() - 1 ;
while (start < = end){
int mid = (start + end) / 2 ;
if (list.get(mid) < score) start = mid + 1 ;
else end = mid - 1 ;
}
return list.size() - start;
}
public void combination( String key, int depth, String [] info){
if (depth = = 4 ){
int score = Integer. parseInt (info[ 4 ]);
if ( ! infoMap.containsKey(key)){
ArrayList < Integer > list = new ArrayList < > ();
list. add (score);
infoMap.put(key,list);
} else infoMap.get(key). add (score);
return ;
}
combination(key + "-" ,depth + 1 ,info);
combination(key + info[depth],depth + 1 ,info);
}
}
from http://katastrophe.tistory.com/85 by ccl(A) rewrite - 2021-11-16 14:27:17