본문 바로가기
1 Day 1 Algorithms

[2019.02.27] Designer PDF Viewer

by 곰돌찌 2019. 2. 27.

Problem


When you select a contiguous block of text in a PDF viewer, the selection is highlighted with a blue rectangle. In this PDF viewer, each word is highlighted independently. For example:

PDF-highighting.png

In this challenge, you will be given a list of letter heights in the alphabet and a string. Using the letter heights given, determine the area of the rectangle highlight in  assuming all letters are  wide.

For example, the highlighted . Assume the heights of the letters are  and . The tallest letter is  high and there are  letters. The hightlighted area will be  so the answer is .

Function Description

Complete the designerPdfViewer function in the editor below. It should return an integer representing the size of the highlighted area.

designerPdfViewer has the following parameter(s):

  • h: an array of integers representing the heights of each letter
  • word: a string

Input Format

The first line contains  space-separated integers describing the respective heights of each consecutive lowercase English letter, ascii[a-z]. 
The second line contains a single word, consisting of lowercase English alphabetic letters.

Constraints

  • , where  is an English lowercase letter.
  •  contains no more than  letters.

Output Format

Print a single integer denoting the area in  of highlighted rectangle when the given word is selected. Do not print units of measure.

Sample Input 0

1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
abc

Sample Output 0

9

Explanation 0

We are highlighting the word abc:

Letter heights are  and . The tallest letter, b, is  high. The selection area for this word is .

Note: Recall that the width of each character is .

Sample Input 1

1 3 1 3 1 4 1 3 2 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 7
zaba

Sample Output 1

28

Explanation 1

The tallest letter in  is  at . The selection area for this word is .


How I solved the problem


# Map을 사용해서 알파벳과 height를 매칭시켜놓으면 편함!!

# 알파벳은 (char)97 + i 로 하면됨(소문자인 경우!)

# 그리고 get으로 그 값을 받아와서 돌리면 편하고 빠름!


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
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 designerPdfViewer function below.
    static int designerPdfViewer(int[] h, String word) {
        int hLength = h.length;
        Map<Character, Integer> alphabetMap = new HashMap<Character, Integer>();
 
        for (int i = 0; i < hLength; i++) {
            alphabetMap.put((char)(97 + i), h[i]);
        }
 
        int highestValue = 0;
        int wordLength = word.length();
 
        for (int i = 0; i < wordLength; i++) {
            char singleWord = word.charAt(i);
            int wordHeight = alphabetMap.get(singleWord);
 
            if (highestValue < wordHeight) {
                highestValue = wordHeight;
            }
        }
 
        return wordLength * highestValue;
 
    }
 
    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[] h = new int[26];
 
        String[] hItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int i = 0; i < 26; i++) {
            int hItem = Integer.parseInt(hItems[i]);
            h[i] = hItem;
        }
 
        String word = scanner.nextLine();
 
        int result = designerPdfViewer(h, word);
 
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


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


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

[2019.03.04] Angry Professor  (0) 2019.03.04
[2019.02.28] Utopian Tree  (0) 2019.02.28
[2019.02.26] The Hurdle Race  (0) 2019.02.26
[2019.02.25] Climbing the Leaderboard  (0) 2019.02.25
[2019.02.22] Picking Numbers  (0) 2019.02.22

댓글