Algorithm/프로그래머스
[백준 온라인 저지] - 베스트셀러
JunGi Jeong
2021. 3. 26. 19:38
sudo code
n개의 입력
n개의 String을 받아서 String[]에 넣기
String[]을 map getorDefault로 카운팅
카운팅한 map에서 최대값인 key를 뽑아서 배열에 넣음
해당 배열 sorting
첫번째 값 출력
1차 시도 -> 일단 map으로 count랑 묶는거까지
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
public class Main {
public class SellingBook {
String name;
int count;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
public static void main(String[] args)
{
SellingBook sb = null;
// 입력도구 선언
Scanner sc = new Scanner(System.in);
// n 값 받기
int n = sc.nextInt();
List bestSeller = new ArrayList<SellingBook>();
String[] books = new String[n];
for(int i = 0; i < n; i++)
{
books[i] = sc.nextLine();
}
Map<String, Integer> map = new HashMap<String, Integer>();
for(String temp : books) map.put(temp, map.getOrDefault(temp, 0) + 1);
}
}
2차시도 map value의 최댓값 배열을 뽑는걸 검색해서 써봤는데 런타임에러
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collections;
class Solution {
public static void main(String[] args){
// 입력도구 선언
Scanner sc = new Scanner(System.in);
// n 값 받기
int n = sc.nextInt();
String[] books = new String[n];
for(int i = 0; i < n; i++)
{
books[i] = sc.nextLine();
}
Map<String, Integer> map = new HashMap<String, Integer>();
for(String temp : books) map.put(temp, map.getOrDefault(temp, 0) + 1);
Integer max = map.entrySet()
.stream()
.max((entry1, entry2) -> entry1.getValue() > entry2.getValue() ? 1 : -1)
.get()
.getValue();
List listOfMax = map.entrySet()
.stream()
.filter(entry -> entry.getValue() == max)
.map(Map.Entry::getKey)
.collect(Collectors.toList());
Collections.sort(listOfMax);
for(int i = 0; i < listOfMax.size(); i++)
{
System.out.println(listOfMax.get(i));
}
}
}
-> 어짜피 출력은 하나만!
3차시도 아래 블로그를 참조하여 구현했으나 런타임에러
import java.util.*;
import java.util.stream.Collectors;
import java.util.Collections;
class Solution {
public static void main(String[] args){
// 입력도구 선언
Scanner sc = new Scanner(System.in);
// n 값 받기
int n = sc.nextInt();
String[] books = new String[n];
for(int i = 0; i < n; i++)
{
books[i] = sc.nextLine();
}
Map<String, Integer> map = new HashMap<String, Integer>();
for(String temp : books) map.put(temp, map.getOrDefault(temp, 0) + 1);
int max = 0;
String bestSeller = "";
// HashMap을 돌며 value가 최대값인 책을 찾음
// 만약 value가 같다면 책이 사전순으로 앞서는 것을 출력
for(String key : map.keySet()) {
int value = map.get(key);
if(max == value && bestSeller.compareTo(key) > 0) {
bestSeller = key;
max = value;
} else if(max < value) {
bestSeller = key;
max = value;
}
}
System.out.println(bestSeller);
}
}
참고
백준 1302번. 베스트셀러 (Java)
문제 링크 : https://www.acmicpc.net/problem/1302 가장 많이 나온 책을 출력하는 문제입니다. 책을 Key로, 횟수를 Value로 저장하는 HashMap 를 사용하여 해결할 수 있습니다. 한가지 주의사항은 value가 같을..
bcp0109.tistory.com
[BOJ] 백준 1302 - 베스트셀러 (자바)
자료구조...,,, 탐색....,, 범위가 작음.... 문제 생길게 없다. 해쉬맵 알고 있다면 쉽게 풀 수 있음 풀이 1. 해쉬맵을 통해 이름과 횟수를 저장한다. (key, value) 처럼 2. 해쉬맵을 전체 탐색하여 가장
zoonvivor.tistory.com