Tuesday, December 18, 2018
683. K Empty Slots
Version #1 TreeSet
Time O(nlogn)
Space O(n)
37.43 %
class Solution {
public int kEmptySlots(int[] flowers, int k) {
// positions of blooming flowers
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < flowers.length; i++) {
int curr = flowers[i];
Integer temp = set.higher(flowers[i]);
if (temp != null && temp - curr - 1 == k) {
return i + 1;
}
temp = set.lower(flowers[i]);
if (temp != null && curr - temp - 1 == k) {
return i + 1;
}
set.add(flowers[i]);
}
return -1;
}
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment