{"id":7351,"date":"2020-09-05T22:18:44","date_gmt":"2020-09-06T05:18:44","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7351"},"modified":"2020-09-05T22:19:08","modified_gmt":"2020-09-06T05:19:08","slug":"leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1578-minimum-deletion-cost-to-avoid-repeating-letters\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1578. Minimum Deletion Cost to Avoid Repeating Letters"},"content":{"rendered":"\n<p>Given a&nbsp;string&nbsp;<code>s<\/code>&nbsp;and an array of integers&nbsp;<code>cost<\/code>&nbsp;where&nbsp;<code>cost[i]<\/code>&nbsp;is the cost of&nbsp;deleting&nbsp;the character&nbsp;<code>i<\/code>&nbsp;in&nbsp;<code>s<\/code>.<\/p>\n\n\n\n<p>Return the minimum cost of deletions&nbsp;such that there are no two identical letters next to each other.<\/p>\n\n\n\n<p>Notice that you will delete the chosen characters at the same time, in other words, after deleting a character, the costs of deleting&nbsp;other characters will not change.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abaac\", cost = [1,2,3,4,5]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Delete the letter \"a\" with cost 3 to get \"abac\" (String without two identical letters next to each other).\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abc\", cost = [1,2,3]\n<strong>Output:<\/strong> 0\n<strong>Explanation:<\/strong> You don't need to delete any character because there are no identical letters next to each other.\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> s = \"aabaa\", cost = [1,2,3,4,1]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> Delete the first and the last character, getting the string (\"aba\").\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s.length == cost.length<\/code><\/li><li><code>1 &lt;= s.length, cost.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= cost[i] &lt;=&nbsp;10^4<\/code><\/li><li><code>s<\/code>&nbsp;contains only lowercase English letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Group by group<\/strong><\/h2>\n\n\n\n<p>For a group of same letters, delete all expect the one with the highest cost.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int minCost(string s, vector<int>& cost) {\n    int t = cost[0];\n    int m = cost[0];\n    int ans = 0;\n    for (int i = 1; i < s.length(); ++i) {\n      if (s[i] != s[i - 1]) {\n        ans += t - m;\n        t = m = 0;\n      }\n      t += cost[i];\n      m = max(m, cost[i]);\n    }\n    return ans + (t - m);\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python3\">\nclass Solution:\n  def minCost(self, s: str, cost: List[int]) -> int:\n    s = '*' + s + '*'\n    cost = [0] + cost + [0]\n    ans = t = m = 0    \n    for i in range(1, len(s)):\n      if s[i] != s[i - 1]:\n        ans += t - m\n        t = m = 0\n      t += cost[i]\n      m = max(m, cost[i])\n    return ans\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;string&nbsp;s&nbsp;and an array of integers&nbsp;cost&nbsp;where&nbsp;cost[i]&nbsp;is the cost of&nbsp;deleting&nbsp;the character&nbsp;i&nbsp;in&nbsp;s. Return the minimum cost of deletions&nbsp;such that there are no two identical letters next to&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[88,306,177,4],"class_list":["post-7351","post","type-post","status-publish","format-standard","hentry","category-string","tag-greedy","tag-group","tag-medium","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7351","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=7351"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7351\/revisions"}],"predecessor-version":[{"id":7353,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7351\/revisions\/7353"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}