Skip to content

Commit 52d9685

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (34.56%), Memory - 18.1 MB (7.56%)
1 parent 4112973 commit 52d9685

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<p>You are given a <strong>0-indexed</strong> array <code>nums</code> of size <code>n</code> consisting of <strong>non-negative</strong> integers.</p>
2+
3+
<p>You need to apply <code>n - 1</code> operations to this array where, in the <code>i<sup>th</sup></code> operation (<strong>0-indexed</strong>), you will apply the following on the <code>i<sup>th</sup></code> element of <code>nums</code>:</p>
4+
5+
<ul>
6+
<li>If <code>nums[i] == nums[i + 1]</code>, then multiply <code>nums[i]</code> by <code>2</code> and set <code>nums[i + 1]</code> to <code>0</code>. Otherwise, you skip this operation.</li>
7+
</ul>
8+
9+
<p>After performing <strong>all</strong> the operations, <strong>shift</strong> all the <code>0</code>&#39;s to the <strong>end</strong> of the array.</p>
10+
11+
<ul>
12+
<li>For example, the array <code>[1,0,2,0,0,1]</code> after shifting all its <code>0</code>&#39;s to the end, is <code>[1,2,1,0,0,0]</code>.</li>
13+
</ul>
14+
15+
<p>Return <em>the resulting array</em>.</p>
16+
17+
<p><strong>Note</strong> that the operations are applied <strong>sequentially</strong>, not all at once.</p>
18+
19+
<p>&nbsp;</p>
20+
<p><strong class="example">Example 1:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> nums = [1,2,2,1,1,0]
24+
<strong>Output:</strong> [1,4,2,0,0,0]
25+
<strong>Explanation:</strong> We do the following operations:
26+
- i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
27+
- i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,<strong><u>4</u></strong>,<strong><u>0</u></strong>,1,1,0].
28+
- i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
29+
- i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,<strong><u>2</u></strong>,<strong><u>0</u></strong>,0].
30+
- i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,<strong><u>0</u></strong>,<strong><u>0</u></strong>].
31+
After that, we shift the 0&#39;s to the end, which gives the array [1,4,2,0,0,0].
32+
</pre>
33+
34+
<p><strong class="example">Example 2:</strong></p>
35+
36+
<pre>
37+
<strong>Input:</strong> nums = [0,1]
38+
<strong>Output:</strong> [1,0]
39+
<strong>Explanation:</strong> No operation can be applied, we just shift the 0 to the end.
40+
</pre>
41+
42+
<p>&nbsp;</p>
43+
<p><strong>Constraints:</strong></p>
44+
45+
<ul>
46+
<li><code>2 &lt;= nums.length &lt;= 2000</code></li>
47+
<li><code>0 &lt;= nums[i] &lt;= 1000</code></li>
48+
</ul>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Approach 1: Brute Force Simulation
2+
3+
# Time: O(n)
4+
# Space: O(1)
5+
6+
class Solution:
7+
def applyOperations(self, nums: List[int]) -> List[int]:
8+
n = len(nums)
9+
modified_nums = []
10+
11+
for index in range(0, n - 1):
12+
if nums[index] == nums[index + 1] and nums[index] != 0:
13+
nums[index] *= 2
14+
nums[index + 1] = 0
15+
16+
for num in nums:
17+
if num != 0:
18+
modified_nums.append(num)
19+
20+
while len(modified_nums) < n:
21+
modified_nums.append(0)
22+
23+
return modified_nums
24+

0 commit comments

Comments
 (0)