You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: solution/2400-2499/2401.Longest Nice Subarray/README_EN.md
+44-53
Original file line number
Diff line number
Diff line change
@@ -66,19 +66,15 @@ It can be proven that no longer nice subarray can be obtained, so we return 3.</
66
66
67
67
### Solution 1: Two Pointers
68
68
69
-
We define a variable $mask$ to record the bitwise OR result of the elements in the current subarray, initially $mask = 0$. Also, we use two pointers $j$ and $i$ to point to the left and right endpoints of the current subarray, initially $i = j = 0$.
69
+
According to the problem description, the position of the binary $1$ in each element of the subarray must be unique to ensure that the bitwise AND result of any two elements is $0$.
70
70
71
-
Next, we traverse the array $nums$ from left to right. For each element $x$ we encounter:
71
+
Therefore, we can use two pointers, $l$ and $r$, to maintain a sliding window such that the elements within the window satisfy the problem's conditions.
72
72
73
-
We perform a bitwise AND operation between it and $mask$. If the result is not $0$, it means that $x$ and at least one element in $mask$ have a binary representation where a certain bit is $1$, and the corresponding bit in the other element's binary representation is $0$. Such pairs of elements cannot satisfy the problem's requirements, so we need to move $j$ to the right until the bitwise AND result of $x$ and $mask$ is $0$.
73
+
We use a variable $\textit{mask}$ to represent the bitwise OR result of the elements within the window. Next, we traverse each element of the array. For the current element $x$, if the bitwise AND result of $\textit{mask}$ and $x$ is not $0$, it means that the current element $x$ has overlapping binary bits with the elements in the window. At this point, we need to move the left pointer $l$ until the bitwise AND result of $\textit{mask}$ and $x$ is $0$. Then, we assign the bitwise OR result of $\textit{mask}$ and $x$ to $\textit{mask}$ and update the answer $\textit{ans} = \max(\textit{ans}, r - l + 1)$.
74
74
75
-
At this point, we have found a subarray that satisfies the problem's requirements. Its length is $i - j + 1$. We compare it with the length of the current longest elegant subarray. If it is longer than the current longest elegant subarray, we update the length of the longest elegant subarray.
75
+
After the traversal, return the answer $\textit{ans}$.
76
76
77
-
Then we perform a bitwise OR operation between $mask$ and $x$, and continue to the next element.
78
-
79
-
Finally, the length of the longest elegant subarray we obtain is the answer.
80
-
81
-
The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is the length of the array $nums$.
77
+
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
82
78
83
79
<!-- tabs:start -->
84
80
@@ -87,13 +83,13 @@ The time complexity is $O(n)$, and the space complexity is $O(1)$. Here, $n$ is
0 commit comments