-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path1_stolen_breakfast_drone.cpp
60 lines (55 loc) · 1.82 KB
/
1_stolen_breakfast_drone.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Find unique integer within vector of duplicate integers.
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* * Stuck Point:
* - could not figure how to optimize further from solution using unordered set (O(n) time & O(n) space)
*
* ** Untangled Hints: can use BIT MANIPULATION (bit manipulation patterns 숙지하기)
*
* ** CANCEL OUT matching numbers by using XOR
* : when same integers are XOR-ed, they just become 0
* -> so pairs cancel each out and the one remaining integer is the unique one
* ( 0 ^ any number = any number)
*
* ** What I learned:
*
* ** BIT MANIPULATION PATTERNS
*
* (1) CANCEL OUT matching numbers using XOR
*
* (2) MULTIPLY / DIVIDE BY 2 (use LEFT SHIFT / RIGHT SHIFT)
*
* => think if you can use BIT MANIPULATION
* to optimize with problem dealing with INTEGERS
*
* * OPTIMIZE
* - initially, had the idea of using set to track pairs by inserting and erasing
* - only when ASKED TO OPTIMIZE TO O(1) space did I start to look for other solutions
*
* => need to learn how to grasp which side I should focus on optimizing
* : space or time ?
*
* ** practice to FIND OPTIMIZATION POINTS
* EVEN AFTER THE PROBLEM HAS BEEN SOLVED
* (learn try to take the problem ONE STEP FURTHER by trying to find any further optimization points)
*
*/
#include <bits/stdc++.h>
using namespace std;
/**
* find the one unique ID in the vector
*/
int findUniqueDeliveryId(const vector<int>& deliveryIds) {
int xor_result = 0;
/** XOR every deliveryId
* (to cancel out matching integers)
*/
for (auto deliveryId : deliveryIds) {
xor_result ^= deliveryId;
}
// the XOR-ed result is the unique deliveryId
return xor_result;
}