Skip to content

Latest commit

ย 

History

History
178 lines (119 loc) ยท 5.07 KB

File metadata and controls

178 lines (119 loc) ยท 5.07 KB

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;
    }
}