8、lintcode学习总结


# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 11:28:01 2018

@author: zhangchaoyu
"""
  
"""
1. 
A + B Problem
Write a function that add two numbers A and B.

Example
Given a=1 and b=2 return 3.

Challenge
Of course you can just return a + b to get accepted. But Can you challenge 
not do it like that?(You should not use + or any arithmetic operators.)
"""

def aplusb(self, a, b):
    # write your code here
    return a+b


"""
2. Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial.

Example
11! = 39916800, so the out should be 2

Challenge
O(log N) time
"""
def trailingZeros(self, n):
    # write your code here, try to do it without arithmetic operators.
    num = n
    zeros = 0
    while(num//5>0):
        zeros += num//5
        num /= 5
    
    return int(zeros)


"""
3. Digit Counts
Count the number of k's between 0 and n. k can be 0 - 9.

Example
if n = 12, k = 1 in

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
we have FIVE 1's (1, 10, 11, 12)
"""

def digitCounts(self, k, n):
    # write your code here
    sum_k = 0
    
    for i in range(n+1):
        if(k == 0 and i == 0):
            sum_k += 1
        
        num = i
        while(num>0):
            if(num%10 == k):
                sum_k += 1
            num = num//10
            
    return sum_k


"""
4. Ugly Number II
Ugly number is a number that only have factors 2, 3 and 5.

Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...

Example
If n=9, return 10.

Challenge
O(n log n) or O(n) time.
"""

def nthUglyNumber(self, n):
    # write your code here
    L = [1]
    
    fac_2 = 0
    fac_3 = 0
    fac_5 = 0
    
    while(len(L)<n):
        num_2 = L[fac_2]*2
        num_3 = L[fac_3]*3
        num_5 = L[fac_5]*5
        
        min_num = min(num_2,num_3,num_5)
        L.append(min_num)
        
        if(min_num == num_2):
            fac_2 += 1
        if(min_num == num_3):
            fac_3 += 1
        if(min_num == num_5):
            fac_5 += 1
    
    return L[-1]


"""
5. Kth Largest Element
Find K-th largest element in an array.

Example
In array [9,3,2,4,8], the 3rd largest element is 4.

In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.

Challenge
O(n) time, O(1) extra memory.
"""
def kthLargestElement(self, k, A):
    return sorted(A, reverse=True)[k - 1]


"""
6. Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array.

Example
A=[1,2,3,4]

B=[2,4,5,6]

return [1,2,2,3,4,4,5,6]

Challenge
How can you optimize your algorithm if one array is very large and the other is very small?
"""

def mergeSortedArray(self, A, B):
        # write your code here
        C = []
        i = 0
        j = 0 
        while(i<len(A) and j<len(B)):
            if(A[i]<=B[j]):
                C.append(A[i])
                i += 1
            else:
                C.append(B[j])
                j += 1
        
        while(i<len(A)):
            C.append(A[i])
            i += 1
        
        while(j<len(B)):
            C.append(B[j])
            j += 1
            
        return C

"""
7. Serialize and Deserialize Binary Tree
Design an algorithm and write code to serialize and deserialize a binary tree.
 Writing the tree to a file is called 'serialization' and reading back from the file 
 to reconstruct the exact same binary tree is 'deserialization'.

Example
An example of testdata: Binary tree {3,9,20,#,#,15,7}, denote the following structure:

  3
 / \
9  20
  /  \
 15   7
Our data serialization use bfs traversal. This is just for when you got wrong answer and want to debug the input.

You can use other method to do serializaiton and deserialization.
"""


def serialize(self, root):
        
    vals = []
    def preOrder(root):
        if not root:
            vals.append('#')
        else:
            vals.append(str(root.val))
            preOrder(root.left)
            preOrder(root.right)
    preOrder(root)
    return ' '.join(vals)

def deserialize(self, data):
    # write your code here
    vals = collections.deque(val for val in data.split())
    def build():
        if vals:
            val = vals.popleft()
            if val == '#':
                return None
            root = TreeNode(int(val))
            root.left = build()
            root.right = build()
            return root
    return build()

"""
8. Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right)

Example
Given "abcdefg".

offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"
Challenge
Rotate in-place with O(1) extra memory.
"""

def rotateString(self, str, offset):
    # write your code here
    left = str[len(str)-offset:]
    right = str[:len(str)-offset]
    str = left+right
    return str

"""
9. Fizz Buzz
Given number n. Print number from 1 to n. But:

when number is divided by 3, print "fizz".
when number is divided by 5, print "buzz".
when number is divided by both 3 and 5, print "fizz buzz".
Example
If n = 15, you should return:

[
  "1", "2", "fizz",
  "4", "buzz", "fizz",
  "7", "8", "fizz",
  "buzz", "11", "fizz",
  "13", "14", "fizz buzz"
]
Challenge
Can you do it with only one if statement?
"""
def fizzBuzz(self, n):
    # write your code here
    result = []
    for i in range(1,n+1):
        if(i%3==0 and i%5==0):
            result.append("fizz buzz")
        elif(i%3==0):
            result.append("fizz")
        elif(i%5==0):
            result.append("buzz")
        else:
            result.append(str(i))
    return result

"""
11. Search Range in Binary Search Tree
Given a binary search tree and a range [k1, k2], return all elements in the given range.

Example
If k1 = 10 and k2 = 22, then your function should return [12, 20, 22].

    20
   /  \
  8   22
 / \
4   12
"""

def searchRange(self, root, k1, k2):
    # write your code here
    res_list = []
    stack = []
    while root or len(stack):
        while root:
            stack.append(root)
            root = root.left
        if len(stack):
            root = stack[-1]
            stack.pop(-1)
            if root.val >= k1 and root.val <= k2:
                res_list.append(root.val)
            root = root.right
    return res_list

"""
12. Min Stack
Implement a stack with min() function, which will return the smallest number in the stack.

It should support push, pop and min operation all in O(1) cost.

Example
push(1)
pop()   // return 1
push(2)
push(3)
min()   // return 2
push(1)
min()   // return 1
"""

class MinStack:
    
    def __init__(self):
        # do intialization if necessary
        self.stack1 = []
        self.stack2 = []

    """
    @param: number: An integer
    @return: nothing
    """
    def push(self, number):
        # write your code here
        self.stack1.append(number)
        if len(self.stack2) == 0 or number <= self.stack2[-1]:
            self.stack2.append(number)
        

    """
    @return: An integer
    """
    def pop(self):
        # write your code here
        top = self.stack1[-1]
        self.stack1.pop()
        if top == self.stack2[-1]:
            self.stack2.pop()
        return top

    """
    @return: An integer
    """
    def min(self):
        # write your code here
        return self.stack2[-1]

"""
13. Implement strStr()
For a given source string and a target string, you should output the first index(from 0) of target string in source string.

If target does not exist in source, just return -1.

Example
If source = "source" and target = "target", return -1.

If source = "abcdabcdefg" and target = "bcd", return 1.

Challenge
O(n2) is acceptable. Can you implement an O(n) algorithm? (hint: KMP)
"""

def strStr(self, source, target):
        # write your code here
        
        if target == "":
            return 0
        
        if source == None or target == None:
            return -1
        
        
        for i in range(len(source)-len(target)+1):
            if source[i] == target[0]:
                flag = 1
                for j in range(1,len(target)):
                    if source[i+j] != target[j]:
                        flag = 0
                        break
                if flag == 1:
                    return i
        return -1

"""
14. First Position of Target
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.

If the target number does not exist in the array, return -1.

Example
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.

Challenge
If the count of numbers is bigger than 2^32, can your code work properly?
"""

def binarySearch(self, nums, target):
        # write your code here
        if target < nums[0] or target > nums[-1]:
            return -1
        
        if target == nums[0]:
            return 0
        
        if target == nums[-1]:
            return len(nums)-1
        
        start = 0
        end = len(nums)-1
        mid = (start+end)//2
        
        while nums[mid] != target and start<(end-1):
            if nums[mid] > target:
                end = mid
                mid = (start+end)//2
            else:
                start = mid
                mid = (start+end)//2
            
        if nums[mid] != target:
            return -1
        
        while nums[mid] == target:
            mid -= 1
        
        return mid+1

"""
15. Permutations
Given a list of numbers, return all possible permutations.

Example
For nums = [1,2,3], the permutations are:

[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
Challenge
Do it without recursion.
"""

def permuteUnique(self, nums):
    # write your code here
    length = len(nums)
    if length == 0: return [[]]
    if length == 1: return [nums]
    nums.sort()
    res = []
    previousNum = None
    for i in range(length):
        if nums[i] == previousNum: continue
        previousNum = nums[i]
        for j in self.permuteUnique(nums[:i] + nums[i+1:]):
            res.append([nums[i]] + j)
    
    return res

"""
16. Permutations II
Given a list of numbers with duplicate number in it. Find all unique permutations.

Example
For numbers [1,2,2] the unique permutations are:

[
  [1,2,2],
  [2,1,2],
  [2,2,1]
]
Challenge
Using recursion to do it is acceptable. If you can do it without recursion, that would be great!
"""
 def permuteUnique(self, nums):
    # write your code here
    length = len(nums)
    if length == 0: return [[]]
    if length == 1: return [nums]
    nums.sort()
    res = []
    previousNum = None
    for i in range(length):
        if nums[i] == previousNum: continue
        previousNum = nums[i]
        for j in self.permuteUnique(nums[:i] + nums[i+1:]):
            res.append([nums[i]] + j)
    
    return res

"""
17. Subsets
Given a set of distinct integers, return all possible subsets.

Example
If S = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]
Challenge
Can you do it in both recursively and iteratively?
"""
def subsets(self, nums):
    # write your code here
    nums.sort()
    length = len(nums)
    if length == 0: return [[]]
    res = []
    
    for subset in self.subsets(nums[:-1]):
        res.append(subset)
        res.append(subset + [nums[-1]])
    
    return res

"""
18. Subsets II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).

Example
Input: [1,2,2]
Output:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
Challenge
Can you do it in both recursively and iteratively?
"""

def subsetsWithDup(self, nums):
    # write your code here
    nums.sort()
    length = len(nums)
    if length == 0: return [[]]
    res = []
    
    num_same = 1
    
    while num_same<length:
        if nums[-num_same-1] == nums[-1]:
            num_same += 1
        else:
            break
    
    for subset in self.subsetsWithDup(nums[:-num_same]):
        res.append(subset)
        for i in range(1,num_same+1):
            res.append(subset + [nums[-1]]*i)
    
    return res

"""
20. Dices Sum
Throw n dices, the sum of the dices' faces is S. Given n, find the all possible value of S along with its probability.

Example
Given n = 1, return [ [1, 0.17], [2, 0.17], [3, 0.17], [4, 0.17], [5, 0.17], [6, 0.17]].
"""

def dicesSum(self, n):
    # Write your code here
    if n==1:
        #return [[1,0.17], [2,0.17], [3,0.17], [4,0.17], [5,0.17], [6,0.17]]
        return [[1,1/6], [2,1/6], [3,1/6], [4,1/6], [5,1/6], [6,1/6]]
    
    length = 5*n+1
    
    #a代表n-1个骰子形成的概率分布,b代表1个骰子形成的概率分布
    a = self.dicesSum(n-1)
    
    b = [[1,1/6], [2,1/6], [3,1/6], [4,1/6], [5,1/6], [6,1/6]]
    
    result = []
    
    #联合算出n个骰子的概率分布,n个骰子最少为n,最多为6n,i代表n个骰子总和
    for i in range(n,n+length):
        p = 0
        #j代表新骰子的数
        for j in range(1,7):
            if i-j >= n-1 and i-j<= 6*(n-1):
                p += a[i-j-n+1][1]*b[j-1][1]
        result.append([i,p])
    
    return result  


"""
22. Flatten List
Given a list, each element in the list can be a list or integer. flatten it into a simply list with integers.

Example
Given [1,2,[1,2]], return [1,2,1,2].

Given [4,[3,[2,[1]]]], return [4,3,2,1].

Challenge
Do it in non-recursive.
"""
def flatten(self, nestedList):
    # Write your code here
    if nestedList is None:
        return []
    
    if type(nestedList) == int:
        return [nestedList]
    
    result = []
    
    for i in range(len(nestedList)):
        if type(nestedList[i]) == int:
            result.append(nestedList[i])
        else:
            result += self.flatten(nestedList[i])
    
    return result

"""
24. LFU Cache
LFU (Least Frequently Used) is a famous cache eviction algorithm.

For a cache with capacity k, if the cache is full and need to evict a key in it, the key with the lease frequently used will be kicked out.

Implement set and get method for LFU cache.

Example
Given capacity=3

set(2,2)
set(1,1)
get(2)
>> 2
get(1)
>> 1
get(2)
>> 2
set(3,3)
set(4,4)
get(3)
>> -1
get(2)
>> 2
get(1)
>> 1
get(4)
>> 4
"""
import sys
import time
class LFUCache:
    """
    @param: capacity: An integer
    """
    """
    D中value是三维的,0为值,1为最新使用时间,2为使用次数
    """
    
    
    def __init__(self, capacity):
        # do intialization if necessary
        self.D = {}
        self.C = capacity
    """
    @param: key: An integer
    @param: value: An integer
    @return: nothing
    """
    def set(self, key, value):
        # write your code here"
        
        v = self.D.get(key)
        
        if v != None:
            v[0] = value
            v[1] = time.time()
            v[2] += 1
        else:
            if len(self.D) < self.C:
                self.D[key] = [value,time.time(),0]
            else:
                """此时容量已满,需找到最不常使用且最久没用的删了替换"""
                time_last = time.time()
                times_used = sys.maxsize
                for k1,v1 in self.D.items():
                    if self.D[k1][2]<times_used:
                        k = k1
                        time_last = self.D[k1][1]
                        times_used = self.D[k1][2]
                    elif self.D[k1][2] == times_used and self.D[k1][1]<time_last:
                        k = k1
                        time_last = self.D[k1][1]
                self.D.pop(k)
                self.D[key] = [value,time.time(),0]
            
    """
    @param: key: An integer
    @return: An integer
    """
    def get(self, key):
        # write your code here
        v = self.D.get(key)
        if v == None:
            return -1
        else:
            v[1] = time.time()
            v[2] += 1
            return v[0]

"""
28. Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix.

This matrix has the following properties:

Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
Example
Consider the following matrix:

[
    [1, 3, 5, 7],
    [10, 11, 16, 20],
    [23, 30, 34, 50]
]
Given target = 3, return true.

Challenge
O(log(n) + log(m)) time
"""

def searchMatrix(self, matrix, target):
        # write your code here
        
        flag = 0
        i=0
        while(i<len(matrix)):
            if target>=matrix[i][0] and target<=matrix[i][-1]:
                flag = 1
                break
            i += 1
        
        if flag == 0:
            return False
        
        for j in range(len(matrix[i])):
            if target == matrix[i][j]:
                return True
        
        return False
    
"""
29. Interleaving String
Given three strings: s1, s2, s3, determine whether s3 is formed by the interleaving of s1 and s2.

Example
For s1 = "aabcc", s2 = "dbbca"

When s3 = "aadbbcbcac", return true.
When s3 = "aadbbbaccc", return false.
Challenge
O(n2) time or better
"""

def isInterleave(self, s1, s2, s3):
    # write your code here
    if len(s1)+len(s2)!=len(s3): return False
    dp=[[False for i in range(len(s2)+1)] for j in range(len(s1)+1)]
    dp[0][0]=True
    for i in range(1,len(s1)+1):
        dp[i][0] = dp[i-1][0] and s3[i-1]==s1[i-1]
    for i in range(1,len(s2)+1):
        dp[0][i] = dp[0][i-1] and s3[i-1]==s2[i-1]
    for i in range(1,len(s1)+1):
        for j in range(1,len(s2)+1):
            dp[i][j] = (dp[i-1][j] and s1[i-1]==s3[i+j-1]) or (dp[i][j-1] and s2[j-1]==s3[i+j-1])
    return dp[len(s1)][len(s2)]

"""
30. Insert Interval
Given a non-overlapping interval list which is sorted by start point.

Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary).

Example
Insert (2, 5) into [(1,2), (5,9)], we get [(1,9)].

Insert (3, 4) into [(1,2), (5,9)], we get [(1,2), (3,4), (5,9)].
"""


class Interval(object):
    def __init__(self, start, end):
        self.start = start
        self.end = end

def insert(self, intervals, newInterval):
    # write your code here
    intervals.append(newInterval)
    intervals.sort(key = lambda x:x.start)
    length=len(intervals)
    res=[]
    for i in range(length):
        if res==[]:
            res.append(intervals[i])
        else:
            size=len(res)
            if res[size-1].start<=intervals[i].start<=res[size-1].end:
                res[size-1].end=max(intervals[i].end, res[size-1].end)
            else:
                res.append(intervals[i])
    return res

"""
31. Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:

All elements < k are moved to the left
All elements >= k are moved to the right
Return the partitioning index, i.e the first index i nums[i] >= k.

Example
If nums = [3,2,2,1] and k=2, a valid answer is 1.

Challenge
Can you partition the array in-place and in O(n)?
"""

def partitionArray(self, nums, k):
    # write your code here
    if len(nums)==0 or min(nums)>k:
        return 0
    
    if max(nums)<k:
        return len(nums)
    
    i=0
    j=len(nums)-1
    
    while i<j:
        while nums[i]<k:
            i+=1
        while nums[j]>=k and i<j:
            j-=1
        if i<j:
            temp = nums[i]
            nums[i] = nums[j]
            nums[j] = temp
    
    return i

"""
32. Minimum Window Substring
Given a string source and a string target, find the minimum window in source which will contain all the characters in target.

Example
For source = "ADOBECODEBANC", target = "ABC", the minimum window is "BANC"

Challenge
Can you do it in time complexity O(n) ?
"""

def minWindow(self, source , target):
        
    if source == "" or target == "":
        return ""
    
    flag = 0
    
    D = {}

    for k in target:
        if k in D:
            D[k] += 1
        else:
            D[k] = 1
    
    len_left = len(target)
    start = 0
    len_min = len(source)
    len_min_start = 0
    len_min_end = len(source)-1
    
    for end in range(len(source)):
        if source[end] in D:
            D[source[end]] -= 1
            if D[source[end]] >= 0:
                len_left -= 1
        if len_left == 0:
            flag = 1
            while True:
                if source[start] in D:
                    if D[source[start]]<0:
                        D[source[start]] += 1
                        start += 1
                        continue
                    else:
                        D[source[start]] += 1
                        len_left += 1
                        if end-start+1<len_min:
                            len_min = end-start+1
                            len_min_start = start
                            len_min_end = end
                        start += 1
                        break
                else:
                    start += 1
    
    if flag == 0:
        return ""
    
    if len_min_end < len(source)-1:
        return source[len_min_start:len_min_end+1]
    else:
        return source[len_min_start:]
                    
"""
33. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to the n-queens puzzle.

Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.

Example
There exist two distinct solutions to the 4-queens puzzle:

[
  // Solution 1
  [".Q..",
   "...Q",
   "Q...",
   "..Q."
  ],
  // Solution 2
  ["..Q.",
   "Q...",
   "...Q",
   ".Q.."
  ]
]
Challenge
Can you do it without recursion?
"""
def solveNQueens(n):
    # write your code here
    def check(k,j,board):
        for i in range(k):
            if board[i]==j or abs(k-i)==abs(board[i]-j):
                return False
        return True
    def dfs(depth,board,valuelist,solution):
        #for i in range(len(board)):
        if depth==len(board):
            solution.append(valuelist)
        for row in range(len(board)):
            if check(depth,row,board):
                s='.'*len(board)
                board[depth]=row
                dfs(depth+1,board,valuelist+[s[:row]+'Q'+s[row+1:]],solution)
    board=[-1 for i in range(n)]
    solution=[]
    dfs(0,board,[],solution)
    return solution

"""
34. N-Queens II
Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

Example
For n=4, there are 2 distinct solutions.
"""
def totalNQueens(self, n):
    # write your code here
    def check(k,j,board):
        for i in range(k):
            if board[i]==j or abs(k-i)==abs(board[i]-j):
                return False
        return True
    def dfs(depth,board,valuelist,solution):
        #for i in range(len(board)):
        if depth==len(board):
            solution.append(valuelist)
        for row in range(len(board)):
            if check(depth,row,board):
                s='.'*len(board)
                board[depth]=row
                dfs(depth+1,board,valuelist+[s[:row]+'Q'+s[row+1:]],solution)
    board=[-1 for i in range(n)]
    solution=[]
    dfs(0,board,[],solution)
    return len(solution)

"""
35. Reverse Linked List
Reverse a linked list.

Example
For linked list 1->2->3, the reversed linked list is 3->2->1

Challenge
Reverse it in-place and in one-pass
"""

"""
Definition of ListNode

class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    """
    @param head: n
    @return: The new head of reversed linked list.
    """
    def reverse(self, head):
        # write your code here
        p = head
        newList = []
        while p:
            newList.insert(0, p.val)
            p = p.next

        p = head
        for v in newList:
            p.val = v
            p = p.next
        return head

"""
36. Reverse Linked List II
Reverse a linked list from position m to n.

Example
Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.

Challenge
Reverse it in-place and in one-pass
"""
"""
Definition of ListNode
class ListNode(object):
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    """
    @param head: ListNode head is the head of the linked list 
    @param m: An integer
    @param n: An integer
    @return: The head of the reversed ListNode
    """
    def reverseBetween(self, head, m, n):
        # write your code here
        L = []
        p = head
        idex = 1
        while idex != m:
            p = p.next
            idex += 1
        
        while idex != n+1:
            L.append(p.val)
            p = p.next
            idex += 1
        
        p = head
        idex = 1
        while idex != m:
            p = p.next
            idex += 1
        
        while idex != n+1:
            p.val = L[len(L)-1-idex+m]
            p = p.next
            idex += 1
        
        
        return head

"""
37. Reverse 3-digit Integer
Reverse a 3-digit integer.

Example
Reverse 123 you will get 321.

Reverse 900 you will get 9.
"""
def reverseInteger(self, number):
        # write your code here
        n1 = (int)(number/100)
        n2 = (int)(number%100/10)
        n3 = number%10
        
        return n3*100+n2*10+n1

"""
38. Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix, return the occurrence of it.

This matrix has the following properties:

Integers in each row are sorted from left to right.
Integers in each column are sorted from up to bottom.
No duplicate integers in each row or column.
Example
Consider the following matrix:

[
  [1, 3, 5, 7],
  [2, 4, 7, 8],
  [3, 5, 9, 10]
]
Given target = 3, return 2.

Challenge
O(m+n) time and O(1) extra space
"""
def searchMatrix(self, matrix, target):
        # write your code here
        if len(matrix) == 0:
            return 0
            
        m = len(matrix)
        n = len(matrix[0])
        sum_count = 0
        
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == target:
                    sum_count += 1
                elif matrix[i][j] > target:
                    break
        return sum_count

"""
39. Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place.
Example
[4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]
Challenge
In-place, O(1) extra space and O(n) time.
"""
def recoverRotatedSortedArray(self, nums):
        # write your code here
        state = nums[0]
        idex = 0
        while(idex<len(nums)):
            if(state>nums[idex]):
                break
            idex += 1
        
        if(idex<len(nums)):
            for i in range((int)(idex/2)):
                temp = nums[i]
                nums[i] = nums[idex-1-i]
                nums[idex-1-i] = temp
            for i in range(idex,(int)((len(nums)-idex)/2)+idex):
                temp = nums[i]
                nums[i] = nums[len(nums)-1-i+idex]
                nums[len(nums)-1-i+idex] = temp
            for i in range((int)(len(nums)/2)):
                temp = nums[i]
                nums[i] = nums[len(nums)-1-i]
                nums[len(nums)-1-i] = temp

"""
40. Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions.
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element.
Example
push(1)
pop()     // return 1
push(2)
push(3)
top()     // return 2
pop()     // return 2
Challenge
implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.
"""

class MyQueue:
    
    def __init__(self):
        # do intialization if necessary
        self.stack = []
    """
    @param: element: An integer
    @return: nothing
    """
    def push(self, element):
        # write your code here
        self.stack.append(element)
    """
    @return: An integer
    """
    def pop(self):
        # write your code here
        a = self.stack[0]
        del self.stack[0]
        return a
    """
    @return: An integer
    """
    def top(self):
        # write your code here
        return self.stack[0]

"""
41. Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum.

Example
Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.

Challenge
Can you do it in time complexity O(n)?
"""
def maxSubArray(self, nums):

        if max(nums)<0:
            return max(nums)
        sum_max = 0
        sum_max_local = 0
        for i in range(len(nums)):
            if nums[i]>=0:
                sum_max_local += nums[i]
            else:
                sum_max_local = max(sum_max_local+nums[i],0)
            sum_max = max(sum_max,sum_max_local)
        return sum_max

"""
42. Maximum Subarray II
Given an array of integers, find two non-overlapping subarrays which have the largest sum.
The number in each subarray should be contiguous.
Return the largest sum.

Example
For given [1, 3, -1, 2, -1, 2], the two subarrays are [1, 3] and [2, -1, 2] or [1, 3, -1, 2] and [2], they both have the largest sum 7.

Challenge
Can you do it in time complexity O(n) ?
"""
def maxTwoSubArrays(self, nums):
        # write your code here
        if len(nums)==0:
            return 0
        
        if len(nums)==1:
            return nums[0]
            
        n_max1 = max(nums[0],nums[1])
        n_max2 = min(nums[0],nums[1])
        
        for i in range(2,len(nums)):
            if nums[i]>n_max1:
                n_max1 = nums[i]
                n_max2 = n_max1
            elif nums[i] >n_max2:
                n_max2 = nums[i]
        
        if n_max2<0:
            return n_max1+n_max2
        
        L_1 = [max(nums[0],0)]
        L_11 = [max(nums[0],0)]
        L_2 = [max(nums[-1],0)]
        L_22 = [max(nums[-1],0)]
        
        for i in range(1,len(nums)):
            
            if nums[i]>=0:
                L_1.append(L_1[-1]+nums[i])
            else:
                L_1.append(max(L_1[-1]+nums[i],0))
            L_11.append(max(L_11[-1],L_1[-1]))
            
            if nums[len(nums)-1-i]>=0:
                L_2.append(L_2[-1]+nums[len(nums)-1-i])
            else:
                L_2.append(max(L_2[-1]+nums[len(nums)-1-i],0))
            L_22.append(max(L_22[-1],L_2[-1]))
        
        sum_max = L_11[-1]
        
        for i in range(1,len(nums)):
            sum_max = max(sum_max, L_11[-i-1]+L_22[i-1])
        
        return sum_max

"""
43. Maximum Subarray III
Given an array of integers and a number k, find k non-overlapping subarrays which have the largest sum.

The number in each subarray should be contiguous.

Return the largest sum.

Example
Given [-1,4,-2,3,-2,3], k=2, return 8
"""
def maxSubArray(self, nums, k):
        # write your code here
        loc = [[0 for i in range(k+1)] for i in range(len(nums)+1)]
        glo = [[0 for i in range(k+1)] for i in range(len(nums)+1)]
        
        for j in range(1,k+1):
            loc[0][j] = -sys.maxsize
            glo[0][j] = -sys.maxsize
            
        for i in range(1,len(nums)+1):
            loc[i][0] = 0
            glo[i][0] = 0
            for j in range(1,k+1):
                if j>i:
                    loc[i][j] = -sys.maxsize
                    glo[i][j] = -sys.maxsize
                else:
                    loc[i][j] = max(loc[i-1][j],glo[i-1][j-1])+nums[i-1]
                    glo[i][j] = max(loc[i][j],glo[i-1][j])
        
        return glo[len(nums)][k]

"""
44. Minimum Subarray
Given an array of integers, find the subarray with smallest sum.

Return the sum of the subarray.

Example
For [1, -1, -2, 1], return -3.
"""
def minSubArray(self, nums):
        # write your code here
        if min(nums)>0:
            return min(nums)
        sum_min = 0
        sum_min_local = 0
        for i in range(len(nums)):
            if nums[i]<=0:
                sum_min_local += nums[i]
            else:
                sum_min_local = min(sum_min_local+nums[i],0)
            sum_min = min(sum_min,sum_min_local)
        return sum_min

"""
45. Maximum Subarray Difference
Given an array with integers.

Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest.

Return the largest difference.

Example
For [1, 2, -3, 1], return 6.

Challenge
O(n) time and O(n) space.
"""

def maxDiffSubArrays(self, nums):
        # write your code here
        
        pl1 = [nums[0]]
        pg1 = [nums[0]]
        nl1 = [nums[-1]]
        ng1 = [nums[-1]]
        
        pl2 = [nums[-1]]
        pg2 = [nums[-1]]
        nl2 = [nums[0]]
        ng2 = [nums[0]]
        
        for i in range(1,len(nums)):
            pl1.append(max(pl1[-1]+nums[i],nums[i]))
            pg1.append(max(pg1[-1],pl1[-1]))
            
            nl2.append(min(nl2[-1]+nums[i],nums[i]))
            ng2.append(min(ng2[-1],nl2[-1]))
            
            nl1.append(min(nl1[-1]+nums[len(nums)-1-i],nums[len(nums)-1-i]))
            ng1.append(min(ng1[-1],nl1[-1]))
            
            pl2.append(max(pl2[-1]+nums[len(nums)-1-i],nums[len(nums)-1-i]))
            pg2.append(max(pg2[-1],pl2[-1]))
                
        sum_max = 0
        for i in range(len(pl1)-1):
            sum_max = max(sum_max,pg1[i]-ng1[len(pl1)-2-i],pg2[i]-ng2[len(pl1)-2-i])
        
        return sum_max

"""
46. Majority Element
Given an array of integers, the majority number is the number that occurs more than half of the size of the array. Find it.

Example
Given [1, 1, 1, 1, 2, 2, 2], return 1

Challenge
O(n) time and O(1) extra space
"""
def majorityNumber(self, nums):
        # write your code here
        D = {}
        for i in nums:
            D[i] = D.get(i,0) + 1
            if D[i] > len(nums)/2:
                return i

"""
47. Majority Element II
Given an array of integers, the majority number is the number that occurs more than 1/3 of the size of the array.

Find it.

Example
Given [1, 2, 1, 2, 1, 3, 3], return 1.

Challenge
O(n) time and O(1) extra space.
"""
def majorityNumber(self, nums):
        # write your code here
        D = {}
        for i in nums:
            D[i] = D.get(i,0) + 1
            if D[i] > len(nums)/3:
                return i

"""
48. Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1/k of the size of the array.

Find it.

Example
Given [3,1,2,3,2,3,3,4,4,4] and k=3, return 3.

Challenge
O(n) time and O(k) extra space
"""
def majorityNumber(self, nums, k):
        # write your code here
        D = {}
        for i in nums:
            D[i] = D.get(i,0) + 1
            if D[i] > len(nums)/k:
                return i

"""
49. Sort Letters by Case
Given a string which contains only letters. Sort it by lower case first and upper case second.

Example
For "abAcD", a reasonable answer is "acbAD"

Challenge
Do it in one-pass and in-place.
"""

def sortLetters(self, chars):
    # write your code here
    s1 = ""
    s2 = ""
    for s in chars:
        if s in "abcdefghijklmnopqrstuvwxyz":
            s1 += s
        else:
            s2 += s
    
    i = 0
    while i < len(chars):
        if i < len(s1):
            chars[i] = s1[i]
        else:
            chars[i] = s2[i-len(s1)]
        i += 1
        
"""
50. Product of Array Exclude Itself
Given an integers array A.

Define B[i] = A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1], calculate B WITHOUT divide operation.

Example
For A = [1, 2, 3], return [6, 3, 2].
"""

def productExcludeItself(self, nums):
    # write your code here
    result = []
    for i in range(len(nums)):
        s = 1
        for j in range(len(nums)):
            if i!=j:
                s *= nums[j]
        result.append(s)
    return result

"""
51. Previous Permutation
Given a list of integers, which denote a permutation.

Find the previous permutation in ascending order.

Example
For [1,3,2,3], the previous permutation is [1,2,3,3]

For [1,2,3,4], the previous permutation is [4,3,2,1]
"""
def previousPermuation(self, nums):
    # write your code here
    i = len(nums)-1
    while i>0 and nums[i] >= nums[i-1]:
        i-=1
    i-=1
    if i>=0:
        j = i+1
        while j<len(nums) and nums[i] > nums[j]:
            j+=1
        j-=1
        nums[i],nums[j] = nums[j],nums[i]
    left = i+1
    right = len(nums)-1
    while left<right:
        nums[left],nums[right] = nums[right],nums[left]
        left += 1
        right -= 1
    return nums

"""
52. Next Permutation
Given a list of integers, which denote a permutation.

Find the next permutation in ascending order.

Example
For [1,3,2,3], the next permutation is [1,3,3,2]

For [4,3,2,1], the next permutation is [1,2,3,4]
"""
def nextPermutation(self, nums):
    # write your code here
    i = len(nums)-1
    while i>0 and nums[i] <= nums[i-1]:
        i-=1
    i-=1
    if i>=0:
        j = i+1
        while j<len(nums) and nums[i] < nums[j]:
            j+=1
        j-=1
        nums[i],nums[j] = nums[j],nums[i]
    left = i+1
    right = len(nums)-1
    while left<right:
        nums[left],nums[right] = nums[right],nums[left]
        left += 1
        right -= 1
    return nums

"""
53. Reverse Words in a String
Given an input string, reverse the string word by word.

For example,
Given s = "the sky is blue",
return "blue is sky the".
"""
def reverseWords(self, s):
    # write your code here
    s1 = ""
    start = 0
    flag = 1
    for i in range(len(s)):
        if s[i] != " " and flag == 0:
            flag = 1
            start = i
        if s[i] == " " and flag == 1:
            s1 =  " " + s[start:i] + s1
            flag = 0
        if i == len(s)-1 and flag == 1:
            s1 = s[start:]+s1
    return s1

"""
54. String to Integer (atoi)
Implement function atoi to convert a string to an integer.

If no valid conversion could be performed, a zero value is returned.

If the correct value is out of the range of representable values, INT_MAX (2147483647) or INT_MIN (-2147483648) is returned.

Example
"10" => 10
"-1" => -1
"123123123123123" => 2147483647
"1.0" => 1
"""


def atoi(self,str):
    # write your code here
    if len(str)==0:
        return 0
    
    flag_times = 0
    flag = 1
    start = 0
    for i in range(len(str)):
        if str[i] == "-":
            flag = -1
            flag_times += 1
        if str[i] == "+":
            flag = 1
            flag_times += 1
        if str[i] == " " or str[i] == "-" or str[i] == "+":
            start += 1
        else:
            break
    
    if str[start].isdigit() == False or flag_times > 1:
        return 0
    
    end = start+1
    for i in range(start+1,len(str)+1):
        if str[start:i].isdigit():
            end = i
    
    
    
    num = flag*int(str[start:end])
    
    if num<=2147483647 and num>=-2147483648:
        return num
    elif num>2147483647:
        return 2147483647
    else:
        return -2147483648
    
"""
55. Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B.

The characters in string A and B are all Upper Case letters.

Example
For A = "ABCD", B = "ACD", return true.

For A = "ABCD", B = "AABC", return false.
"""
def compareStrings(self, A, B):
    # write your code here
    if len(B) == 0:
        return True
    
    D = {}
    for s in B:
        D[s] = D.get(s,0) + 1
    
    num = len(B)
    
    for s in A:
        if D.get(s,0)>0:
            num -= 1
            D[s] -= 1
    
    if num == 0:
        return True
    else:
        return False

"""
56. Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are zero-based.

Example
numbers=[2, 7, 11, 15], target=9

return [0, 1]

Challenge
Either of the following solutions are acceptable:

O(n) Space, O(nlogn) Time
O(n) Space, O(n) Time
"""
def twoSum(self, numbers, target):
    # write your code here
    for i in range(len(numbers)):
        for j in range(i+1,len(numbers)):
            if numbers[i]+numbers[j] == target:
                return [i,j]
    return [0,0]

"""
57. 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Example
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is:

(-1, 0, 1)
(-1, -1, 2)
"""
def threeSum(self, numbers):
    # write your code here
    result = []
     
    numbers1 = sorted(numbers)
    
    for i in range(len(numbers1)):
        for j in range(i+1,len(numbers1)):
            for k in range(j+1,len(numbers1)):
                if numbers1[i]+numbers1[j]+numbers1[k]==0 and [numbers1[i],numbers1[j],numbers1[k]] not in result:
                    result.append([numbers1[i],numbers1[j],numbers1[k]])
    return result

"""
58. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?

Find all unique quadruplets in the array which gives the sum of target.

Example
Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution set is:

(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)
"""
def fourSum(self, numbers, target):
    # write your code here
    result = []
 
    numbers1 = sorted(numbers)
    
    for i in range(len(numbers1)-3):
        if i and numbers1[i] == numbers1[i - 1]:
            continue
        for j in range(i+1,len(numbers1)-2):
            if j != i + 1 and numbers1[j] == numbers1[j - 1]:
                continue
            k = j+1
            l = len(numbers1)-1
            while k<l:
                if numbers1[i]+numbers1[j]+numbers1[k]+numbers1[l]==target and [numbers1[i],numbers1[j],numbers1[k],numbers1[l]] not in result:
                    result.append([numbers1[i],numbers1[j],numbers1[k],numbers1[l]])
                elif numbers1[i]+numbers1[j]+numbers1[k]+numbers1[l] > target:
                    l-=1
                else:
                    k+=1
                
    return result

"""
59. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers.

Example
For example, given array S = [-1 2 1 -4], and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).

Challenge
O(n^2) time, O(1) extra space

Notice
You may assume that each input would have exactly one solution.
"""
def threeSumClosest(self, numbers, target):
        # write your code here
        numbers1 = sorted(numbers)
            
        idex = [0,1,2]
        
        gap_best = numbers1[0]+numbers1[1]+numbers1[2]-target
        
        for i in range(len(numbers1)-2):
            j=i+1
            k=len(numbers1)-1
            while j<k:
                if abs(numbers1[i]+numbers1[j]+numbers1[k]-target)<abs(gap_best):
                    idex = [i,j,k]
                    gap_best = numbers1[i]+numbers1[j]+numbers1[k]-target
                if numbers1[i]+numbers1[j]+numbers1[k]-target < 0:
                    j+=1
                elif numbers1[i]+numbers1[j]+numbers1[k]-target > 0:
                    k-=1
                else:
                    return target
        return gap_best+target
    
"""
60. Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume NO duplicates in the array.

Example
[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

Challenge
O(log(n)) time
"""
def searchInsert(self, A, target):
    # write your code here
    if len(A) == 0:
        return 0
    
    if target <= A[0]:
        return 0
    if target > A[-1]:
        return len(A)
    if target == A[-1]:
        return len(A)-1
    
    start = 0
    end = len(A)-1
    mid = (int)((start+end)/2)
    while mid != start:
        if A[mid] == target:
            return mid
        elif A[mid] < target:
            start = mid
            mid = (int)((start+end)/2)
        else:
            end = mid
            mid = (int)((start+end)/2)
    return end

"""
61. Search for a Range
Given a sorted array of n integers, find the starting and ending position of a given target value.

If the target is not found in the array, return [-1, -1].

Example
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].

Challenge
O(log n) time.
"""
def searchRange(self, A, target):
    # write your code here
    if len(A) == 0:
        return [-1, -1]
    
    if target < A[0] or target > A[-1]:
        return [-1, -1]
        
    start = 0
    end = len(A)-1
    mid = (int)((start+end)/2)
    while mid != start:
        if A[mid] == target:
            break;
        elif A[mid] < target:
            start = mid
            mid = (int)((start+end)/2)
        else:
            end = mid
            mid = (int)((start+end)/2)
    
    if A[mid] != target:
        if A[end] == target:
            return [end, end]
        else:
            return [-1,-1]
    else:
        start = mid
        while start > 0:
            if A[start-1] != target:
                break
            start -= 1
        end = mid
        while end < len(A)-1:
            if A[end+1] != target:
                break
            end += 1
        return [start, end]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值