Skip to content

feat: add solutions to lc problems: No.1546,1547,1551 #2072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@

<!-- 这里可写通用的实现逻辑 -->

贪心 + 前缀和。ans 表示结果,初始值为 0。
**方法一:贪心 + 前缀和 + 哈希表**

贪心:当我们发现以下标 i 结尾的子数组和为 target 时,ans++,然后继续往后查找。
我们遍历数组 $nums$,利用前缀和 + 哈希表的方法,寻找和为 $target$ 的子数组,若找到,则答案加一,然后我们将前缀和置为 $0$,继续遍历数组 $nums$,直到遍历完整个数组。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。

<!-- tabs:start -->

Expand All @@ -65,18 +67,18 @@
```python
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
i, n = 0, len(nums)
ans = 0
i, n = 0, len(nums)
while i < n:
s = 0
seen = {0}
vis = {0}
while i < n:
s += nums[i]
if s - target in seen:
if s - target in vis:
ans += 1
break
i += 1
seen.add(s)
vis.add(s)
i += 1
return ans
```
Expand All @@ -88,22 +90,20 @@ class Solution:
```java
class Solution {
public int maxNonOverlapping(int[] nums, int target) {
int i = 0, n = nums.length;
int ans = 0;
while (i < n) {
int ans = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
Set<Integer> vis = new HashSet<>();
int s = 0;
Set<Integer> seen = new HashSet<>();
seen.add(0);
vis.add(0);
while (i < n) {
s += nums[i];
if (seen.contains(s - target)) {
if (vis.contains(s - target)) {
++ans;
break;
}
++i;
seen.add(s);
vis.add(s);
}
++i;
}
return ans;
}
Expand All @@ -116,22 +116,19 @@ class Solution {
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
int i = 0, n = nums.size();
int ans = 0;
while (i < n) {
int ans = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
unordered_set<int> vis{{0}};
int s = 0;
unordered_set<int> seen;
seen.insert(0);
while (i < n) {
s += nums[i];
if (seen.count(s - target)) {
if (vis.count(s - target)) {
++ans;
break;
}
++i;
seen.insert(s);
vis.insert(s);
}
++i;
}
return ans;
}
Expand All @@ -141,23 +138,44 @@ public:
### **Go**

```go
func maxNonOverlapping(nums []int, target int) int {
i, n, ans := 0, len(nums), 0
for i < n {
func maxNonOverlapping(nums []int, target int) (ans int) {
n := len(nums)
for i := 0; i < n; i++ {
s := 0
seen := map[int]bool{0: true}
for i < n {
vis := map[int]bool{0: true}
for ; i < n; i++ {
s += nums[i]
if seen[s-target] {
if vis[s-target] {
ans++
break
}
seen[s] = true
i++
vis[s] = true
}
i++
}
return ans
return
}
```

### **TypeScript**

```ts
function maxNonOverlapping(nums: number[], target: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let s = 0;
const vis: Set<number> = new Set();
vis.add(0);
for (; i < n; ++i) {
s += nums[i];
if (vis.has(s - target)) {
++ans;
break;
}
vis.add(s);
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,31 @@

## Solutions

**Solution 1: Greedy + Prefix Sum + Hash Table**

We traverse the array $nums$, using the method of prefix sum + hash table, to find subarrays with a sum of $target$. If found, we increment the answer by one, then we set the prefix sum to $0$ and continue to traverse the array $nums$ until the entire array is traversed.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

<!-- tabs:start -->

### **Python3**

```python
class Solution:
def maxNonOverlapping(self, nums: List[int], target: int) -> int:
i, n = 0, len(nums)
ans = 0
i, n = 0, len(nums)
while i < n:
s = 0
seen = {0}
vis = {0}
while i < n:
s += nums[i]
if s - target in seen:
if s - target in vis:
ans += 1
break
i += 1
seen.add(s)
vis.add(s)
i += 1
return ans
```
Expand All @@ -63,22 +69,20 @@ class Solution:
```java
class Solution {
public int maxNonOverlapping(int[] nums, int target) {
int i = 0, n = nums.length;
int ans = 0;
while (i < n) {
int ans = 0, n = nums.length;
for (int i = 0; i < n; ++i) {
Set<Integer> vis = new HashSet<>();
int s = 0;
Set<Integer> seen = new HashSet<>();
seen.add(0);
vis.add(0);
while (i < n) {
s += nums[i];
if (seen.contains(s - target)) {
if (vis.contains(s - target)) {
++ans;
break;
}
++i;
seen.add(s);
vis.add(s);
}
++i;
}
return ans;
}
Expand All @@ -91,22 +95,19 @@ class Solution {
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
int i = 0, n = nums.size();
int ans = 0;
while (i < n) {
int ans = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
unordered_set<int> vis{{0}};
int s = 0;
unordered_set<int> seen;
seen.insert(0);
while (i < n) {
s += nums[i];
if (seen.count(s - target)) {
if (vis.count(s - target)) {
++ans;
break;
}
++i;
seen.insert(s);
vis.insert(s);
}
++i;
}
return ans;
}
Expand All @@ -116,23 +117,44 @@ public:
### **Go**

```go
func maxNonOverlapping(nums []int, target int) int {
i, n, ans := 0, len(nums), 0
for i < n {
func maxNonOverlapping(nums []int, target int) (ans int) {
n := len(nums)
for i := 0; i < n; i++ {
s := 0
seen := map[int]bool{0: true}
for i < n {
vis := map[int]bool{0: true}
for ; i < n; i++ {
s += nums[i]
if seen[s-target] {
if vis[s-target] {
ans++
break
}
seen[s] = true
i++
vis[s] = true
}
i++
}
return ans
return
}
```

### **TypeScript**

```ts
function maxNonOverlapping(nums: number[], target: number): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
let s = 0;
const vis: Set<number> = new Set();
vis.add(0);
for (; i < n; ++i) {
s += nums[i];
if (vis.has(s - target)) {
++ans;
break;
}
vis.add(s);
}
}
return ans;
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
int i = 0, n = nums.size();
int ans = 0;
while (i < n) {
int s = 0;
unordered_set<int> seen;
seen.insert(0);
while (i < n) {
s += nums[i];
if (seen.count(s - target)) {
++ans;
break;
}
++i;
seen.insert(s);
}
++i;
}
return ans;
}
class Solution {
public:
int maxNonOverlapping(vector<int>& nums, int target) {
int ans = 0, n = nums.size();
for (int i = 0; i < n; ++i) {
unordered_set<int> vis{{0}};
int s = 0;
while (i < n) {
s += nums[i];
if (vis.count(s - target)) {
++ans;
break;
}
++i;
vis.insert(s);
}
}
return ans;
}
};
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
func maxNonOverlapping(nums []int, target int) int {
i, n, ans := 0, len(nums), 0
for i < n {
func maxNonOverlapping(nums []int, target int) (ans int) {
n := len(nums)
for i := 0; i < n; i++ {
s := 0
seen := map[int]bool{0: true}
for i < n {
vis := map[int]bool{0: true}
for ; i < n; i++ {
s += nums[i]
if seen[s-target] {
if vis[s-target] {
ans++
break
}
seen[s] = true
i++
vis[s] = true
}
i++
}
return ans
return
}
Loading