{"id":8552,"date":"2021-08-09T19:44:29","date_gmt":"2021-08-10T02:44:29","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8552"},"modified":"2021-08-09T19:45:17","modified_gmt":"2021-08-10T02:45:17","slug":"leetcode-1894-find-the-student-that-will-replace-the-chalk","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1894-find-the-student-that-will-replace-the-chalk\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1894. Find the Student that Will Replace the Chalk"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;students in a class numbered from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>. The teacher will give each student a problem starting with the student number&nbsp;<code>0<\/code>, then the student number&nbsp;<code>1<\/code>, and so on until the teacher reaches the student number&nbsp;<code>n - 1<\/code>. After that, the teacher will restart the process, starting with the student number&nbsp;<code>0<\/code>&nbsp;again.<\/p>\n\n\n\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>chalk<\/code>&nbsp;and an integer&nbsp;<code>k<\/code>. There are initially&nbsp;<code>k<\/code>&nbsp;pieces of chalk. When the student number&nbsp;<code>i<\/code>&nbsp;is given a problem to solve, they will use&nbsp;<code>chalk[i]<\/code>&nbsp;pieces of chalk to solve that problem. However, if the current number of chalk pieces is&nbsp;<strong>strictly less<\/strong>&nbsp;than&nbsp;<code>chalk[i]<\/code>, then the student number&nbsp;<code>i<\/code>&nbsp;will be asked to&nbsp;<strong>replace<\/strong>&nbsp;the chalk.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>index<\/strong>&nbsp;of the student that will&nbsp;<strong>replace<\/strong>&nbsp;the chalk<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> chalk = [5,1,5], k = 22\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>The students go in turns as follows:\n- Student number 0 uses 5 chalk, so k = 17.\n- Student number 1 uses 1 chalk, so k = 16.\n- Student number 2 uses 5 chalk, so k = 11.\n- Student number 0 uses 5 chalk, so k = 6.\n- Student number 1 uses 1 chalk, so k = 5.\n- Student number 2 uses 5 chalk, so k = 0.\nStudent number 0 does not have enough chalk, so they will have to replace it.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> chalk = [3,4,1,2], k = 25\n<strong>Output:<\/strong> 1\n<strong>Explanation: <\/strong>The students go in turns as follows:\n- Student number 0 uses 3 chalk so k = 22.\n- Student number 1 uses 4 chalk so k = 18.\n- Student number 2 uses 1 chalk so k = 17.\n- Student number 3 uses 2 chalk so k = 15.\n- Student number 0 uses 3 chalk so k = 12.\n- Student number 1 uses 4 chalk so k = 8.\n- Student number 2 uses 1 chalk so k = 7.\n- Student number 3 uses 2 chalk so k = 5.\n- Student number 0 uses 3 chalk so k = 2.\nStudent number 1 does not have enough chalk, so they will have to replace it.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>chalk.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= chalk[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= k &lt;= 10<sup>9<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Sum up all the students. k %= sum to skip all the middle rounds.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int chalkReplacer(vector<int>& chalk, int k) {    \n    long sum = accumulate(begin(chalk), end(chalk), 0L);\n    k %= sum;\n    for (size_t i = 0; i < chalk.size(); ++i)\n      if ((k -= chalk[i]) < 0) return i;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;students in a class numbered from&nbsp;0&nbsp;to&nbsp;n &#8211; 1. The teacher will give each student a problem starting with the student number&nbsp;0, then the student&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,158],"class_list":["post-8552","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-mod","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8552","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8552"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8552\/revisions"}],"predecessor-version":[{"id":8554,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8552\/revisions\/8554"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8552"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8552"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8552"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}