일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- springmvc
- container
- swaggerUrl
- gitbash
- GitHub
- springframeworkruntime
- 부스트코스
- 파일삭제
- springboot
- assume
- Swagger
- assume True
- assume That
- DispatcherServlet
- 원격저장소
- assuming That
- MVC
- 컴퓨터과학
- c언어
- .idea
- MVC모듈
- Git
- 팀과제
- CS50
- Junit5
- .out
- 스프링프레임워크
- springsecurity
- securityconfig
- Spring
- Today
- Total
도담이 먹여 살려야하는 집사
[2021.01.22]Compare the Triplets 본문
Problem
Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty.
The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).
The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].
- If a[i] > b[i], then Alice is awarded 1 point.
- If a[i] < b[i], then Bob is awarded 1 point.
- If a[i] = b[i], then neither person receives a point.
Comparison points is the total points a person earned.
Given a and b, determine their respective comparison points.
Example
a = [1, 2, 3]
b = [3, 2, 1]
- For elements *0*, Bob is awarded a point because a[0] .
- For the equal elements a[1] and b[1], no points are earned.
- Finally, for elements 2, a[2] > b[2] so Alice receives a point.
The return array is [1, 1] with Alice's score first and Bob's second.
Function Description
Complete the function compareTriplets in the editor below.
compareTriplets has the following parameter(s):
- int a[3]: Alice's challenge rating
- int b[3]: Bob's challenge rating
Return
- int[2]: Alice's score is in the first position, and Bob's score is in the second.
Input Format
The first line contains 3 space-separated integers, a[0], a[1], and a[2], the respective values in triplet a.
The second line contains 3 space-separated integers, b[0], b[1], and b[2], the respective values in triplet b.
Constraints
- 1 ≤ a[i] ≤ 100
- 1 ≤ b[i] ≤ 100
Sample Input 0
5 6 7
3 6 10
Sample Output 0
1 1
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
// Complete the compareTriplets function below.
static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
int alice = 0;
int bob = 0;
for(int i = 0; i<3; i++) {
if(a.get(i)>b.get(i)) {
alice++;
}else if (a.get(i)<b.get(i)) {
bob++;
}
}
return Arrays.asList(alice,bob);
}
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
List<Integer> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> b = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
List<Integer> result = compareTriplets(a, b);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
[ArrayList]
ArrayList is a part of collection framework and is present in java.util package. It provides us with dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed. This class is found in java.util package.
- Collection Framework의 일부 >> List 인터페이스를 상속받음 크기가 가변적으로 변하는 선형리스트
- java.util 패키지
- java 동적 배열 제공 >> 객체들이 추가되어 저장 용량(capacity)을 초과한다면 자동으로 부족한 크기만큼 저장 용량이 늘어나는 특징
ArrayList 선언
ArrayList list = new ArrayList();//타입 미설정 Object로 선언된다.
ArrayList<Student> members = new ArrayList<Student>();//타입설정 Student객체만 사용가능
ArrayList<Integer> num = new ArrayList<Integer>();//타입설정 int타입만 사용가능
ArrayList<Integer> num2 = new ArrayList<>();//new에서 타입 파라미터 생략가능
ArrayList<Integer> num3 = new ArrayList<Integer>(10);//초기 용량(capacity)지정
ArrayList<Integer> list2 = new ArrayList<Integer>(Arrays.asList(1,2,3));//생성시 값추가
※제네릭스는 선언할 수 있는 타입이 객체 타입입니다. int는 기본자료형이기 때문에 들어갈수 없으므로 int를 객체화시킨 wrapper클래스를 사용해야 합니다.
ArrayList Adding Elements
- add(Object) : ArrayList의 끝에 요소를 추가하는데 사용됨.
- add(int index, Object) : ArrayList의 특정 인덱스에 요소를 추가하는데 사용됨.
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(3); //값 추가
list.add(null); //null값도 add가능
list.add(1,10); //index 1뒤에 10 삽입
ArrayList<Student> members = new ArrayList<Student>();
Student student = new Student(name,age);
members.add(student);
members.add(new Member("홍길동",15));
'Algorithm' 카테고리의 다른 글
Day7 Grading Students (0) | 2021.02.03 |
---|