https://programmers.co.kr/learn/courses/30/lessons/72412
코딩테스트 연습 - 순위 검색
["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"] ["java and backend and junior and pizza 100","pyt
programmers.co.kr
무지성 무효율 풀이
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static int[] Solution(String[] info, String[] query){ ArrayList<Integer> arr = new ArrayList<>(); int flag = 0;int cnt =0; for(int i=0;i<query.length;i++){ cnt=0; String[] qArr = query[i].split(" and | "); for(int j=0;j<info.length;j++){ flag=0; String[] infArr = info[j].split(" "); for(int k=0;k< qArr.length - 1;k++){ if(qArr[k].equals("-") || qArr[k].equals(infArr[k])) continue; else { flag=1; break; } } if(flag==0) { if(Integer.parseInt(qArr[4]) <= Integer.parseInt(infArr[4])) cnt++; } } arr.add(cnt); } int[] answer = arr.stream().mapToInt(i -> i).toArray(); return answer; } | cs |
hashmap, bitmasking, binarysearch를 깃들인 정답풀이
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | import java.util.*; class Solution { public int[] solution(String[] info, String[] query) { Map<String, List<Integer>> map = new HashMap<>(); for(String in : info){ String[] split = in.split(" "); for(int i=0;i<(1<<4);i++){ StringBuilder key = new StringBuilder(); for(int j=0;j<4;j++){ if((i&(1<<j))>0){ key.append(split[j]); } } map.computeIfAbsent(key.toString(), s -> new ArrayList<Integer>()).add(Integer.parseInt(split[4])); } } for(Map.Entry<String, List<Integer>> entry : map.entrySet()) entry.getValue().sort(null); int[] answer = new int[query.length]; for(int i=0;i< query.length;i++){ String[] splits = query[i].replaceAll("-","").replaceAll(" and ","").split(" "); List<Integer> list = map.getOrDefault(splits[0],new ArrayList<Integer>()); int start = 0; int end = list.size(); int value = Integer.parseInt(splits[1]); while(start<end){ int mid = (start+end)/2; if(list.get(mid)<value){ start = mid+1; }else{ end = mid; } } answer[i]=list.size()-start; } return answer; } } | cs |
'공부 > PS' 카테고리의 다른 글
[프로그래머스] 짝지어 제거하기 JAVA (0) | 2021.07.28 |
---|---|
[프로그래머스]124나라의 숫자 JAVA (0) | 2021.07.28 |
[프로그래머스]모의고사 Java (0) | 2021.07.24 |
[JAVA] 순열, 중복순열, 조합, 중복조합 (0) | 2021.07.24 |
[프로그래머스]문자열압축 (0) | 2021.07.22 |