Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion README.md

This file was deleted.

27 changes: 27 additions & 0 deletions arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Arrays

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

На каждую задачу отдельный Pull Request

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vivelapaix разделил на три ветки.

+ [Subarray Sum Equals K](#subarray-sum-equals-k)

## Subarray sum equals k

https://leetcode.com/problems/subarray-sum-equals-k/

```python
from collections import defaultdict


class Solution(object):
def subarraySum(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""

_list, _dict, sums_count = [0] + [sum(nums[:index + 1]) for index in range(len(nums))], defaultdict(int), 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Придется упростить через кумулятивную сумму. Слишком сложно да и еще с подсчетом сумм

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я хотел добавить импорт, но leetcode как-то не может это сделать, поэтому переписал в виде генератора списка. Можно сделать и через reduce, наверное. С reduce, может, попроще станет, стоит ли?

for i in range(len(nums)):
_dict[_list[i]] += 1
sums_count += _dict[_list[i + 1] - k]
return sums_count

```