โจทย์: วันที่ i ทำงาน A[i] วินาที เวลาว่างอ่านหนังสือ หนังสือทั้งเล่มใช้ t วินาที หาวันที่จบหนังสือ (1 วัน = 86400 วินาที)
Input: A=[86400,86398], t=2 → Output: 2 (วันที่ 1 ทำงานเต็มวัน วันที่ 2 ว่าง 2 วินาที จบ)
ขั้นตอนแก้
cnt := 1
for each x in A:
t -= 86400 - x
if t <= 0: return cnt
cnt++
return cntตัวอย่างโค้ด
#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A, int t) {
int cnt = 1, n = A.size();
for (int i = 0; i < n; i++) {
t -= 86400 - A[i];
if (t <= 0) return cnt;
cnt++;
}
return cnt;
}
int main() {
vector<int> A = {86400, 86398};
int t = 2;
cout << solve(A,t) << endl;
}Input
{86400,86398}, 2Output
2