返回介绍

solution / 1800-1899 / 1894.Find the Student that Will Replace the Chalk / README_EN

发布于 2024-06-17 01:03:13 字数 5804 浏览 0 评论 0 收藏 0

1894. Find the Student that Will Replace the Chalk

中文文档

Description

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 pieces_.

 

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

Solutions

Solution 1: Sum and Modulo + Simulation

Since the students' answers are conducted in rounds, we can add up the chalk needed by all students to get a total $s$. Then we take the remainder of $k$ by $s$, which can tell us the remaining number of chalks after the last round.

Next, we just need to simulate the last round. Initially, the remaining number of chalks is $k$, and the student with the number $0$ starts to answer the question. When the remaining number of chalks is less than the current student needs, the current student needs to replenish the chalk, and we directly return the current student's number $i$. Otherwise, we subtract the chalk needed by the current student from the remaining chalk, and add one to the current student's number $i$ for the next simulation.

The time complexity is $O(n)$, where $n$ is the number of students. The space complexity is $O(1)$.

class Solution:
  def chalkReplacer(self, chalk: List[int], k: int) -> int:
    s = sum(chalk)
    k %= s
    for i, x in enumerate(chalk):
      if k < x:
        return i
      k -= x
class Solution {
  public int chalkReplacer(int[] chalk, int k) {
    long s = 0;
    for (int x : chalk) {
      s += x;
    }
    k %= s;
    for (int i = 0;; ++i) {
      if (k < chalk[i]) {
        return i;
      }
      k -= chalk[i];
    }
  }
}
class Solution {
public:
  int chalkReplacer(vector<int>& chalk, int k) {
    long long s = accumulate(chalk.begin(), chalk.end(), 0LL);
    k %= s;
    for (int i = 0;; ++i) {
      if (k < chalk[i]) {
        return i;
      }
      k -= chalk[i];
    }
  }
};
func chalkReplacer(chalk []int, k int) int {
  s := 0
  for _, x := range chalk {
    s += x
  }
  k %= s
  for i := 0; ; i++ {
    if k < chalk[i] {
      return i
    }
    k -= chalk[i]
  }
}
function chalkReplacer(chalk: number[], k: number): number {
  let s = 0;
  for (const x of chalk) {
    s += x;
  }
  k %= s;
  for (let i = 0; ; ++i) {
    if (k < chalk[i]) {
      return i;
    }
    k -= chalk[i];
  }
}
impl Solution {
  pub fn chalk_replacer(chalk: Vec<i32>, k: i32) -> i32 {
    let mut s: i64 = chalk
      .iter()
      .map(|&x| x as i64)
      .sum();
    let mut k = (k as i64) % s;
    for (i, &x) in chalk.iter().enumerate() {
      if k < (x as i64) {
        return i as i32;
      }
      k -= x as i64;
    }
    0
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文