java: add «2419. Longest Subarray With Maximum Bitwise AND»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
fe390db878
commit
5449ec348a
1 changed files with 25 additions and 0 deletions
25
java/longest-subarray-with-maximum-bitwise-and.java
Normal file
25
java/longest-subarray-with-maximum-bitwise-and.java
Normal file
|
@ -0,0 +1,25 @@
|
|||
class Solution {
|
||||
public int longestSubarray(int[] nums) {
|
||||
int maximum = 0;
|
||||
|
||||
int longest = 0, current = 0;
|
||||
for (var num : nums) {
|
||||
if (maximum < num) {
|
||||
maximum = num;
|
||||
longest = current = 1;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (num == maximum) {
|
||||
++current;
|
||||
} else {
|
||||
current = 0;
|
||||
}
|
||||
|
||||
longest = Math.max(longest, current);
|
||||
}
|
||||
|
||||
return longest;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue