Skip to content

Commit

Permalink
april 6
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSpiridonov committed Apr 6, 2021
1 parent d12d02c commit 32ea6eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions Challenges/2021/April-LeetCoding-Challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ None
|32.|[Longest Valid Parentheses](https://leetcode.com/problems/longest-valid-parentheses/)|~~Python~~|Hard|
|622.|[Design Circular Queue](https://leetcode.com/problems/design-circular-queue/)|[Python](/Medium/622.DesignCircularQueue.py)|Medium|
|775.|[Global and Local Inversions](https://leetcode.com/problems/global-and-local-inversions/)|[Python](/Medium/775.GlobalandLocalInversions.py)|Medium|
|1551.|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/)|[Python](/Medium/1551.MinimumOperationstoMakeArrayEqual.py)|Medium|

## License
The code is open-source and licensed under the [MIT License](/LICENSE).
47 changes: 47 additions & 0 deletions Medium/1551.MinimumOperationstoMakeArrayEqual.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
You have an array arr of length n where arr[i] = (2 * i) + 1
for all valid values of i (i.e. 0 <= i < n).
In one operation, you can select two indices x and y
where 0 <= x, y < n and subtract 1 from arr[x] and add
1 to arr[y] (i.e. perform arr[x] -=1 and arr[y] += 1).
The goal is to make all the elements of the array equal.
It is guaranteed that all the elements of the array can
be made equal using some operations.
Given an integer n, the length of the array. Return the
minimum number of operations needed to make all the
elements of arr equal.
Example:
Input: n = 3
Output: 2
Explanation: arr = [1, 3, 5]
First operation choose x = 2 and y = 0,
this leads arr to be [2, 3, 4]
In the second operation choose x = 2 and
y = 0 again, thus arr = [3, 3, 3].
Example:
Input: n = 6
Output: 9
Constraints:
- 1 <= n <= 10^4
'''
#Difficulty: Medium
#301 / 301 test cases passed.
#Runtime: 76 ms
#Memory Usage: 14.3 MB

#Runtime: 76 ms, faster than 38.50% of Python3 online submissions for Minimum Operations to Make Array Equal.
#Memory Usage: 14.3 MB, less than 50.38% of Python3 online submissions for Minimum Operations to Make Array Equal.

class Solution:
def minOperations(self, n: int) -> int:
x = (2 + 2*(n-1)) // 2
y = 0
for i in range(n//2):
y += x - (i*2+1)
return y
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python solutions of LeetCode problems.
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-530%2F1637-orange)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-531%2F1637-orange)&nbsp;
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
<br><br>
Expand All @@ -22,7 +22,7 @@ In this repository provided my Python solutions of LeetCode problems.
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 27/31
- [February LeetCoding Challenge](/Challenges/2021/February-LeetCoding-Challenge.md) - 23/28
- [March LeetCoding Challenge](/Challenges/2021/March-LeetCoding-Challenge.md) - 23/31
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 3/30
- [April LeetCoding Challenge](/Challenges/2021/April-LeetCoding-Challenge.md) - 4/30
<br><br>
## Solutions
*P.S. If you like this, please leave me a star.*
Expand Down Expand Up @@ -525,6 +525,7 @@ In this repository provided my Python solutions of LeetCode problems.
|1544.|[Make The String Great](https://leetcode.com/problems/make-the-string-great/)|[Python](/Easy/1544.MakeTheStringGreat(string).py)|Easy|`String` in-place|
|1545.|[Find Kth Bit in Nth Binary String](https://leetcode.com/problems/find-kth-bit-in-nth-binary-string/)|[Python](/Medium/1545.FindKthBitinNthBinaryString.py)|Medium|Brute Force|
|1550.|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/)|[Python](/Easy/1550.ThreeConsecutiveOdds.py)|Easy|simple counter|
|1551.|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/)|[Python](/Medium/1551.MinimumOperationstoMakeArrayEqual.py)|Medium||
|1556.|[Thousand Separator](https://leetcode.com/problems/thousand-separator/)|[Python](/Easy/1556.ThousandSeparator.py)|Easy|`String`, `List`|
|1557.|[Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/)|[Python](/Medium/1557.MinimumNumberofVerticestoReachAllNodes.py)|Medium|`Set`, kinda Brute Force|
|1561.|[Maximum Number of Coins You Can Get](https://leetcode.com/problems/maximum-number-of-coins-you-can-get/)|[Python](/Medium/1561.MaximumNumberofCoinsYouCanGet.py)|Medium||
Expand Down

0 comments on commit 32ea6eb

Please sign in to comment.