Skip to content

Commit

Permalink
update: 添加问题“3264.K次乘运算后的最终数组I”的代码和题解
Browse files Browse the repository at this point in the history
  • Loading branch information
LetMeFly666 committed Dec 13, 2024
1 parent 39ee7ec commit a6db8cc
Show file tree
Hide file tree
Showing 10 changed files with 301 additions and 173 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
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)
14 changes: 11 additions & 3 deletions Solutions/Other-Accumulation-SomeTips.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ git clone --branch paper --single-branch [email protected]:LetMeFly666/SecFFT.git
git reset --hard/--soft/--mixed HEAD^
```

<font color="red">下面先别看了,写的不对</font>
其中:

其中`--hard`会强制变成上个版本(工作区暂存区清空),`--soft`会将相对上一个版本的变化放到暂存区,`--mixed`(默认选项)会将相对上一个版本的变化放到工作区。
+ `--hard`会强制变成上个版本(工作区暂存区清空)
+ `--soft`会将相对上一个版本的变化保留到暂存区和工作区(已经add到暂存区过的变化还在暂存区 未add的变化还在工作区)
+ `--mixed`(默认选项)会将相对上一个版本的变化全部放到工作区。

另一个解释版本:

+ `--hard`:完全回退提交,丢弃暂存区和文件的所有修改。
+ `--soft`:回退提交,但保留文件和暂存区的修改。
+ `--mixed`:回退提交,丢弃暂存区的修改,但保留文件的修改。

举个例子:

Expand Down Expand Up @@ -134,7 +142,7 @@ git reset --hard/--soft/--mixed HEAD^
>
> 注意Windows系统中`cmd`中的`^`大概是连接符的意思,可以使用`git reset --hard "HEAD^"``git reset --hard HEAD"^"``git reset --hard HEAD^^`来表示`HEAD^`
>
> 请注意,如果有未跟踪的内容(例如`echo 4 > 4`但是不`git add`),那么无论`git reset`时传递哪个参数,文件`4`都会原封不动地躺在工作区(这是因为历史记录中也没有文件`4` TODO: 历史记录中存在文件4
> 如果有****未跟踪的内容(例如`echo 4 > 4`但是不`git add`),那么无论`git reset`时传递哪个参数,文件`4`都会原封不动地躺在工作区(这是因为历史记录中也没有文件`4`
#### 查看日志/commit记录
Expand Down
8 changes: 7 additions & 1 deletion Solutions/Other-English-LearningNotes-SomeWords.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ tags: [其他, 知识, 英语, Notes]
|inlet|n. 水湾,小港,注入口||||
|<font color="#28bea0" title="二次复习">drench</font>|v. 使湿透<br/>n. 弄湿,雨淋,浸渍液,(制革时浸泡熟皮的)脱灰水|
|||
|inundate|v. 淹没,使不胜负荷,使应接不暇,泛滥|
|<font color="#28bea0" title="二次复习">inundate</font>|v. 淹没,使不胜负荷,使应接不暇,泛滥|
|thrash|v. 抽打,痛打,猛烈扭动,彻底击败|
|shovel|n. 铲,铲斗,一铲的量<br/>v. 铲,铲起|
|deceitful|adj. 不诚实的,骗人的|
Expand Down Expand Up @@ -857,6 +857,12 @@ tags: [其他, 知识, 英语, Notes]
|clown|n. 丑角,小丑,蠢货,笨蛋<br/>v. 做出蠢相,扮小丑|
|triangular|adj. 三角形的,涉及三方的|
|Marxist|adj. 马克思主义的<br/>n. 马克思主义者|
|||
|dart|v. 急冲,突进,投掷(标枪等),放射<br/>n. 标枪,箭,刺,镳|
|disgrace|n. 耻辱,丢脸,不光彩<br/>v. 使丢脸,使蒙羞,使名誉扫地|
|monopolize|v. 垄断,独占,包办,占去(某人的大部分注意力/时间)|
|exasperation|n. 愤怒,激化,加剧|
|layday|n. 约定装卸日(航运合同中的一个名词)|

<p class="wordCounts">单词收录总数</p>

Expand Down
Loading

0 comments on commit a6db8cc

Please sign in to comment.