Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d521ebb

Browse files
D. J.:
- Added the leetcode problem and solution for 1679
1 parent eef620d commit d521ebb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@
258258
- [1218 Longest Arithmetic Subsequence of Given Difference](https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/description/)
259259
- [1312 Minimum Insertion Steps to Make a String Palindrome](https://leetcode.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/description/)
260260
- [1534 Count Good Triplets](https://leetcode.com/problems/count-good-triplets/description/)
261+
- [1679 Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/description/)
261262
- [1732 Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/description/)
262263
- [1991 Find the Middle Index in Array](https://leetcode.com/problems/find-the-middle-index-in-array/description/)
263264
- [1922 Count Good Numbers](https://leetcode.com/problems/count-good-numbers/description/)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import collections
2+
from typing import List
3+
4+
5+
class Solution:
6+
"""Base class for all LeetCode Problems."""
7+
8+
def maxOperations(self, nums: List[int], k: int) -> int:
9+
"""
10+
You are given an integer array nums and an integer k.
11+
12+
In one operation, you can pick two numbers from the array whose sum equals k
13+
and remove them from the array.
14+
15+
Return the maximum number of operations you can perform on the array.
16+
"""
17+
# Time Complexity: O(n)
18+
# Space Complexity: O(n)
19+
values = collections.defaultdict(int)
20+
count = 0
21+
for n in nums:
22+
diff = k - n
23+
if diff in values:
24+
count += 1
25+
if values[diff] == 1:
26+
del values[diff]
27+
else:
28+
values[diff] -= 1
29+
else:
30+
values[n] += 1
31+
return count
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._1679_max_number_of_k_sum_pairs import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "k", "expected"],
10+
argvalues=[
11+
([1, 2, 3, 4], 5, 2),
12+
([3, 1, 3, 4, 3], 6, 1),
13+
([2, 5, 4, 4, 1, 3, 4, 4, 1, 4, 4, 1, 2, 1, 2, 2, 3, 2, 4, 2], 3, 4),
14+
],
15+
)
16+
def test_func(nums: List[int], k: int, expected: int):
17+
"""Tests the solution of a LeetCode problem."""
18+
max_operations = Solution().maxOperations(nums, k)
19+
assert max_operations == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /