Skip to content

Commit 380a0a0

Browse files
authored
Merge pull request #494 from 0chnxxx/0chnxxx/ltc/380
[LTC] Lv2 / Insert Delete GetRandom O(1) / 8분
2 parents 1040e0f + 3bf9695 commit 380a0a0

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import kotlin.random.Random
2+
3+
/**
4+
* Implement the RandomizedSet class:
5+
* RandomizedSet() Initializes the RandomizedSet object.
6+
* bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.
7+
* bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.
8+
* int getRandom() Returns a random element from the current set of elements (it's guaranteed that at least one element exists when this method is called). Each element must have the same probability of being returned.
9+
* You must implement the functions of the class such that each function works in average O(1) time complexity.
10+
*
11+
* Constraints:
12+
* -2^31 <= val <= 2^31 - 1
13+
* At most 2 * 105^ calls will be made to insert, remove, and getRandom.
14+
* There will be at least one element in the data structure when getRandom is called.
15+
*/
16+
17+
fun main() {
18+
val randomizedSet = RandomizedSet()
19+
20+
println(randomizedSet.insert(1)) // Inserts 1 to the set. Returns true as 1 was inserted successfully.
21+
println(randomizedSet.remove(2)) // Returns false as 2 does not exist in the set.
22+
println(randomizedSet.insert(2)) // Inserts 2 to the set, returns true. Set now contains [1,2].
23+
println(randomizedSet.getRandom()) // getRandom() should return either 1 or 2 randomly.
24+
println(randomizedSet.remove(1)) // Removes 1 from the set, returns true. Set now contains [2].
25+
println(randomizedSet.insert(2)) // 2 was already in the set, so return false.
26+
println(randomizedSet.getRandom()) // Since 2 is the only number in the set, getRandom() will always return 2.
27+
}
28+
29+
class RandomizedSet() {
30+
// 값과 인덱스 매핑
31+
private val map = HashMap<Int, Int>()
32+
// 실제 값 저장
33+
private val list = ArrayList<Int>()
34+
35+
// 시간 복잡도 : O(1)
36+
// 공간 복잡도 : O(1)
37+
fun insert(`val`: Int): Boolean {
38+
if (map.containsKey(`val`)) return false
39+
40+
map[`val`] = list.size
41+
42+
list.add(`val`)
43+
44+
return true
45+
}
46+
47+
// 시간 복잡도 : O(1)
48+
// 공간 복잡도 : O(1)
49+
fun remove(`val`: Int): Boolean {
50+
val index = map[`val`] ?: return false
51+
val lastVal = list.last()
52+
53+
// 마지막 값을 삭제 대상 자리로 옮김
54+
list[index] = lastVal
55+
map[lastVal] = index
56+
57+
// 마지막 원소 제거
58+
list.removeAt(list.size - 1)
59+
map.remove(`val`)
60+
61+
return true
62+
}
63+
64+
// 시간 복잡도 : O(1)
65+
// 공간 복잡도 : O(1)
66+
fun getRandom(): Int {
67+
return list[Random.nextInt(list.size)]
68+
}
69+
}
70+
71+
//class RandomizedSet() {
72+
// val set = mutableSetOf<Int>()
73+
//
74+
// // 시간 복잡도 : O(N)
75+
// // 공간 복잡도 : O(1)
76+
// fun insert(`val`: Int): Boolean {
77+
// return set.add(`val`)
78+
// }
79+
//
80+
// // 시간 복잡도 : O(N)
81+
// // 공간 복잡도 : O(1)
82+
// fun remove(`val`: Int): Boolean {
83+
// return set.remove(`val`)
84+
// }
85+
//
86+
// // 시간 복잡도 : O(N)
87+
// // 공간 복잡도 : O(N)
88+
// fun getRandom(): Int {
89+
// return set.random()
90+
// }
91+
//}

0 commit comments

Comments
 (0)