Mô tả bài toán
Viết function solution(events, threshold, cooldownMs). Đầu vào và đầu ra là giá trị JSON. Không sửa mảng hoặc đối tượng đầu vào. Không hỗ trợ thư viện ngoài, truy cập mạng hoặc hàm bất đồng bộ. events:{timestamp:nonnegative integer,ok:boolean}[] in nondecreasing time; threshold:positive integer; cooldownMs:nonnegative integer. Initially closed with zero failures. Reject while timestamp<openUntil without changing state. At/after expiry reset to closed and zero failures before accepting. Accepted success resets failures; failure increments them and reaching threshold opens until timestamp+cooldownMs. Return {accepted,state:'open'|'closed'} after each event. Zero cooldown is valid. Giới hạn đầu vào: tối đa 50.000 phần tử mỗi mảng và 1.000 ký tự mỗi chuỗi. Số phải hữu hạn; số nguyên và tổng số nguyên nằm trong phạm vi số nguyên an toàn của JavaScript. Các kiểu và quy tắc hợp lệ khác theo mô tả ở trên.
Ví dụ đầu vào và đầu ra
Ví dụ 1
Đối số của hàm
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 1,
"ok": false
},
{
"timestamp": 2,
"ok": true
},
{
"timestamp": 11,
"ok": true
}
],
2,
10
]Giá trị trả về
[
{
"accepted": true,
"state": "closed"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": false,
"state": "open"
},
{
"accepted": true,
"state": "closed"
}
]Ví dụ 2
Đối số của hàm
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 10,
"ok": false
},
{
"timestamp": 20,
"ok": true
}
],
1,
10
]Giá trị trả về
[
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "closed"
}
]Ví dụ 3
Đối số của hàm
[
[
{
"timestamp": 0,
"ok": false
},
{
"timestamp": 10,
"ok": false
},
{
"timestamp": 10,
"ok": true
}
],
1,
10
]Giá trị trả về
[
{
"accepted": true,
"state": "open"
},
{
"accepted": true,
"state": "open"
},
{
"accepted": false,
"state": "open"
}
]