JSFiddle - React, Tailwind, and code Playground

by kigorw

JavaScript

//The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109)
//— the number of ingredients and the number of grams of the magic powder.

var n = 1  // number of ingridients
var k = 1000000000 // number of grams of magic powder
var a = [1] //a1, a2, ..., an (1 ≤ ai ≤ 109) // num of gramms of ingridient needed
var b = [100000000] // num of gramms of ingridient that has


var n = 2  // number of ingridients
var k = 2 // number of grams of magic powder
var a = [1, 2] //a1, a2, ..., an (1 ≤ ai ≤ 109) // num of gramms of ingridient needed
var b = [1, 2] // num of gramms of ingridient that has

function cookies(amountNeeded, amountProvided) {
	return amountNeeded > amountProvided
  	? [0, amountProvided - amountNeeded]
    : [Math.floor(amountProvided / amountNeeded), amountProvided % amountNeeded]
}
console.log(cookies(57, 11))

function makeRecipes(amountProvided, amountNeeded) {
	var result = _.zip(amountProvided, amountNeeded)
  result.sort(function(a, b) { return a[0] - b[0] })
  return result
}

function howManyCanCook(amountsProvided, amountsNeeded) {
	var result = -1
  amountsProvided.forEach(function(a, i) {
  	var cookingResult = cookies(amountsNeeded[i], a)
    console.log(amountsNeeded[i], JSON.stringify(cookingResult))
  	if (result == -1) result = cookingResult[0]
    if (result > cookingResult[0]) {
    	result = cookingResult[0]
    }
  })
  return result
}

function cook(amountsProvided, amountsNeeded, cookiesAmount) {
	return amountsProvided.map(function(amountProvided, i) {
  	return amountProvided - amountsNeeded[i] * cookiesAmount
  })
}

// experiment 1
var n = 2  // number of ingridients
var k = 2 // number of grams of magic powder
var a = [1, 2] //a1, a2, ..., an (1 ≤ ai ≤ 109) // num of gramms of ingridient needed
var b = [3, 11] // num of gramms of ingridient that has

var recipes = makeRecipes(b, a)
console.log('hehe', JSON.stringify(recipes))
// var canCookAmount = howManyCanCook(b, a)
//var leftover = cook(b, a,...