Skip to content
This repository was archived by the owner on May 16, 2021. It is now read-only.

Commit 7d3d406

Browse files
committed
8 problems
1 parent 1c8fdb5 commit 7d3d406

8 files changed

+612
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <string>
2+
#include <vector>
3+
using std::string;
4+
using std::vector;
5+
6+
class Solution {
7+
public:
8+
int minDistance(string word1, string word2) {
9+
return word1.size() + word2.size() - 2 * LCS(word1, word2);
10+
}
11+
private:
12+
int LCS(const string &s1, const string &s2) {
13+
int ls1 = s1.size();
14+
int ls2 = s2.size();
15+
if (ls1 == 0 || ls2 == 0) {
16+
return 0;
17+
}
18+
if (ls1 < ls2) {
19+
return LCS(s2, s1);
20+
}
21+
22+
vector<vector<int>> dp(2, vector<int>(ls2 + 1, 0));
23+
int f, nf;
24+
int i, j;
25+
26+
f = 0;
27+
nf = !f;
28+
for (i = 1; i <= ls1; ++i) {
29+
dp[f][0] = 0;
30+
for (j = 1; j <= ls2; ++j) {
31+
if (s1[i - 1] == s2[j - 1]) {
32+
dp[f][j] = dp[nf][j - 1] + 1;
33+
} else {
34+
dp[f][j] = max(dp[f][j - 1], dp[nf][j]);
35+
}
36+
}
37+
f = !f;
38+
nf = !f;
39+
}
40+
int res = dp[nf][ls2];
41+
dp.clear();
42+
43+
return res;
44+
}
45+
};

design-in-memory-file-system_1_AC.cpp

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
// I hate multiple inheritance, but I hate this problem even more.
2+
// So be it.
3+
#include <map>
4+
#include <string>
5+
#include <vector>
6+
using std::map;
7+
using std::string;
8+
using std::vector;
9+
10+
static const int TYPE_FILE = 1;
11+
static const int TYPE_DIR = 2;
12+
13+
class Node {
14+
public:
15+
Node(const string &_name): name(_name) {}
16+
17+
string getName() {
18+
return name;
19+
}
20+
21+
virtual int type() = 0;
22+
protected:
23+
string name;
24+
};
25+
26+
class FileInterface {
27+
public:
28+
virtual string getContent() = 0;
29+
virtual void appendContent(const string &s) = 0;
30+
virtual void setContent(const string &s) = 0;
31+
};
32+
33+
class FileNode: public Node, public FileInterface {
34+
public:
35+
FileNode(const string &_name): Node(_name), content("") {}
36+
37+
virtual int type() {
38+
return TYPE_FILE;
39+
}
40+
41+
virtual string getContent() {
42+
return content;
43+
}
44+
45+
virtual void appendContent(const string &s) {
46+
content += s;
47+
}
48+
49+
virtual void setContent(const string &s) {
50+
content = s;
51+
}
52+
protected:
53+
string content;
54+
};
55+
56+
class DirInterface {
57+
public:
58+
virtual Node *getChild(const string &_name) = 0;
59+
virtual void addChild(const string &_name, Node *_node) = 0;
60+
virtual vector<string> getList() = 0;
61+
};
62+
63+
class DirNode: public Node, public DirInterface {
64+
public:
65+
DirNode(const string &_name): Node(_name) {}
66+
67+
virtual int type() {
68+
return TYPE_DIR;
69+
}
70+
71+
virtual Node *getChild(const string &_name) {
72+
if (child.find(_name) != child.end()) {
73+
return child[_name];
74+
} else {
75+
return NULL;
76+
}
77+
}
78+
79+
virtual void addChild(const string &_name, Node *_node) {
80+
if (_name == "" || _node == NULL) {
81+
return;
82+
}
83+
84+
Node *ptr = getChild(_name);
85+
if (ptr != NULL) {
86+
delete ptr;
87+
}
88+
child[_name] = _node;
89+
}
90+
91+
virtual vector<string> getList() {
92+
vector<string> res;
93+
for (auto &p: child) {
94+
res.push_back(p.first);
95+
}
96+
return res;
97+
}
98+
protected:
99+
map<string, Node *> child;
100+
};
101+
102+
class FileSystem {
103+
public:
104+
FileSystem() {
105+
root = new DirNode("");
106+
}
107+
108+
vector<string> ls(string path) {
109+
vector<string> vt = tokenize(path);
110+
Node *ptr = root;
111+
Node *ptr1;
112+
113+
// Needed for type casting
114+
DirInterface *pd;
115+
FileInterface *pf;
116+
117+
for (auto &tk: vt) {
118+
if (ptr->type() != TYPE_DIR) {
119+
return vector<string>();
120+
}
121+
pd = (DirNode *)ptr;
122+
if ((ptr1 = pd->getChild(tk)) == NULL) {
123+
return vector<string>();
124+
}
125+
ptr = ptr1;
126+
}
127+
if (ptr->type() == TYPE_DIR) {
128+
pd = (DirNode *)ptr;
129+
return pd->getList();
130+
} else {
131+
vector<string> res;
132+
res.push_back(ptr->getName());
133+
return res;
134+
}
135+
}
136+
137+
void mkdir(string path) {
138+
vector<string> vt = tokenize(path);
139+
Node *ptr = root;
140+
Node *ptr1;
141+
142+
// Needed for type casting
143+
DirInterface *pd;
144+
FileInterface *pf;
145+
146+
for (auto &tk: vt) {
147+
pd = (DirNode *)ptr;
148+
if ((ptr1 = pd->getChild(tk)) == NULL) {
149+
ptr1 = new DirNode(tk);
150+
pd->addChild(tk, ptr1);
151+
}
152+
ptr = ptr1;
153+
}
154+
}
155+
156+
void addContentToFile(string filePath, string content) {
157+
vector<string> vt = tokenize(filePath);
158+
Node *ptr = root;
159+
Node *ptr1;
160+
161+
// Needed for type casting
162+
DirInterface *pd;
163+
FileInterface *pf;
164+
165+
int n = vt.size();
166+
int i;
167+
for (i = 0; i < n - 1; ++i) {
168+
pd = (DirNode *)ptr;
169+
if ((ptr1 = pd->getChild(vt[i])) == NULL) {
170+
ptr1 = new DirNode(vt[i]);
171+
pd->addChild(vt[i], ptr1);
172+
}
173+
ptr = ptr1;
174+
}
175+
pd = (DirNode *)ptr;
176+
if ((ptr1 = pd->getChild(vt[n - 1])) == NULL) {
177+
ptr1 = new FileNode(vt[n - 1]);
178+
pd->addChild(vt[n - 1], ptr1);
179+
}
180+
ptr = ptr1;
181+
182+
if (ptr->type() == TYPE_DIR) {
183+
// Error
184+
return;
185+
}
186+
pf = (FileNode *)ptr;
187+
pf->appendContent(content);
188+
}
189+
190+
string readContentFromFile(string filePath) {
191+
vector<string> vt = tokenize(filePath);
192+
Node *ptr = root;
193+
Node *ptr1;
194+
195+
// Needed for type casting
196+
DirInterface *pd;
197+
FileInterface *pf;
198+
199+
for (auto &tk: vt) {
200+
pd = (DirNode *)ptr;
201+
if ((ptr1 = pd->getChild(tk)) == NULL) {
202+
// Error
203+
return "";
204+
}
205+
ptr = ptr1;
206+
}
207+
if (ptr->type() != TYPE_FILE) {
208+
// Error
209+
return "";
210+
}
211+
pf = (FileNode *)ptr;
212+
return pf->getContent();
213+
}
214+
private:
215+
Node *root;
216+
217+
vector<string> tokenize(const string &s) {
218+
vector<string> v;
219+
string tk;
220+
221+
int ls = s.size();
222+
int i = 0;
223+
while (true) {
224+
while (i < ls && s[i] == '/') {
225+
++i;
226+
}
227+
if (i >= ls) {
228+
break;
229+
}
230+
while (i < ls && s[i] != '/') {
231+
tk.push_back(s[i++]);
232+
}
233+
v.push_back(tk);
234+
tk.clear();
235+
if (i >= ls) {
236+
break;
237+
}
238+
}
239+
240+
return v;
241+
}
242+
};
243+
244+
/**
245+
* Your FileSystem object will be instantiated and called as such:
246+
* FileSystem obj = new FileSystem();
247+
* vector<string> param_1 = obj.ls(path);
248+
* obj.mkdir(path);
249+
* obj.addContentToFile(filePath,content);
250+
* string param_4 = obj.readContentFromFile(filePath);
251+
*/

erect-the-fence_1_AC.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Damn, convex hull.
2+
/**
3+
* Definition for a point.
4+
* struct Point {
5+
* int x;
6+
* int y;
7+
* Point() : x(0), y(0) {}
8+
* Point(int a, int b) : x(a), y(b) {}
9+
* };
10+
*/
11+
#include <algorithm>
12+
#include <vector>
13+
using std::sort;
14+
using std::vector;
15+
16+
int crossProduct(const Point &p1, const Point &p2, const Point &p3)
17+
{
18+
return (p2.x - p1.x) * (p3.y - p1.y) - (p3.x - p1.x) * (p2.y - p1.y);
19+
}
20+
21+
Point mp;
22+
bool comparator(const Point &p1, const Point &p2)
23+
{
24+
// Left turn
25+
int d = crossProduct(mp, p1, p2);
26+
if (d != 0) {
27+
return d > 0;
28+
}
29+
d = abs(p1.x - mp.x) + abs(p1.y - mp.y) - abs(p2.x - mp.x) - abs(p2.y - mp.y);
30+
if (d != 0) {
31+
return d < 0;
32+
}
33+
return false;
34+
}
35+
36+
class Solution {
37+
public:
38+
vector<Point> outerTrees(vector<Point> &points) {
39+
auto &vp = points;
40+
int n = vp.size();
41+
if (n < 3) {
42+
return vp;
43+
}
44+
45+
int mi = 0;
46+
int i;
47+
for (i = 1; i < n; ++i) {
48+
if (vp[i].x < vp[mi].x || (vp[i].x == vp[mi].x && vp[i].y < vp[mi].y)) {
49+
mi = i;
50+
}
51+
}
52+
swap(vp[0], vp[mi]);
53+
mp = vp[0];
54+
sort(vp.begin() + 1, vp.end(), comparator);
55+
56+
vector<Point> res;
57+
res.push_back(mp);
58+
59+
int pr;
60+
for (i = 1; i <= n; ++i) {
61+
while (res.size() >= 2 &&
62+
(pr = crossProduct(res[res.size() - 2], res[res.size() - 1], vp[i % n])) < 0) {
63+
res.pop_back();
64+
}
65+
res.push_back(vp[i % n]);
66+
}
67+
res.pop_back();
68+
if (res.size() == n) {
69+
return res;
70+
}
71+
72+
// Check for colinear points that have been left out.
73+
i = n - 2;
74+
while (i >= 1 && crossProduct(mp, vp[i], vp[n - 1]) == 0) {
75+
res.push_back(vp[i]);
76+
--i;
77+
}
78+
79+
return res;
80+
}
81+
};

0 commit comments

Comments
 (0)