{"id":7411,"date":"2020-09-23T01:37:56","date_gmt":"2020-09-23T08:37:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7411"},"modified":"2020-09-23T17:51:35","modified_gmt":"2020-09-24T00:51:35","slug":"leetcode-1595-minimum-cost-to-connect-two-groups-of-points","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/dynamic-programming\/leetcode-1595-minimum-cost-to-connect-two-groups-of-points\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1595. Minimum Cost to Connect Two Groups of Points"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1595. Minimum Cost to Connect Two Groups of Points - \u5237\u9898\u627e\u5de5\u4f5c EP358\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/ba-3-B4IMII?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>You are given two groups of points&nbsp;where the first group&nbsp;has&nbsp;<code>size<sub>1<\/sub><\/code>&nbsp;points,&nbsp;the second group&nbsp;has&nbsp;<code>size<sub>2<\/sub><\/code>&nbsp;points,&nbsp;and&nbsp;<code>size<sub>1<\/sub>&nbsp;&gt;=&nbsp;size<sub>2<\/sub><\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>cost<\/code>&nbsp;of the connection between any two points&nbsp;are given in an&nbsp;<code>size<sub>1<\/sub>&nbsp;x&nbsp;size<sub>2<\/sub><\/code>&nbsp;matrix where&nbsp;<code>cost[i][j]<\/code>&nbsp;is the cost of connecting point&nbsp;<code>i<\/code>&nbsp;of the first group and point&nbsp;<code>j<\/code>&nbsp;of the second group. The groups are connected if&nbsp;<strong>each point in both&nbsp;groups is&nbsp;connected to one or more points in the opposite&nbsp;group<\/strong>. In other words, each&nbsp;point in the first group must be connected to at least one point in the second group, and each&nbsp;point in the second group must be connected to at least one point in the first group.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum cost it takes to connect the two groups<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/03\/ex1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [[15, 96], [36, 2]]\n<strong>Output:<\/strong> 17\n<strong>Explanation<\/strong>: The optimal way of connecting the groups is:\n1--A\n2--B\nThis results in a total cost of 17.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/03\/ex2.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [[1, 3, 5], [4, 1, 1], [1, 5, 3]]\n<strong>Output:<\/strong> 4\n<strong>Explanation<\/strong>: The optimal way of connecting the groups is:\n1--A\n2--B\n2--C\n3--A\nThis results in a total cost of 4.\nNote that there are multiple points connected to point 2 in the first group and point A in the second group. This does not matter as there is no limit to the number of points that can be connected. We only care about the minimum total cost.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> cost = [[2, 5, 1], [3, 4, 7], [8, 1, 2], [6, 2, 4], [3, 8, 8]]\n<strong>Output:<\/strong> 10\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>size<sub>1<\/sub>&nbsp;== cost.length<\/code><\/li><li><code>size<sub>2<\/sub>&nbsp;== cost[i].length<\/code><\/li><li><code>1 &lt;=&nbsp;size<sub>1<\/sub>,&nbsp;size<sub>2<\/sub>&nbsp;&lt;= 12<\/code><\/li><li><code>size<sub>1<\/sub>&nbsp;&gt;=&nbsp;size<sub>2<\/sub><\/code><\/li><li><code>0 &lt;= cost[i][j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Bistmask DP<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-1.png\" alt=\"\" class=\"wp-image-7414\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-2.png\" alt=\"\" class=\"wp-image-7415\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2020\/09\/1595-ep358-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>dp[i][s] := min cost to connect first i (1-based) points in group1 and a set of points (represented by a bitmask s) in group2.<\/p>\n\n\n\n<p>ans = dp[m][1 &lt;&lt; n &#8211; 1]<\/p>\n\n\n\n<p>dp[i][s | (1 &lt;&lt; j)] := min(dp[i][s] + cost[i][j], dp[i-1][s] + cost[i][j])<\/p>\n\n\n\n<p>Time complexity: O(m*n*2^n)<br>Space complexity: O(m*2^n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/Bottom up<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int connectTwoGroups(vector<vector<int>>& cost) {\n    constexpr int kInf = 1e9;\n    const int m = cost.size();\n    const int n = cost[0].size();\n    \/\/ dp[i][s] := min cost to connect first i points in group1 \n    \/\/ and points (bitmask s) in group2.\n    vector<vector<int>> dp(m + 1, vector<int>(1 << n, kInf));\n    dp[0][0] = 0;\n    for (int i = 0; i < m; ++i)\n      for (int j = 0; j < n; ++j)\n        for (int s = 0; s < 1 << n; ++s)\n          dp[i + 1][s | (1 << j)] = min({dp[i + 1][s | (1 << j)], \n                                         dp[i + 1][s] + cost[i][j],\n                                         dp[i][s] + cost[i][j]});\n    return dp[m].back();\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two groups of points&nbsp;where the first group&nbsp;has&nbsp;size1&nbsp;points,&nbsp;the second group&nbsp;has&nbsp;size2&nbsp;points,&nbsp;and&nbsp;size1&nbsp;&gt;=&nbsp;size2. The&nbsp;cost&nbsp;of the connection between any two points&nbsp;are given in an&nbsp;size1&nbsp;x&nbsp;size2&nbsp;matrix where&nbsp;cost[i][j]&nbsp;is the cost&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[621,18,217],"class_list":["post-7411","post","type-post","status-publish","format-standard","hentry","category-dynamic-programming","tag-bitmask","tag-dp","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7411"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7411\/revisions"}],"predecessor-version":[{"id":7418,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7411\/revisions\/7418"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}