Lamp switch 5353. III

There are n pieces of the lamp room, numbered from 1 to n, from left to right in a row. Initially, all the lights are shut.

At time k (k is in the range 0 to n - 1), we open Light [k] the lamp.

To turn blue color of the lamp must simultaneously satisfy the following two conditions:

Lamp is turned on.
Row before it (the left side) of all lights are in the open state.
Please go back to be able to make all the lights are turned into open blue number of times.

Input: Light = [2,1,3,5,4]
 Output: 3
 Explanation: All open timing blue lights are respectively 1, 2 and 4

1. Law look, not to 2:00, to the line 1, the maximum is two, then the count is 2, 3 to a maximum of 3, count 3, line 5 to a maximum value of 5, the count No 4

2. When the idea of ​​the game, first of all light situations hash up, when i went to i before entry and equal hash [i] you can

class Solution(object):
    def numTimesAllBlue(self, light):
        """
        :type light: List[int]
        :rtype: int
        """
        cur = 0
        res = 0
        for i in range(len(light)):
            cur=max(cur,light[i])
            if cur ==i+1:
                res+=1
        return res

class Solution(object):
    def numTimesAllBlue(self, light):
        """
        :type light: List[int]
        :rtype: int
        """
        hash_ = [0 for _ in range(len(light)+1)]
        res = []
        sum_=0
        for i in range(1,len(light)+1):
            sum_+=i
            hash_[i] = sum_
        
        sum2 = 0
        for i in range(1,len(light)+1):
            sum2+=light[i-1]
            if sum2==hash_[i]:
                res.append(i-1)
        return len(res)

 

Published 200 original articles · won praise 17 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_36328915/article/details/104748653