본문 바로가기
1 Day 1 Algorithms

[2019.03.04] Angry Professor

by 곰돌찌 2019. 3. 4.

Problem


A Discrete Mathematics professor has a class of students. Frustrated with their lack of discipline, he decides to cancel class if fewer than some number of students are present when class starts. Arrival times go from on time () to arrived late ().

Given the arrival time of each student and a threshhold number of attendees, determine if the class is canceled.

Input Format

The first line of input contains , the number of test cases.

Each test case consists of two lines.

The first line has two space-separated integers,  and , the number of students (size of ) and the cancellation threshold. 
The second line contains  space-separated integers () describing the arrival times for each student.

Note: Non-positive arrival times () indicate the student arrived early or on time; positive arrival times () indicate the student arrived  minutes late.

For example, there are  students who arrive at times . Four are there on time, and two arrive late. If there must be  for class to go on, in this case the class will continue. If there must be , then class is cancelled.

Function Description

Complete the angryProfessor function in the editor below. It must return YES if class is cancelled, or NO otherwise.

angryProfessor has the following parameter(s):

  • k: the threshold number of students
  • a: an array of integers representing arrival times

Constraints

Output Format

For each test case, print the word YES if the class is canceled or NO if it is not.

Note 
If a student arrives exactly on time , the student is considered to have entered before the class started.

Sample Input

2
4 3
-1 -3 4 2
4 2
0 -1 2 1

Sample Output

YES
NO

Explanation

For the first test case, . The professor wants at least  students in attendance, but only  have arrived on time ( and ) so the class is cancelled.

For the second test case, . The professor wants at least  students in attendance, and there are  who have arrived on time ( and ) so the class is not cancelled.


How I solved the problem


# 지문을 잘 읽야함. 0보다 크거나 작은 경우가 수업이 시작하기 전에 들어온 학생들임!

# for문을 돌려서 수업이 시작하기 전에 들어온 학생들을 구해서 그 값과 수업을 할지 안할지의 기준인 k와 비교하여 조건에 맞게 reutrn 해주도록 코드를 짜면됨.


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the angryProfessor function below.
    static String angryProfessor(int k, int[] a) {
        int attendStudentCount = 0;
 
        //count of students who were late for class
        for (int student : a) {
            if (student <= 0) {
                attendStudentCount++;
            }
        }
        
        //compare count of late student and k (to check if class will go on)
        if (k <= attendStudentCount) {
            return "NO";
        else {
            return "YES";
        }
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int t = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int tItr = 0; tItr < t; tItr++) {
            String[] nk = scanner.nextLine().split(" ");
 
            int n = Integer.parseInt(nk[0]);
 
            int k = Integer.parseInt(nk[1]);
 
            int[] a = new int[n];
 
            String[] aItems = scanner.nextLine().split(" ");
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
            for (int i = 0; i < n; i++) {
                int aItem = Integer.parseInt(aItems[i]);
                a[i] = aItem;
            }
 
            String result = angryProfessor(k, a);
 
            bufferedWriter.write(result);
            bufferedWriter.newLine();
        }
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.03.06] Viral Advertisement  (0) 2019.03.06
[2019.03.05] Beautiful Days at the Movies  (0) 2019.03.05
[2019.02.28] Utopian Tree  (0) 2019.02.28
[2019.02.27] Designer PDF Viewer  (0) 2019.02.27
[2019.02.26] The Hurdle Race  (0) 2019.02.26

댓글