[백준][ts/js] 13305: 주유소

Pyotato·2023년 5월 12일
0

[백준][js/ts]

목록 보기
4/21
post-thumbnail

로직

logic

  • 가능한 모든 케이스 구하기 (그리디)
  • 리터당 가격이 일정하다면 (배열에서 min값과 max값이 동일) forloop 거치지 않고 리턴
  • 현재가격과 지금까지 비교했던 값을 비교해서 작을 때만 업데이틓해준다.
  • 리터 최소가격 * 거리를 합해 최소 비용을 구하기

풀이

// https://www.acmicpc.net/problem/1182
const N_S: string | null = prompt("enter number of cases and target number:");
if (N_S) {
  const N: number = parseInt(N_S.split(" ")[0]);
  const S: number = parseInt(N_S.split(" ")[1]);
  const test_str: string | null = prompt("Enter sequence:");
  const t: Array<number> | null = test_str
    ? test_str.split(" ").map((v: string) => parseInt(v))
    : [];
  let count: number = 0;

  if (N === t.length) {
    const dfs = (n: number, s: number) => {
      if (n >= N) {
        return count;
      }
      s += t[n];
      if (s === S) {
        count++;
      }
      dfs(n + 1, s);
      dfs(n + 1, s - t[n]);
      return count;
    };

    console.log(dfs(0, 0));
  } else {
    console.log("invalid number of args");
  }
} else {
  console.log("invalid args");
}

// test
// 5 0
// -7 -3 -2 5 8

profile
https://pyotato-dev.tistory.com/ 로 이사중 🚚💨🚛💨🚚💨

0개의 댓글