Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions Jongeun/Day27/133_CloneGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> neighbors;
Node() {
val = 0;
neighbors = vector<Node*>();
}
Node(int _val) {
val = _val;
neighbors = vector<Node*>();
}
Node(int _val, vector<Node*> _neighbors) {
val = _val;
neighbors = _neighbors;
}
};
*/

class Solution
{
public:
Node *cloneGraph(Node *node)
{
vector<Node *> nodes(101, nullptr);
vector<bool> visited(101, false);
queue<Node *> q;
q.push(node);

if (node == nullptr)
{
return nullptr;
}

while (!q.empty())
{
Node *cur = q.front();
q.pop();
vector<Node *> cpNeighbors;

for (auto n : cur->neighbors)
{
if (!visited[n->val])
{
q.push(n);
if (!nodes[n->val])
{
// Not exist
Node *temp = new Node(n->val);
nodes[n->val] = temp;
}
}

// Add a neighbor
cpNeighbors.push_back(nodes[n->val]);
}

if (!nodes[cur->val])
{
// not existed
Node *newNode = new Node(cur->val, cpNeighbors);
nodes[cur->val] = newNode;
visited[cur->val] = true;
}
else
{
nodes[cur->val]->neighbors = cpNeighbors;
visited[cur->val] = true;
}
}

return nodes[1];
}
};
16 changes: 16 additions & 0 deletions Jongeun/Day27/198_HouseRobber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public:
int rob(vector<int> &nums)
{
int first = 0, second = 0;
for (int i = 0; i < nums.size(); i++)
{
int tempt = first;
first = second;
second = max(second, tempt + nums[i]);
}

return second;
}
};
27 changes: 27 additions & 0 deletions Jongeun/Day27/55_JumpGame.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution
{
public:
bool canJump(vector<int> &nums)
{
int l = 0;
int maxIndex = 0;

while (l < nums.size() - 1)
{
maxIndex = max(maxIndex, l + nums[l]);
if (maxIndex >= nums.size() - 1)
{
return true;
}

if (l == maxIndex)
{
return false;
}

l++;
}

return true;
}
};