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 需要 补充 粉笔。
请你返回需要 补充 粉笔的学生 编号 。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题的思路是前缀和 + 二分法。我们利用一个数组记录前缀和这样就可以知道一轮下来,当所有学生都回答完一轮题目的时候,粉笔是否会被用完。如果第一轮还未走完就会被用完,我们就可以返回那个需要补充粉笔的学生的下标 i 了。如果第一轮没有用完,我们再利用二分法在前缀和数组里面找哪一个学生是需要补充粉笔的学生。记录前缀和的时候注意数据类型是 long,否则结果会不对。
时间O(n)
空间O(n)
Java实现
1 class Solution { 2 public int chalkReplacer(int[] chalk, int k) { 3 int len = chalk.length; 4 long[] preSum = new long[len]; 5 preSum[0] = chalk[0]; 6 for (int i = 1; i < len; i++) { 7 preSum[i] = preSum[i - 1] + chalk[i]; 8 } 9 10 if (preSum[len - 1] <= k) { 11 k %= preSum[len - 1]; 12 } 13 int start = 0; 14 int end = len - 1; 15 while (start <= end) { 16 int mid = start + (end - start) / 2; 17 if (k == preSum[mid]) { 18 return mid + 1; 19 } else if (k < preSum[mid]) { 20 end = mid - 1; 21 } else { 22 start = mid + 1; 23 } 24 } 25 return start; 26 } 27 }
记录完前缀和之后,我们可以不需要用二分法找那个目标学生,因为从时间复杂度上看,用不用二分其实是一样的,因为时间复杂度主要还是用在前缀和。这里我们也可以用线性时间找到那个需要补充粉笔的学生的下标。
时间O(n)
空间O(1)
Java实现
1 class Solution { 2 public int chalkReplacer(int[] chalk, int k) { 3 int sum = 0; 4 for (int i = 0; i < chalk.length; i++) { 5 sum += chalk[i]; 6 k -= chalk[i]; 7 if (k < 0) { 8 return i; 9 } 10 } 11 12 k %= sum; 13 for (int i = 0; i < chalk.length; i++) { 14 k -= chalk[i]; 15 if (k < 0) { 16 return i; 17 } 18 } 19 return -1; 20 } 21 }