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

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

wants to merge 1 commit into from

Conversation

deipanema
Copy link
Member

@deipanema deipanema commented Mar 14, 2023

구현 방법

  1. 물이 새는 곳의 개수(N)와 테이프 길이(L)를 선언합니다.
  2. 물이 새는 곳의 위치 배열을 선언합니다. 내림차순으로 되어있는 테스트 케이스가 있기 때문에 오름차순 정렬 합니다.
  3. current로 현재 위치를 나타냅니다. end로 하나의 테이프 길이로 막을 수 있는 인덱스를 나타냅니다. result에 사용된 총 테이프 개수를 나타냅니다.
  4. while() 반복문으로 current가 N까지 반복합니다.
    4-1. 현재 위치에서 하나의 테이프를 사용한 길이(L)를 더하고 0.5 간격을 빼줍니다. 이를 end에 할당합니다.
    4-2. 테이프를 한 번 사용했으니 result++ 합니다.
    4-3. 인덱스를 +1 옮겨요.
    4-4. if 조건문 다음 인덱스가 end보다 작은 경우 이전 테이프로 물 새는 곳을 막았기 때문에 넘어갑니다.
  5. result를 반환합니다.

시간 복잡도

  • 문자 데이터에서 숫자 데이터로 타입 변환 시 + 연산자보다 Number() 래퍼 객체로 변환할 때 시간이 적게 들어요.

참고

https://github.com/ndb796/BOJ_JavaScript/blob/main/solutions/1449.js

@deipanema deipanema self-assigned this Mar 14, 2023
@deipanema deipanema linked an issue Mar 14, 2023 that may be closed by this pull request
@deipanema deipanema requested a review from ViGilanteAF March 16, 2023 05:37
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개만 입력했을 경우도 한번 생각 해보면 좋을듯 합니다.
이럴경우 아예 그냥 프로그램이 종료가 되던가 아니면 하나더 입력 요청을 하는 코드도 있으면 좋을듯 합니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BOJ] 1449. 수리공 항승
2 participants