본문 바로가기
반응형

algorithn3

Linked List Random Node [leetcode] 문제 Given a singly linked list, return a random node's value from the linked list. Each node must have the same probability of being chosen. Implement the Solution class: Solution(ListNode head) Initializes the object with the integer array nums. int getRandom() Chooses a node randomly from the list and returns its value. All the nodes of the list should be equally likely to be choosen. Example 1.. 2022. 1. 7.
Car Pooling [leetcode] 문제 1094. Car Pooling Medium Example 1: Input: trips = [[2,1,5],[3,3,7]], capacity = 4 Output: false Example 2: Input: trips = [[2,1,5],[3,3,7]], capacity = 5 Output: true Constraints: 1 2022. 1. 7.
Intersection of Two Arrays II [leetcode] 문제 풀이 설명 어려운 문제는 아니였다. 그래도 역시 생각보다 오래 고민했다. 한 시간 안되게? 일단 교집합이라고 문제 제목이 나와있기 때문에 단순히 교집합으로 풀어야 겠다 생각하면 안된다. 무구한 삽질을 이미 다른 문제를 통해 경험해 봤기 때문에 python이 제공하는 set 연산을 하면 안된다는걸 알고 있다. 첫번째 케이스의 경우 set 연산으로 수행할 경우 [2,2] 가 아니라 [2]로 나오게 된다. → 교집합 연산을 위해서는 리스트가 아니라 집합으로 먼저 바뀌어야 하고 그럼 [1,2,2,1] → [1,2] 로 중복이 제거 나는 이렇게 풀었다 (코드를 보면 알겠지만) 크기가 작은 리스트의 숫자를 꺼낸다. 해당 숫자가 다른 리스트에 들어있는지 판단한다. 들어 있다면 해당 숫자를 크기가 큰쪽에서 제거한.. 2021. 9. 18.
반응형