avatar
Get first Greater in a array# JobHunting - 待字闺中
s*e
1
Implement an array on ints which supports
1) void lookup(int idx) : value stored at idx, in amortized O(1) time.
2) void append(int value) : adds a value at the end of the array, in
amortized
O(1) time.
3) void set(int idx, int val) : sets the value at idx to val. if idx >
length
, throws an error. Complexity amortized O(1) time.
4) int GetFirstMaxIndex(int val): Gets the first index such that the element
at the idx is > val. Complexity O(logn).
ex [2, 10,5,6,80]
input : 6 output : 10
input :20 output : 80
avatar
l*a
2
ArrayList for the first 3 requirements
for the 4th, create a new array as 2,10,10,10,80 and then u can get it

element

【在 s******e 的大作中提到】
: Implement an array on ints which supports
: 1) void lookup(int idx) : value stored at idx, in amortized O(1) time.
: 2) void append(int value) : adds a value at the end of the array, in
: amortized
: O(1) time.
: 3) void set(int idx, int val) : sets the value at idx to val. if idx >
: length
: , throws an error. Complexity amortized O(1) time.
: 4) int GetFirstMaxIndex(int val): Gets the first index such that the element
: at the idx is > val. Complexity O(logn).

avatar
g*e
3
array list append不是每次都是O(1)吧

【在 l*****a 的大作中提到】
: ArrayList for the first 3 requirements
: for the 4th, create a new array as 2,10,10,10,80 and then u can get it
:
: element

avatar
c*n
4
then what's the time complexity for set(0, 100) operation using the example
given by LZ ?

【在 l*****a 的大作中提到】
: ArrayList for the first 3 requirements
: for the 4th, create a new array as 2,10,10,10,80 and then u can get it
:
: element

avatar
l*a
5
http://docs.oracle.com/javase/6/docs/api/
The add operation runs in amortized constant time, that is, adding n
elements requires O(n) time.

【在 g**e 的大作中提到】
: array list append不是每次都是O(1)吧
avatar
l*a
6
感觉1)2)3)成立的情况下,4)无法实现。
avatar
w*y
7

nice.

【在 l*****a 的大作中提到】
: ArrayList for the first 3 requirements
: for the 4th, create a new array as 2,10,10,10,80 and then u can get it
:
: element

avatar
f*e
8
add a fake value to every element:
fake(a[i+1])=max{fake(a[i]), a[i+1]}
binary search val among fake(a[1...n])

element

【在 s******e 的大作中提到】
: Implement an array on ints which supports
: 1) void lookup(int idx) : value stored at idx, in amortized O(1) time.
: 2) void append(int value) : adds a value at the end of the array, in
: amortized
: O(1) time.
: 3) void set(int idx, int val) : sets the value at idx to val. if idx >
: length
: , throws an error. Complexity amortized O(1) time.
: 4) int GetFirstMaxIndex(int val): Gets the first index such that the element
: at the idx is > val. Complexity O(logn).

avatar
z*x
9
what about set() operation?

【在 f*****e 的大作中提到】
: add a fake value to every element:
: fake(a[i+1])=max{fake(a[i]), a[i+1]}
: binary search val among fake(a[1...n])
:
: element

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。