zoukankan      html  css  js  c++  java
  • [leetcode]244. Shortest Word Distance II最短单词距离(允许连环call)

    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

    题意:

    还是数组中两个单词的最短距离。

    相对于之前[leetcode]243. Shortest Word Distance最短单词距离

    这题要求对于function,允许连环call(言外之意是,不能naive的扫数组了)

    Solution1: HashMap + Merge Sort

    1.  Since "method will be called repeatedly many times", it will cost much time to scan the whole input String array again and again. 

        We can use a hashmap in advance, saving each string as a key and its corresponding indices as a value.

    2. Use pointer i, j to scan list1, list2 seperately, updating the shortest distance.

     

    code

     1 class WordDistance {
     2     HashMap <String, List<Integer>> map;
     3     
     4     public WordDistance(String[] words) {
     5         map = new HashMap<>();
     6         for(int i = 0 ; i< words.length; i++){
     7             String w = words[i];
     8             if(map.containsKey(w)){
     9                 map.get(w).add(i);
    10             }else{
    11                 List<Integer> list = new ArrayList<>();
    12                 list.add(i);
    13                 map.put(w, list);
    14             }
    15         }
    16         
    17     }
    18 
    19     public int shortest(String word1, String word2) {
    20         List<Integer> l1 = map.get(word1);
    21         List<Integer> l2 = map.get(word2);
    22         // --------------merger sort 思想  | ------------------------
    23         // --------------merger sort |/ ------------------------
    24         int result = Integer.MAX_VALUE;
    25         int i = 0;
    26         int j = 0;
    27         while(i<l1.size() && j<l2.size()) {
    28             result = Math.min(result, Math.abs(l1.get(i)- l2.get(j)));
    29             if(l1.get(i)<l2.get(j)){
    30                 i++;
    31             }else{
    32                 j++;
    33             }
    34         }
    35         // --------------merger sort------------------------
    36         return result;    
    37     }
    38 }

    复杂度:

    时间
    存储:O(N)     map是用来存储的。用时为扫一遍given array的时间。 
    查找:O(m+n) list是用来查找的。用时为扫list1的时间+扫list2的时间。

    空间
    存储:O(N)     map是用来存储的。将whole array的元素都放入了map。 (内心OS:map的空间是由key的个数决定的)
    查找:O(N)     list是用来查找的。 list所占空间为each string的corresponding 

  • 相关阅读:
    async/await的一些用法
    软件须把握两个点: 错误可控, 复杂度可控
    C++内存管理原则
    *复赛前提醒
    一些好用的网站
    NOIP真题汇总
    信息学中的一些些数论
    [Luogu 1052] noip 05 过河
    浅谈概率期望动态规划
    [Luogu 1312] noip11 Mayan游戏
  • 原文地址:https://www.cnblogs.com/liuliu5151/p/9148514.html
Copyright © 2011-2022 走看看