본문 바로가기

Leetcode

Leetcode 380. Insert Delete GetRandom O(1)

오늘은 아침부터 기분이 무지 좋다.

 

내가 좋아하는 그릭 요거트랑 그래놀라를 베이글에 발라 먹고 !

부모님께서 연구실까지 태워주셨는데 오늘 날씨도 최고다.

 

오늘 문제는 그냥 하나의 <클래스> 의 내장함수를 만드는 것이다.

 

파이썬의 기본 함수를 써서 쉽게 ! 아주 쉽게 해결하였다.

 

class RandomizedSet:

    def __init__(self):
        self.set = []

    def insert(self, val: int) -> bool:
        if val in self.set:
            return False
        self.set.append(val)
        return True

    def remove(self, val: int) -> bool:
        if val in self.set:
            self.set.remove(val)
            return True
        return False
        
    def getRandom(self) -> int:
        return self.set[random.randrange(0, len(self.set))]


# Your RandomizedSet object will be instantiated and called as such:
# obj = RandomizedSet()
# param_1 = obj.insert(val)
# param_2 = obj.remove(val)
# param_3 = obj.getRandom()

 

 

 

똑같은 코드로 돌렸을 때 아까는 848ms였는데... 다시 돌리니 런타임이 늘어났다.

 

흠...

여튼 오늘도 하나 완료 !

 

이제 다른 것 신경쓰지 말고, 딱 10년만 몰두해보자! 할수있다!

'Leetcode' 카테고리의 다른 글

Leetcode 226. Invert Binary Tree  (0) 2021.10.26
Leetcode 155. Min Stack  (0) 2021.10.25
Leetcode 151. Reverse Words in a String  (0) 2021.10.20
Leetcode 496. Next Greater Element I  (0) 2021.10.19
Leetcode 993. Cousins in Binary Tree  (0) 2021.10.18