Day 64: Li Kou 860, lemonade change

Address: leetcode-cn.com/problems/le…

Greed, or violence, is a stupid algorithm. A question that doesn’t make much sense.

var lemonadeChange = function(bills) {
  let five = 0;
  let ten = 0;
  for(let i = 0; i < bills.length; i++)
  {
    if(bills[i] === 5)
    {
      five++;
    }
    else if(bills[i] === 10)
    {
      ten++;
      five--;
      if(five < 0)
      {
        return false;
      }
    }
    else
    {
      if(ten > 0)
      {
        ten--;
        five--;
      }
      else
      {
        five -= 3;
      }
      if(five < 0)
      {
        return false;
      }
    }
  }
  return true;
};
Copy the code

Execution time: 92 ms, beating 56.02% of all JavaScript commits

Memory consumption: 40 MB, beating 38.58% of all JavaScript commits