Skip to content

Commit

Permalink
Merge pull request #651 from LetMeFly666/3264
Browse files Browse the repository at this point in the history
添加问题“3264.K次乘运算后的最终数组I”的代码和题解
  • Loading branch information
LetMeFly666 authored Dec 13, 2024
2 parents 6b91e7c + a6db8cc commit 3bc3cca
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 189 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* @Author: LetMeFly
* @Date: 2024-12-13 09:32:29
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-12-13 09:33:35
*/
#ifdef _WIN32
#include "_[1,2]toVector.h"
#endif

class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
while (k--) {
int m = nums[0], loc = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] < m) {
m = nums[i];
loc = i;
}
}
nums[loc] *= multiplier;
}
return nums;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* @Author: LetMeFly
* @Date: 2024-12-13 09:40:31
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-12-13 09:42:10
*/
package main

func getFinalState(nums []int, k int, multiplier int) []int {
for round := 0; round < k; round++ {
m, loc := nums[0], 0
for i, val := range nums {
if val < m {
m, loc = val, i
}
}
nums[loc] *= multiplier
}
return nums
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @Author: LetMeFly
* @Date: 2024-12-13 09:37:28
* @LastEditors: LetMeFly.xyz
* @LastEditTime: 2024-12-13 09:40:00
*/
class Solution {
public int[] getFinalState(int[] nums, int k, int multiplier) {
for (int round = 0; round < k; round++) {
int m = nums[0], loc = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < m) {
m = nums[i];
loc = i;
}
}
nums[loc] *= multiplier;
}
return nums;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'''
Author: LetMeFly
Date: 2024-12-13 09:34:55
LastEditors: LetMeFly.xyz
LastEditTime: 2024-12-13 09:36:26
'''
from typing import List

class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
for _ in range(k):
m, loc = nums[0], 0
for i, val in enumerate(nums):
if val < m:
m, loc = val, i
nums[loc] *= multiplier
return nums
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@
|3255.长度为K的子数组的能量值II|中等|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/07/LeetCode%203255.%E9%95%BF%E5%BA%A6%E4%B8%BAK%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84%E7%9A%84%E8%83%BD%E9%87%8F%E5%80%BCII/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143591327" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-power-of-k-size-subarrays-ii/solutions/2980432/letmefly-3255chang-du-wei-k-de-zi-shu-zu-rags/" target="_blank">LeetCode题解</a>|
|3258.统计满足K约束的子字符串数量I|简单|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/12/LeetCode%203258.%E7%BB%9F%E8%AE%A1%E6%BB%A1%E8%B6%B3K%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%95%B0%E9%87%8FI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143726399" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-substrings-that-satisfy-k-constraint-i/solutions/2986598/letmefly-3258tong-ji-man-zu-k-yue-shu-de-fs44/" target="_blank">LeetCode题解</a>|
|3259.超级饮料的最大强化能量|中等|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/11/01/LeetCode%203259.%E8%B6%85%E7%BA%A7%E9%A5%AE%E6%96%99%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%BA%E5%8C%96%E8%83%BD%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/143429899" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-energy-boost-from-two-drinks/solutions/2973620/letmefly-3259chao-ji-yin-liao-de-zui-da-eeusa/" target="_blank">LeetCode题解</a>|
|3264.K次乘运算后的最终数组I|简单|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/solutions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/13/LeetCode%203264.K%E6%AC%A1%E4%B9%98%E8%BF%90%E7%AE%97%E5%90%8E%E7%9A%84%E6%9C%80%E7%BB%88%E6%95%B0%E7%BB%84I/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144442552" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/solutions/3018990/letmefly-3264k-ci-cheng-yun-suan-hou-de-ahxh2/" target="_blank">LeetCode题解</a>|
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Expand Down
22 changes: 11 additions & 11 deletions Solutions/LeetCode 2931.购买物品的最大开销.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ tags: [题解, LeetCode, 困难, 贪心, 数组, 矩阵, 排序, 堆(优先队
直接调用库函数排序就没有利用“每个商家的商品非递增”这一特性,且排序复杂度$mn\log (mn)$ 平均大于 $mn\times m$。

> | $ m $ | $mn\log (mn)\gt mn\times m$ |
> |:-------:|:144428274:|
> | 1 | $$ n \geq 3 $$ |
> | 2 | $$ n \geq 5 $$ |
> | 3 | $$ n \geq 9 $$ |
> | 4 | $$ n \geq 17 $$ |
> | 5 | $$ n \geq 33 $$ |
> | 6 | $$ n \geq 65 $$ |
> | 7 | $$ n \geq 129 $$|
> | 8 | $$ n \geq 257 $$|
> | 9 | $$ n \geq 513 $$|
> | 10 | $$ n \geq 1025 $$ |
> |:-------:|:---:|
> | 1 | $ n \geq 3 $ |
> | 2 | $ n \geq 5 $ |
> | 3 | $ n \geq 9 $ |
> | 4 | $ n \geq 17 $ |
> | 5 | $ n \geq 33 $ |
> | 6 | $ n \geq 65 $ |
> | 7 | $ n \geq 129 $|
> | 8 | $ n \geq 257 $|
> | 9 | $ n \geq 513 $|
> | 10 | $ n \geq 1025 $ |
因此,我们可以使用$m$个指针,每个指针指向这家商店购买到了哪件商品,每次从$m$家里选择最便宜的那件就好。

Expand Down
198 changes: 198 additions & 0 deletions Solutions/LeetCode 3264.K次乘运算后的最终数组I.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
---
title: 3264.K 次乘运算后的最终数组 I
date: 2024-12-13 09:43:07
tags: [题解, LeetCode, 简单, 数组, 数学, 模拟, 堆(优先队列)]
---

# 【LetMeFly】3264.K 次乘运算后的最终数组 I:模拟

力扣题目链接:[https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/](https://leetcode.cn/problems/final-array-state-after-k-multiplication-operations-i/)

<p>给你一个整数数组&nbsp;<code>nums</code>&nbsp;,一个整数&nbsp;<code>k</code>&nbsp;&nbsp;和一个整数&nbsp;<code>multiplier</code>&nbsp;。</p>

<p>你需要对 <code>nums</code>&nbsp;执行 <code>k</code>&nbsp;次操作,每次操作中:</p>

<ul>
<li>找到 <code>nums</code>&nbsp;中的 <strong>最小</strong>&nbsp;值&nbsp;<code>x</code>&nbsp;,如果存在多个最小值,选择最 <strong>前面</strong>&nbsp;的一个。</li>
<li>将 <code>x</code>&nbsp;替换为&nbsp;<code>x * multiplier</code>&nbsp;。</li>
</ul>

<p>请你返回执行完 <code>k</code>&nbsp;次乘运算之后,最终的 <code>nums</code>&nbsp;数组。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [2,1,3,5,6], k = 5, multiplier = 2</span></p>

<p><span class="example-io"><b>输出:</b>[8,4,6,5,6]</span></p>

<p><strong>解释:</strong></p>

<table>
<tbody>
<tr>
<th>操作</th>
<th>结果</th>
</tr>
<tr>
<td>1 次操作后</td>
<td>[2, 2, 3, 5, 6]</td>
</tr>
<tr>
<td>2 次操作后</td>
<td>[4, 2, 3, 5, 6]</td>
</tr>
<tr>
<td>3 次操作后</td>
<td>[4, 4, 3, 5, 6]</td>
</tr>
<tr>
<td>4 次操作后</td>
<td>[4, 4, 6, 5, 6]</td>
</tr>
<tr>
<td>5 次操作后</td>
<td>[8, 4, 6, 5, 6]</td>
</tr>
</tbody>
</table>
</div>

<p><strong class="example">示例 2:</strong></p>

<div class="example-block">
<p><span class="example-io"><b>输入:</b></span>nums = [1,2], k = 3, multiplier = 4</p>

<p><span class="example-io"><b>输出:</b></span>[16,8]</p>

<p><strong>解释:</strong></p>

<table>
<tbody>
<tr>
<th>操作</th>
<th>结果</th>
</tr>
<tr>
<td>1 次操作后</td>
<td>[4, 2]</td>
</tr>
<tr>
<td>2 次操作后</td>
<td>[4, 8]</td>
</tr>
<tr>
<td>3 次操作后</td>
<td>[16, 8]</td>
</tr>
</tbody>
</table>
</div>

<p>&nbsp;</p>

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

<ul>
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li><code>1 &lt;= k &lt;= 10</code></li>
<li><code>1 &lt;= multiplier &lt;= 5</code></li>
</ul>



## 解题方法:模拟

进行$k$次如下操作:

> 每次找到最小值所在位置,将最小值乘以$multiplier$
最后返回$nums$原数组。

+ 时间复杂度$O(len(nums)\times k)$
+ 空间复杂度$O(1)$

### AC代码

#### C++

```cpp
class Solution {
public:
vector<int> getFinalState(vector<int>& nums, int k, int multiplier) {
while (k--) {
int m = nums[0], loc = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[i] < m) {
m = nums[i];
loc = i;
}
}
nums[loc] *= multiplier;
}
return nums;
}
};
```
#### Python
```python
from typing import List
class Solution:
def getFinalState(self, nums: List[int], k: int, multiplier: int) -> List[int]:
for _ in range(k):
m, loc = nums[0], 0
for i, val in enumerate(nums):
if val < m:
m, loc = val, i
nums[loc] *= multiplier
return nums
```

#### Java

```java
class Solution {
public int[] getFinalState(int[] nums, int k, int multiplier) {
for (int round = 0; round < k; round++) {
int m = nums[0], loc = 0;
for (int i = 1; i < nums.length; i++) {
if (nums[i] < m) {
m = nums[i];
loc = i;
}
}
nums[loc] *= multiplier;
}
return nums;
}
}
```

#### Go

```go
package main

func getFinalState(nums []int, k int, multiplier int) []int {
for round := 0; round < k; round++ {
m, loc := nums[0], 0
for i, val := range nums {
if val < m {
m, loc = val, i
}
}
nums[loc] *= multiplier
}
return nums
}
```

> 同步发文于CSDN和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2024/12/13/LeetCode%203264.K%E6%AC%A1%E4%B9%98%E8%BF%90%E7%AE%97%E5%90%8E%E7%9A%84%E6%9C%80%E7%BB%88%E6%95%B0%E7%BB%84I/)哦~
>
> Tisfy:[https://letmefly.blog.csdn.net/article/details/144442552](https://letmefly.blog.csdn.net/article/details/144442552)
Loading

0 comments on commit 3bc3cca

Please sign in to comment.