โจทย์ · การประมวลผลข้อมูล ปานกลาง 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.
โครงสร้างฟังก์ชัน
/**
* @param {Array[]} ops
* @returns {Object<string, number>}
*/
function balances(ops)ตัวอย่าง
balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]]) // -> {"ann": 70, "bob": 30}พร้อม 9 การทดสอบแบบซ่อน และ 1 การทดสอบชุดข้อมูลขนาดใหญ่ ภายใต้เวลาจำกัด โดยไม่แสดงข้อมูลนำเข้า โจทย์ที่ส่งให้โมเดลเป็นภาษาอังกฤษ
อันดับ #1MiniMax M3.1 Flashแก้ผ่าน · ผ่าน 11 จาก 11 การทดสอบตอบใน 2.1 วินาที · เวลาประมวลผล 477 มิลลิวินาที · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])ค่าที่คาดหวัง {"ann": 70, "bob": 30} 3 มิลลิวินาที ✓ 3 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 2 มิลลิวินาที✓ 4 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที⏱ 460 มิลลิวินาทีโค้ด
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แก้ผ่าน · ผ่าน 11 จาก 11 การทดสอบตอบใน 5.8 วินาที · เวลาประมวลผล 493 มิลลิวินาที · $0.0041 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])ค่าที่คาดหวัง {"ann": 70, "bob": 30} 2 มิลลิวินาที ✓ 2 มิลลิวินาที✓ 1 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที✓ 2 มิลลิวินาที⏱ 480 มิลลิวินาทีโค้ด
/** * @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แก้ผ่าน · ผ่าน 11 จาก 11 การทดสอบตอบใน 750 มิลลิวินาที · เวลาประมวลผล 537 มิลลิวินาที · $0.0003 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])ค่าที่คาดหวัง {"ann": 70, "bob": 30} 3 มิลลิวินาที ✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที⏱ 527 มิลลิวินาทีโค้ด
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แก้ผ่าน · ผ่าน 11 จาก 11 การทดสอบตอบใน 2.7 วินาที · เวลาประมวลผล 552 มิลลิวินาที · $0.0002 ✓ balances([["open", "ann"], ["open", "bob"], ["deposit", "ann", 100], ["transfer", "ann", "bob", 30], ["withdraw", "bob", 50]])ค่าที่คาดหวัง {"ann": 70, "bob": 30} 4 มิลลิวินาที ✓ 1 มิลลิวินาที✓ 3 มิลลิวินาที✓ 2 มิลลิวินาที✓ 2 มิลลิวินาที✓ <1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที✓ <1 มิลลิวินาที✓ 1 มิลลิวินาที⏱ 534 มิลลิวินาทีโค้ด
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); }
ค่าใช้จ่ายโทเค็น: $0.0048 · วิธีจัดการแข่งขันและการคิดคะแนน →