본문 바로가기
1 Day 1 Algorithms

[2019.03.13] Jumping on the Clouds: Revisited

by 곰돌찌 2019. 3. 13.

Problem


Aerith is playing a cloud hopping game. In this game, there are sequentially numbered clouds that can be thunderheads or cumulus clouds. Her character must jump from cloud to cloud until it reaches the start again.

To play, Aerith is given an array of clouds,  and an energy level . She starts from  and uses  unit of energy to make a jump of size  to cloud . If Aerith lands on a thundercloud, , her energy () decreases by additional units. The game ends when Aerith lands back on cloud .

Given the values of , , and the configuration of the clouds as an array , can you determine the final value of  after the game ends?

For example, give  and , the indices of her path are . Her energy level reduces by  for each jump to . She landed on one thunderhead at an additional cost of  energy units. Her final energy level is .

Note: Recall that  refers to the modulo operation. In this case, it serves to make the route circular. If Aerith is at  and jumps , she will arrive at .

Function Description

Complete the jumpingOnClouds function in the editor below. It should return an integer representing the energy level remaining after the game.

jumpingOnClouds has the following parameter(s):

  • c: an array of integers representing cloud types
  • k: an integer representing the length of one jump

Input Format

The first line contains two space-separated integers,  and , the number of clouds and the jump distance. 
The second line contains  space-separated integers  where . Each cloud is described as follows:

  • If , then cloud  is a cumulus cloud.
  • If , then cloud  is a thunderhead.

Constraints

Output Format

Print the final value of  on a new line.

Sample Input

8 2
0 0 1 0 0 1 1 0

Sample Output

92

Explanation

In the diagram below, red clouds are thunderheads and purple clouds are cumulus clouds:

game board

Observe that our thunderheads are the clouds numbered , and . Aerith makes the following sequence of moves:

  1. Move: , Energy: .
  2. Move: , Energy: .
  3. Move: , Energy: .
  4. Move: , Energy: .


How I solved the problem


# 배열에서 점프의 단위로 나누었을 때 0이 되는 값들을 계산해줘야함.

# 한 번 점프를 할 때마다 무조건 에너지는 1을 소비하게 되는데 만약 배열에서 값이 1인경우에는 천둥번개 지역이기 때문에 추가로 에너지를 2를 더 소모한다는 부분을 체크해줘야함.


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
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 jumpingOnClouds function below.
    static int jumpingOnClouds(int[] c, int k) {
        int cLength = c.length
        int energe = 100;
 
        for (int i = 0; i < cLength; i++) {
            
            if ((i % k) == 0) {
                energe -= 1;
 
                if (c[i] == 1) {
                    energe -= 2;
                }
            }
        }
 
        return energe;
    }
 
    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")));
 
        String[] nk = scanner.nextLine().split(" ");
 
        int n = Integer.parseInt(nk[0]);
 
        int k = Integer.parseInt(nk[1]);
 
        int[] c = new int[n];
 
        String[] cItems = scanner.nextLine().split(" ");
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
 
        for (int i = 0; i < n; i++) {
            int cItem = Integer.parseInt(cItems[i]);
            c[i] = cItem;
        }
 
        int result = jumpingOnClouds(c, k);
 
        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();
 
        bufferedWriter.close();
 
        scanner.close();
    }
}
 
cs


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


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

[2019.03.15] Extra Long Factorials  (0) 2019.03.15
[2019.03.14] Find Digits  (0) 2019.03.14
[2019.03.12] Sequence Equation  (0) 2019.03.12
[2019.03.11] Circular Array Rotation  (0) 2019.03.11
[2019.03.07] Save the Prisoner!  (0) 2019.03.07

댓글