Codility 4-1 PermCheck
A non-empty zero-indexed 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:
int solution(int A[], int N);
that, given a zero-indexed 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.
Assume that:
- 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].
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Copyright 2009โ2018 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
์ฃผ์ด์ง ๋ฐฐ์ด์ด ์์ด์ธ์ง ์๋์ง๋ฅผ ์ฒดํฌํด์ผ ํ๋ ๋ฌธ์ .
4, 1, 3, 2๋ 1~4๋ฅผ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ์์ด์ด๋ผ์ 1์ ๋ฆฌํด, 4, 1, 3์ 2๊ฐ ๋น ์ง ์์ด์ด๊ธฐ๋๋ฌธ์ 0์ ๋ฆฌํดํ๋ ๋ฌธ์ ๋ค.
์ฒ์์๋ ์๋์ฒ๋ผ ํ์๋ค. ์ด์ ๋ฌธ์ ์ฒ๋ผ ์ ์ฒด ํฉ์ ๋น๊ตํ๋ฉด ๋์ง ์์๊น? ํ๊ณ .
// you can also use imports, for example:
import java.util.*;
import java.util.Arrays;
import java.util.stream.IntStream;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
long sum = IntStream.range(1, Arrays.stream(A).max().getAsInt() + 1).sum();
return (Arrays.stream(A).sum() == sum) ? 1 : 0;
}
}๊ทผ๋ฐ ์ผ๋จ ์คํธ๋ฆผ์ ์จ์ ์๊ฐ์ด๊ณผ๊ฐ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ ํฐ ๋ฌธ์ ๋ {2, 2, 2, 4}๋ {1, 2, 3, 4}๋ ๊ฐ์ ํฉ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์ ์์ด์ ์กฐ๊ฑด์ ๋ง์ง ์๋๋ค.
๊ทธ๋ฆฌ๊ณ ๋๋ฒ์งธ๋..ํด๋น ๊ฐ์ด ์๋์ง ์ฒดํฌํ๋ boolean ๋ฐฐ์ด์ ๋ง๋ค์ด์ ์ฒดํฌํ๋ ๋ฐฉ๋ฒ์ด์๋ค. A ๋ฐฐ์ด๊ณผ ๊ฐ์ ํฌ๊ธฐ์ ๋ฐฐ์ด์ด ์์ด์ผ ํ๊ธฐ ๋๋ฌธ์ ๊ณต๊ฐ ๋ณต์ก๋๊ฐ ๋์ด๋์ง๋ง ํ์คํ ๋ ๊ฒ ๊ฐ์๋ค.
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
int n = Arrays.stream(A).max().getAsInt();
Boolean check[] = new Boolean[n + 1];
Arrays.fill(check, false);
check[0] = true;
for(int i=0; i<A.length; i++) {
if(n < A[i]) return 0;
check[A[i]] = true;
}
return (Arrays.stream(check).filter(c -> c == false).count() == 0) ? 1 : 0;
}
}์์ ๋ฐฉ๋ฒ์์๋ checkํ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ฅผ A๋ฐฐ์ด์ ์ต๋๊ฐ์ผ๋ก ์คฌ๋ค. 4,5,6,7 ... 5000์ธ ๊ฒฝ์ฐ 4~5000๊น์ง์ ์์์์ด ๋ง์ผ๋๊น ์ ๋ฐ๊ฑธ ์ฒดํฌํ ์ ์์ด์ผ ํ ๊ฒ ๊ฐ์์ ์ ๋ ๊ฒ ์คฌ๋๋ฐ. ์ ๋ ๊ฒ ์ฃผ๋ ๊ฒฝ์ฐ 1, 1์ด๋ฐ ์์์์ ๋ํด์ ํฌ๊ธฐ 1์ check ๋ฐฐ์ด์ด ์์ฑ๋๊ณ , A์ ๊ธธ์ด๋ 2์ด๊ธฐ ๋๋ฌธ์ ์๋ฌ๊ฐ ๋๋ค.
// you can also use imports, for example:
import java.util.*;
import java.util.Arrays;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
Boolean check[] = new Boolean[A.length + 1];
Arrays.fill(check, false);
check[0] = true;
for(int i=0; i<A.length; i++) {
if(A.length < A[i]) return 0;
check[A[i]] = true;
}
return (Arrays.stream(check).filter(c -> c == false).count() == 0) ? 1 : 0;
}
}๋ง์ง๋ง์ผ๋ก ์์ฒ๋ผ ๊ตฌํํ์๋ ๋ต์๋ค ๋ง์ง๋ง ์๊ฐ์ด๊ณผ๊ฐ ๋๋ ํ ์คํธ์ผ์ด์ค๊ฐ ๋ช ์์๋ค.
์๋์ฒ๋ผ ์คํธ๋ฆผ์ ๊ฑท์ด๋ด๊ณ , for loop๋ก ๋ง๋ค๋ฉด ์๋์ ๊ฐ์ ์๊ฐ๋ณต์ก๋์, 100% ์ ๋ต๋ฅ ์ด ๋์จ๋ค.
๋ณ๋ก ์ ์ด๋ ค์ด ๋ฌธ์ ์ธ๋ฐ ๋๋ฌด ๊ณ ์์ ๋ง์ด ํ ๊ฒ ๊ฐ๋ค..
Detected time complexity: O(N) or O(N * log(N))
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
Boolean check[] = new Boolean[A.length + 1];
Arrays.fill(check, false);
check[0] = true;
for(int i=0; i<A.length; i++) {
if(A.length < A[i]) return 0;
check[A[i]] = true;
}
for(int i=0; i<check.length; i++) {
if(check[i] == false) return 0;
}
return 1;
}
}