您的位置:首页 > 其它

Leetcode: Sparse Matrix Multiplication

2015-12-28 11:02 183 查看

Question

Given two sparse matrices A and B, return the result of AB.

You may assume that A’s column number is equal to B’s row number.

Example:

A = [

[ 1, 0, 0],

[-1, 0, 3]

]

B = [

[ 7, 0, 0 ],

[ 0, 0, 0 ],

[ 0, 0, 1 ]

]

[code] |  1 0 0 |   | 7 0 0 |   |  7 0 0 |


AB = | -1 0 3 | x | 0 0 0 | = | -7 0 3 |

| 0 0 1 |

Hide Company Tags LinkedIn

Hide Tags Hash Table

My first try

Time complexity: O(n4)O(n^4)

[code]class Solution(object):
    def multiply(self, A, B):
        """
        :type A: List[List[int]]
        :type B: List[List[int]]
        :rtype: List[List[int]]
        """

        if len(A)==0 or len(B)==0:
            return 0

        m, n = len(A), len(B[0])
        res = [ [0]*n for i in range(m) ] 

        for i in range(m):
            i_zero = False
            for j in range(n):
                if i_zero==True:
                    res[i][j] = 0

                temp = 0
                for i_value in A[i]:
                    if i_value!=0:
                        i_zero = True 
                    for j_index in range(len(B)):
                        j_value = B[j_index][j]
                        temp += i_value*j_value

                res[i][j] = temp

        return res

    # second method
    # 1. scan all elem, record all non_zero elements
    # 2. find the intersection

    # def mul(self, list1, list2):
    #    return sum( [temp1*temp2 for temp1, temp2 in zip(list1,list2)] )


Error

Time exceeded.

The second try

Accept

Time complexity: O(n3)O(n^3)

Time : 400+ (s)

[code]class Solution(object):
    def multiply(self, A, B):
        """
        :type A: List[List[int]]
        :type B: List[List[int]]
        :rtype: List[List[int]]
        """

        if len(A)==0 or len(B)==0:
            return 0

        m, n = len(A), len(B[0])
        res = [ [0]*n for i in range(m) ] 

        for i in range(m):
            i_zero = True
            for ind in range(n):
                if A[i][ind]!=0:
                    i_zero = False
                    break

            for j in range(n):
                if i_zero==True:
                    res[i][j] = 0
                    continue

                for mul_ind in range(len(A[0])):
                    res[i][j] += A[i][mul_ind] * B[mul_ind][j]

        return res


The third try

Accept

did some optimizations

[code]class Solution(object):
    def multiply(self, A, B):
        """
        :type A: List[List[int]]
        :type B: List[List[int]]
        :rtype: List[List[int]]
        """

        if len(A)==0 or len(B)==0:
            return 0

        m, n = len(A), len(B[0])
        res = [ [0]*n for i in range(m) ] 

        zerom, zeron = [True]*m, [True]*n
        for i in range(m):
            for index in range(len(A[0])):
                if A[i][index]!=0:
                    zerom[i] = False
                    break

        for i in range(n):
            for index in range(len(B)):
                if B[index][i]!=0:
                    zeron[i] = False
                    break

        for i in range(m):
            if zerom[i]==True:
                continue

            for j in range(n):
                if zeron[j]==True:
                    continue

                for mul_ind in range(len(A[0])):
                    res[i][j] += A[i][mul_ind] * B[mul_ind][j]

        return res
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: