forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1852
No.1852.Distinct Numbers in Each Subarray
- Loading branch information
Showing
6 changed files
with
264 additions
and
5 deletions.
There are no files selected for viewing
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
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
25 changes: 25 additions & 0 deletions
25
solution/1800-1899/1852.Distinct Numbers in Each Subarray/Solution.cpp
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,25 @@ | ||
class Solution { | ||
public: | ||
vector<int> distinctNumbers(vector<int>& nums, int k) { | ||
int cnt[100010] = {0}; | ||
int x = 0; | ||
for (int i = 0; i < k; ++i) { | ||
if (cnt[nums[i]]++ == 0) { | ||
++x; | ||
} | ||
} | ||
int n = nums.size(); | ||
vector<int> ans(n - k + 1); | ||
ans[0] = x; | ||
for (int i = k; i < n; ++i) { | ||
if (--cnt[nums[i - k]] == 0) { | ||
--x; | ||
} | ||
if (cnt[nums[i]]++ == 0) { | ||
++x; | ||
} | ||
ans[i - k + 1] = x; | ||
} | ||
return ans; | ||
} | ||
}; |
17 changes: 17 additions & 0 deletions
17
solution/1800-1899/1852.Distinct Numbers in Each Subarray/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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
func distinctNumbers(nums []int, k int) []int { | ||
cnt := map[int]int{} | ||
for _, v := range nums[:k] { | ||
cnt[v]++ | ||
} | ||
ans := []int{len(cnt)} | ||
for i := k; i < len(nums); i++ { | ||
u := nums[i-k] | ||
cnt[u]-- | ||
if cnt[u] == 0 { | ||
delete(cnt, u) | ||
} | ||
cnt[nums[i]]++ | ||
ans = append(ans, len(cnt)) | ||
} | ||
return ans | ||
} |
24 changes: 24 additions & 0 deletions
24
solution/1800-1899/1852.Distinct Numbers in Each Subarray/Solution.java
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,24 @@ | ||
class Solution { | ||
public int[] distinctNumbers(int[] nums, int k) { | ||
int[] cnt = new int[100010]; | ||
int x = 0; | ||
for (int i = 0; i < k; ++i) { | ||
if (cnt[nums[i]]++ == 0) { | ||
++x; | ||
} | ||
} | ||
int n = nums.length; | ||
int[] ans = new int[n - k + 1]; | ||
ans[0] = x; | ||
for (int i = k; i < n; ++i) { | ||
if (--cnt[nums[i - k]] == 0) { | ||
--x; | ||
} | ||
if (cnt[nums[i]]++ == 0) { | ||
++x; | ||
} | ||
ans[i - k + 1] = x; | ||
} | ||
return ans; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
solution/1800-1899/1852.Distinct Numbers in Each Subarray/Solution.py
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,14 @@ | ||
class Solution: | ||
def distinctNumbers(self, nums: List[int], k: int) -> List[int]: | ||
n = len(nums) | ||
cnt = Counter(nums[:k]) | ||
ans = [len(cnt)] | ||
for i in range(k, n): | ||
u = nums[i - k] | ||
cnt[u] -= 1 | ||
if cnt[u] == 0: | ||
cnt.pop(u) | ||
|
||
cnt[nums[i]] += 1 | ||
ans.append(len(cnt)) | ||
return ans |