Skip to content

[BOJ] 1449. 수리공 항승 #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions 27. 탐욕법/BOJ/1449.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const input = require("fs").readFileSync(filePath).toString().split("\n");

// const [N, L] = input[0].split(" "); // N: 물이 새는 곳의 개수, L: 테이프 길이
const N = +input[0].split(" ")[0]; // 물이 새는 곳의 개수
const L = +input[0].split(" ")[1]; // 테이프 길이

const pos = input[1]
.split(" ")
.map(v => +v)
.sort((a, b) => a - b);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음수값을 포함해서 정수형으로 정렬을 해줘야 하는 이유가 있을까요??? '문제에서 가장 왼쪽에서 정수 만큼 떨어진 거리만 물이샌다' 라고 하는데요.
nums.sort(function compareNumbers(a, b) { return a - b; });

Copy link
Member Author

@deipanema deipanema Mar 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음... 질문 이해가 잘 안되네용😂
혹시 적어놓으신 코드로 바꾸는 게 좋겠다는 말씀이신가요~?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아뇨 제가 적어놓은코드는 그냥 메모로 적어놓은코드입니다.
작성하신 코드중에 .sort((a,b) => a-b); 이 부분이 음수 가 나와도 정수형으로 변환해서 정렬해주는 부분으로 이해했는데 이부분이 굳이 꼭 필요한가 싶어서요


let current = 0;
let end = 0;
let result = 0;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약에 사용자가 물이 새는 곳이 4곳이라고 하고 기 위치를 3개만 입력했을 경우도 한번 생각 해보면 좋을듯 합니다.
이럴경우 아예 그냥 프로그램이 종료가 되던가 아니면 하나더 입력 요청을 하는 코드도 있으면 좋을듯 합니다.

while (current < N) {
if (end < pos[current]) {
end = pos[current] + L - 0.5;
result++;
}
current++;
}

console.log(result);