본문 바로가기
1 Day 1 Algorithms

[2019.02.15] Drawing Book

by 곰돌찌 2019. 2. 15.

Problem


Brie’s Drawing teacher asks her class to open their books to a page number. Brie can either start turning pages from the front of the book or from the back of the book. She always turns pages one at a time. When she opens the book, page  is always on the right side:

image

When she flips page , she sees pages  and . Each page except the last page will always be printed on both sides. The last page may only be printed on the front, given the length of the book. If the book is  pages long, and she wants to turn to page , what is the minimum number of pages she will turn? She can start at the beginning or the end of the book.

Given  and , find and print the minimum number of pages Brie must turn in order to arrive at page .

Function Description

Complete the pageCount function in the editor below. It should return the minimum number of pages Brie must turn.

pageCount has the following parameter(s):

  • n: the number of pages in the book
  • p: the page number to turn to

Input Format

The first line contains an integer , the number of pages in the book. 
The second line contains an integer, , the page that Brie's teacher wants her to turn to.

Constraints

Output Format

Print an integer denoting the minimum number of pages Brie must turn to get to page .

Sample Input 0

6
2

Sample Output 0

1

Explanation 0

If Brie starts turning from page , she only needs to turn  page:

Untitled Diagram(6).png

If Brie starts turning from page , she needs to turn  pages:

Untitled Diagram(3).png

Because we want to print the minumum number of page turns, we print  as our answer.

Sample Input 1

5
4

Sample Output 1

0

Explanation 1

If Brie starts turning from page , she needs to turn  pages:

Untitled Diagram(4).png

If Brie starts turning from page , she doesn't need to turn any pages:

Untitled Diagram(5).png

Because we want to print the minimum number of page turns, we print  as our answer.


How I solved the problem


# 앞에서 뒤로 가는 로직과 뒤에서 앞으로 가는 로직 두개로 나뉘어서 생각해야함

# 앞에서 뒤로 갈 때의 page 파라미터 2개랑 뒤에서 앞으로 갈 때 page 파라미터 2개로 봄

# for문 하나에서 어떠한 page라도 p에 걸리면 break 해줌!

# count 파라미터가 하나인 이유는 최소 number를 리턴하면 되기 때문에!

# 맨 뒤의 페이지는 홀수일 때와 짝수일 때 page의 위치가 다르므로 이 부분만 생각해주면됨!


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
67
68
69
70
71
72
73
74
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
 
public class Solution {
 
    /*
     * Complete the pageCount function below.
     */
    static int pageCount(int n, int p) {
        /*
         * Write your code here.
         */
        int maxNumTurns = (n / 2+ 1;
        int count = 0;
        int leftPage = 0;
        int rightPage = 1;
        int backLeftPage = 0;
        int backRightPage = 0;
 
        if (n % 2 == 0) { //if totalPage is even
            backLeftPage = n;
            backRightPage = n + 1;
        } else { //is totalPage is odd
            backLeftPage = n - 1;
            backRightPage = n;
        }
 
        for (int i = 0; i < maxNumTurns; i++) {
            //if one of the page matches p
            if (rightPage == p || leftPage == p || backRightPage == p || backLeftPage == p) {
                break;
            }
            
            //if all pages don't match p
            //both add 2
            rightPage += 2;
            leftPage += 2;
 
            // both substract 2
            backLeftPage -= 2;
            backRightPage -= 2;
 
            //count is also added 1
            count++;
        }
        
        return count;
    }
 
    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 n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");
 
        int p = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])*");
 
        int result = pageCount(n, p);
 
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


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


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

[2019.02.19] Cats and a Mouse  (0) 2019.02.19
[2019.02.18] Electronics Shop  (0) 2019.02.18
[2019.02.14] Bon Appetit  (0) 2019.02.14
[2019.02.13] Day of the Programmer  (0) 2019.02.13
[2019.02.12] Migratory Birds  (0) 2019.02.12

댓글