1. 以后看到个数比较少,性能比较高,就要第一时间想到位操作!

这道题目mock没有通过。超时了。。。。。。

原来题目解法的思路非常非常好!

  1. 开始我关注于降低n*n的复杂度,但是这道题目复杂度高在每个字符串长,所以一定要问清楚题目
  2.  
  3. 新做的记录:
  1. package com.company;
  2.  
  3. import java.util.*;
  4. import java.util.List;
  5.  
  6. class Solution {
  7. public int maxProduct(String[] words) {
  8. // int是32位,足够处理了。真的很牛逼
  9. // 以后看到个数比较少,性能比较高,就要第一时间想到位操作!
  10. // 开始我关注于降低n*n的复杂度,但是这道题目复杂度高在每个字符串长,所以一定要问清楚题目
  11. List<Integer> ilist = new ArrayList<>();
  12. for (int i=0; i<words.length; i++) {
  13. int nb = 0;
  14. for (int j=0; j<words[i].length(); j++) {
  15. nb |= 1 << (words[i].charAt(j) - 'a');
  16. }
  17. ilist.add(nb);
  18. }
  19. int ret = 0;
  20. for (int i=0; i<words.length; i++) {
  21. for (int j=i+1; j<words.length; j++) {
  22. if ((ilist.get(i) & ilist.get(j)) == 0) {
  23. if (words[i].length() * words[j].length() > ret) {
  24. ret = words[i].length() * words[j].length();
  25. }
  26. }
  27. }
  28. }
  29. return ret;
  30. }
  31. }
  32.  
  33. public class Main {
  34.  
  35. public static void main(String[] args) {
  36. // write your code here
  37. System.out.println("Hello");
  38. Solution solution = new Solution();
  39.  
  40. String[] words= {"cdea","bdd"};
  41. int ret = solution.maxProduct(words);
  42. System.out.printf("Get ret: %d\n", ret);
  43.  
  44. }
  45. }

原来做过的记录

  1. https://leetcode.com/problems/maximum-product-of-word-lengths/
  2.  
  3. // 我在尽量做到比n*n效率更高
  4. // 对比new solution和previous solution
  5. // new solution 是纯n*n,优化在字符串比较
  6. // 对于大数据,耗时0ms
  7. // previous solution理论上比n*n要快,
  8. // 但是因为涉及vector的频繁增删、复制
  9. // 实际要慢的多,对于大数据耗时800+ms
  10.  
  11. // 总的来看,stl container的复制、删除等,耗时很大
  12.  
  13. class Solution {
  14. vector<pair<int, int>> vec;
  15. vector<string> words;
  16. int **DP;
  17. // 比较的好方法
  18. int *bits;
  19.  
  20. static bool my_compair(const string &a, const string &b) {
  21. if (a.size() > b.size()) {
  22. return true;
  23. }
  24. else {
  25. return false;
  26. }
  27. }
  28.  
  29. void insert(int i, int j) {
  30. int f = words[i].size() * words[j].size();
  31.  
  32. int vlen = vec.size();
  33. int begin = 0;
  34. int end = vlen - 1;
  35.  
  36. int mid;
  37. int tmp;
  38. while (begin <= end) {
  39. mid = (begin + end) / 2;
  40. tmp = words[vec[mid].first].size() * words[vec[mid].second].size();
  41.  
  42. if (f == tmp) {
  43. begin = mid + 1;
  44. break;
  45. }
  46. else if (f > tmp) {
  47. end = mid - 1;
  48. }
  49. else {
  50. begin = mid + 1;
  51. }
  52. }
  53. vec.insert(vec.begin()+begin, make_pair(i, j));
  54. }
  55.  
  56. int valid(int i, int j) {
  57. if ((bits[i] & bits[j]) != 0) {
  58. return 0;
  59. }
  60. return words[i].size() * words[j].size();
  61. }
  62.  
  63. public:
  64. int maxProduct(vector<string>& w) {
  65. words = w;
  66.  
  67. int wlen = words.size();
  68. if (wlen == 0) {
  69. return 0;
  70. }
  71. sort(words.begin(), words.end(), my_compair);
  72.  
  73. // 初始化bits
  74. bits = new int[wlen];
  75. memset(bits, 0, sizeof(int)*wlen);
  76. for (int i=0; i<wlen; i++) {
  77. string wstr = words[i];
  78. int slen = wstr.size();
  79. for (int j=0; j<slen; j++) {
  80. bits[i] |= 1 << (wstr[j]-'a');
  81. }
  82. }
  83.  
  84. // new solution(0 ms for big test case)
  85. int result = 0;
  86. for (int i=0; i<wlen-1; i++) {
  87. for (int j=i+1; j<wlen; j++) {
  88. if ((bits[i]&bits[j]) == 0) {
  89. int tmp = words[i].size() * words[j].size();
  90. if (tmp > result) {
  91. result = tmp;
  92. }
  93. }
  94. }
  95. }
  96. return result;
  97.  
  98. // previous solution (800ms for big test case)
  99. DP = new int*[wlen];
  100. for (int i=0; i<wlen; i++) {
  101. DP[i] = new int[wlen];
  102. // 注意,new出来的数据初始值,不一定为0
  103. memset(DP[i], 0, sizeof(int)*wlen);
  104. }
  105.  
  106. // 根据相乘的长度排序
  107. vec.push_back(make_pair(0, 1));
  108. DP[0][1] = 1;
  109.  
  110. int fir;
  111. int sec;
  112. int tmp;
  113.  
  114. while (!vec.empty()) {
  115. fir = vec[0].first;
  116. sec = vec[0].second;
  117. vec.erase(vec.begin());
  118.  
  119. tmp = valid(fir, sec);
  120. if (tmp > result) {
  121. result = tmp;
  122. }
  123.  
  124. if (fir + 1 < sec && DP[fir+1][sec] == 0 &&
  125. words[fir+1].size() * words[sec].size() > result) {
  126. insert(fir+1, sec);
  127. DP[fir+1][sec] = 1;
  128. }
  129. if (sec + 1 < wlen && DP[fir][sec+1] == 0 &&
  130. words[fir].size() * words[sec+1].size() > result) {
  131. insert(fir, sec+1);
  132. DP[fir][sec+1] = 1;
  133. }
  134. }
  135. return result;
  136. }
  137. };
  138.  
  139. // 下面是我在 Mock里面做的,超时了。重来。
  140. package com.company;
  141.  
  142. import java.awt.*;
  143. import java.util.*;
  144. import java.util.List;
  145.  
  146. class Solution {
  147. public int maxProduct(String[] words) {
  148. // 直接用n*n*size的方法肯定不好
  149. // 注意限制条件, lower case的字符
  150.  
  151. Map<Integer, Set<Integer>> mp = new HashMap<>();
  152.  
  153. List<Integer> clist = new ArrayList<>();
  154.  
  155. for (int i=0; i<words.length; i++) {
  156. clist.add(i);
  157.  
  158. // 过滤
  159. char[] chs = words[i].toCharArray();
  160. Set<Integer> wSet = new HashSet();
  161. for (char ch :chs) {
  162. wSet.add(ch - 'a');
  163. }
  164. Iterator<Integer> iter = wSet.iterator();
  165. while (iter.hasNext()) {
  166. int key = iter.next();
  167. if (!mp.containsKey(key)) {
  168. Set<Integer> st = new HashSet<>();
  169. st.add(i);
  170. mp.put(key, st);
  171. }
  172. else {
  173. Set<Integer> st = mp.get(key);
  174. st.add(i);
  175. mp.put(key, st);
  176. }
  177. }
  178.  
  179. }
  180.  
  181. int ret = 0;
  182. for (int i=0; i<words.length; i++) {
  183. Set<Integer> oSet = new HashSet<>(clist);
  184. char[] chs = words[i].toCharArray();
  185. Set<Integer> wSet = new HashSet();
  186. for (char ch :chs) {
  187. wSet.add(ch - 'a');
  188. }
  189. Iterator<Integer> iter = wSet.iterator();
  190. while (iter.hasNext()) {
  191. int key = iter.next();
  192. Set<Integer> st = mp.get(key);
  193. oSet.removeAll(st);
  194. }
  195. iter = oSet.iterator();
  196. while (iter.hasNext()) {
  197. int index = iter.next();
  198. if (words[i].length() * words[index].length() > ret) {
  199. ret = words[i].length() * words[index].length();
  200. }
  201. }
  202. }
  203. return ret;
  204.  
  205. }
  206. }
  207.  
  208. public class Main {
  209.  
  210. public static void main(String[] args) {
  211. // write your code here
  212. System.out.println("Hello");
  213. Solution solution = new Solution();
  214.  
  215. String[] words= {};
  216. int ret = solution.maxProduct(words);
  217. System.out.printf("Get ret: %d\n", ret);
  218.  
  219. }
  220. }

好!maximum-product-of-word-lengths的更多相关文章

  1. leetcode 318. Maximum Product of Word Lengths

    传送门 318. Maximum Product of Word Lengths My Submissions QuestionEditorial Solution Total Accepted: 1 ...

  2. [LeetCode] Maximum Product of Word Lengths 单词长度的最大积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  3. LeetCode 【318. Maximum Product of Word Lengths】

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  4. 318. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  5. Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  6. 318. Maximum Product of Word Lengths ——本质:英文单词中字符是否出现可以用26bit的整数表示

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  7. Java [Leetcode 318]Maximum Product of Word Lengths

    题目描述: Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where ...

  8. leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)

    https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...

  9. [Swift]LeetCode318. 最大单词长度乘积 | Maximum Product of Word Lengths

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

  10. [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积

    Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...

随机推荐

  1. VS调试Libevent流程

    下载源码包: libevent--stable.tar.gz 第一:编译libevent 进入VS2010命令提示,切换到libevent的所在目录 nmake /f Makefile.nmake 编 ...

  2. Codeforces Round #231 (Div2) 迟到的解题报告

    题目A: 给一个火柴等式,可以从左边移动一根到右边,也可以从右边移到左边,但是不能移动“+”,”=“的火柴, 而且加法里面的数都要大于0(很重要的条件),基本上注意到这点的都过了,没注意的都被HACK ...

  3. mysql触发器使用实例

    DELIMITER $$ USE `db`$$ DROP TRIGGER `member_walletinit_trigger`$$ CREATE TRIGGER `member_walletinit ...

  4. 利用dsniff的tcpkill杀TCP连接

    利用dsniff的tcpkill杀TCP连接 Linux连接久久不能释放的现象不常见,但偶然也会发生.进程虽不复存在,但是客户端的连接咬定青山不放松,死活也不肯吐出连接,导致重启进程时因操作系统判断监 ...

  5. chrome 网络面板

    Chrome Timeline的指标说明:Blocked.Connect.Send.Wait.Receive Blocked time includes any pre-processing time ...

  6. ASP.NET 4.5新特性WebAPI从入门到精通

    在新出的MVC4中,增加了WebAPI,用于提供REST风格的WebService,新生成的WebAPI项目和典型的MVC项目一样,包含主要的Models.Views.Controllers等文件夹和 ...

  7. Masonry自动布局

    介绍,入门: http://www.cocoachina.com/ios/20141219/10702.html 下载: http://code.cocoachina.com/detail/30114 ...

  8. 从.NET 1.1 升级到.NET 4.0 遇到 线程间操作无效: 从不是创建控件 [XX] 的线程访问它.

    有两种方式解决 1.在窗体构造函数中写Control.CheckForIllegalCrossThreadCalls =false;2.使用Invoke等委托函数 问题原因是 .NET2.0 以后拒绝 ...

  9. dom对象详解--document对象(三)

     form对象 form对象代表一个HTML表单,在HTML文档中<form>每出现一次,form对象就会被创建.从dom对象层次图看,document.forms对象是当前文档所有for ...

  10. Spring REST for DELETE Request Method Not Supoorted

    http://stackoverflow.com/questions/22055251/sending-data-with-angularjs-http-delete-request I have a ...