본문 바로가기
1 Day 1 Algorithms

[2019.03.12] Sequence Equation

by 곰돌찌 2019. 3. 12.

Problem


Given a sequence of  integers,  where each element is distinct and satisfies . For each where , find any integer  such that  and print the value of  on a new line.

For example, assume the sequence . Each value of  between  and , the length of the sequence, is analyzed as follows:

  1. , so 
  2. , so 
  3. , so 
  4. , so 
  5. , so 

The values for  are .

Function Description

Complete the permutationEquation function in the editor below. It should return an array of integers that represent the values of .

permutationEquation has the following parameter(s):

  • p: an array of integers

Input Format

The first line contains an integer , the number of elements in the sequence. 
The second line contains  space-separated integers  where .

Constraints

  • , where .
  • Each element in the sequence is distinct.

Output Format

For each  from  to , print an integer denoting any valid  satisfying the equation  on a new line.

Sample Input 0

3
2 3 1

Sample Output 0

2
3
1

Explanation 0

Given the values of , and , we calculate and print the following values for each  from  to :

  1. , so we print the value of  on a new line.
  2. , so we print the value of  on a new line.
  3. , so we print the value of  on a new line.

Sample Input 1

5
4 3 5 1 2

Sample Output 1

1
3
5
4
2

How I solved the problem


# 주어진 값을 Map에 넣음! key가 주어진 값이고 value는 순서(인덱스 + 1)가 넣어짐.

# 처음에 key의 값을 찾은 다음 그 값을 한 번더 Map에서 찾으면 permutation equation이 될 수 있음!!!!


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
 
public class Solution {
 
    // Complete the permutationEquation function below.
    static int[] permutationEquation(int[] p) {
        int pLength = p.length;
        Map<Integer, Integer> permutation = new HashMap<Integer, Integer>();
        int[] result = new int[pLength];
 
        for (int i = 0; i < pLength; i++) {
            permutation.put(p[i], (i + 1));
        }
 
        for (int i = 0; i < pLength; i++) {
            int x = i + 1;
            int py = permutation.get(x);
            result[i] = permutation.get(py);
        }
 
        return result;
    }
 
    private static final Scanner scanner = new Scanner(System.in);
 
    public static void main(String[] args) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
 
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        int[] p = new int[n];
 
        String[] pItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int i = 0; i < n; i++) {
            int pItem = Integer.parseInt(pItems[i]);
            p[i] = pItem;
        }
 
        int[] result = permutationEquation(p);
 
        for (int i = 0; i < result.length; i++) {
            bufferedWriter.write(String.valueOf(result[i]));
 
            if (i != result.length - 1) {
                bufferedWriter.write("\n");
            }
        }
 
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


[출처 : https://www.hackerrank.com ]


'1 Day 1 Algorithms' 카테고리의 다른 글

[2019.03.14] Find Digits  (0) 2019.03.14
[2019.03.13] Jumping on the Clouds: Revisited  (0) 2019.03.13
[2019.03.11] Circular Array Rotation  (0) 2019.03.11
[2019.03.07] Save the Prisoner!  (0) 2019.03.07
[2019.03.06] Viral Advertisement  (0) 2019.03.06

댓글