-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLongestSubarrayWithMaximumBitwiseAnd.java
More file actions
73 lines (70 loc) · 1.83 KB
/
LongestSubarrayWithMaximumBitwiseAnd.java
File metadata and controls
73 lines (70 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*https://leetcode.com/problems/longest-subarray-with-maximum-bitwise-and/submissions/*/
class Solution {
int[] segmentTree;
int len;
public int longestSubarray(int[] nums) {
int max = 0, n = nums.length;
for (int i = 0; i < n; ++i)
if (nums[i] > max)
max = nums[i];
int result = 0;
int start = -1, end = -1;
for (int i = 0; i < n; ++i)
{
if (nums[i] == max)
{
if (start == -1) start = end = i;
else end = i;
}
else
{
result = Math.max(result,end-start+1);
start = end = -1;
}
}
if (start != -1)
result = Math.max(result,end-start+1);
return result;
}
}
class Solution {
public int longestSubarray(int[] nums) {
int max = 0, n = nums.length;
for (int i = 0; i < n; ++i)
if (nums[i] > max)
max = nums[i];
int result = 0, count = 0;
for (int i = 0; i < n; ++i)
{
if (nums[i] == max)
{
++count;
result = Math.max(result,count);
}
else count = 0;
}
return result;
}
}
class Solution {
public int longestSubarray(int[] nums) {
int n = nums.length;
int res = 0;
int i = 0;
int max = 0;
while (i < n) {
if (nums[i] >= max) {
int j = i;
while (j < n && nums[j] == nums[i]) j++;
if (nums[i] > max) {
max = nums[i];
res = j - i;
} else {
res = Math.max(res, j - i);
}
i = j;
} else i++;
}
return res;
}
}