Skip to content

Commit adaca63

Browse files
author
Jie Feng
committed
haha
1 parent 94d244a commit adaca63

File tree

14 files changed

+270
-81
lines changed

14 files changed

+270
-81
lines changed

codeforces/437B

-99.4 KB
Binary file not shown.

codeforces/437B.o

-183 KB
Binary file not shown.

codeforces/438C

-151 KB
Binary file not shown.

codeforces/438C.o

-205 KB
Binary file not shown.

codeforces/temp.o

-63.1 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include<iostream>
2+
#include<vector>
3+
using namespace std;
4+
5+
6+
7+
bool find_element_in_matrix(vector<vector<int> >a, int x){
8+
int n = a.size();
9+
if (n == 0) return false;
10+
int m = a[0].size();
11+
12+
int iL = 0, iR = n-1;
13+
while(iL <= iR){
14+
int iM = (iL + iR) >> 1;
15+
if (a[iM][0] > x)
16+
iR = iM - 1;
17+
else iL = iM+1;
18+
}
19+
if (iR == -1) return false;
20+
21+
int jL = 0, jR = m-1;
22+
while(jL <= jR){
23+
int jM = (jL + jR) >> 1;
24+
if (a[iR][jM] > x)
25+
jR = jM - 1;
26+
else jL = jM + 1;
27+
}
28+
if (jR == -1 || a[iR][jR] != x) return false;
29+
else return true;
30+
}

interview/lru-cache.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <map>
2+
using namespace std;
3+
4+
struct DoubleList{
5+
int key;
6+
int val;
7+
DoubleList *next, *pre;
8+
DoubleList():next(NULL), pre(NULL){}
9+
DoubleList(int key, int val):key(key), val(val), next(NULL), pre(NULL){
10+
}
11+
};
12+
13+
class LRUCache{
14+
private:
15+
DoubleList *head, *tail;
16+
map<int, DoubleList*> M;
17+
int capacity;
18+
public:
19+
LRUCache(int capacity):capacity(capacity) {
20+
head = new DoubleList();
21+
tail = new DoubleList();
22+
head->next = tail;
23+
tail->pre = head;
24+
}
25+
void disConnect(DoubleList* cur){
26+
cur->pre->next = cur->next;
27+
cur->next->pre = cur->pre;
28+
}
29+
void insertHead(DoubleList* cur){
30+
cur->next = head->next;
31+
head->next->pre = cur;
32+
33+
head->next = cur;
34+
cur->pre = head;
35+
}
36+
int get(int key) {
37+
if (M.find(key) == M.end()) return -1;
38+
else{
39+
DoubleList* cur = M[key];
40+
if (cur != head->next){
41+
disConnect(cur);
42+
insertHead(cur);
43+
}
44+
return cur->val;
45+
}
46+
}
47+
48+
void eraseLast(){
49+
DoubleList* erase = tail->pre;
50+
M.erase(erase->key); // !!!
51+
52+
erase->pre->next = tail;
53+
tail->pre = erase->pre;
54+
55+
free(erase);
56+
}
57+
void add(int key, int value){
58+
DoubleList* cur = new DoubleList(key, value);
59+
M[key] = cur;
60+
insertHead(cur);
61+
}
62+
void set(int key, int value) {
63+
if (M.find(key) == M.end()){
64+
if (M.size() == capacity)
65+
eraseLast();
66+
add(key, value);
67+
}else {
68+
DoubleList* cur = M[key];
69+
disConnect(cur);
70+
insertHead(cur);
71+
cur->val = value;
72+
}
73+
}
74+
};

interview/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
target= convert-BST-to-doublelinkedlist
1+
target= lru-cache
22

33
all:
44
#g++ -o $(target) -g $(target).cpp -w
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#include<string>
2+
#include<iostream>
3+
#include<queue>
4+
#include<vector>
5+
#include<map>
6+
#include<set>
7+
using namespace std;
8+
9+
10+
11+
class solution{
12+
private:
13+
int totalIndex = 0;
14+
int n;
15+
map<string, int> label2Index;
16+
vector<string> index2Label;
17+
vector<int> indegree;
18+
vector<set<int> > edges;
19+
vector<bool> visited;
20+
inline int setIndex(string s){
21+
if(label2Index.find(s) == label2Index.end())
22+
label2Index[s] = totalIndex++;
23+
}
24+
void dumpIndex(set<pair<string,string> > P){
25+
for (auto i = P.begin(); i != P.end(); i++){
26+
setIndex(i->first);
27+
setIndex(i->second);
28+
}
29+
30+
n = label2Index.size();
31+
index2Label = vector<string>(n);
32+
indegree = vector<int>(n, 0);
33+
edges = vector<set<int> >(n);
34+
35+
for (auto i = label2Index.begin(); i != label2Index.end(); i++){
36+
index2Label[i->second] = i->first;
37+
}
38+
}
39+
public:
40+
solution(){
41+
}
42+
bool validTree(int x){
43+
visited = vector<bool>(n, false);
44+
queue<int> Q;
45+
Q.push(x);
46+
int visitedNum = 0;
47+
while(!Q.empty()){
48+
int u = Q.front();
49+
Q.pop();
50+
visited[u] = true;
51+
visitedNum++;
52+
for (auto i = edges[u].begin(); i != edges[u].end(); i++){
53+
if (!visited[*i]){
54+
Q.push(*i);
55+
}else return false;
56+
}
57+
}
58+
return visitedNum == n;
59+
}
60+
void printTree(int u, int level){
61+
cout << string(level, ' ') << index2Label[u] << endl;
62+
for (auto i = edges[u].begin(); i != edges[u].end(); i++){
63+
printTree(*i, level+1);
64+
}
65+
}
66+
void printTreeFromPairSet(set<pair<string,string> > P){
67+
dumpIndex(P);
68+
for (auto i = P.begin(); i != P.end(); i++){
69+
int i1 = label2Index[i->first];
70+
int i2 = label2Index[i->second];
71+
edges[i1].insert(i2);
72+
indegree[i2]++;
73+
}
74+
75+
int head = -1;
76+
int countHeads = 0;
77+
for (int i = 0; i < indegree.size(); i++){
78+
if (indegree[i] == 0){
79+
head = i;
80+
countHeads++;
81+
}
82+
}
83+
if (countHeads != 1 || !validTree(head)){
84+
cout << "These pairs can't consist a Tree!!!" << endl;
85+
}else{
86+
printTree(head, 0);
87+
}
88+
}
89+
};
90+
91+
int main(){
92+
set<pair<string, string> > P;
93+
P.insert(make_pair("a", "b"));
94+
P.insert(make_pair("b", "c"));
95+
P.insert(make_pair("a", "d"));
96+
P.insert(make_pair("d", "e"));
97+
P.insert(make_pair("d", "f"));
98+
P.insert(make_pair("d", "g"));
99+
solution s;
100+
s.printTreeFromPairSet(P);
101+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
int search(int A[], int n, int target) {
4+
int L = 0, R = n-1;
5+
if (target >= A[0]){
6+
while(L <= R){
7+
int M = (L + R) >> 1;
8+
if (A[M] < A[0]) R = M - 1;
9+
else if (target < A[M]) R = M - 1;
10+
else L = M + 1;
11+
}
12+
if (R != n && A[R] == target) return R;
13+
else return -1;
14+
}else{
15+
while(L <= R){
16+
int M = (L + R) >> 1;
17+
if (A[M] >= A[0]) L = M + 1;
18+
else if (target < A[M]) R = M - 1;
19+
else L = M + 1;
20+
}
21+
if (R != n && A[R] == target) return R;
22+
else return -1;
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)