leetcode刷题笔记-math

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Sengo_GWU/article/details/82372725

462. Minimum Moves to Equal Array Elements II

这题只要记住The Median Minimizes the Sum of Absolute Deviations (The L1L1 Norm)

Suppose we have a set SS of real numbers. Show that

∑s∈S|s−x|∑s∈S|s−x|

is minimal if xx is equal to the median

证明:https://math.stackexchange.com/questions/113270/the-median-minimizes-the-sum-of-absolute-deviations-the-l-1-norm

781. Rabbits in Forest

第一次解法,通过,但是写法感觉略复杂,虽然时间复杂度是O(n)

class Solution(object):
    def numRabbits(self, answers):
        """
        :type answers: List[int]
        :rtype: int
        """
        if not answers:
            return 0
        res = 0
        answers = sorted(answers)
        rest = 0
        for i in xrange(len(answers)):
            if i == 0:
                res += answers[i] + 1
                rest = answers[i]
            else:
                if answers[i] == 0:
                    res += 1
                elif answers[i] != answers[i-1]:
                    res += answers[i] + 1
                    rest = answers[i]
                elif answers[i] == answers[i-1] and rest > 0:
                    rest -= 1
                elif answers[i] == answers[i-1] and rest == 0:
                    res += answers[i] + 1
                    rest = answers[i]
                
        return res

参考了讨论里面的答案:写法很优雅速度更慢

class Solution(object):
    def numRabbits(self, answers):
        """
        :type answers: List[int]
        :rtype: int
        """
        c = collections.Counter(answers)
        return sum((c[i]+i) / (i+1) * (i+1) for i in c)

319. Bulb Switcher

思路:

class Solution(object):
    def bulbSwitch(self, n):
        return int(math.sqrt(n))

41. First Missing Positive

做的第一题Hard题,主要难点在O(n)和O(c)space。

思路:不得不说太棒了!

396. Rotate Function 

思路:

F(k) = F(k-1) + Sum - nA(-k) 

class Solution(object):
    def maxRotateFunction(self, A):
        """
        :type A: List[int]
        :rtype: int
        """
        if not A:
            return 0
        
        Sum = sum(A)
        n = len(A)
        F = [0] * n
        for i in xrange(n):
            if i == 0:
                F[i] = sum(e*i for i, e in enumerate(A))
            else:
                F[i] = F[i-1] + Sum - n*A[-i]
                
        return max(F) 

313. Super Ugly Number

class Solution(object):
    def nthSuperUglyNumber(self, n, primes):
        """
        :type n: int
        :type primes: List[int]
        :rtype: int
        """
        uglys = [1]
        points = [0] * len(primes)
        for i in xrange(n - 1):
            Min = 2**32
            for j in xrange(len(primes)):
                Min = min(Min, uglys[points[j]] * primes[j])

            for jj in xrange(len(primes)):
                if uglys[points[jj]] * primes[jj] == Min:
                    points[jj] += 1
     
            uglys.append(Min)
 
        return uglys[-1]

334. Increasing Triplet Subsequence 

这么简单粗暴的方法为啥我一开始没想出来?

class Solution(object):
    def increasingTriplet(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        c1, c2 = 2**31, 2**31
        for n in nums:
            if n <= c1:
                c1 = n
            elif n <= c2:
                c2 = n
            else:
                return True
        return False

50. Pow(x, n)

class Solution(object):
    def myPow(self, x, n):
        """
        :type x: float
        :type n: int
        :rtype: float
        """
        if n < 0:
            return 1 / self.myPow(x, -n)
        
        if n == 0:
            return 1
        elif n == 2:
            return x*x
        
        if n % 2 == 0:
            return self.myPow(self.myPow(x, n/2), 2)
        else:
            return x * self.myPow(self.myPow(x, n/2), 2)

猜你喜欢

转载自blog.csdn.net/Sengo_GWU/article/details/82372725