当前位置:网站首页>Simulation volume leetcode [general] 1894 Find the student number who needs to add chalk
Simulation volume leetcode [general] 1894 Find the student number who needs to add chalk
2022-07-18 14:01:00 【Encounter simulation volume】
1894. Find the number of students who need to add chalk
There are... In one class n A student , The number is 0 To n - 1 . Each student will answer the questions in turn , The number is 0 First of all, I want to answer , And then it's numbered 1 Of the students , And so on , Until the number is n - 1 Of the students , Then the teacher will repeat the process , Renumber from 0 The students began to answer questions .
Give you a length of n And subscript from 0 The starting array of integers chalk And an integer k . In the beginning, there were a total of k A piece of chalk . When the number is i When students answer questions , He will consume chalk[i] A piece of chalk . If there's any chalk left Strictly less than chalk[i] , So students i need Add chalk .
Please return to need Add Chalk students Number .
Example 1:
Input :chalk = [5,1,5], k = 22
Output :0
explain : Students' consumption of chalk is as follows :
- The number is 0 The students who use 5 A piece of chalk , then k = 17 .
- The number is 1 The students who use 1 A piece of chalk , then k = 16 .
- The number is 2 The students who use 5 A piece of chalk , then k = 11 .
- The number is 0 The students who use 5 A piece of chalk , then k = 6 .
- The number is 1 The students who use 1 A piece of chalk , then k = 5 .
- The number is 2 The students who use 5 A piece of chalk , then k = 0 .
The number is 0 Of the students don't have enough chalk , So he needs chalk .
Example 2:
Input :chalk = [3,4,1,2], k = 25
Output :1
explain : Students' consumption of chalk is as follows :
- The number is 0 The students who use 3 A piece of chalk , then k = 22 .
- The number is 1 The students who use 4 A piece of chalk , then k = 18 .
- The number is 2 The students who use 1 A piece of chalk , then k = 17 .
- The number is 3 The students who use 2 A piece of chalk , then k = 15 .
- The number is 0 The students who use 3 A piece of chalk , then k = 12 .
- The number is 1 The students who use 4 A piece of chalk , then k = 8 .
- The number is 2 The students who use 1 A piece of chalk , then k = 7 .
- The number is 3 The students who use 2 A piece of chalk , then k = 5 .
- The number is 0 The students who use 3 A piece of chalk , then k = 2 .
The number is 1 Of the students don't have enough chalk , So he needs chalk .
Tips :
chalk.length == n
1 <= n <= 105
1 <= chalk[i] <= 105
1 <= k <= 109
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Code :
import time
from typing import List
class Solution:
def __init__(self):
pass
def chalkReplacer(self, chalk: List[int], k: int) -> int:
k = k%sum(chalk)
for i,c in enumerate(chalk):
k-=c
if k<0:return i
def test(data_test):
s = Solution()
return s.chalkReplacer(*data_test)
def test_obj(data_test):
result = [None]
obj = Solution(*data_test[1][0])
for fun, data in zip(data_test[0][1::], data_test[1][1::]):
if data:
res = obj.__getattribute__(fun)(*data)
else:
res = obj.__getattribute__(fun)()
result.append(res)
return result
if __name__ == '__main__':
datas = [
[[5,1,5],22],
[[3,4,1,2],25],
[[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1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,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],999999999],
]
for data_test in datas:
t0 = time.time()
print('-' * 50)
print('input:', data_test)
print('output:', test(data_test))
print(f'use time:{
time.time() - t0}s')
remarks :
GitHub:https://github.com/monijuan/leetcode_python
CSDN Summary : Simulation volume Leetcode Summary of questions _ Paper blog -CSDN Blog
You can add QQ Group communication :1092754609
leetcode_python.utils See the description on the summary page for details
First brush questions , Then generated by script blog, If there is any mistake, please leave a message , I see it will be revised ! thank you !
边栏推荐
- 一条龙蛇形排序(我第一道笔试题)
- The principle and implementation of PageRank
- T100excel导入写法模板
- [target tracking] image inter frame difference target detection based on background subtraction and MATLAB simulation
- 三极管的基础知识(下)②
- 盒子模型、文档流、定位、布局和响应式设计
- XML file delete comments
- OpenCv:将视频转化为连续的图像帧
- Collection和Collections区别
- 2.4_ 9 MySQL by separator, row to column
猜你喜欢

Ora-19625 exception handling record

腾讯员工发帖找对象,表示偏爱程序员!评论火了......丨黑马头条

Sword finger offer19 regular expression matching string dynamic programming

Matlab manipulator modeling kinematics simulation + trajectory planning
![[phase locked loop] design and Simulation of all digital phase locked loop based on MATLAB](/img/3c/9fe4aec90506cef4bf0a639366263d.png)
[phase locked loop] design and Simulation of all digital phase locked loop based on MATLAB

盒子模型、文档流、定位、布局和响应式设计

Idea merges dev branch code into master and so on

The digital transformation forum for small and medium-sized enterprises in Shandong Province was successfully held, and Jiuzhou cloud empowers small and medium-sized enterprises to upgrade their digit

University epidemic prevention and control system based on jsp+servlet

Dwelling apartment rental system based on jsp+servlet
随机推荐
解道--探索术
二叉树的深度遍历
Rectangular sort....
联盛德W801-如何提高采集多路ADC效率
Sword finger offer19 regular expression matching string dynamic programming
AB PLC learning notes
preg_ Replace Code Execution Vulnerability [bjdctf2020]zjctf, but so
How does golang calculate constellations and zodiac signs based on birthdays
T100excel导入写法模板
Cultural tourism Night Tour: new opportunities for urban economic recovery and growth
二叉搜索树BST
高数 | 积分中值定理的开闭区间、积分第一中值定理及其推广
【Jmeter】Jmeter 设置默认语言为中文
Base64 encoding and decoding principle and C language implementation
Opencv:05 filter
[FPGA tutorial case 25] Tan (x) calculation through NCO core and divider
ORA-01033
Install MySQL 5.7.23 in Linux
总结与归纳:深度神经网络中的数据融合方法
kettle版本8.2的中文乱码问题