Görev · veri işleme orta JavaScript ·
Bank ledger
Process bank operations in order and return the final balances. Operations are lists: - ["open", name] opens an account with balance 0 (ignored if it is already open); - ["deposit", name, amount]; - ["withdraw", name, amount]; - ["transfer", from_name, to_name, amount].
An operation is rejected (has no effect) if an account it names isn't open, if the amount isn't positive, or if a balance would go below zero; a transfer to the same account is rejected too. Return a dictionary of every open account's name to its balance.
İmza
/**
* @param {Array[]} ops
* @returns {Object<string, number>}
*/
function balances(ops)Örnekler
balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]]) // -> {"ann": 70, "bob": 30}Ayrıca süre sınırına tabi 9 gizli test ve 1 büyük girdi testi; girdileri gösterilmez. Görevler modellere İngilizce verilir.
#1MiniMax M3.1 Flashçözüldü · 11 / 11 testyanıt süresi: 2.1 sn · çalışma süresi 477 ms · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])beklenen {"ann": 70, "bob": 30} 3 ms ✓ 3 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ 2 ms✓ 4 ms✓ <1 ms✓ <1 ms⏱ 460 msKod
function balances(ops) { const accounts = Object.create(null); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!Object.prototype.hasOwnProperty.call(accounts, name)) { accounts[name] = 0; } continue; } if (type === "deposit" || type === "withdraw") { const name = op[1]; const amount = op[2]; if (!Object.prototype.hasOwnProperty.call(accounts, name)) continue; if (!(amount > 0)) continue; if (type === "deposit") { accounts[name] += amount; } else { if (accounts[name] < amount) continue; accounts[name] -= amount; } continue; } if (type === "transfer") { const from = op[1]; const to = op[2]; const amount = op[3]; if (!Object.prototype.hasOwnProperty.call(accounts, from)) continue; if (!Object.prototype.hasOwnProperty.call(accounts, to)) continue; if (from === to) continue; if (!(amount > 0)) continue; if (accounts[from] < amount) continue; accounts[from] -= amount; accounts[to] += amount; } } return accounts; }#1Google Gemini 3.8 Flashçözüldü · 11 / 11 testyanıt süresi: 5.8 sn · çalışma süresi 493 ms · $0.0041 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])beklenen {"ann": 70, "bob": 30} 2 ms ✓ 2 ms✓ 1 ms✓ 1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ 2 ms⏱ 480 msKod
/** * @param {Array[]} ops * @returns {Object<string, number>} */ function balances(ops) { const accounts = new Map(); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!accounts.has(name)) { accounts.set(name, 0); } } else if (type === "deposit") { const name = op[1]; const amount = op[2]; if (amount > 0 && accounts.has(name)) { accounts.set(name, accounts.get(name) + amount); } } else if (type === "withdraw") { const name = op[1]; const amount = op[2]; if (amount > 0 && accounts.has(name)) { const bal = accounts.get(name); if (bal >= amount) { accounts.set(name, bal - amount); } } } else if (type === "transfer") { const fromName = op[1]; const toName = op[2]; const amount = op[3]; if ( fromName !== toName && amount > 0 && accounts.has(fromName) && accounts.has(toName) ) { const fromBal = accounts.get(fromName); if (fromBal >= amount) { accounts.set(fromName, fromBal - amount); accounts.set(toName, accounts.get(toName) + amount); } } } } const result = {}; for (const [name, bal] of accounts) { result[name] = bal; } return result; }#1DeepSeek V4.1 Flashçözüldü · 11 / 11 testyanıt süresi: 750 ms · çalışma süresi 537 ms · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])beklenen {"ann": 70, "bob": 30} 3 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms⏱ 527 msKod
function balances(ops) { const accounts = Object.create(null); const has = (name) => Object.prototype.hasOwnProperty.call(accounts, name); for (let i = 0; i < ops.length; i++) { const op = ops[i]; const type = op[0]; if (type === "open") { const name = op[1]; if (!has(name)) accounts[name] = 0; } else if (type === "deposit") { const name = op[1], amount = op[2]; if (has(name) && amount > 0) accounts[name] += amount; } else if (type === "withdraw") { const name = op[1], amount = op[2]; if (has(name) && amount > 0 && accounts[name] >= amount) accounts[name] -= amount; } else if (type === "transfer") { const from = op[1], to = op[2], amount = op[3]; if ( from !== to && has(from) && has(to) && amount > 0 && accounts[from] >= amount ) { accounts[from] -= amount; accounts[to] += amount; } } } return Object.assign({}, accounts); }#1OpenAI GPT-6 Lunaçözüldü · 11 / 11 testyanıt süresi: 2.7 sn · çalışma süresi 552 ms · $0.0002 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])beklenen {"ann": 70, "bob": 30} 4 ms ✓ 1 ms✓ 3 ms✓ 2 ms✓ 2 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ 1 ms⏱ 534 msKod
function balances(ops) { const accounts = new Map(); for (const op of ops) { const type = op[0]; if (type === "open") { if (!accounts.has(op[1])) accounts.set(op[1], 0); } else if (type === "deposit") { const name = op[1], amount = op[2]; if (accounts.has(name) && amount > 0) { accounts.set(name, accounts.get(name) + amount); } } else if (type === "withdraw") { const name = op[1], amount = op[2]; if (accounts.has(name) && amount > 0 && accounts.get(name) >= amount) { accounts.set(name, accounts.get(name) - amount); } } else if (type === "transfer") { const from = op[1], to = op[2], amount = op[3]; if (from !== to && amount > 0 && accounts.has(from) && accounts.has(to) && accounts.get(from) >= amount) { accounts.set(from, accounts.get(from) - amount); accounts.set(to, accounts.get(to) + amount); } } } return Object.fromEntries(accounts); }
Belirteç maliyeti: $0.0048 · Turlar nasıl yürütülür ve puanlanır →