{"id":386205,"date":"2024-06-29T06:34:18","date_gmt":"2024-06-29T06:34:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=386205"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2024-06-29T06:34:18","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=386205","title":{"rendered":"<span>LeetCode 956 (Hard). Solution of the day. Tallest Billboard. Swift. DP<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Description<\/h3>\n<p>You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.<\/p>\n<p>You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.<\/p>\n<p>Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.<\/p>\n<p><strong>Example 1<\/strong>:<br \/>Input: rods = [1,2,3,6]<br \/>Output: 6<br \/>Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.<\/p>\n<p><strong>Example 2<\/strong>:<br \/>Input: rods = [1,2,3,4,5,6]<br \/>Output: 10<br \/>Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.<\/p>\n<p><strong>Example 3<\/strong>:<br \/>Input: rods = [1,2]<br \/>Output: 0<br \/>Explanation: The billboard cannot be supported, so we return 0.<\/p>\n<p><strong>Constraints<\/strong>:<br \/>1 &lt;= rods.length &lt;= 20<br \/>1 &lt;= rods[i] &lt;= 1000<br \/>sum(rods[i]) &lt;= 5000<\/p>\n<h3>Approach<\/h3>\n<p>The code uses dynamic programming to solve the problem. It maintains a dictionary dp, where the keys represent the possible height differences between the two billboards, and the values represent the maximum sum of heights achieved for each height difference.<\/p>\n<p>The code iterates through each rod in the given input rods. For each rod i, it creates a new dictionary cur to store the updated values for dp. Then, it iterates through the existing entries in dp and updates the values in cur based on three cases:<\/p>\n<ol>\n<li>\n<p>Adding the current rod i to the same height difference: cur[sum + i] = max(dp[sum]! + i, cur[sum + i, default: 0])<\/p>\n<\/li>\n<li>\n<p>Keeping the same height difference: cur[sum] = max(dp[sum]!, cur[sum, default: 0])<\/p>\n<\/li>\n<li>\n<p>Subtracting the current rod i from the height difference: cur[sum &#8212; i] = max(dp[sum]!, cur[sum &#8212; i, default: 0])<\/p>\n<\/li>\n<\/ol>\n<p>After iterating through all the rods, the final result is obtained from dp[0], which represents the maximum possible sum of heights for a height difference of 0 (i.e., the two billboards have equal heights).<\/p>\n<h3>Complexity<\/h3>\n<p><strong>Time complexity<\/strong>: O(n * m).<\/p>\n<p>Code (Swift)<\/p>\n<pre><code class=\"swift\">class Solution {     func tallestBillboard(_ rods: [Int]) -> Int {         var dp = [0: 0]          for i in rods {             var cur = [Int: Int]()             for (sum, height) in dp {                 cur[sum + i] = max(dp[sum]! + i, cur[sum + i, default: 0])                 cur[sum] = max(dp[sum]!, cur[sum, default: 0])                 cur[sum - i] = max(dp[sum]!, cur[sum - i, default: 0])             }             dp = cur         }          return dp[0, default: 0]     } }<\/code><\/pre>\n<p>Sources:\u00a0<a href=\"https:\/\/github.com\/sergeyleschev\/leetcode-swift\/blob\/main\/0501-1000\/0956.%20Tallest%20Billboard.swift\" rel=\"noopener noreferrer nofollow\"><u>Github<\/u><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/743654\/\"> https:\/\/habr.com\/ru\/articles\/743654\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h3>Description<\/h3>\n<p>You are installing a billboard and want it to have the largest height. The billboard will have two steel supports, one on each side. Each steel support must be an equal height.<\/p>\n<p>You are given a collection of rods that can be welded together. For example, if you have rods of lengths 1, 2, and 3, you can weld them together to make a support of length 6.<\/p>\n<p>Return the largest possible height of your billboard installation. If you cannot support the billboard, return 0.<\/p>\n<p><strong>Example 1<\/strong>:<br \/>Input: rods = [1,2,3,6]<br \/>Output: 6<br \/>Explanation: We have two disjoint subsets {1,2,3} and {6}, which have the same sum = 6.<\/p>\n<p><strong>Example 2<\/strong>:<br \/>Input: rods = [1,2,3,4,5,6]<br \/>Output: 10<br \/>Explanation: We have two disjoint subsets {2,3,5} and {4,6}, which have the same sum = 10.<\/p>\n<p><strong>Example 3<\/strong>:<br \/>Input: rods = [1,2]<br \/>Output: 0<br \/>Explanation: The billboard cannot be supported, so we return 0.<\/p>\n<p><strong>Constraints<\/strong>:<br \/>1 &lt;= rods.length &lt;= 20<br \/>1 &lt;= rods[i] &lt;= 1000<br \/>sum(rods[i]) &lt;= 5000<\/p>\n<h3>Approach<\/h3>\n<p>The code uses dynamic programming to solve the problem. It maintains a dictionary dp, where the keys represent the possible height differences between the two billboards, and the values represent the maximum sum of heights achieved for each height difference.<\/p>\n<p>The code iterates through each rod in the given input rods. For each rod i, it creates a new dictionary cur to store the updated values for dp. Then, it iterates through the existing entries in dp and updates the values in cur based on three cases:<\/p>\n<ol>\n<li>\n<p>Adding the current rod i to the same height difference: cur[sum + i] = max(dp[sum]! + i, cur[sum + i, default: 0])<\/p>\n<\/li>\n<li>\n<p>Keeping the same height difference: cur[sum] = max(dp[sum]!, cur[sum, default: 0])<\/p>\n<\/li>\n<li>\n<p>Subtracting the current rod i from the height difference: cur[sum &#8212; i] = max(dp[sum]!, cur[sum &#8212; i, default: 0])<\/p>\n<\/li>\n<\/ol>\n<p>After iterating through all the rods, the final result is obtained from dp[0], which represents the maximum possible sum of heights for a height difference of 0 (i.e., the two billboards have equal heights).<\/p>\n<h3>Complexity<\/h3>\n<p><strong>Time complexity<\/strong>: O(n * m).<\/p>\n<p>Code (Swift)<\/p>\n<pre><code class=\"swift\">class Solution {     func tallestBillboard(_ rods: [Int]) -> Int {         var dp = [0: 0]          for i in rods {             var cur = [Int: Int]()             for (sum, height) in dp {                 cur[sum + i] = max(dp[sum]! + i, cur[sum + i, default: 0])                 cur[sum] = max(dp[sum]!, cur[sum, default: 0])                 cur[sum - i] = max(dp[sum]!, cur[sum - i, default: 0])             }             dp = cur         }          return dp[0, default: 0]     } }<\/code><\/pre>\n<p>Sources:\u00a0<a href=\"https:\/\/github.com\/sergeyleschev\/leetcode-swift\/blob\/main\/0501-1000\/0956.%20Tallest%20Billboard.swift\" rel=\"noopener noreferrer nofollow\"><u>Github<\/u><\/a><\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/743654\/\"> https:\/\/habr.com\/ru\/articles\/743654\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-386205","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=386205"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/386205\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=386205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=386205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=386205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}