This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int kEmptySlots(int[] flowers, int k) { | |
if (flowers == null || flowers.length < 1) { | |
return -1; | |
} | |
// core logic | |
TreeSet<Integer> bloomingTreeSet = new TreeSet<>(); | |
int day = 0; | |
for(int flower: flowers) { | |
day++; | |
bloomingTreeSet.add(flower); | |
Integer higher = bloomingTreeSet.higher(flower); | |
Integer lower = bloomingTreeSet.lower(flower); | |
if (higher != null && higher - flower - 1 == k || | |
lower != null && flower - lower - 1 == k ) { | |
return day; | |
} | |
} | |
return -1; | |
} | |
} |
No comments:
Post a Comment