{"id":100252,"date":"2020-05-21T11:49:27","date_gmt":"2020-05-21T03:49:27","guid":{"rendered":"http:\/\/4563.org\/?p=100252"},"modified":"2020-05-21T11:49:27","modified_gmt":"2020-05-21T03:49:27","slug":"lintcode-%e9%a2%98%e8%a7%a3-%e4%b8%80%e8%87%b4%e6%80%a7%e5%93%88%e5%b8%8c-ii-%c2%b7-consistent-hashing-ii","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=100252","title":{"rendered":"[lintcode \u9898\u89e3] \u4e00\u81f4\u6027\u54c8\u5e0c II \u00b7 Consistent Hashing II"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  [lintcode \u9898\u89e3] \u4e00\u81f4\u6027\u54c8\u5e0c II \u00b7 Consistent Hashing II               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : hakunamatata11 <\/span>  <span><i><\/i> 0<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>[\u9898\u76ee\u63cf\u8ff0]<\/p>\n<p>\u5728 Consistent Hashing I \u4e2d\u6211\u4eec\u4ecb\u7ecd\u4e86\u4e00\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\u4e00\u81f4\u6027\u54c8\u5e0c\u7b97\u6cd5\uff0c\u8fd9\u4e2a\u7b80\u5355\u7684\u7248\u672c\u6709\u4e24\u4e2a\u7f3a\u9677\uff1a<\/p>\n<ol>\n<li>\u589e\u52a0\u4e00\u53f0\u673a\u5668\u4e4b\u540e\uff0c\u6570\u636e\u5168\u90e8\u4ece\u5176\u4e2d\u4e00\u53f0\u673a\u5668\u8fc7\u6765\uff0c\u8fd9\u4e00\u53f0\u673a\u5668\u7684\u8bfb\u8d1f\u8f7d\u8fc7\u5927\uff0c\u5bf9\u6b63\u5e38\u7684\u670d\u52a1\u4f1a\u9020\u6210\u5f71\u54cd\u3002<\/li>\n<li>\u5f53\u589e\u52a0\u5230 3 \u53f0\u673a\u5668\u7684\u65f6\u5019\uff0c\u6bcf\u53f0\u670d\u52a1\u5668\u7684\u8d1f\u8f7d\u91cf\u4e0d\u5747\u8861\uff0c\u4e3a 1:1:2 \u3002<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u5f15\u5165\u4e86 micro-shards \u7684\u6982\u5ff5\uff0c\u4e00\u4e2a\u66f4\u597d\u7684\u7b97\u6cd5\u662f\u8fd9\u6837\uff1a<\/p>\n<ol>\n<li>\u5c06 360\u00b0 \u7684\u533a\u95f4\u5206\u5f97\u66f4\u7ec6\u3002\u4ece 0~359 \u53d8\u4e3a\u4e00\u4e2a 0 ~ n-1 \u7684\u533a\u95f4\uff0c\u5c06\u8fd9\u4e2a\u533a\u95f4\u9996\u5c3e\u76f8\u63a5\uff0c\u8fde\u6210\u4e00\u4e2a\u5706\u3002<\/li>\n<li>\u5f53\u52a0\u5165\u4e00\u53f0\u65b0\u7684\u673a\u5668\u7684\u65f6\u5019\uff0c\u968f\u673a\u9009\u62e9\u5728\u5706\u5468\u4e2d\u6492 k \u4e2a\u70b9\uff0c\u4ee3\u8868\u8fd9\u53f0\u673a\u5668\u7684 k \u4e2a micro-shards \u3002<\/li>\n<li>\u6bcf\u4e2a\u6570\u636e\u5728\u5706\u5468\u4e0a\u4e5f\u5bf9\u5e94\u4e00\u4e2a\u70b9\uff0c\u8fd9\u4e2a\u70b9\u901a\u8fc7\u4e00\u4e2a hash function \u6765\u8ba1\u7b97\u3002<\/li>\n<li>\u4e00\u4e2a\u6570\u636e\u8be5\u5c5e\u4e8e\u54ea\u53f0\u673a\u5668\u8d1f\u8d23\u7ba1\u7406\uff0c\u662f\u6309\u7167\u8be5\u6570\u636e\u5bf9\u5e94\u7684\u5706\u5468\u4e0a\u7684\u70b9\u5728\u5706\u4e0a\u987a\u65f6\u9488\u78b0\u5230\u7684\u7b2c\u4e00\u4e2a micro-shard \u70b9\u6240\u5c5e\u7684\u673a\u5668\u6765\u51b3\u5b9a\u3002<\/li>\n<li>n \u548c k \u5728\u771f\u5b9e\u7684 NoSQL \u6570\u636e\u5e93\u4e2d\u4e00\u822c\u662f 2^64 \u548c 1000 \u3002<\/li>\n<\/ol>\n<p>\u8bf7\u5b9e\u73b0\u8fd9\u79cd\u5f15\u5165\u4e86 micro-shard \u7684 consistent hashing \u7684\u65b9\u6cd5\u3002\u4e3b\u8981\u5b9e\u73b0\u5982\u4e0b\u7684\u4e09\u4e2a\u51fd\u6570\uff1a<\/p>\n<ol>\n<li>create(int n, int k)<\/li>\n<li>addMachine(int machine_id) \/\/ add a new machine, return a list of shard ids.<\/li>\n<li>getMachineIdByHashCode(int hashcode) \/\/ return machine id<\/li>\n<\/ol>\n<p>\u5728\u7ebf\u8bc4\u6d4b\u5730\u5740: https:\/\/www.lintcode.com\/problem\/consistent-hashing-ii\/?utm_source=sc-v2ex-fks<\/p>\n<p>[\u6837\u4f8b] \u6837\u4f8b 1:<\/p>\n<pre><code>\u8f93\u5165:   create(100, 3)   addMachine(1)   getMachineIdByHashCode(4)   addMachine(2)   getMachineIdByHashCode(61)   getMachineIdByHashCode(91) \u8f93\u51fa:    [77,83,86]   1   [15,35,93]   1   2 <\/code><\/pre>\n<p>\u6837\u4f8b 2:<\/p>\n<pre><code>\u8f93\u5165:    create(10, 5)   addMachine(1)   getMachineIdByHashCode(4)   addMachine(2)   getMachineIdByHashCode(0)   getMachineIdByHashCode(1)   getMachineIdByHashCode(2)   getMachineIdByHashCode(3)   getMachineIdByHashCode(4)   getMachineIdByHashCode(5) \u8f93\u51fa:    [2,3,5,6,7]   1   [0,1,4,8,9]   2   2   1   1   2   1 <\/code><\/pre>\n<p>[\u9898\u89e3]<\/p>\n<pre><code>public class Solution {      public int n, k;     public Set&lt;Integer&gt; ids = null;     public Map&lt;Integer, List&lt;Integer&gt;&gt; machines = null;      \/\/ @param n a positive integer     \/\/ @param k a positive integer     \/\/ @return a Solution object     public static Solution create(int n, int k) {         \/\/ Write your code here         Solution solution = new Solution();         solution.n = n;         solution.k = k;         solution.ids = new TreeSet&lt;Integer&gt;();         solution.machines = new HashMap&lt;Integer, List&lt;Integer&gt;&gt;();         return solution;     }      \/\/ @param machine_id an integer     \/\/ @return a list of shard ids     public List&lt;Integer&gt; addMachine(int machine_id) {         \/\/ Write your code here         Random ra = new Random();         List&lt;Integer&gt; random_nums = new ArrayList&lt;Integer&gt;();         for (int i = 0; i &lt; k; ++i) {             int index = ra.nextInt(n);             while (ids.contains(index))                 index = ra.nextInt(n);             ids.add(index);             random_nums.add(index);         }          Collections.sort(random_nums);         machines.put(machine_id, random_nums);         return random_nums;     }      \/\/ @param hashcode an integer     \/\/ @return a machine id     public int getMachineIdByHashCode(int hashcode) {         \/\/ Write your code here         int distance = n + 1;         int machine_id = 0;         for (Map.Entry&lt;Integer, List&lt;Integer&gt;&gt; entry : machines.entrySet()) {             int key = entry.getKey();             List&lt;Integer&gt; random_nums = entry.getValue();             for (Integer num : random_nums) {                 int d = num - hashcode;                 if (d &lt; 0)                     d += n;                 if (d &lt; distance) {                     distance = d;                     machine_id = key;                 }             }         }         return machine_id;     } } <\/code><\/pre>\n<p>[\u5176\u4ed6\u8bed\u8a00\u53c2\u8003\u7b54\u6848] https:\/\/www.lintcode.com\/problem\/consistent-hashing-ii\/?utm_source=sc-v2ex-fks<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>0<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>[lintcode \u9898\u89e3] \u4e00\u81f4\u6027&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100252"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100252"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100252\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100252"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}