Vasya - Clerk ( 6 kyu )

문제

The new “Avengers” movie has just been released! There are a lot of people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 dollars bill. A “Avengers” ticket costs 25 dollars.

Vasya is currently working as a clerk. He wants to sell a ticket to every single person in this line.

Can Vasya sell a ticket to each person and give the change if he initially has no money and sells the tickets strictly in the order people follow in the line?

Return YES, if Vasya can sell a ticket to each person and give the change. Otherwise return NO.

Examples:

// === JavaScript ==

tickets([25, 25, 50]) // => YES
tickets([25, 100])
        // => NO. Vasya will not have enough money to give change to 100 dollars

25달러짜리 티켓을 파는데 잔돈이 부족하면 NO, 가능하다면 YES를 리턴

풀이

function tickets(peopleInLine){
  let obj = { price: 0 };
  for ( let i of peopleInLine ) {
    if ( i === 25 ) {
      obj.price += 25
    } else if ( i === 50 ) {
      obj.price -= 25
      if ( obj.price < 0 ) return "NO";
      obj.price += 50
    } else if ( i === 100 ) {
      obj.price -= 75
      if ( obj.price < 0 ) return "NO";
    }
  }
  return "YES";
}

if문으로 쭉 엮어서 현재 보유한 금액이 거스름돈보다 적으면 NO 리턴, 포문 탈줄시 YES 리턴

생각하는데 그래도 시간이 조금 걸렸음.

다른 사람의 풀이

죄다 스위치 케이스로 해놨는데, 복잡해서 걍 안읽어봄 ㅎㅎㅎ


'자료구조, 알고리즘 > Codewars 문제' 카테고리의 다른 글

Unique In Order ( 6 kyu )  (0) 2017.11.26
Counting Duplicates ( 6 kyu )  (0) 2017.11.26
Build a pile of Cubes ( 6 kyu )  (0) 2017.11.25
Take a Ten Minute Walk ( 6 kyu )  (0) 2017.11.24
IQ Test ( 6 kyu )  (0) 2017.11.24

+ Recent posts