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
74 changes: 74 additions & 0 deletions Jongeun/Day22/51. N_Queens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
class Solution
{
public:
vector<vector<string>> solveNQueens(int n)
{
vector<vector<string>> result;
vector<string> board;
for (int i = 0; i < n; i++)
{
string temp = "";
for (int j = 0; j < n; j++)
{
temp.push_back('.');
}
board.push_back(temp);
}
vector<int> col(n + 1);
_solveNQueens(result, col, board, 0, n);

return result;
}

void _solveNQueens(vector<vector<string>> &result, vector<int> &col, vector<string> &board, int step, int n)
{
if (isPromising(col, step))
{
if (step == n)
{
// push the col
for (int i = 1; i < n + 1; i++)
{
board[i - 1][col[i] - 1] = 'Q';
}
result.push_back(board);

// restore
for (int i = 1; i < n + 1; i++)
{
board[i - 1][col[i] - 1] = '.';
}

return;
}
else
{
for (int j = 1; j < n + 1; j++)
{
col[step + 1] = j;
_solveNQueens(result, col, board, step + 1, n);
}
}
}
}

bool isPromising(vector<int> &col, int step)
{
if (step == 0)
{
return true;
}

int j = 1;
while (j < step)
{
if (col[step] == col[j] || abs(col[step] - col[j]) == (step - j))
{
return false;
}
j++;
}

return true;
}
};
16 changes: 16 additions & 0 deletions Jongeun/Day22/70_ClimbingStairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public:
int climbStairs(int n)
{
vector<int> table(46);
table[0] = 1;
table[1] = 1;
for (int i = 2; i < n + 1; i++)
{
table[i] = table[i - 1] + table[i - 2];
}

return table[n];
}
};
17 changes: 17 additions & 0 deletions Jongeun/Day22/746_MinCostClimbingStairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution
{
public:
int minCostClimbingStairs(vector<int> &cost)
{
int size = cost.size();
int first = 0;
int second = 0;
for (int i = 0; i < size - 1; i++)
{
int tempt = second;
second = min(first + cost[i], second + cost[i + 1]);
first = tempt;
}
return second;
}
};