[HackerRank][Python3] Priyanka and Toys
                2018. 9. 8. 21:23 |
                
                    프로그래밍/HackerRank
                
            
            
            
        Problem :
https://www.hackerrank.com/challenges/priyanka-and-toys/problem
My Solution :
#!/usr/bin/env python3
def toys(w):
    w = sorted(w)
    count = 1
    min_w = w[0]
    i = 0
    while i < n:
        if min_w + 4 < w[i]:
            count += 1
            min_w = w[i]
        i += 1
    return count
n = int(input())
w = list(map(int, input().rstrip().split()))
result = toys(w)
print(result)
Comment :
처음에는 위와 같이 정렬을 먼저 하고 접근하였다. 하지만 정렬하지 않고 w가 가질 수 있는 크기만큼 list를 만들어 푸는 아래와 같은 방법도 있다. 두 방법 중 어떤 방법이 더 효율적일까? 그것은 n과 w가 가질 수 있는 값의 범위에 따라 달라진다.
My Solution2 :
#!/usr/bin/env python3
def toys(w):
    containers = [0]*10001
    for container in w:
        containers[container] = 1
    count = i = 0
    while i < 10001:
        if containers[i]:
            count += 1
            i += 4
        i += 1
    return count
n = int(input())
w = list(map(int, input().rstrip().split()))
result = toys(w)
print(result)
'프로그래밍 > HackerRank' 카테고리의 다른 글
| [HackerRank][Python3] Maximizing XOR (0) | 2018.09.08 | 
|---|---|
| [HackerRank][Python3] Permuting Two Arrays (0) | 2018.09.08 | 
| [HackerRank][Python3] Jim and the Orders (0) | 2018.09.08 | 
| [HackerRank][Python3] Largest Permutation (0) | 2018.09.08 | 
| [HackerRank][Python3] Beautiful Pairs (0) | 2018.09.08 | 
| [HackerRank][Python3] Maximum Perimeter Triangle (0) | 2018.09.08 | 
| [HackerRank][Python3] Grid Challenge (0) | 2018.09.08 | 
| [HackerRank][Python3] Strange Counter (0) | 2018.09.07 | 
최근에 달린 댓글 최근에 달린 댓글