what about the optimization of code? may be you can use stack and hashmap to store count of each element's greater element on their right