There are n
students in a class numbered from 0
to n - 1
. The teacher will give each student a problem starting with the student number 0
, then the student number 1
, and so on until the teacher reaches the student number n - 1
. After that, the teacher will restart the process, starting with the student number 0
again.
You are given a 0-indexed integer array chalk
and an integer k
. There are initially k
pieces of chalk. When the student number i
is given a problem to solve, they will use chalk[i]
pieces of chalk to solve that problem. However, if the current number of chalk pieces is strictly less than chalk[i]
, then the student number i
will be asked to replace the chalk.
Return the index of the student that will replace the chalk.
Example 1:
Input: chalk = [5,1,5], k = 22
Output: 0
Explanation: The students go in turns as follows:
- Student number 0 uses 5 chalk, so k = 17.
- Student number 1 uses 1 chalk, so k = 16.
- Student number 2 uses 5 chalk, so k = 11.
- Student number 0 uses 5 chalk, so k = 6.
- Student number 1 uses 1 chalk, so k = 5.
- Student number 2 uses 5 chalk, so k = 0.
Student number 0 does not have enough chalk, so they will have to replace it.
Example 2:
Input: chalk = [3,4,1,2], k = 25
Output: 1
Explanation: The students go in turns as follows:
- Student number 0 uses 3 chalk so k = 22.
- Student number 1 uses 4 chalk so k = 18.
- Student number 2 uses 1 chalk so k = 17.
- Student number 3 uses 2 chalk so k = 15.
- Student number 0 uses 3 chalk so k = 12.
- Student number 1 uses 4 chalk so k = 8.
- Student number 2 uses 1 chalk so k = 7.
- Student number 3 uses 2 chalk so k = 5.
- Student number 0 uses 3 chalk so k = 2.
Student number 1 does not have enough chalk, so they will have to replace it.
Constraints:
chalk.length == n
1 <= n <= 105
1 <= chalk[i] <= 105
1 <= k <= 109
题意:一个班级里有 n
个学生,编号为 0
到 n - 1
。每个学生会依次回答问题,编号为 0
的学生先回答,然后是编号为 1
的学生,以此类推,直到编号为 n - 1
的学生,然后老师会重复这个过程,重新从编号为 0
的学生开始回答问题。
给你一个长度为 n
且下标从 0
开始的整数数组 chalk
和一个整数 k
。一开始粉笔盒里总共有 k
支粉笔。当编号为 i
的学生回答问题时,他会消耗 chalk[i]
支粉笔。如果剩余粉笔数量 严格小于 chalk[i]
,那么学生 i
需要 补充 粉笔。请你返回需要 补充 粉笔的学生 编号 。
对 chalks[]
累计求和,由于数据范围比较大,需要使用 long long
避免溢出,再将 k
对和值取余,接着顺序遍历数组,不断从余值中减去 chalks[i]
,直到余值为负数:
class Solution {
public:
int chalkReplacer(vector<int>& chalk, int k) {
int n = chalk.size();
long long sum = 0;
for (int i = 0; i < n; ++i) sum += chalk[i];
k %= sum;
for (int i = 0; i < n; ++i) {
k -= chalk[i];
if (k < 0) return i;
}
return n;
}
};
运行效率如下:
执行用时:148 ms, 在所有 C++ 提交中击败了43.85% 的用户
内存消耗:72.6 MB, 在所有 C++ 提交中击败了63.31% 的用户
时间复杂度为: O ( c h a l k . l e n g t h × 2 ) O(chalk.length \times 2) O(chalk.length×2) ,空间复杂度为: O ( 1 ) O(1) O(1) 。
class Solution {
public:
int chalkReplacer(vector<int>& chalk, int k) {
int n = chalk.size();
vector<long long> sum(n);
sum[0] = chalk[0];
for (int i = 1; i < n; ++i) sum[i] = sum[i - 1] + chalk[i];
k %= sum[n - 1];
//找到第一个>k的前缀和值位置
return upper_bound(sum.begin(), sum.end(), k) - sum.begin();
}
};
运行效率如下:
执行用时:148 ms, 在所有 C++ 提交中击败了43.85% 的用户
内存消耗:79.2 MB, 在所有 C++ 提交中击败了10.79% 的用户
时间复杂度为: O ( c h a l k . l e n g t h + log ( c h a l k . l e n g t h ) ) O(chalk.length + \log (chalk.length)) O(chalk.length+log(chalk.length)) ,空间复杂度为: O ( c h a l k . l e n g t h ) O(chalk.length) O(chalk.length) 。此处效率优势不大。