The Minion Game
やっとHackerRankの難易度メディアの問題だ
初めて問題を見るのは今の問題より難しいですが、中?と思った.
これはもう一度考えさせる問題です.私の実力が足りないとは思いもよらなかった.
初めて問題を見るのは今の問題より難しいですが、中?と思った.
これはもう一度考えさせる問題です.私の実力が足りないとは思いもよらなかった.
문제:
Kevin and Stuart want to play the 'The Minion Game'.
Game Rules
Both players are given the same string, .
Both players have to make substrings using the letters of the string .
Stuart has to make words starting with consonants.
Kevin has to make words starting with vowels.
The game ends when both players have made all possible substrings.
Scoring
A player gets +1 point for each occurrence of the substring in the string .
For Example:
String = BANANA
Kevin's vowel beginning word = ANA
Here, ANA occurs twice in BANANA. Hence, Kevin will get 2 Points.
For better understanding, see the image below:
풀이
처음엔 문제 설명 그대로 주어진
부분 문자열과 문자열을 하나하나 비교하며 점수를 매겼다. 결과는 3개의 case에서 TimeOut..
모든 부분 문자열을 출력해보고 알 수 있었다.
B,
BA, A,
BAN, AN,
BANA, ANA,
BANAN, ANAN,
BANANA ANANA,
위의 예시를 보면 index에따라 점수는 len(string)-index 로 고정되어 있다.
index에 해당하는 글자가 모음인지 자음인지에 따라 알맞게 player에게 점수를 주면 된다.
따라서 O(N)으로 해결가능한 문제였다.
코드:
def minion_game(string):
# your code goes here
len_string = len(string)
player = [["Stuart",0],["Kevin",0]]
for i in range(len_string):
if string[i] in "AEIOU":
player[1][1]+= len_string-i
else:
player[0][1]+= len_string-i
if player[0][1] == player[1][1] :
print("Draw")
elif player[0][1] < player[1][1]:
print(player[1][0], player[1][1])
elif player[0][1] > player[1][1]:
print(player[0][0],player[0][1])
Reference
この問題について(The Minion Game), 我々は、より多くの情報をここで見つけました https://velog.io/@ifelifelse/The-Minion-Gameテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol