[LeetCode#244] Shortest Word Distance II
2015-09-14 00:09
302 查看
Problem:
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
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.
For example,
Assume that words =
Given word1 =
Given word1 =
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Analysis:
Solution:
This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?
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.
For example,
Assume that words =
["practice", "makes", "perfect", "coding", "makes"].
Given word1 =
“coding”, word2 =
“practice”, return 3.
Given word1 =
"makes", word2 =
"coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Analysis:
This is an updated version of Shortest Word Distance. Since it would be called many times, we should not do the search in traditional O(n) way. One apparent thing we should optimize is to reduce uncessary check and comparision. We should exclude the words that we are not care about. The instant idea is to use a hashmap. Key: the word; Value: a list of indexes associated with the word. Through this way, we can get all information we want by retrieving the HashMap, and only use two realated list. ArrayList<Integer> list1 = map.get(word1); ArrayList<Integer> list2 = map.get(word2); Even though we narrow down the elements we need to search, there could be a efficiency pitfall if we fail to implement it rightly. Traditionally we would try to search the shortest distance through following way. for (int i = 0; i < list1.length(); i++) { for (int j = 0; j < list2.length(); j++) { min = Math.min(min, Math.abs(j - i)); } } Apparently, at the worst case, the time complexity is O(n^2), which is even worse than our previous solution. Why? Cause there are many uncessary comparisions. --------------------------------------------------------------------------------------------------------------- word 1: [1, 5, 8] word 2: [2, 10] Since we have already compared min with |2-1|, why we still need to compare |8-1|, since all elements after 2 must have larger distance than |2-1| (with 1 fixed). It seems I get used with "for-loop" method to scan lists, while ignoring we actually could also use "while-loop" over two lists. ------------------------------------------------------------------- while (i < m && j < n) { min = Math.min(min, Math.abs(list1.get(i)-list2.get(j))); if (list1.get(i) < list2.get(j)) i++; else j++; } } ------------------------------------------------------------------- The reason we could see the example: word 1: [1, 5, 8] word 2: [2, 10] After we finished the scan: "1, 2", "1" is no longer could have combination "1, x (x > 2)" has shorter distance than "1, 2".
Solution:
public class WordDistance { HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>> (); public WordDistance(String[] words) { for (int i = 0; i < words.length; i++) { String word = words[i]; if (map.containsKey(word)) { map.get(word).add(i); } else{ ArrayList<Integer> item = new ArrayList<Integer> (); item.add(i); map.put(word, item); } } } public int shortest(String word1, String word2) { ArrayList<Integer> list1 = map.get(word1); ArrayList<Integer> list2 = map.get(word2); int i = 0, j = 0; int min = Integer.MAX_VALUE; int m = list1.size(); int n = list2.size(); while (i < m && j < n) { min = Math.min(min, Math.abs(list1.get(i)-list2.get(j))); if (list1.get(i) < list2.get(j)) i++; else j++; } return min; } }
相关文章推荐
- JNI调用
- spark 最新版1.0本地模式安装_spark入门学习一
- Lintcode搜索区间
- php服务器版本更新工具up2server
- Unity3D笔记第十三天——关节、布料
- 网络操作系统和分布式操作系统的区别
- Linux中批量删除redis中的key
- [知其然不知其所以然-5] 为什么我的风扇温度很低却转不停/或者温度很高却根本不转?
- 深入浅出:进程、线程、协程、同步、异步、回调(转载)
- Google Hacking
- pptpd免radius限速、限连接+自由定制功能脚本
- Spring资源
- java线程系列---Runnable和Thread的区别
- javascript数组操作
- Spark的Rpct模块的学习
- 我在hp的第一次JavaWeb总结
- CentOS6.4下Mysql数据库的安装与配置
- laravel blog 二
- 关于DLL的导出.def文件
- codecombat安息之云山峰41关代码分享