Skip to content

Commit 115a1e7

Browse files
Sync LeetCode submission Runtime - 119 ms (16.67%), Memory - 24 MB (33.33%)
1 parent eea98c7 commit 115a1e7

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p>
2+
3+
<p>In one operation, you can <strong>increase</strong> or <strong>decrease</strong> any element of <code>nums</code> by 1.</p>
4+
5+
<p>An array is called <strong>modulo alternating</strong> if there exist two <strong>distinct</strong> integers <code>x</code> and <code>y</code> (<code>0 &lt;= x, y &lt; k</code>) such that:</p>
6+
7+
<ul>
8+
<li>For every <strong>even</strong> index <code>i</code>, <code>nums[i] % k == x</code></li>
9+
<li>For every <strong>odd</strong> index <code>i</code>, <code>nums[i] % k == y</code></li>
10+
</ul>
11+
12+
<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> <strong>modulo alternating</strong>.</p>
13+
14+
<p>&nbsp;</p>
15+
<p><strong class="example">Example 1:</strong></p>
16+
17+
<div class="example-block">
18+
<p><strong>Input:</strong> <span class="example-io">nums = [1,4,2,8], k = 3</span></p>
19+
20+
<p><strong>Output:</strong> <span class="example-io">2</span></p>
21+
22+
<p><strong>Explanation:</strong></p>
23+
24+
<ul>
25+
<li>Let&#39;s choose <code>x = 1</code> for even indices and <code>y = 2</code> for odd indices.</li>
26+
<li>Perform the following operations:
27+
<ul>
28+
<li>Increment <code>nums[1] = 4</code> by 1, giving <code>nums = [1, 5, 2, 8]</code>.</li>
29+
<li>Decrement <code>nums[2] = 2</code> by 1, giving <code>nums = [1, 5, 1, 8]</code>.</li>
30+
</ul>
31+
</li>
32+
<li>Now, for even indices, <code>nums[i] % k = 1</code>, and for odd indices, <code>nums[i] % k = 2</code>.</li>
33+
<li>Thus, the total number of operations required is 2.</li>
34+
</ul>
35+
</div>
36+
37+
<p><strong class="example">Example 2:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1], k = 3</span></p>
41+
42+
<p><strong>Output:</strong> <span class="example-io">1</span></p>
43+
44+
<p><strong>Explanation:</strong></p>
45+
46+
<ul>
47+
<li>Incrementing <code>nums[1]</code> by 1 gives <code>nums = [1, 2, 1]</code>, which satisfies the condition with <code>x = 1</code> and <code>y = 2</code>.</li>
48+
<li>Thus, the total number of operations required is 1.</li>
49+
</ul>
50+
</div>
51+
52+
<p>&nbsp;</p>
53+
<p><strong>Constraints:</strong></p>
54+
55+
<ul>
56+
<li><code>1 &lt;= nums.length &lt;= 100</code></li>
57+
<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
58+
<li><code>2 &lt;= k &lt;= 100</code></li>
59+
</ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int minOperations(vector<int>& nums, int k) {
4+
int mx, mn;
5+
int n = nums.size();
6+
int ans = 1e9;
7+
for (int x=0; x<k; x++) {
8+
for (int y=0; y<k; y++) {
9+
if (x == y) continue;
10+
int cur = 0;
11+
for (int i=0; i<n; i++) {
12+
int val = nums[i] % k;
13+
if (i%2) {
14+
mn = min(y, val);
15+
mx = max(y, val);
16+
}
17+
else {
18+
mn = min(x, val);
19+
mx = max(x, val);
20+
}
21+
int ops = min(mx - mn, mn + k - mx);
22+
cur += ops;
23+
}
24+
ans = min(ans, cur);
25+
}
26+
}
27+
return ans;
28+
}
29+
};

0 commit comments

Comments
 (0)