-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgos_v2.cpp
84 lines (73 loc) · 2.48 KB
/
algos_v2.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <bits/stdc++.h>
using namespace std;
/*
** It is ok if this class remains an empty class and just contains comments
** explaining certain algorithms that DO NOT QUITE FIT INTO SPECIFIC UTIL CLASSES
** having said that, having method is a plus++ :P
*/
class AlgosV2 {
public:
/*
** Monotonous Stack problems:
** IMP ones being: rain water trapping and area in histogram
** In mono stack problems what we do is we push an element in stack if some condition is met,
** else we start poping elements untill condition is met,
** meanwhile calculate the possible res using the elements that are getting popped
** since these elements we won't encounter again.
** In this way mono stack approach sometimes results in O(n) time complexity
** for eg. see problem_solutions::trap for rain water problem
*/
/*
** In a array with each number occuring twice accept TWO numbers.
** Find those two numbers
** Algo:
** res = xor all (this will be a^b)
** i = first set bit of res; (there will be one since two number are different)
** start xoring res with those which have ith bit set after traversing res = a
** start xoring res with those which have ith bit unset after traversing res = b
*/
/*
** Single Number occurs once, rest thrice
** Look at the handing for INT_MIN, since its binary represntation is: [1..(31 0s)..]
** so we build result from first 31 bits, then handle sign
*/
int singleNumber(vector<int>& nums) {
vector<int> bits(32, 0);
for (long a: nums) {
for (int i=0; i<32; i++) {
if (a & (1 << i)) {
bits[i] = (bits[i] + 1) % 3;
}
}
}
int res = 0;
for (int i =30; i>=0; i--) {
res = 2 * res + bits[i];
}
return bits[31] ? res ^ INT_MIN : res;
}
/*
** Boyer-Moore Voting Algorithm
** Return an item which appears more than n/2 times
** Linear time and O(1) space!!
** Since the element appears more than n/2 times
**
*/
int majorityElement(vector<int>& A) {
int n = A.size();
int cur = A[0];
int count = 1;
for (int i=1; i<n; i++) {
if (A[i] == cur) {
count++;
} else {
count--;
}
if (count == 0) {
cur = A[i];
count = 1;
}
}
return cur;
}
};