Why is my python so slow?

  • -1

    I just used the standard hashmap solution. But why it only beats 12% python programmer.. Any hints?

    class WordDistance(object):
    def init(self, words):
    initialize your data structure here.
    :type words: List[str]
    self.hashMap = {}
    for ind, w in enumerate(words):
    if w not in self.hashMap:
    self.hashMap[w] = []

    def shortest(self, word1, word2):
        Adds a word into the data structure.
        :type word1: str
        :type word2: str
        :rtype: int
        l1, l2 = self.hashMap[word1], self.hashMap[word2]
        ind1 = ind2 = 0
        dis = sys.maxint
        while ind1 < len(l1) and ind2 < len(l2):
            if l1[ind1] < l2[ind2]:
                dis = min(l2[ind2] - l1[ind1], dis)
                ind1 += 1
                dis = min(l1[ind1] - l2[ind2], dis)
                ind2 += 1
        return dis

  • 0

    I think that if you submit more times, it would become more normal. Otherwise it looks pretty much the same with others and mine. Btw, remember to check your post before you post it, your constructer is mass up...It's not hard to show some respect to others.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.