力扣 1700. 无法吃午餐的学生数量

模拟1

1
2
3
4
5
6
7
8
9
10
11
def countStudents(students, sandwiches):
s1 = sum(students)
s0 = len(students) - s1
for x in sandwiches:
if x == 1 and s1:
s1 -= 1
elif x == 0 and s0:
s0 -= 1
else:
break
return s0 + s1

模拟2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
def countStudents(students, sandwiches):
cur, flag = len(sandwiches), True
while flag:
for i in range(cur):
if sandwiches[0] == students[0]:
sandwiches.pop(0)
students.pop(0)
else:
students.append(students.pop(0))
if cur == len(sandwiches):
flag = False
else:
cur = len(sandwiches)
return cur
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信