Skip to content

Commit 886ae35

Browse files
committed
962_Maximum_Width_Ramp
1 parent 542f13f commit 886ae35

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ Also, there are open source implementations for basic data structs and algorithm
205205
| 953 | [Verifying an Alien Dictionary](https://leetcode.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/953_Verifying_an_Alien_Dictionary.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/953_Verifying_an_Alien_Dictionary.java) | Use hashmap to store index of each value, then create a comparator based on this index, O(n) and O(n) |
206206
| 954 | [Array of Doubled Pairs](https://leetcode.com/contest/weekly-contest-114/problems/array-of-doubled-pairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/954_Array_of_Doubled_Pairs.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/954_Array_of_Doubled_Pairs.java) | Sort, then use hashmap to store the frequency of each value. Then, check n, 2 * n in hashmap, O(nlogn) and O(n) |
207207
| 961 | [N-Repeated Element in Size 2N Array](https://leetcode.com/problems/n-repeated-element-in-size-2n-array/submissions/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/961_N-Repeated_Element_in_Size_2N_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/961_N-Repeated_Element_in_Size_2N_Array.java) | Hash and count number, O(n) and O(n) |
208+
| 962 | [Maximum Width Ramp](https://leetcode.com/problems/maximum-width-ramp/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/962_Maximum_Width_Ramp.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/962_Maximum_Width_Ramp.java) | 1. Sort index by value, then transfer problem into finding max gap between index, O(nlogn) and O(1)<br> |
208209

209210
| # | To Understand |
210211
|---| ----- |

java/962_Maximum_Width_Ramp.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.awt.Point;
2+
class Solution {
3+
public int maxWidthRamp(int[] A) {
4+
int N = A.length;
5+
Integer[] B = new Integer[N];
6+
for (int i = 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+
int ans = 0;
12+
int m = N;
13+
for (int i: B) {
14+
ans = Math.max(ans, i - m);
15+
m = Math.min(m, i);
16+
}
17+
return ans;
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+
}

python/962_Maximum_Width_Ramp.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class Solution(object):
2+
# def maxWidthRamp(self, A):
3+
# """
4+
# :type A: List[int]
5+
# :rtype: int
6+
# """
7+
# # TLE
8+
# if not A or len(A) == 0:
9+
# return 0
10+
# for ans in range(len(A) - 1, 0, -1):
11+
# for i in range(len(A)):
12+
# if i + ans > len(A) - 1:
13+
# break
14+
# if (A[i + ans] >= A[i]):
15+
# return ans
16+
# return 0
17+
18+
def maxWidthRamp(self, A):
19+
ans = 0
20+
m = float('inf')
21+
# Sort index based on value
22+
for i in sorted(range(len(A)), key=A.__getitem__):
23+
ans = max(ans, i - m)
24+
m = min(m, i)
25+
return ans
26+
27+
28+
# def maxWidthRamp(self, A):
29+
# N = len(A)
30+
# ans = 0
31+
# candidates = [(A[N - 1], N - 1)]
32+
# # candidates: i's decreasing, by increasing value of A[i]
33+
# for i in xrange(N - 2, -1, -1):
34+
# # Find largest j in candidates with A[j] >= A[i]
35+
# jx = bisect.bisect(candidates, (A[i],))
36+
# if jx < len(candidates):
37+
# ans = max(ans, candidates[jx][1] - i)
38+
# else:
39+
# candidates.append((A[i], i))
40+
# return ans
41+
42+
43+
if __name__ == '__main__':
44+
s = Solution()
45+
print s.maxWidthRamp([6, 0, 8, 2, 1, 5])
46+
print s.maxWidthRamp([9, 8, 1, 0, 1, 9, 4, 0, 4, 1])

0 commit comments

Comments
 (0)