HackerRankの質問に答えるWarm-up Challengs
8634 ワード
The HackerRank Interview Preparation Kit > Warm-up Challenges Counting Valleys Sales by Match Jumping on the Clouds Repeated String 🟢 Counting Valleys
質問リンク
朕シミュレーション
質問リンク
¥set利用
質問リンク
¥2,000欲張り法
質問リンク
メモリ/時間の複雑さに注意してください
質問リンク
朕シミュレーション
def countingValleys(steps, path):
# initialize
cur_h, valley_cnt = 0, 0
is_valley, is_in_spot = False, False
# search
for state in path:
is_down = state == 'D'
# check in spot
if cur_h == 0 and not is_in_spot:
is_valley = is_down
is_in_spot = True
# update cur_h
cur_h = cur_h - 1 if is_down else cur_h + 1
# check out spot
if cur_h == 0 and is_in_spot:
valley_cnt = valley_cnt + 1 if is_valley else valley_cnt
is_in_spot = False
return valley_cnt
🟢 Sales by Match質問リンク
¥set利用
def sockMerchant(n, ar):
sock_set = set()
answer = 0
for sock in ar:
if sock not in sock_set:
sock_set.add(sock)
else:
sock_set.remove(sock)
answer += 1
return answer
🟢 Jumping on the Clouds質問リンク
¥2,000欲張り法
def jumpingOnClouds(c):
LAST_IDX = len(c) - 1
SAFE, DANGER = 0, 1
cur_idx, step_num = 0, 0
while cur_idx < LAST_IDX - 1:
can_two_step = c[cur_idx+2] == SAFE
cur_idx = cur_idx + 2 if can_two_step else cur_idx + 1
step_num += 1
return step_num if cur_idx == LAST_IDX else step_num + 1
🟢 Repeated String質問リンク
メモリ/時間の複雑さに注意してください
def repeatedString(s, n):
STR_LEN = len(s)
div, mod = divmod(n, STR_LEN)
return s.count('a') * div + s[:mod].count('a')
Reference
この問題について(HackerRankの質問に答えるWarm-up Challengs), 我々は、より多くの情報をここで見つけました https://velog.io/@tjdud0123/HackerRank-문제풀이-Warm-up-Challenges-pythonテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol