Coding Test/baekjoon

[백준 1713] 게임- 후보 추천하기(solved.ac - 실버 1)

조용장 2023. 3. 20. 23:58

https://www.acmicpc.net/problem/1713

 

1713번: 후보 추천하기

첫째 줄에는 사진틀의 개수 N이 주어진다. (1 ≤ N ≤ 20) 둘째 줄에는 전체 학생의 총 추천 횟수가 주어지고, 셋째 줄에는 추천받은 학생을 나타내는 번호가 빈 칸을 사이에 두고 추천받은 순서대

www.acmicpc.net

풀이

문제를 보았을 때 알 수 있는 힌트

오랜만에 다시 문제를 풀다보니 구현 문제를 풀어보게 되었다.

조건을 맞춰서 잘 구현만 하면 통과할 수 있는 문제이다.

1. 먼저 학생들을 사진 틀에 다 넣어둔다.

2. 다 넣었다면 추천 횟수가 적은 학생을 삭제하고 그 자리에 추가하면 된다. 그러나 횟수가 같은 경우, 가장 오래된 사진을 삭제하면 된다.

이를 위해서 구조체를 하나 만들어서 문제를 해결했다.

구조체에는 이름, 추천 횟수, 사진틀에 걸린 기간을 추가하여 문제를 해결해 보았다.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.stream.Collectors;

public class Main {
	static Scanner sc = new Scanner(System.in);


	public static void main(String[] args) {
		// 1713 문제
		int picturetle = Integer.parseInt(sc.nextLine());
		int pCount = Integer.parseInt(sc.nextLine());

		class Person implements Comparable<Person>{
			int name;
			int count;
			int date;

			public Person(int name, int count, int date) {
				super();
				this.name = name;
				this.count = count;
				this.date = date;
			}

			public Person() {
				// TODO Auto-generated constructor stub
			}
			
			public int getName() {
				return name;
			}

			public void setName(int name) {
				this.name = name;
			}

			public int getCount() {
				return count;
			}

			public void setCount(int count) {
				this.count = count;
			}

			public int getDate() {
				return date;
			}

			public void setDate(int date) {
				this.date = date;
			}

			@Override
		    public int compareTo(Person o) {
				if(o.getCount()==this.getCount()) {
					return o.getDate()-this.getDate();
				}
				return this.getCount()-o.getCount();
		    }
		}

		List<Person> ps = new ArrayList<>();
		String[] temp = sc.nextLine().split(" ");
		for (String s : temp) {
			
			if(ps.size()==0) {
				ps.add(new Person(Integer.parseInt(s),1,1));
			}
			else {
				// 같은게 있는지 확인
				boolean flag = true;
				for (Person p : ps) {
					// 같은게 있네? 추가!
					if(p.name == Integer.parseInt(s)) {
						p.count++;
						flag=false;
					}
				}
				// 같은게 없다면 ps에 추가
				// 근데 개수를 초과하면? 추가하면 안됨
				if(flag) {
					if(ps.size()!=picturetle) {
						ps.add(new Person(Integer.parseInt(s),1,1));
					}
					// 여기서 이제 변경해야함.
					else {
						// 가장 적은 횟수
						// 같은 경우 오래된 수(date가 큰 경우)
						Collections.sort(ps);
						ps.remove(0);
						ps.add(new Person(Integer.parseInt(s),1,1));
						

					}
				}
				
			}
			for (Person p : ps) {
				p.date++;
			}
			

		}
		TreeSet<Integer> ans = new TreeSet<>();
		for (Person s1 : ps) {
			ans.add(s1.name);
		}
		for (Integer i : ans) {
			System.out.print(i+" ");
		}

		// 
		// count -> date 순으로 정렬해야함.
		

	}
}

https://github.com/dydwkd486/coding_test/blob/main/baekjoon/java/baekjoon1713/Main.java

 

GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리

코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.

github.com