We will find a way, we always have.

-interstellar

Problem Solving/๋ฆฌํŠธ์ฝ”๋“œ

[LeetCode] 380. Insert Delete GetRandom O(1)

Redddy 2024. 1. 21. 13:21

๐Ÿ”ˆ ๋ฌธ์ œ

Implement the RandomizedSet class:

RandomizedSet() Initializes the RandomizedSet object.

bool insert(int val) Inserts an item val into the set if not present. Returns true if the item was not present, false otherwise.

bool remove(int val) Removes an item val from the set if present. Returns true if the item was present, false otherwise.

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.

You must implement the functions of the class such that each function works in average O(1) time complexity.

 

๐Ÿ’ซ ์˜ˆ์‹œ

Input
["RandomizedSet", "insert", "remove", "insert", "getRandom", "remove", "insert", "getRandom"]
[[], [1], [2], [2], [], [1], [2], []]
Output
[null, true, false, true, 2, true, false, 2]

Explanation
RandomizedSet randomizedSet = new RandomizedSet();
randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully.
randomizedSet.remove(2); // Returns false as 2 does not exist in the set.
randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2].
randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly.
randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2].
randomizedSet.insert(2); // 2 was already in the set, so return false.
randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2.

 

๐ŸŽฑ ์ œํ•œ

-231 <= val <= 231 - 1
At most 2 * 105 calls will be made to insert, remove, and getRandom.
There will be at least one element in the data structure when getRandom is called.

 

 

๐Ÿ“š ๋ฌธ์ œ ํ’€์ด

๋ฌธ์ œ์˜ ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ •๋ฆฌํ•ด๋ณด์ž๋ฉด, ์‚ฝ์ž…, ์‚ญ์ œ ๊ทธ๋ฆฌ๊ณ  ๋žœ๋คํ•œ ๊ฐ’ ์ถ”์ถœํ•˜๋Š” ๋™์ž‘์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ชจ๋‘ O(1) ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. 

 

์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋ฅผ O(1)์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ๋Š” Set์ด ์žˆ๋‹ค. ํ•˜์ง€๋งŒ Set์—์„œ ๋žœ๋คํ•œ ๊ฐ’์„ ์ถ”์ถœํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ธ๋ฑ์Šค ์ ‘๊ทผ์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋ ‡๋‹ค๋ฉด ๋žœ๋คํ•œ ๊ฐ’ ์ถ”์ถœ์„ O(1)์— ๋™์ž‘ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ๋Š” List๊ฐ€ ์žˆ๋‹ค. 0๋ถ€ํ„ฐ ํ˜„์žฌ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ๊ธธ์ด ์ค‘ ๋žœ๋คํ•œ ๊ฐ’์„ ๋ฝ‘๊ณ  ์ธ๋ฑ์Šค๋กœ ์ ‘๊ทผํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋ ‡์ง€๋งŒ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ฝ์ž…์€ O(1)์ด์ง€๋งŒ ์‚ญ์ œ๋Š” O(1)์ด ์•„๋‹ˆ๋‹ค. ์ด์œ ๋Š” ์‚ฝ์ž… ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๋’ค์— ์›์†Œ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜์—ฌ ์ด์ „ ์›์†Œ๋“ค์˜ ์ฃผ์†Œ์˜ ๋ณ€ํ•จ์ด ์—†์ง€๋งŒ ์‚ญ์ œ์˜ ๊ฒฝ์šฐ ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ์›์†Œ๋ฅผ ์ฐพ๊ณ  ๊ทธ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ์‚ญ์ œ๋œ ์›์†Œ ๋’ค์— ์žˆ๋Š” ๊ฐ’๋“ค์€ ์ „๋ถ€ ์•ž์œผ๋กœ ํ•œ์นธ์”ฉ ๋‹น๊ฒจ์™€์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ์•ž์˜ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š”๋ฐ์—๋Š” O(n), ๊ฐ€์žฅ ๋’ค์˜ ์›์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š”๋ฐ์—๋Š” O(1)์ด ์†Œ์š”๋œ๋‹ค. 

์šฐ๋ฆฌ๋Š” ์ด์ ์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค! 

ํ˜„์žฌ ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์‚ญ์ œํ•˜๋Š” ์š”์†Œ๋ฅผ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์žˆ๋Š” ์š”์†Œ๋ž‘ ์œ„์น˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด๋™ํ•œ ์‚ญ์ œํ•˜๊ณ ์ž ํ•˜๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค๋ฉด, swap O(1), pop O(1) ์œผ๋กœ ์‚ญ์ œ๋ฅผ O(1)์— ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ๊ฐ’์„ ์ €์žฅํ•  ๋•Œ ์ €์žฅํ•˜๊ณ ์ž ํ•˜๋Š” ๊ฐ’์˜ ์ธ๋ฑ์Šค๋„ ํ•จ๊ป˜ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์…‹๋Œ€์‹  Map์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค์—๋Š” ์›์†Œ๋ฅผ, ๊ฐ’์—๋Š” ์ธ๋ฑ์Šค๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ•  ๊ฒƒ์ด๋‹ค. ๋งต ์—ญ์‹œ ํ•ด์‹œ ๊ธฐ๋ฐ˜์œผ๋กœ ๋™์ž‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฝ์ž… ์‚ญ์ œ์— O(1)์ด ์†Œ์š”๋œ๋‹ค. 

 

 

๊ฝค ์•ผ๋ฌด์ง„ ๋ฌธ์ œ์˜€๋‹ค. 

๐Ÿ’ป ์ฝ”๋“œ

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class RandomizedSet {
 
    private Map<Integer, Integer> index;
    private List<Integer> nums;
    private Random rand;
    
    public RandomizedSet() {
        this.index = new HashMap<>();
        this.nums = new ArrayList<>();
        this.rand = new Random();
    }
    
    public boolean insert(int val) {
        if (!index.containsKey(val)) {
            index.put(val, nums.size());
            nums.add(val);
            return true;
        }
        return false;
    }
    
    public boolean remove(int val) {
        if (index.containsKey(val)) {
            int remIdx = index.get(val);
            int lastVal = nums.get(nums.size()-1);
            nums.set(remIdx, lastVal);
            nums.remove(nums.size()-1);
            index.put(lastVal, remIdx);
            index.remove(val);
            
            return true;
        }
        return false;
    }
    
    public int getRandom() {
        int randomIndex = rand.nextInt(nums.size());
        return nums.get(randomIndex);
    }
}
 
cs

 

 

๐Ÿ”— ๋ฌธ์ œ๋งํฌ 

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

'Problem Solving > ๋ฆฌํŠธ์ฝ”๋“œ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[LeetCode] 3036. Number of Subarrays That Match a Pattern II  (0) 2024.02.11
[LeetCode] 1657. Determine if Two Strings Are Close  (1) 2024.01.14