https://www.hackerrank.com/challenges/designer-pdf-viewer/problem
배열 h에 글자의 크기가 들어온다. word에서 가장 큰 글자의 크기로 하이라이트를 하는데 필요한 공간을 계산하는 문제다. zaba면 가장 큰 글자인 z의 크기가 7mm이므로 7x4(글자수) = 28을 출력하면 된다.
[Java]
public static int designerPdfViewer(List<Integer> h, String word) {
// Write your code here
int max = 0, index;
for(int i=0; i<word.length(); i++){
index = ((int)word.charAt(i))-97;
if(h.get(index) > max)
max = h.get(index);
}
return max*word.length();
}
max는 가장 큰 글자의 높이다. index에 문자를 정수형으로 바꾸고 'a' 값만큼 빼줬다. h.get(index)가 max보다 크면 max값을 바꿔줬다. max와 word길이를 곱한 값을 반환한다.
[C]
int designerPdfViewer(int h_count, int* h, char* word) {
int max = 1, len = strlen(word);
for(int i=0; i<len; i++){
if(h[(int)word[i]-'a'] > max) max = h[(int)word[i]-'a'];
}
return len*max;
}
'HackerRank > Algorithms' 카테고리의 다른 글
[HackerRank] Sorting > Closest Numbers (0) | 2021.11.21 |
---|---|
[HackerRank] Warmup > Staircase (0) | 2021.11.08 |
[HackerRank] Implementation > Counting Valleys (0) | 2021.11.07 |
[HackerRank] Implementation > Electronics Shop (0) | 2021.10.10 |
[HackerRank] Implementation > Cats and a Mouse (0) | 2021.10.04 |