-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #906 from 0xff-dev/2799
Add solution and test-cases for problem 2799
- Loading branch information
Showing
3 changed files
with
61 additions
and
9 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
leetcode/2701-2800/2799.Count-Complete-Subarrays-in-an-Array/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# [2799.Count Complete Subarrays in an Array][title] | ||
|
||
## Description | ||
You are given an array `nums` consisting of **positive** integers. | ||
|
||
We call a subarray of an array **complete** if the following condition is satisfied: | ||
|
||
- The number of **distinct** elements in the subarray is equal to the number of distinct elements in the whole array. | ||
|
||
Return the number of **complete** subarrays. | ||
|
||
A **subarray** is a contiguous non-empty part of an array. | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: nums = [1,3,1,2,2] | ||
Output: 4 | ||
Explanation: The complete subarrays are the following: [1,3,1,2], [1,3,1,2,2], [3,1,2] and [3,1,2,2]. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: nums = [5,5,5,5] | ||
Output: 10 | ||
Explanation: The array consists only of the integer 5, so any subarray is complete. The number of subarrays that we can choose is 10. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/count-complete-subarrays-in-an-array | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
22 changes: 20 additions & 2 deletions
22
leetcode/2701-2800/2799.Count-Complete-Subarrays-in-an-Array/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,23 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(nums []int) int { | ||
in := make(map[int]struct{}) | ||
for _, n := range nums { | ||
in[n] = struct{}{} | ||
} | ||
distinct := len(in) | ||
ans := 0 | ||
for i := 0; i < len(nums); i++ { | ||
ti := map[int]struct{}{nums[i]: {}} | ||
if len(ti) == distinct { | ||
ans++ | ||
} | ||
for pre := i - 1; pre >= 0; pre-- { | ||
ti[nums[pre]] = struct{}{} | ||
if len(ti) == distinct { | ||
ans++ | ||
} | ||
} | ||
} | ||
return ans | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters