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

1. pythonic

def cutTheSticks(arr):
    # Write your code here
    
    arr.sort(reverse = True)
    
    sticks_cut = []
    while arr:
        sticks_cut.append(len(arr))
        
        min_value = arr.pop()
        while arr and min_value == arr[-1]:
            arr.pop()
            
    return sticks_cut

2. 구식

def cutTheSticks(arr):
    # Write your code here
    
    sticks_cut = []
    while len(arr) > 1:
        cutoff = min(arr)
        sticks_cut.append(len(arr))
        
        arr = [num - cutoff for num in arr]
        temp_arr = []
        
        for num in arr:
            if not num:
                continue
            temp_arr.append(num)
            
        arr = temp_arr
        
    if len(arr) == 1:
        sticks_cut.append(1)
        
    return sticks_cut

 

def libraryFine(d1, m1, y1, d2, m2, y2):
    # Write your code here
    fine = 0
    
    if y1 <= y2 and m1 <= m2 and d1 <= d2:
        return fine
    else:
        if y1 == y2 and m1 == m2 and d1 >= d2:
            d_diff = d1 - d2
            fine += (15 * d_diff)
        elif y1 == y2 and m1 >= m2:
            m_diff = m1 - m2
            fine += (500 * m_diff)
        elif y1 >= y2:
            y_diff = y1 - y2
            fine += (10000 * y_diff)
        
    return fine
def squares(a, b):
    # Write your code here
    a = math.ceil(math.sqrt(a))
    b = math.floor(math.sqrt(b)) + 1
    
    return b - a
def appendAndDelete(s, t, k):
    # Write your code here
    cnt = 0
    for a, b in zip(s, t):
        if a != b:
            break
        cnt += 1
    
    s = s[cnt:]
    t = t[cnt:]
    
    sl = len(s)
    tl = len(t)
    
    # 경우가 맞는건지 모르겠다.
    # 1. 횟수와 move 횟수가 같은 경우
    # 2. 이동 횟수가 k 이하이면서 짝수배인 경우(문자열이 2개니까 행동도 2번씩, 홀수면 안됨)
    # 3. 전체 행동 횟수가 k보다 낮은 경우
    if (k == sl + tl) or ((k - sl + tl) % 2 == 0 and k >= sl + tl) or (k >= cnt*2 + sl + tl):
        return 'Yes'
    else:
        return 'No'