{"id":10057,"date":"2023-07-11T18:12:33","date_gmt":"2023-07-12T01:12:33","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10057"},"modified":"2023-07-11T18:13:15","modified_gmt":"2023-07-12T01:13:15","slug":"leetcode-2769-find-the-maximum-achievable-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2769-find-the-maximum-achievable-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2769. Find the Maximum Achievable Number"},"content":{"rendered":"\n<p>You are given two integers,&nbsp;<code>num<\/code>&nbsp;and&nbsp;<code>t<\/code>.<\/p>\n\n\n\n<p>An integer&nbsp;<code>x<\/code>&nbsp;is called&nbsp;<strong>achievable<\/strong>&nbsp;if it can become equal to&nbsp;<code>num<\/code>&nbsp;after applying the following operation no more than&nbsp;<code>t<\/code>&nbsp;times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Increase or decrease&nbsp;<code>x<\/code>&nbsp;by&nbsp;<code>1<\/code>, and simultaneously increase or decrease&nbsp;<code>num<\/code>&nbsp;by&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the maximum possible achievable number<\/em>. It can be proven that there exists at least one achievable number.<\/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> num = 4, t = 1\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> The maximum achievable number is x = 6; it can become equal to num after performing this operation:\n1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. \nIt can be proven that there is no achievable number larger than 6.\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> num = 3, t = 2\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong> The maximum achievable number is x = 7; after performing these operations, x will equal num: \n1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.\n2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.\nIt can be proven that there is no achievable number larger than 7.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= num, t&nbsp;&lt;= 50<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Always decrease x and always increase num, the max achievable number  x = num + t * 2<\/p>\n\n\n\n<p>Time complexity: O(1)<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++\">\/\/ Author: Huahua\nclass Solution {\npublic:\n  int theMaximumAchievableX(int num, int t) {\n    return num + t * 2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given two integers,&nbsp;num&nbsp;and&nbsp;t. An integer&nbsp;x&nbsp;is called&nbsp;achievable&nbsp;if it can become equal to&nbsp;num&nbsp;after applying the following operation no more than&nbsp;t&nbsp;times: Increase or decrease&nbsp;x&nbsp;by&nbsp;1, and simultaneously&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88],"class_list":["post-10057","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10057","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=10057"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10057\/revisions"}],"predecessor-version":[{"id":10059,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10057\/revisions\/10059"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}