Divide and Conquer

[1]:
import IPython; IPython.display.HTML('''<script>code_show=true; function code_toggle() { if (code_show){ $('div.nbinput').show(); } else { $('div.nbinput').hide(); } code_show = !code_show} $( document ).ready(code_toggle);</script><form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
[1]:

169. Majority Element

Easy

Given an array nums of size n, return the majority element.

The majority element is the element that appears more than floor(n/2) times. You may assume that the majority element always exists in the array.

Example 2:

Input: nums = [2,2,1,1,1,2,2]
Output: 2

Constraints:

n == nums.length
1 <= n <= 5 * 10^4
-10^9 <= nums[i] <= 10^9

Follow-up: Could you solve the problem in linear time and in O(1) space?

[3]:
# The Boyer-Moore Voting Algorithm: Because this majority element occurs more than n/2 (floor value) times, even if other elements will 'vote against it', it will win

def majorityElement(nums):
    res = count = 0
    for n in nums:
        if count==0:
            res = n
        count += (1 if n==res else -1)
    return res

majorityElement([2, 2, 1, 1, 1, 2, 2])
[3]:
2

153. Find Minimum in Rotated Sorted Array

Medium

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.

Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n-2]].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

Example 1:

Input: nums = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.

Constraints:

n == nums.length
1 <= n <= 5000
-5000 <= nums[i] <= 5000
All the integers of nums are unique.
nums is sorted and rotated between 1 and n times.
[4]:
def findMin(nums):
    l = 0
    r = len(nums) - 1
    while l < r-1:
        i = (l + r)//2
        if nums[i] > nums[r]:
            l = i
        else:
            r = i

    return min(nums[l], nums[r])

findMin([3, 4, 5, 1, 2])
[4]:
1

912. Sort an Array

Medium

Given an array of integers nums, sort the array in ascending order and return it.

You must solve the problem without using any built-in functions in O(nlog(n)) time complexity and with the smallest space complexity possible.

Example 1:

Input: nums = [5,2,3,1]
Output: [1,2,3,5]
Explanation: After sorting the array, the positions of some numbers are not changed (for example, 2 and 3), while the positions of other numbers are changed (for example, 1 and 5).

Constraints:

1 <= nums.length <= 5 * 10^4
-5 * 10^4 <= nums[i] <= 5 * 10^4
[14]:
def sortArray(nums):
    n = len(nums)
    if n in [0, 1]:
        return nums
    else:
        left = sortArray(nums[:n//2])
        right = sortArray(nums[n//2:])

        i = j = 0
        merged = []
        while (i != len(left)) and (j != len(right)):
            if left[i] < right[j]:
                merged.append(left[i])
                i += 1
            else:
                merged.append(right[j])
                j += 1

        if i==len(left):
            merged += right[j:]
        else:
            merged += left[i:]

        return merged

sortArray([5, 2, 3, 1])
[14]:
[1, 2, 3, 5]