最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Algorithm confusion in codewars - Stack Overflow

programmeradmin1浏览0评论

So here is the problem:

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.

My code:

function tickets(peopleInLine) {
  var speakVasya = "";
  var vasyaValue = 0;
  for (var i = 0; i < peopleInLine.length; i++) {
    if (peopleInLine[i] - 25 > vasyaValue) {
      speakVasya = "NO";
      break;
    } else {
      vasyaValue += 25;
      speakVasya = "YES";
    }
  }
  document.write(speakVasya);
}


tickets([25, 25, 50]);

Live demo here /

My problem is, according to codewars,my solution passes 7 tests but fails at 2, but i can't understand which ones as it doesn't give the arguements which puts in tickets function.

So here is the problem:

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.

My code:

function tickets(peopleInLine) {
  var speakVasya = "";
  var vasyaValue = 0;
  for (var i = 0; i < peopleInLine.length; i++) {
    if (peopleInLine[i] - 25 > vasyaValue) {
      speakVasya = "NO";
      break;
    } else {
      vasyaValue += 25;
      speakVasya = "YES";
    }
  }
  document.write(speakVasya);
}


tickets([25, 25, 50]);

Live demo here https://jsfiddle/py234z11/1/

My problem is, according to codewars,my solution passes 7 tests but fails at 2, but i can't understand which ones as it doesn't give the arguements which puts in tickets function.

Share Improve this question asked May 3, 2016 at 20:17 Dimitris XydasDimitris Xydas 2332 gold badges5 silver badges9 bronze badges 4
  • 1 I feel like there is a lot of required information missing. If everyone has a 25 dollar bill and a ticket is 25 then he won't need any change and can just sell each person in line one ticket. – IMTheNachoMan Commented May 3, 2016 at 20:20
  • 1 @IMTheNachoMan yes sorry english is not my first language and trying my best to explain it. So if everyone had 25 dollars in a lets say line of 30 people ie tickets([25,25,25,25....]) the programm will print YES as it should because she would never run out of money. But if the first guy had 25 and second guy had 100 dollar bill waiting for 75 change, poor Vasya would not have any spare change so it would print NO – Dimitris Xydas Commented May 3, 2016 at 20:25
  • Without seeing your input lines how can we tell. – bhspencer Commented May 3, 2016 at 20:25
  • I worked as a cashier in a cinema in Europe and we used to refuse entrance to people who only had 25-euro banknotes... – Stef Commented Nov 27, 2023 at 16:05
Add a ment  | 

2 Answers 2

Reset to default 4

Counting the amount of money he holds is not enough - you need to keep track of the count of the bills.

For example, if you have one $100 bill, you cannot make change for someone with a $50 or $100 bill, you need the specific bill (the $25 bill to make change for $50, and $25 and $50 OR three $25 bills to make change for the $100 bill).

So rather than keep a total money collected, keep track of the number of bills collected.

My C# solution for this problem, edited

public static string CanSellTickets(int[] peopleInLine) {

        int count25 = 0;
        int count50 = 0;

        for (var i = 0; i < peopleInLine.Length; i++) {
            if (peopleInLine[i] == 25) {
                count25++;
            }
            else if (peopleInLine[i] == 50) {
                if (count25 == 0) {
                    return "NO";
                }
                else {
                    count25--;
                    count50++;
                }
            }
            else if (peopleInLine[i] == 100) {
                if (count50 >= 1 && count25 >= 1) {
                    count25--;
                    count50--;
                }
                else if (count50 == 0 && count25 >= 3) {
                    count25 = count25 - 3;
                }
                else {
                    return "NO";
                }
            }
        }

        return "YES";
    }
发布评论

评论列表(0)

  1. 暂无评论