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

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

1. K로 나눠지는지를 볼때 두 수의 합은 두 수의 나머지의 합을 봐도 무방함
2. 두 수의 합이므로 K // 2 의 범위에서 떨어지지 않는 수를 찾고, count가 더 많은 수를 더해줌
3. 짝수인 경우엔 하나만 고려해서 더해줌

def nonDivisibleSubset(k, s):
    # Write your code here
    r = [0] * k
    cnt = 0
    
    for i in s:
        r[i % k] += 1
            
    for i in range(k//2 + 1):
        if i == 0 or k == i * 2:
            cnt += bool(r[i])
        else:
            cnt += max(r[i], r[k - i])
    
    return cnt

상세 설명 ↓
https://cs.stackexchange.com/questions/57873/maximum-subset-pairwise-not-divisible-by-k

 

Maximum subset pairwise not divisible by $K$

I have a set of numbers, and want to calculate the maximum subset such that the sum of any two of it's elements is not divisible by an integer $K$. I tried to solve this problem, but I have found the

cs.stackexchange.com

 

'# 코딩 문제 관련 > 파이썬' 카테고리의 다른 글

Jumping on the Clouds  (0) 2022.08.23
[HackerRank-python] Repeated String  (0) 2022.08.22
[HackerRank-python] Cut the sticks  (0) 2022.08.11
[HackerRank-python] Library Fine  (0) 2022.08.09
[HackerRank-python] Sherlock and Squares  (0) 2022.08.09