python 1 line Viper Attacking ;)


  • 0
    Z

    The name and question is kind of distraction, but I like it.

    using reduce:

    def findPoisonedDuration(self, times, dura):
            return reduce(lambda a, i: a + min(times[i] - times[i-1], dura),
                          xrange(1, len(times)),
                          dura if times else 0)
    

    using sum (faster than reduce):

    def findPoisonedDuration(self, times, dura):
        return sum(min(times[i] - times[i-1], dura) for i in xrange(1, len(times))) + dura if times else 0
    

    equivalent (somehow fastest):

    def findPoisonedDuration(self, times, dura):
        ans = dura if times else 0
        for i in xrange(1, len(times)):
            ans += min(times[i] - times[i-1], dura)
        return ans

  • 0
    Z

    And I just realized this could also be "Viper Attacked".


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.