Skip to content

Commit 4affe00

Browse files
More code
1 parent 282f533 commit 4affe00

File tree

4 files changed

+223
-14
lines changed

4 files changed

+223
-14
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## Count Set Bits with lookup table
2+
3+
To have a lookup table is a very fast way to check values. This is extremely useful for questions like [***Program to count number of set bits in an (big) array***](https://www.geeksforgeeks.org/program-to-count-number-of-set-bits-in-an-big-array/).
4+
5+
Basiclly we have an array of 256 entrys that record the number of set bits for 0 - 255 (Cover 8-bit representation). Then we could simply break 32-bit number into 4 parts and use the table to check for each 8 bit and sum it up.
6+
7+
```c++
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
11+
int BitsSetTable256[256];
12+
13+
// Function to initialise the lookup table
14+
void initialize()
15+
{
16+
17+
// To initially generate the
18+
// table algorithmically
19+
BitsSetTable256[0] = 0;
20+
for (int i = 0; i < 256; i++)
21+
{
22+
BitsSetTable256[i] = (i & 1) +
23+
BitsSetTable256[i / 2];
24+
}
25+
}
26+
27+
// Function to return the count
28+
// of set bits in n
29+
int countSetBits(int n)
30+
{
31+
unsigned char *num = (unsigned char *) &n;
32+
return (BitsSetTable256[num[0]] +
33+
BitsSetTable256[num[1]] +
34+
BitsSetTable256[num[2]] +
35+
BitsSetTable256[num[3]]);
36+
37+
/*return (BitsSetTable256[n & 0xff] +
38+
BitsSetTable256[(n >> 8) & 0xff] +
39+
BitsSetTable256[(n >> 16) & 0xff] +
40+
BitsSetTable256[n >> 24]);*/
41+
}
42+
43+
// Driver code
44+
int main()
45+
{
46+
// Initialise the lookup table
47+
initialize();
48+
int n = 9;
49+
cout << countSetBits(n);
50+
}
51+
```

Interview/Company/facebook/algorithm_prepare.md

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,16 @@
4646
## String
4747
1. Valid Anagram Easy
4848
2. Integer to English Words Hard
49+
3. Read 4 bytes Easy
50+
4. Read 4 bytes II (call multiple times) Hard
4951

5052
## Bits Manipulation
5153
1. Is Power of Four Easy
54+
2. Bitwise AND of Numbers Range Medium
5255

53-
## Data Structure
54-
1. LRU cache
56+
## Data Structure
57+
1. LRU cache Hard
58+
2. Exclusive Time of Functions Medium
5559

5660
## Implementation
5761

@@ -1767,4 +1771,149 @@ public:
17671771
return res;
17681772
}
17691773
};
1770-
```
1774+
```
1775+
1776+
### **Candy**
1777+
1778+
***Big O:*** O(1) speed, O(1) space
1779+
```
1780+
Tips:
1781+
1782+
Only need to count the prefix part of n and m.
1783+
```
1784+
```c++
1785+
class Solution {
1786+
public:
1787+
int rangeBitwiseAnd(int m, int n) {
1788+
while (m < n) {
1789+
n &= n - 1;
1790+
}
1791+
1792+
return m & n;
1793+
}
1794+
};
1795+
```
1796+
1797+
### **Read N Characters Given Read4**
1798+
1799+
***Big O:*** O(log(n)) speed, O(1) space
1800+
```
1801+
Tips:
1802+
1803+
Use a temperary string to store read4 results. Be careful with situation where read4 returns with fewer than 4 bytes.
1804+
```
1805+
```c++
1806+
class Solution {
1807+
public:
1808+
1809+
int read(char *buf, int n) {
1810+
int read_bytes = 0, read4_bytes = 4;
1811+
char tmp_buf[4];
1812+
1813+
while (read_bytes < n && read4_bytes == 4) {
1814+
read4_bytes = read4(tmp_buf);
1815+
1816+
for (int i = 0; i < read4_bytes; i++) {
1817+
if (read_bytes >= n)
1818+
return read_bytes;
1819+
*buf++ = tmp_buf[i];
1820+
read_bytes ++;
1821+
}
1822+
}
1823+
1824+
return read_bytes;
1825+
}
1826+
};
1827+
```
1828+
1829+
### **Read N Characters Given Read4 II (call multiple times)**
1830+
1831+
***Big O:*** O(log(n)) speed, O(1) space
1832+
```
1833+
Tips:
1834+
1835+
Use private data variables to keep track of the leftover position from last call.
1836+
```
1837+
```c++
1838+
class Solution {
1839+
int pos = 0, read4_bytes = 0;
1840+
char tmp_buf[4];
1841+
1842+
public:
1843+
int read(char *buf, int n) {
1844+
int read_bytes = 0;
1845+
1846+
// if there is leftover
1847+
while (pos < read4_bytes && read_bytes < n) {
1848+
*buf++ = tmp_buf[pos++];
1849+
read_bytes ++;
1850+
}
1851+
1852+
while (read_bytes < n) {
1853+
read4_bytes = read4(tmp_buf);
1854+
1855+
for (pos = 0; pos < read4_bytes;) {
1856+
if (read_bytes >= n)
1857+
return read_bytes;
1858+
1859+
*buf++ = tmp_buf[pos++];
1860+
read_bytes ++;
1861+
}
1862+
1863+
if (read4_bytes != 4)
1864+
break;
1865+
}
1866+
1867+
return read_bytes;
1868+
}
1869+
};
1870+
```
1871+
1872+
### **Exclusive Time of Functions**
1873+
1874+
***Big O:*** O(n) speed, O(n) space
1875+
```
1876+
Tips:
1877+
1878+
Use of stack. When you update the time, remember to deduce the duplicate time part of the top item in the stack.
1879+
```
1880+
```c++
1881+
class Solution {
1882+
struct Log {
1883+
int id;
1884+
bool isStart;
1885+
int time;
1886+
};
1887+
1888+
Log getLog(string& s) {
1889+
string id, isStart, time;
1890+
stringstream ss(s);
1891+
getline(ss, id, ':');
1892+
getline(ss, isStart, ':');
1893+
getline(ss, time, ':');
1894+
1895+
return {stoi(id), isStart == "start", stoi(time)};
1896+
}
1897+
1898+
public:
1899+
vector<int> exclusiveTime(int n, vector<string>& logs) {
1900+
vector<int> exclusive(n, 0);
1901+
stack<Log> s;
1902+
1903+
for(auto& log: logs) {
1904+
Log l = getLog(log);
1905+
if(l.isStart)
1906+
s.push(l);
1907+
else {
1908+
int time = l.time - s.top().time + 1;
1909+
exclusive[l.id] += time;
1910+
1911+
s.pop();
1912+
if(!s.empty())
1913+
exclusive[s.top().id] -= time;
1914+
}
1915+
}
1916+
1917+
return exclusive;
1918+
}
1919+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Leetcode problems with OS flavor
2+
3+
1. Read 4 bytes Easy
4+
2. Read 4 bytes II (call multiple times) Hard
5+
3. LRU cache Hard
6+
4. LFU cache Hard
7+
5. Exclusive Time of Functions Medium
8+
6. Task Scheduler Medium

README.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,18 @@
9090
13. Bits Manipulation
9191
1. [Reverse Bits](Data_Struct_Implementation/BitsManipulation/reverseBits.md)
9292
2. Flip a monochrome bitmap
93-
3. [Length of the Longest Consecutive 1s in Binary Representation](https://www.geeksforgeeks.org/length-longest-consecutive-1s-binary-representation/)
94-
4. [Find most significant set bit of a number](https://www.geeksforgeeks.org/find-significant-set-bit-number/)
95-
5. [Swap two number without temporary varaible](https://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/)
96-
6. [Number is a power of 2](https://www.geeksforgeeks.org/bits-manipulation-important-tactics/)
97-
7. [Swap all odd and even bits](Data_Struct_Implementation/BitsManipulation/swapOddEvenBIts.md)
98-
8. [Find position of the only set bit](Data_Struct_Implementation/BitsManipulation/positionOfSetBit.md)
99-
9. [Find the element that appears once and other elements appear 3 times](Data_Struct_Implementation/BitsManipulation/singleNumber.md)
100-
10. [Signness Check for two integers](Data_Struct_Implementation/BitsManipulation/signessCheck.md)
101-
11. [Turn off the rightmost set bit](Data_Struct_Implementation/BitsManipulation/turnoffRightmostBit.md)
102-
12. [Rotate bits to left/rigtht](Data_Struct_Implementation/BitsManipulation/rotateLeftRight.md)
103-
13. [Swap bits in a given number](Data_Struct_Implementation/BitsManipulation/SwapBitsInNumber.md)
93+
3. [Count bits with lookup table](Data_Struct_Implementation\BitsManipulation\countBitsLookUpTable.md)
94+
4. [Length of the Longest Consecutive 1s in Binary Representation](https://www.geeksforgeeks.org/length-longest-consecutive-1s-binary-representation/)
95+
5. [Find most significant set bit of a number](https://www.geeksforgeeks.org/find-significant-set-bit-number/)
96+
6. [Swap two number without temporary varaible](https://www.geeksforgeeks.org/swap-two-numbers-without-using-temporary-variable/)
97+
7. [Number is a power of 2](https://www.geeksforgeeks.org/bits-manipulation-important-tactics/)
98+
8. [Swap all odd and even bits](Data_Struct_Implementation/BitsManipulation/swapOddEvenBIts.md)
99+
9. [Find position of the only set bit](Data_Struct_Implementation/BitsManipulation/positionOfSetBit.md)
100+
10. [Find the element that appears once and other elements appear 3 times](Data_Struct_Implementation/BitsManipulation/singleNumber.md)
101+
11. [Signness Check for two integers](Data_Struct_Implementation/BitsManipulation/signessCheck.md)
102+
12. [Turn off the rightmost set bit](Data_Struct_Implementation/BitsManipulation/turnoffRightmostBit.md)
103+
13. [Rotate bits to left/rigtht](Data_Struct_Implementation/BitsManipulation/rotateLeftRight.md)
104+
14. [Swap bits in a given number](Data_Struct_Implementation/BitsManipulation/SwapBitsInNumber.md)
104105
14. Concurrency
105106
1. Implement a Spinlock/Mutex/Semaphone
106107
1. [Test-and-set](https://en.wikipedia.org/wiki/Test-and-set)

0 commit comments

Comments
 (0)