返回介绍

solution / 2700-2799 / 2769.Find the Maximum Achievable Number / README_EN

发布于 2024-06-17 01:03:00 字数 3037 浏览 0 评论 0 收藏 0

2769. Find the Maximum Achievable Number

中文文档

Description

You are given two integers, num and t.

An integer x is called achievable if it can become equal to num after applying the following operation no more than t times:

  • Increase or decrease x by 1, and simultaneously increase or decrease num by 1.

Return _the maximum possible achievable number_. It can be proven that there exists at least one achievable number.

 

Example 1:

Input: num = 4, t = 1
Output: 6
Explanation: The maximum achievable number is x = 6; it can become equal to num after performing this operation:
1- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5. 
It can be proven that there is no achievable number larger than 6.

Example 2:

Input: num = 3, t = 2
Output: 7
Explanation: The maximum achievable number is x = 7; after performing these operations, x will equal num: 
1- Decrease x by 1, and increase num by 1. Now, x = 6 and num = 4.
2- Decrease x by 1, and increase num by 1. Now, x = 5 and num = 5.
It can be proven that there is no achievable number larger than 7.

 

Constraints:

  • 1 <= num, t <= 50

Solutions

Solution 1: Mathematics

Notice that every time we can decrease $x$ by $1$ and increase $num$ by $1$, the difference between $x$ and $num$ will decrease by $2$, and we can do this operation at most $t$ times, so the maximum reachable number is $num + t \times 2$.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

class Solution:
  def theMaximumAchievableX(self, num: int, t: int) -> int:
    return num + t * 2
class Solution {
  public int theMaximumAchievableX(int num, int t) {
    return num + t * 2;
  }
}
class Solution {
public:
  int theMaximumAchievableX(int num, int t) {
    return num + t * 2;
  }
};
func theMaximumAchievableX(num int, t int) int {
  return num + t*2
}
function theMaximumAchievableX(num: number, t: number): number {
  return num + t * 2;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文