Skip to content

김지수: 햄버거 분배 #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions soojik/week15/햄버거분배/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package soojik.week15.햄버거분배;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Queue;
import java.util.LinkedList;
import java.util.StringTokenizer;

public class Main {
// 주어진 데이터 N, K, 식탁 정보
static int N, K;
static String table;
// 사람과 햄버거 인덱스만 모아놓은 Queue<Integer> persons, hamburgers
static Queue<Integer> persons = new LinkedList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사람과 햄버거를 queue에 넣으신 거 좋습니다!

static Queue<Integer> hamburgers = new LinkedList<>();
public static void main(String[] args) throws IOException {
System.setIn(new FileInputStream("soojik/week15/햄버거분배/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

StringTokenizer st = new StringTokenizer(br.readLine());

N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());

table = br.readLine();

for (int i = 0; i < N; i++) {
if (table.charAt(i) == 'H') {
hamburgers.add(i);
continue;
}
persons.add(i);
}

int answer = 0;

int p, h;
while (!persons.isEmpty()) {
// persons 에서 하나씩 꺼내서
p = persons.poll();

// hamburgers 하나씩 비교
while (!hamburgers.isEmpty()) {
// 만약 가장 앞 값이 현재 사람이 뒤쪽으로 닿을 수 없는 위치라면 탐색 중단
// 앞쪽으로 닿을 수 없는 거리의 햄버거라면 이미 앞쪽의 탐색에서 버려졌을 것
if (p + K < hamburgers.peek()) {
break;
}

h = hamburgers.poll();

// 그 외에 닿을 수 있는 거리면 answer 1 증가시키고 탐색 중단
if (p - K <= h && h <= p + K) {
++answer;
break;
}
}
}

System.out.println(answer);
}
}
5 changes: 5 additions & 0 deletions soojik/week15/햄버거분배/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
20 1
HHPHPPHHPPHPPPHPHPHP

20 2
HHHHHPPPPPHPHPHPHHHP