일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 코플릿
- 개발자의 책장
- join
- array.push()
- 개발자_조이킴
- array.slice()
- 자바스크립트
- 정규표현식
- MySQL
- 블록체인
- 재귀함수
- Programmers
- Hackerrank
- 배열
- 코딩테스트
- JavaScript
- 최강의 인생
- select
- 프로그래머스
- Where
- 알고리즘
- 코드스테이츠
- 역행자
- 코딩공부
- node.js
- Developer_JoyKim
- array
- for문
- SQL
- Algorithms
Archives
- Today
- Total
CodingSpace
[HackerRank/Algorithms] Search - Ice Cream Parlor 본문
Problem. Search - Ice Cream Parlor
Link.
https://www.hackerrank.com/challenges/icecream-parlor/problem?isFullScreen=true
Description.
Two friends like to pool their money and go to the ice cream parlor. They always choose two distinct flavors and they spend all of their money.
Given a list of prices for the flavors of ice cream, select the two that will cost all of the money they have.
Key Point.
My Answer.
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
/*
* Complete the 'icecreamParlor' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER m
* 2. INTEGER_ARRAY arr
*/
function icecreamParlor(m, arr) {
for(let i = 0; i < arr.length - 1; i++) {
// Step1. 첫번째 친구의 값을 결정
let friend_1 = arr[i];
// Step2. 설정된 첫번째 친구 값을 기준으로
// 문제의 조건을 만족하는 조합이 있는지 조회
for(let j = 0; j < arr.length; j++) {
// 첫번째와 두번째 친구의 인덱스가 다른경우만 조회한다
if(i !== j) {
let friend_2 = arr[j];
// Step3. 문제의 조건에 만족하는 조합인 경우
// 해당 인덱스 조합을 반환한다.
if(friend_1 + friend_2 === m) {
return [i + 1, j + 1]
}
}
}
}
}
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const t = parseInt(readLine().trim(), 10);
for (let tItr = 0; tItr < t; tItr++) {
const m = parseInt(readLine().trim(), 10);
const n = parseInt(readLine().trim(), 10);
const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
const result = icecreamParlor(m, arr);
ws.write(result.join(' ') + '\n');
}
ws.end();
}
References.
'HackerRank > Algorithm' 카테고리의 다른 글
[HackerRank/Algorithms] Sorting - Big Sorting (0) | 2022.09.10 |
---|---|
[HackerRank/Algorithms] Implementation - Equalize the Array (0) | 2022.09.08 |
[HackerRank/Algorithms] Strings - CamelCase (0) | 2022.09.06 |
[HackerRank/Algorithms] Implementation - Cut the sticks (0) | 2022.09.04 |
[HackerRank/Algorithms] Warmup - Compare the Triplets (0) | 2022.09.03 |
Comments