To prove safety offer - child's play (the rest of the circle number)

Title Description

Children's Day each year, cow-off will prepare some small gifts to visit the orphanage children, this year is also true. As an experienced veteran off HF cattle, naturally we prepared some games. Among them, there is a game like this: First, let the children surrounded by a large circle. He then randomly assigned a number m, so that number of children 0 gettin number. Cried every time m-1 the children sing a song to be out of the line, then any of the gift box in choosing a gift, and not return to the circle, starting with his next child, continue 0 ... m -1 number of packets .... go on .... until the last remaining child, you can not perform, and get the cattle off the rare "Detective Conan" Collector's Edition (limited places oh !! ^ _ ^). You want to try next, which the children will get the gifts it? (NOTE: The number of children is from 0 to n-1)

If there are no children, please return -1

Thinking

Originally still entangled there is no way to calculate directly, no later discovered. Every element can only delete the specified

class Solution:
    def LastRemaining_Solution(self, n, m):
        if n == 0:
            return -1
        nums = [i for i in range(n)]
        delIndex = 0
        while len(nums)>1:
            delIndex = (delIndex+(m-1))%len(nums)
            del nums[delIndex]
        return nums[0]

 

Published 82 original articles · won praise 2 · Views 4348

Guess you like

Origin blog.csdn.net/qq_22498427/article/details/104977086