본문 바로가기
1 Day 1 Algorithms

[2018.12.28] Scope

by 곰돌찌 2018. 12. 28.

Problem


Objective 
Today we're discussing scope. Check out the Tutorial tab for learning materials and an instructional video!


The absolute difference between two integers,  and , is written as . The maximum absolute difference between two integers in a set of positive integers, , is the largest absolute difference between any two integers in .

The Difference class is started for you in the editor. It has a private integer array () for storing  non-negative integers, and a public integer () for storing the maximum absolute difference.

Task 
Complete the Difference class by writing the following:

  • A class constructor that takes an array of integers as a parameter and saves it to the  instance variable.
  • computeDifference method that finds the maximum absolute difference between any  numbers in  and stores it in the  instance variable.

Input Format

You are not responsible for reading any input from stdin. The locked Solution class in your editor reads in  lines of input; the first line contains , and the second line describes the  array.

Constraints

  • , where 

Output Format

You are not responsible for printing any output; the Solution class will print the value of the  instance variable.

Sample Input

3
1 2 5

Sample Output

4

Explanation

The scope of the  array and  integer is the entire class instance. The class constructor saves the argument passed to the constructor as the  instance variable (where the computeDifference method can access it).

To find the maximum difference, computeDifference checks each element in the array and finds the maximum difference between any  elements:  
 

The maximum of these differences is , so it saves the value  as the  instance variable. The locked stub code in the editor then prints the value stored as , which is .


How I solved the problem


# 결국 Arrays.sort를 통해 가장 큰 수와 가장 작은 수의 차이를 구하면 됨.

# 아래에서 difference.maximunDifference 를 호출하는데 이에 대한 값이 computeDifference()에 있음. 여기서 maximunDifference의 값을 지정함. 즉, computeDifference에서 Diffrence클래스의 전체 Scope(범위, 영역)을 담당하고 있는 maximunDifference에 값을 저장할 수 있음.


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
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
 
 
class Difference {
      private int[] elements;
      public int maximumDifference;
 
    // Add your code here
    Difference(int[] elements) {
        this.elements = elements;
    }
 
    public void computeDifference() {
        Arrays.sort(elements);
 
        int indexNum = elements.length - 1;
 
        maximumDifference = elements[indexNum]  - elements[0];
    }
// End of Difference class
 
public class Solution {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        sc.close();
 
        Difference difference = new Difference(a);
 
        difference.computeDifference();
 
        System.out.print(difference.maximumDifference);
    }
}
cs


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

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

[2019.01.03] Exceptions - String to Integer  (0) 2019.01.03
[2019.01.02] Linked List  (0) 2019.01.02
[2018.12.27] Abstract Classes  (0) 2018.12.27
[2018.12.26] Inheritance  (0) 2018.12.26
[2018.12.21] Binary Numbers  (0) 2018.12.21

댓글