244. Shortest Word Distance II


Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list. Your method will be called repeatedly many times with different parameters.

Example:
Assume that words = [“practice”, “makes”, “perfect”, “coding”, “makes”].

Input: word1 = “coding”, word2 = “practice”
Output: 3
Input: word1 = "makes", word2 = "coding"
Output: 1

Note:

You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

方法1:

思路:

如果沿用243的最高效方法,反而这道题会非常慢。这是因为读入的时候反正一定会pass一遍单词,而如果使用hash的方法,第二遍只可能减少遍历次数。而题目给出的提示是需要多次查询,那么每次遍历整个list一定慢于hash查找。首先来看和243一样的办法。

Complexity

Time complexity: O(kL), 进行k次查询, 总list长度是L
Space complexity: O(L);

class WordDistance {
public:
    WordDistance(vector<string> words) {
        words_copy.assign(words.begin(), words.end());
    }
    
    int shortest(string word1, string word2) {
        int n = words_copy.size();
        int p1 = -1, p2 = -1;
        int result = INT_MAX;
        for (int i = 0; i < n; i++){
            if (words_copy[i]  == word1){
                p1 = i;
                if (p2 != -1 ){
                    result = min(result, abs(p1 - p2));
                }
            }
            if (words_copy[i] == word2){
                p2 = i;
                if (p1 != -1){
                    result = min(result, abs(p1 - p2));
                }
            }
        }
        return result;
    }

private:
    vector<string> words_copy;
};

/**
 * Your WordDistance object will be instantiated and called as such:
 * WordDistance obj = new WordDistance(words);
 * int param_1 = obj.shortest(word1,word2);
 */

方法2: hash

Complexity

Time complexity: O (kMN), M,N是两个单词的vector长度,那么只有当MN << L的时候,hash会远远快于方法1。实际上方法1 用时1776 ms,方法2用时60ms,说明这个假设成立。
Space complexity: O(L)

class WordDistance {
public:
    WordDistance(vector<string> words) {
        for (int i = 0; i < words.size(); i++){
            hash[words[i]].push_back(i);   
        }
    }
    
    int shortest(string word1, string word2) {
        vector<int> p1 = hash[word1];
        vector<int> p2 = hash[word2];
        int result = INT_MAX;
        for (int i: p1){
            for (int j : p2){
                result = min(result, abs(i - j));
            }
        }
        return result;
    }

private:
    unordered_map<string, vector<int>> hash;
};

/**
 * Your WordDistance object will be instantiated and called as such:
 * WordDistance obj = new WordDistance(words);
 * int param_1 = obj.shortest(word1,word2);
 */

方法3: hash + two pointers

思路:

仍然是用hash,但是用双指针的方式来计算最小距离:用双指针遍历p1, p2,每次将较小的index后移,去寻找能够缩短距离的解,直到有一个list被遍历完,解不可能进一步缩小,返回最小值。

Complexity

Time complexity: O(k(M + N)),最长是m或n
Space complexity: O(L)

class WordDistance {
public:
    WordDistance(vector<string> words) {
        for (int i = 0; i < words.size(); i++){
            hash[words[i]].push_back(i);   
        }
    }
    
    int shortest(string word1, string word2) {
        int i = 0, j = 0, result = INT_MAX;
        while (i < hash[word1].size() && j < hash[word2].size()){
            result = min(result, abs(hash[word1][i] - hash[word2][j]));
            if (hash[word1][i] < hash[word2][j]){
                i++;
            }
            else {
                j++;
            }
        }
        return result;
    }

private:
    unordered_map<string, vector<int>> hash;
};

/**
 * Your WordDistance object will be instantiated and called as such:
 * WordDistance obj = new WordDistance(words);
 * int param_1 = obj.shortest(word1,word2);
 */
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值