Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit17051e6

Browse files
962_Maximum_Width_Ramp
1 parente61581a commit17051e6

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

‎962_Maximum_Width_Ramp.java‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
importjava.awt.Point;
2+
classSolution {
3+
publicintmaxWidthRamp(int[]A) {
4+
intN =A.length;
5+
Integer[]B =newInteger[N];
6+
for (inti =0;i <N; ++i)
7+
B[i] =i;
8+
// Sort index based on value
9+
Arrays.sort(B, (i,j) -> ((Integer)A[i]).compareTo(A[j]));
10+
11+
intans =0;
12+
intm =N;
13+
for (inti:B) {
14+
ans =Math.max(ans,i -m);
15+
m =Math.min(m,i);
16+
}
17+
returnans;
18+
}
19+
20+
/*public int maxWidthRamp(int[] A) {
21+
int N = A.length;
22+
23+
int ans = 0;
24+
List<Point> candidates = new ArrayList();
25+
candidates.add(new Point(A[N-1], N-1));
26+
27+
// candidates: i's decreasing, by increasing value of A[i]
28+
for (int i = N-2; i >= 0; --i) {
29+
// Find largest j in candidates with A[j] >= A[i]
30+
int lo = 0, hi = candidates.size();
31+
while (lo < hi) {
32+
int mi = lo + (hi - lo) / 2;
33+
if (candidates.get(mi).x < A[i])
34+
lo = mi + 1;
35+
else
36+
hi = mi;
37+
}
38+
if (lo < candidates.size()) {
39+
int j = candidates.get(lo).y;
40+
ans = Math.max(ans, j - i);
41+
} else {
42+
candidates.add(new Point(A[i], i));
43+
}
44+
}
45+
return ans;
46+
}*/
47+
48+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp