일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- 스프링
- Spring Boot
- thymeleaf
- C++
- Spring Mail
- mybatis
- springboot
- Dependency
- pair
- GOF
- 스프링 부트
- pom.xml
- maven
- Spring
- 프로그래머스
- 스프링부트
- codility
- HashMap
- 코딩테스트
- 의존성관리
- Collections
- list
- java
- spring scheduler
- 스프링 메일
- Arrays
- vuejs #vue #js #프론트엔드 #nodejs #클라이언트사이드 #템플릿엔진
- 스프링 스케줄러
- 프로젝트 구조
- @Scheduled
- Today
- Total
Rooted In Develop
[Codility] Counting Elements - PermCheck / Java 본문
1. 문제
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] = 2
is a permutation, but array A such that:
A[0] = 4 A[1] = 1 A[2] = 3
is not a permutation, because value 2 is missing.
The goal is to check whether array A is a permutation.
Write a function:
class Solution { public int solution(int[] 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] = 2
the function should return 1.
Given array A such that:
A[0] = 4 A[1] = 1 A[2] = 3
the 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].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
2. 코드
import java.util.*;
class Solution {
public int solution(int[] A) {
Arrays.sort(A);
for(int i=0; i<A.length; i++) {
if(A[i] != i+1) {
return 0;
}
}
return 1;
}
}
'Algorithm Test > Java' 카테고리의 다른 글
[Codility] Prefix Sums - GenomicRangeQuery / Java (0) | 2020.06.14 |
---|---|
[Codility] Prefix Sums - CountDiv / Java (0) | 2020.06.14 |
[Codility] Counting Elements - MissingInteger / Java (0) | 2020.06.14 |
[Codility] Counting Elements - MaxCounters / Java (0) | 2020.06.14 |
[Codility] Counting Elements - FrogRiverOne / Java (0) | 2020.06.14 |