다른 방법도 생각나는데, 코딜리티가 딕셔너리를 좋아하길래 넣어줬더니 맞았다. 내맘대로 풀면 88점이 나온다.
A non-empty array A consisting of N integers is given.A permutation is a sequence containing each element from 1 to N once, and only once.For example, array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2is a permutation, but array A such that: A[0] = 4 A[1] = 1 A[2] = 3is not a permutation, because value 2 is missing.The goal is to check whether array A is a permutation.Write a function:def solution(A)that, given an array A, returns 1 if array A is a permutation and 0 if it is not.For example, given array A such that: A[0] = 4 A[1] = 1 A[2] = 3 A[3] = 2the function should return 1.Given array A such that: A[0] = 4 A[1] = 1 A[2] = 3the function should return 0.Write an efficient algorithm for the following assumptions:N is an integer within the range [1..100,000];each element of array A is an integer within the range [1..1,000,000,000].( -> O(nlogn) 혹은 O(n)으로 풀란 소리)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
import collections
def solution(A):
# write your code in Python 3.6
max_a = max(A)
min_a = min(A)
len_a = len(A)
if min_a !=1 or max_a!=len_a:
return 0
A = collections.Counter(A)
#print(A)
for i in range(1, max_a+1):
if i not in A:
return 0
return 1
'알고리즘' 카테고리의 다른 글
최소 신장 트리의 이해2 개선된 프림 (0) | 2020.10.24 |
---|---|
프로그래머스 게임맵 최단거리 (0) | 2020.10.23 |
코딜리티 MissingInteger (0) | 2020.10.21 |
코딜리티 pdf 번역 리뷰 (0) | 2020.10.21 |
codility: MaxCounters 파이썬 (0) | 2020.10.20 |