Leetcode_Divide and Conquer

Troika of Google big data processing: GFS, Bigtable and MapReduce

The main idea of ​​MapReduce:

Divide the original problem into several sub-problems recursively, until the sub-problems meet the boundary conditions, stop the recursion. Break down the sub-problems one by one, combine the solved sub-problems, and finally the algorithm will merge layer by layer to get the answer to the original problem

The steps of the divide and conquer algorithm:

  • Divide: Recursively decompose the problem into sub-problems
  • Governance: break down these smaller sub-problems one by one
  • Combine: Merge the solved sub-problems layer by layer, and finally get the solution of the original problem

Situations where divide and conquer applies

  • The computational complexity of the original problem increases with the size of the problem
  • The original problem can be broken down into smaller sub-problems
  • The structure and nature of the sub-problems are the same as the original problem, and they are independent of each other. The sub-problems do not contain common sub-sub-problems
  • The solutions of the subproblems decomposed from the original problem can be combined into the solution of the problem

algorithm

169. Most Elements

python code:

class Solution:
    def majorityElement(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        left = self.majorityElement(nums[:len(nums)//2])
        right = self.majorityElement(nums[len(nums)//2:])

        if left == right:
            return right
        elif nums.count(right) > nums.count(left):
            return right
        else:
            return left

53. Maximum Subsequence Sum

python code

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        if len(nums) == 1:
            return nums[0]
        left = self.maxSubArray(nums[:len(nums)//2])
        right = self.maxSubArray(nums[len(nums)//2:])
        
        max_l = nums[len(nums)//2]
        tmp = 0
        for i in range(len(nums)//2,len(nums)):
            tmp += nums[i]
            max_l = max(tmp,max_l)
        max_r = nums[len(nums)//2-1]
        tmp_1 = 0
        for i in range(len(nums)//2-1,-1,-1):
            tmp_1 += nums[i]
            max_r = max(tmp_1,max_r)
        return max(left,right,max_l+max_r)

50. Pow(x, n)

python code

class Solution:
    def myPow(self, x: float, n: int) -> float:
        if n < 0 :
            x = 1/x
            n = -n
        if n == 0:
            return 1
        if n%2 == 1:
            p = x*self.myPow(x, n-1)
            return p
        return self.myPow(x*x,n/2)

References

One of the five commonly used algorithms: divide and conquer algorithm

Python divide and conquer algorithm you don't know

Guess you like

Origin blog.csdn.net/BigCabbageFy/article/details/108081214