Skip to content

feat: add new lc problems #1407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# [2806. 取整购买后的账户余额](https://leetcode.cn/problems/account-balance-after-rounded-purchase)

[English Version](/solution/2800-2899/2806.Account%20Balance%20After%20Rounded%20Purchase/README_EN.md)

## 题目描述

<!-- 这里写题目描述 -->

<p>一开始,你的银行账户里有&nbsp;<code>100</code>&nbsp;块钱。</p>

<p>给你一个整数<code>purchaseAmount</code>&nbsp;,它表示你在一次购买中愿意支出的金额。</p>

<p>在一个商店里,你进行一次购买,实际支出的金额会向 <strong>最近</strong>&nbsp;的&nbsp;<code>10</code>&nbsp;的 <strong>倍数</strong>&nbsp;取整。换句话说,你实际会支付一个&nbsp;<strong>非负</strong>&nbsp;金额&nbsp;<code>roundedAmount</code>&nbsp;,满足&nbsp;<code>roundedAmount</code>&nbsp;是&nbsp;<code>10</code>&nbsp;的倍数且&nbsp;<code>abs(roundedAmount - purchaseAmount)</code>&nbsp;的值 <strong>最小</strong>&nbsp;。</p>

<p>如果存在多于一个最接近的 <code>10</code>&nbsp;的倍数,<strong>较大的倍数</strong>&nbsp;是你的实际支出金额。</p>

<p>请你返回一个整数,表示你在愿意支出金额为<em>&nbsp;</em><code>purchaseAmount</code><em>&nbsp;</em>块钱的前提下,购买之后剩下的余额。</p>

<p><strong>注意:</strong> <code>0</code>&nbsp;也是&nbsp;<code>10</code>&nbsp;的倍数。</p>

<p>&nbsp;</p>

<p><strong>示例 1:</strong></p>

<pre><b>输入:</b>purchaseAmount = 9
<b>输出:</b>90
<b>解释:</b>这个例子中,最接近 9 的 10 的倍数是 10 。所以你的账户余额为 100 - 10 = 90 。
</pre>

<p><strong>示例 2:</strong></p>

<pre><b>输入:</b>purchaseAmount = 15
<b>输出:</b>80
<b>解释:</b>这个例子中,有 2 个最接近 15 的 10 的倍数:10 和 20,较大的数 20 是你的实际开销。
所以你的账户余额为 100 - 20 = 80 。
</pre>

<p>&nbsp;</p>

<p><strong>提示:</strong></p>

<ul>
<li><code>0 &lt;= purchaseAmount &lt;= 100</code></li>
</ul>

## 解法

<!-- 这里可写通用的实现逻辑 -->

**方法一:枚举 + 模拟**

我们在 $[0, 100]$ 的范围内枚举所有的 $10$ 的倍数,然后找到与 `purchaseAmount` 最接近的那个数,记为 $x$,那么答案就是 $100 - x$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
diff, x = 100, 0
for y in range(100, -1, -10):
if (t := abs(y - purchaseAmount)) < diff:
diff = t
x = y
return 100 - x
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
public int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
}
```

### **C++**

```cpp
class Solution {
public:
int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
};
```

### **Go**

```go
func accountBalanceAfterPurchase(purchaseAmount int) int {
diff, x := 100, 0
for y := 100; y >= 0; y -= 10 {
t := abs(y - purchaseAmount)
if t < diff {
diff = t
x = y
}
}
return 100 - x
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**

```ts
function accountBalanceAfterPurchase(purchaseAmount: number): number {
let [diff, x] = [100, 0];
for (let y = 100; y >= 0; y -= 10) {
const t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# [2806. Account Balance After Rounded Purchase](https://leetcode.com/problems/account-balance-after-rounded-purchase)

[中文文档](/solution/2800-2899/2806.Account%20Balance%20After%20Rounded%20Purchase/README.md)

## Description

<p>Initially, you have a bank account balance of <code>100</code> dollars.</p>

<p>You are given an integer <code>purchaseAmount</code> representing the amount you will spend on a purchase in dollars.</p>

<p>At the store where you will make the purchase, the purchase amount is rounded to the <strong>nearest multiple</strong> of <code>10</code>. In other words, you pay a <strong>non-negative</strong> amount, <code>roundedAmount</code>, such that <code>roundedAmount</code> is a multiple of <code>10</code> and <code>abs(roundedAmount - purchaseAmount)</code> is <strong>minimized</strong>.</p>

<p>If there is more than one nearest multiple of <code>10</code>, the <strong>largest multiple</strong> is chosen.</p>

<p>Return <em>an integer denoting your account balance after making a purchase worth </em><code>purchaseAmount</code><em> dollars from the store.</em></p>

<p><strong>Note:</strong> <code>0</code> is considered to be a multiple of <code>10</code> in this problem.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> purchaseAmount = 9
<strong>Output:</strong> 90
<strong>Explanation:</strong> In this example, the nearest multiple of 10 to 9 is 10. Hence, your account balance becomes 100 - 10 = 90.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> purchaseAmount = 15
<strong>Output:</strong> 80
<strong>Explanation:</strong> In this example, there are two nearest multiples of 10 to 15: 10 and 20. So, the larger multiple, 20, is chosen.
Hence, your account balance becomes 100 - 20 = 80.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li><code>0 &lt;= purchaseAmount &lt;= 100</code></li>
</ul>

## Solutions

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
diff, x = 100, 0
for y in range(100, -1, -10):
if (t := abs(y - purchaseAmount)) < diff:
diff = t
x = y
return 100 - x
```

### **Java**

```java
class Solution {
public int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
}
```

### **C++**

```cpp
class Solution {
public:
int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
};
```

### **Go**

```go
func accountBalanceAfterPurchase(purchaseAmount int) int {
diff, x := 100, 0
for y := 100; y >= 0; y -= 10 {
t := abs(y - purchaseAmount)
if t < diff {
diff = t
x = y
}
}
return 100 - x
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **TypeScript**

```ts
function accountBalanceAfterPurchase(purchaseAmount: number): number {
let [diff, x] = [100, 0];
for (let y = 100; y >= 0; y -= 10) {
const t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
```

### **...**

```

```

<!-- tabs:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
func accountBalanceAfterPurchase(purchaseAmount int) int {
diff, x := 100, 0
for y := 100; y >= 0; y -= 10 {
t := abs(y - purchaseAmount)
if t < diff {
diff = t
x = y
}
}
return 100 - x
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public int accountBalanceAfterPurchase(int purchaseAmount) {
int diff = 100, x = 0;
for (int y = 100; y >= 0; y -= 10) {
int t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def accountBalanceAfterPurchase(self, purchaseAmount: int) -> int:
diff, x = 100, 0
for y in range(100, -1, -10):
if (t := abs(y - purchaseAmount)) < diff:
diff = t
x = y
return 100 - x
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function accountBalanceAfterPurchase(purchaseAmount: number): number {
let [diff, x] = [100, 0];
for (let y = 100; y >= 0; y -= 10) {
const t = Math.abs(y - purchaseAmount);
if (t < diff) {
diff = t;
x = y;
}
}
return 100 - x;
}
Loading