해커랭크(HackerRank)

Designer PDF Viewer

cepiloth 2018. 8. 19. 17:31
반응형


1. 문제


입력으로 들어오는 int 배열은 각각의 글자의 높이 정보를 갖고 있다.

높이 : 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

글자 : a b c d e f g h i j k l m n o p q r s t u v w x y z



abc 라는 글자가 있다면 각 글자의 높이는 아래와 같다.

a = 1

b = 3

c = 1


높이가 가장 큰 글자 기준으로 highlight 사각형을 그려야 하기 때문에


답 = 글자 수 * 가장 큰 글자 height

답 = 3 * 3

답 = 9


2. 알고리즘

키워드 - 구현


3. 코드


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
#include <bits/stdc++.h>
 
using namespace std;
 
int designerPdfViewer(vector <int> h, string word) {
    // Complete this function
    const int size = word.size();
 
    int maxH = 0;
    for(int i =0; i<size; i++) {
        char ch = word[i] - 'a';
        if(h[ch] > maxH) {
            maxH = h[ch];
        }
    }
 
    return maxH * size;
}
 
int main() {
    vector<int> h(26);
    for(int h_i = 0; h_i < 26; h_i++){
       cin >> h[h_i];
    }
    string word;
    cin >> word;
    int result = designerPdfViewer(h, word);
    cout << result << endl;
    return 0;
}
cs

반응형