Skip to content

Commit 41f80a4

Browse files
authored
Merge pull request #90 from AlgorithmIsMyLife/27일차_Jongeun
Add day27
2 parents 69f1937 + 2236c1a commit 41f80a4

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

Jongeun/Day27/133_CloneGraph.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> neighbors;
7+
Node() {
8+
val = 0;
9+
neighbors = vector<Node*>();
10+
}
11+
Node(int _val) {
12+
val = _val;
13+
neighbors = vector<Node*>();
14+
}
15+
Node(int _val, vector<Node*> _neighbors) {
16+
val = _val;
17+
neighbors = _neighbors;
18+
}
19+
};
20+
*/
21+
22+
class Solution
23+
{
24+
public:
25+
Node *cloneGraph(Node *node)
26+
{
27+
vector<Node *> nodes(101, nullptr);
28+
vector<bool> visited(101, false);
29+
queue<Node *> q;
30+
q.push(node);
31+
32+
if (node == nullptr)
33+
{
34+
return nullptr;
35+
}
36+
37+
while (!q.empty())
38+
{
39+
Node *cur = q.front();
40+
q.pop();
41+
vector<Node *> cpNeighbors;
42+
43+
for (auto n : cur->neighbors)
44+
{
45+
if (!visited[n->val])
46+
{
47+
q.push(n);
48+
if (!nodes[n->val])
49+
{
50+
// Not exist
51+
Node *temp = new Node(n->val);
52+
nodes[n->val] = temp;
53+
}
54+
}
55+
56+
// Add a neighbor
57+
cpNeighbors.push_back(nodes[n->val]);
58+
}
59+
60+
if (!nodes[cur->val])
61+
{
62+
// not existed
63+
Node *newNode = new Node(cur->val, cpNeighbors);
64+
nodes[cur->val] = newNode;
65+
visited[cur->val] = true;
66+
}
67+
else
68+
{
69+
nodes[cur->val]->neighbors = cpNeighbors;
70+
visited[cur->val] = true;
71+
}
72+
}
73+
74+
return nodes[1];
75+
}
76+
};

Jongeun/Day27/198_HouseRobber.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution
2+
{
3+
public:
4+
int rob(vector<int> &nums)
5+
{
6+
int first = 0, second = 0;
7+
for (int i = 0; i < nums.size(); i++)
8+
{
9+
int tempt = first;
10+
first = second;
11+
second = max(second, tempt + nums[i]);
12+
}
13+
14+
return second;
15+
}
16+
};

Jongeun/Day27/55_JumpGame.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution
2+
{
3+
public:
4+
bool canJump(vector<int> &nums)
5+
{
6+
int l = 0;
7+
int maxIndex = 0;
8+
9+
while (l < nums.size() - 1)
10+
{
11+
maxIndex = max(maxIndex, l + nums[l]);
12+
if (maxIndex >= nums.size() - 1)
13+
{
14+
return true;
15+
}
16+
17+
if (l == maxIndex)
18+
{
19+
return false;
20+
}
21+
22+
l++;
23+
}
24+
25+
return true;
26+
}
27+
};

0 commit comments

Comments
 (0)