아래 블로그의 방법이 파이썬스럽고 깔끔해보인다.

https://velog.io/@jongmin97/hackerRank-ACM-ICPC-Team

 

[HackerRank] ACM ICPC Team

Hackerrank ACM ICPC Team 문제입니다. 난이도는 쉬움입니다.

velog.io

 

def acmTeam(topic):
    # Write your code here
    result = [0, 0]
    
    for i in range(len(topic) - 1):
        for j in range(i + 1, len(topic)):
            sum_pair = str(int(topic[i]) + int(topic[j]))
            count = sum_pair.count('1') + sum_pair.count('2')
            
            if count > result[0]:
                result[0] = count
                result[1] = 1
            elif count == result[0]:
                result[1] += 1
            
    return result

가장 원시적이라고 생각했던 방법이 좋은 방법 중 하나였다.

def queensAttack(n, k, r_q, c_q, obstacles):
    # Write your code here
    if n == 0:
        return 0
    
    # Timeout 방지
    obs = dict()
    if(obstacles): 
        for ob in obstacles:
            obs[ob[0],ob[1]] = 1
 
    row_move = [1, -1, 0, 0, 1, -1, 1, -1]
    col_move = [0, 0, -1, 1, -1, -1, 1, 1]
    
    tot_attack = 0
    
    for r_m, c_m in zip(row_move, col_move):
        r = r_q + r_m
        c = c_q + c_m
        
        while True:
            if (r, c) in obs:
                break
            if r < 1 or r > n or c < 1 or c > n:
                break
            
            r += r_m
            c += c_m
                
            tot_attack += 1    
            
    return tot_attack
def equalizeArray(arr):
    # Write your code herepopo
    num_dict = {}
    
    for i in arr:
        if num_dict.get(i):
            num_dict[i] += 1
        else:
            num_dict[i] = 1
            
    key_value = sorted(num_dict.items(), key = lambda x: x[1], reverse = True)[0][0]
    k_num = arr.count(key_value)
    
    return len(arr) - k_num
def jumpingOnClouds(c):
    # Write your code here
    current = 0
    needed_jump = 0
    
    while current < len(c):
        p = current + 1
        pp = current + 2
        
        if pp < len(c) and c[pp] != 1:
            current = pp
        elif p < len(c) and c[p] != 1:
            current = p
        else:
            break
            
        needed_jump += 1
        
    return needed_jump
def repeatedString(s, n):
    # Write your code here
    
    s_len = len(s)
    
    if s_len == 1 and s == 'a':
        return n
    else:
        a_num = 0
        for i in s:
            if i == 'a':
                a_num +=1
                
        a_num = a_num * (n // s_len)
        if n % s_len != 0:
            for i in range(n % s_len):
                if s[i] == 'a':
                    a_num += 1
            
        return a_num