Skip to content

Commit c4ea402

Browse files
committed
leetcode
1 parent a2be9dc commit c4ea402

4 files changed

+343
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
3+
4+
5+
-* 1493. Longest Subarray of 1's After Deleting One Element *-
6+
7+
8+
Given a binary array nums, you should delete one element from it.
9+
10+
Return the size of the longest non-empty subarray containing only 1's in the resulting array.
11+
Return 0 if there is no such subarray.
12+
13+
14+
15+
Example 1:
16+
17+
Input: nums = [1,1,0,1]
18+
Output: 3
19+
Explanation: After deleting the number in position 2, [1,1,1] contains 3 numbers with value of 1's.
20+
Example 2:
21+
22+
Input: nums = [0,1,1,1,0,1,1,0,1]
23+
Output: 5
24+
Explanation: After deleting the number in position 4, [0,1,1,1,1,1,0,1] longest subarray with value of 1's is [1,1,1,1,1].
25+
Example 3:
26+
27+
Input: nums = [1,1,1]
28+
Output: 2
29+
Explanation: You must delete one element.
30+
31+
32+
Constraints:
33+
34+
1 <= nums.length <= 105
35+
nums[i] is either 0 or 1.
36+
37+
38+
39+
*/
40+
41+
import 'dart:math';
42+
43+
class A {
44+
int longestSubarray(List<int> nums) {
45+
final int n = nums.length;
46+
47+
int left = 0;
48+
int zeros = 0;
49+
int ans = 0;
50+
51+
for (int right = 0; right < n; right++) {
52+
if (nums[right] == 0) {
53+
zeros++;
54+
}
55+
while (zeros > 1) {
56+
if (nums[left] == 0) {
57+
zeros--;
58+
}
59+
left++;
60+
}
61+
ans = max(ans, right - left + 1 - zeros);
62+
}
63+
return (ans == n) ? ans - 1 : ans;
64+
}
65+
}
66+
67+
//=========================Union Find============
68+
69+
class DisjointSet {
70+
late List<int> parent;
71+
late List<int> size;
72+
73+
DisjointSet(int n) {
74+
size = List<int>.filled(n + 1, 1);
75+
parent = List<int>.filled(n + 1, 0);
76+
77+
for (int i = 0; i <= n; ++i) parent[i] = i;
78+
}
79+
80+
int findParent(int node) {
81+
if (parent[node] == node) return node;
82+
return parent[node] = findParent(parent[node]);
83+
}
84+
85+
void unionBySize(int u, int v) {
86+
int pU = findParent(u);
87+
int pV = findParent(v);
88+
89+
if (pU == pV) return;
90+
91+
if (size[pU] < size[pV]) {
92+
parent[pU] = pV;
93+
size[pV] += size[pU];
94+
} else {
95+
parent[pV] = pU;
96+
size[pU] += size[pV];
97+
}
98+
}
99+
}
100+
101+
class Solution {
102+
int longestSubarray(List<int> nums) {
103+
final int n = nums.length;
104+
int s = 0;
105+
106+
for (final int x in nums) s += x;
107+
if (s == n) return s - 1;
108+
if (s == 1 || s == 0) return s;
109+
110+
final DisjointSet ds = DisjointSet(n);
111+
112+
for (int i = 0; i < n - 1; ++i) {
113+
if (nums[i] == 1) {
114+
if (nums[i + 1] == 1) {
115+
if (ds.findParent(i) != ds.findParent(i + 1)) {
116+
ds.unionBySize(i, i + 1);
117+
}
118+
}
119+
}
120+
}
121+
122+
int ans = 0;
123+
for (int i = 1; i < n - 1; ++i) {
124+
if (nums[i] == 0) {
125+
int tSum = 0;
126+
if (nums[i - 1] == 1) {
127+
tSum += ds.size[ds.parent[i - 1]];
128+
}
129+
if (nums[i + 1] == 1) {
130+
tSum += ds.size[ds.parent[i + 1]];
131+
}
132+
ans = tSum > ans ? tSum : ans;
133+
}
134+
}
135+
136+
ans = ans > ds.size[0] ? ans : ds.size[0];
137+
return ans;
138+
}
139+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
type DisjointSet struct {
4+
parent []int
5+
size []int
6+
}
7+
8+
func NewDisjointSet(n int) *DisjointSet {
9+
size := make([]int, n+1)
10+
parent := make([]int, n+1)
11+
12+
for i := 0; i <= n; i++ {
13+
size[i] = 1
14+
parent[i] = i
15+
}
16+
17+
return &DisjointSet{
18+
parent: parent,
19+
size: size,
20+
}
21+
}
22+
23+
func (ds *DisjointSet) findParent(node int) int {
24+
if ds.parent[node] == node {
25+
return node
26+
}
27+
ds.parent[node] = ds.findParent(ds.parent[node])
28+
return ds.parent[node]
29+
}
30+
31+
func (ds *DisjointSet) unionBySize(u, v int) {
32+
pU := ds.findParent(u)
33+
pV := ds.findParent(v)
34+
35+
if pU == pV {
36+
return
37+
}
38+
39+
if ds.size[pU] < ds.size[pV] {
40+
ds.parent[pU] = pV
41+
ds.size[pV] += ds.size[pU]
42+
} else {
43+
ds.parent[pV] = pU
44+
ds.size[pU] += ds.size[pV]
45+
}
46+
}
47+
48+
func longestSubarray(nums []int) int {
49+
n := len(nums)
50+
s := 0
51+
52+
for _, x := range nums {
53+
s += x
54+
}
55+
if s == n {
56+
return s - 1
57+
}
58+
if s == 1 || s == 0 {
59+
return s
60+
}
61+
62+
ds := NewDisjointSet(n)
63+
64+
for i := 0; i < n-1; i++ {
65+
if nums[i] == 1 && nums[i+1] == 1 {
66+
if ds.findParent(i) != ds.findParent(i+1) {
67+
ds.unionBySize(i, i+1)
68+
}
69+
}
70+
}
71+
72+
ans := 0
73+
for i := 1; i < n-1; i++ {
74+
if nums[i] == 0 {
75+
tSum := 0
76+
if nums[i-1] == 1 {
77+
tSum += ds.size[ds.findParent(i-1)]
78+
}
79+
if nums[i+1] == 1 {
80+
tSum += ds.size[ds.findParent(i+1)]
81+
}
82+
if tSum > ans {
83+
ans = tSum
84+
}
85+
}
86+
}
87+
88+
if ans > ds.size[0] {
89+
return ans
90+
}
91+
return ds.size[0]
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# 🔥 Union-Find 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
The code provided aims to solve a problem of finding the length of the longest subarray with the maximum number of 1s, with the allowance of flipping at most one 0 to a 1.
4+
5+
Let's go through the code step by step and explain its functionality:
6+
7+
## DisjointSet Class
8+
9+
- The `DisjointSet` class represents the disjoint-set data structure.
10+
- It has two instance variables: `parent` and `size`.
11+
- The `parent` list maintains the parent of each element, and the `size` list stores the size of each set.
12+
- The constructor `DisjointSet(int n)` initializes the `size` and `parent` lists with size `n+1` and sets each element's parent as itself.
13+
- The `findParent(int node)` method implements the find operation of the disjoint-set data structure. It uses path compression optimization to flatten the tree structure and returns the parent of the given `node`.
14+
- The `unionBySize(int u, int v)` method performs the union operation of the disjoint-set data structure. It finds the parents of `u` and `v`, and if they are different, it merges the smaller set into the larger set and updates the size accordingly.
15+
16+
## Solution Class
17+
18+
- The `Solution` class contains the `longestSubarray` method that takes a list of integers `nums` as input and returns the length of the longest subarray.
19+
- It initializes the variable `s` as the sum of all elements in `nums`.
20+
- If `s` is equal to the length of `nums`, it means all elements are 1, so it returns `s - 1` (since we can flip at most one 0 to a 1).
21+
- If `s` is equal to 1 or 0, it means all elements are 0 or all elements are already 1, respectively, so it returns `s`.
22+
- It creates an instance of the `DisjointSet` class called `ds` with the length of `nums`.
23+
- It iterates through the `nums` list from index 0 to `n-1` and performs union operations for adjacent 1s in the `ds` disjoint set.
24+
- It then iterates through the `nums` list from index 1 to `n-1` and checks for 0s to calculate the length of the longest subarray.
25+
- Inside this loop, if the current element is 0, it calculates the sum of sizes of adjacent sets that contain 1s.
26+
- It updates the `ans` variable with the maximum length of the subarray obtained so far.
27+
- Finally, it compares `ans` with the size of the root set (representing the count of 1s without any 0 flips) and returns the maximum value between them.
28+
29+
### Time Complexity Analysis
30+
31+
The time complexity of the code is O(n), where n is the length of the input list `nums`. This complexity arises due to the two iterations over the `nums` list. Both iterations have linear time complexity.
32+
33+
### Space Complexity Analysis
34+
35+
The space complexity of the code is O(n). This is because the `DisjointSet` class uses two lists, `parent` and `size`, each with a length of `n+1`. Additionally, a few auxiliary variables are used, but their space requirements are negligible compared to the `DisjointSet` arrays.
36+
37+
In conclusion, the code utilizes the Union-Find (Disjoint-Set) algorithm to solve the problem efficiently. It has a time complexity of O(n) and a space complexity of O(n).
38+
39+
```dart
40+
class DisjointSet {
41+
late List<int> parent;
42+
late List<int> size;
43+
44+
DisjointSet(int n) {
45+
size = List<int>.filled(n + 1, 1);
46+
parent = List<int>.filled(n + 1, 0);
47+
48+
for (int i = 0; i <= n; ++i) parent[i] = i;
49+
}
50+
51+
int findParent(int node) {
52+
if (parent[node] == node) return node;
53+
return parent[node] = findParent(parent[node]);
54+
}
55+
56+
void unionBySize(int u, int v) {
57+
int pU = findParent(u);
58+
int pV = findParent(v);
59+
60+
if (pU == pV) return;
61+
62+
if (size[pU] < size[pV]) {
63+
parent[pU] = pV;
64+
size[pV] += size[pU];
65+
} else {
66+
parent[pV] = pU;
67+
size[pU] += size[pV];
68+
}
69+
}
70+
}
71+
72+
class Solution {
73+
int longestSubarray(List<int> nums) {
74+
final int n = nums.length;
75+
int s = 0;
76+
77+
for (final int x in nums) s += x;
78+
if (s == n) return s - 1;
79+
if (s == 1 || s == 0) return s;
80+
81+
final DisjointSet ds = DisjointSet(n);
82+
83+
for (int i = 0; i < n - 1; ++i) {
84+
if (nums[i] == 1) {
85+
if (nums[i + 1] == 1) {
86+
if (ds.findParent(i) != ds.findParent(i + 1)) {
87+
ds.unionBySize(i, i + 1);
88+
}
89+
}
90+
}
91+
}
92+
93+
int ans = 0;
94+
for (int i = 1; i < n - 1; ++i) {
95+
if (nums[i] == 0) {
96+
int tSum = 0;
97+
if (nums[i - 1] == 1) {
98+
tSum += ds.size[ds.parent[i - 1]];
99+
}
100+
if (nums[i + 1] == 1) {
101+
tSum += ds.size[ds.parent[i + 1]];
102+
}
103+
ans = tSum > ans ? tSum : ans;
104+
}
105+
}
106+
107+
ans = ans > ds.size[0] ? ans : ds.size[0];
108+
return ans;
109+
}
110+
}
111+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
242242
- [**1970.** Last Day Where You Can Still Cross](LastDayWhereYouCanStillCross/last_day_where_you_can_still_cross.dart)
243243
- [**859.** Buddy Strings](BuddyStrings/buddy_strings.dart)
244244
- [**137.** Single Number II](SingleNumberII/single_number_ii.dart)
245+
- [**1493.** Longest Subarray of 1's After Deleting One Element](LongestSubarrayOf1SAfterDeletingOneElement/longest_subarray_of_1s_after_deleting_one_element.dart)
245246

246247
## Reach me via
247248

0 commit comments

Comments
 (0)