Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

69-9kyo-hwang #232

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const vector<pair<int, int>> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

vector<vector<int>> Board;

bool OutOfBound(int r, int c)
{
return r < 0 || r >= Board.size() || c < 0 || c >= Board[0].size();
}

int DFS(pair<int, int> ALoc, pair<int, int> BLoc)
{
const auto& [r, c] = ALoc;
if(Board[r][c] == 0)
{
return 0;
}

int Answer = 0;
Board[r][c] = 0;

for(const auto& [dr, dc] : Offset)
{
int nr = r + dr, nc = c + dc;
if(OutOfBound(nr, nc) || Board[nr][nc] == 0)
{
continue;
}

int RetVal = DFS(BLoc, {nr, nc}) + 1;

if(Answer % 2 == 0 && RetVal % 2 == 1) Answer = RetVal;
else if(Answer % 2 == 0 && RetVal % 2 == 0) Answer = max(Answer, RetVal);
else if(Answer % 2 == 1 && RetVal % 2 == 1) Answer = min(Answer, RetVal);
}

Board[r][c] = 1;
return Answer;
}

int solution(vector<vector<int>> InBoard, vector<int> InALoc, vector<int> InBLoc)
{
Board = InBoard;
return DFS({InALoc[0], InALoc[1]}, {InBLoc[0], InBLoc[1]});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <string>
#include <vector>
#include <functional>
#include <set>

using namespace std;
using FPoint = pair<int, int>;

vector<vector<FPoint>> GetBlocksFrom(vector<vector<int>>& Board, bool IsBoard = true)
{
const vector<FPoint> Offset{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
auto OutOfBound = [&](int x, int y)
{
return x < 0 || x >= Board.size() || y < 0 || y >= Board.size();
};

vector<vector<FPoint>> Blocks;

function<void(int, int)> DFS = [&](int x, int y)
{
Blocks.back().emplace_back(x, y);
Board[x][y] = (IsBoard ? 1 : 0);

for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(!OutOfBound(nx, ny) && Board[nx][ny] == (IsBoard ? 0 : 1))
{
DFS(nx, ny);
}
}
};

for(int i = 0; i < Board.size(); ++i)
{
for(int j = 0; j < Board.size(); ++j)
{
if(IsBoard && Board[i][j] == 0
|| !IsBoard && Board[i][j] == 1)
{
Blocks.push_back({});
DFS(i, j);
}
}
}

return Blocks;
}

vector<vector<int>> GetGridFrom(const vector<FPoint>& Block)
{
auto [MinX, MinY] = Block[0];
auto [MaxX, MaxY] = Block[0];

for(const auto& [x, y] : Block)
{
if(x < MinX) MinX = x;
else if(x > MaxX) MaxX = x;

if(y < MinY) MinY = y;
else if(y > MaxY) MaxY = y;
}

vector<vector<int>> Grid(MaxX - MinX + 1, vector<int>(MaxY - MinY + 1, 0));
for(const auto& [x, y] : Block)
{
Grid[x - MinX][y - MinY] = 1;
}

return Grid;
}

int Rotate(vector<vector<int>>& Grid)
{
vector<vector<int>> Rotated(Grid[0].size(), vector<int>(Grid.size(), 0));
int NumCell = 0;

for(int i = 0; i < Grid.size(); ++i)
{
for(int j = 0; j < Grid[0].size(); ++j)
{
if(Grid[i][j] == 1) NumCell++;
Rotated[j][Grid.size() - 1 - i] = Grid[i][j];
}
}

Grid = Rotated;
return NumCell;
}

int solution(vector<vector<int>> InGameBoard, vector<vector<int>> InTable)
{
vector<vector<FPoint>> BoardBlocks = GetBlocksFrom(InGameBoard, true);
vector<vector<FPoint>> TableBlocks = GetBlocksFrom(InTable, false);

set<vector<FPoint>> UsedTableBlocks;
int Answer = 0;

for(const vector<FPoint>& BoardBlock : BoardBlocks)
{
bool IsFilled = false;
vector<vector<int>> BoardBlockGrid = GetGridFrom(BoardBlock);

for(const vector<FPoint>& TableBlock : TableBlocks)
{
if(IsFilled) break;
if(UsedTableBlocks.count(TableBlock)) continue;

vector<vector<int>> TableBlockGrid = GetGridFrom(TableBlock);
for(int i = 0; i < 4; ++i)
{
int NumCell = Rotate(TableBlockGrid);
if(BoardBlockGrid == TableBlockGrid)
{
Answer += NumCell;
UsedTableBlocks.emplace(TableBlock);
IsFilled = true;
break;
}
}
}
}

return Answer;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <string>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

struct Comparator
{
bool operator()(const pair<int, int>& Lhs, const pair<int, int>& Rhs) const
{
const auto& [LhsCard, LhsCoin] = Lhs;
const auto& [RhsCard, RhsCoin] = Rhs;

return LhsCoin == RhsCoin ? LhsCard < RhsCard : LhsCoin < RhsCoin;
}
};

int solution(int InCoin, vector<int> InCards)
{
const int N = (int)InCards.size();
const int RequiredSum = N + 1;
reverse(InCards.begin(), InCards.end());

set<pair<int, int>, Comparator> Deck;
for(int i = 0; i < N / 3; ++i)
{
Deck.emplace(InCards.back(), 0); InCards.pop_back();
}

int Round;
for(Round = 1; !InCards.empty(); ++Round)
{
Deck.emplace(InCards.back(), 1); InCards.pop_back();
Deck.emplace(InCards.back(), 1); InCards.pop_back();

bool FindCombination = false;
for(const auto& [Card, Coin] : Deck)
{
int TargetCard = RequiredSum - Card;
if(Card == TargetCard)
{
continue;
}

int TargetCoin;
if(Deck.count({TargetCard, 0}))
{
TargetCoin = 0;
}
else if(Deck.count({TargetCard, 1}))
{
TargetCoin = 1;
}
else
{
continue;
}

if(Coin + TargetCoin <= InCoin)
{
InCoin -= (Coin + TargetCoin);
Deck.erase({Card, Coin});
Deck.erase({TargetCard, TargetCoin});
FindCombination = true;
break;
}
}

if(false == FindCombination)
{
break;
}
}

return Round;
}
33 changes: 33 additions & 0 deletions 9-kyo-hwang/Greedy/๋””ํŽœ์Šค ๊ฒŒ์ž„.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <string>
#include <vector>
#include <queue>

using namespace std;

int solution(int N, int K, vector<int> InEnemies)
{
priority_queue<int> NumofEnemiesBlocked;
int Round = 0;

for(int Enemy : InEnemies)
{
N -= Enemy;
NumofEnemiesBlocked.emplace(Enemy);

if(N < 0)
{
if(K == 0)
{
break;
}

N += NumofEnemiesBlocked.top();
NumofEnemiesBlocked.pop();
K--;
}

Round++;
}

return Round;
}
7 changes: 6 additions & 1 deletion 9-kyo-hwang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@
| 61์ฐจ์‹œ | 2024.8.08 | Implementation | [ํ…Œ์ด๋ธ” ํ•ด์‹œ ํ•จ์ˆ˜](https://school.programmers.co.kr/learn/courses/30/lessons/147354) | [#214](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/214) |
| 62์ฐจ์‹œ | 2024.8.12 | Graph Traversal | [๋ฌด์ธ๋„ ์—ฌํ–‰](https://school.programmers.co.kr/learn/courses/30/lessons/154540) | [#217](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/217) |
| 63์ฐจ์‹œ | 2024.9.3 | Binary Search | [๊ตฌ๊ฐ„ ๋‚˜๋ˆ„๊ธฐ2](https://www.acmicpc.net/problem/13397) | [#218](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/218) |
| 64์ฐจ์‹œ | 2024.9.8 | Binary Search | [์ง•๊ฒ€๋‹ค๋ฆฌ](https://school.programmers.co.kr/learn/courses/30/lessons/43236) | [#221](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/221) |
| 64์ฐจ์‹œ | 2024.9.8 | Binary Search | [์ง•๊ฒ€๋‹ค๋ฆฌ](https://school.programmers.co.kr/learn/courses/30/lessons/43236) | [#221](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/221) |
| 65์ฐจ์‹œ | 2024.9.11 | Greedy | [๋””ํŽœ์Šค ๊ฒŒ์ž„](https://school.programmers.co.kr/learn/courses/30/lessons/142085) | [#224](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/224) |
| 66์ฐจ์‹œ | 2024.9.14 | Sliding Window | [ํ• ์ธ ํ–‰์‚ฌ](https://school.programmers.co.kr/learn/courses/30/lessons/131127) | [#225](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/225) |
| 67์ฐจ์‹œ | 2024.9.23 | Graph Traversal | [ํผ์ฆ ์กฐ๊ฐ ์ฑ„์šฐ๊ธฐ](https://school.programmers.co.kr/learn/courses/30/lessons/84021) | [#228](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/228) |
| 68์ฐจ์‹œ | 2024.10.1 | Greedy | [2024 KAKAO WINTER INTERNSHIP n + 1 ์นด๋“œ ๊ฒŒ์ž„](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#230](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/230) |
| 69์ฐจ์‹œ | 2024.10.8 | Graph Traversal | [2022 KAKAO BLIND RECRUITMENT ์‚ฌ๋ผ์ง€๋Š” ๋ฐœํŒ](https://school.programmers.co.kr/learn/courses/30/lessons/92345) | [#232](https://github.com/AlgoLeadMe/AlgoLeadMe-3/pull/232) |
35 changes: 35 additions & 0 deletions 9-kyo-hwang/Sliding Window/ํ• ์ธ ํ–‰์‚ฌ.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <string>
#include <vector>
#include <unordered_map>

using namespace std;

int solution(vector<string> InWants, vector<int> InNumbers, vector<string> InDiscounts)
{
unordered_map<string, int> NumberbyWants;
for(int i = 0; i < 9; ++i)
{
NumberbyWants[InDiscounts[i]]++;
}

int Answer = 0;
for(int i = 9; i < InDiscounts.size(); ++i)
{
NumberbyWants[InDiscounts[i]]++;
bool Flag = true;

for(int j = 0; j < InWants.size(); ++j)
{
if(NumberbyWants[InWants[j]] != InNumbers[j])
{
Flag = false;
break;
}
}

Answer += Flag;
NumberbyWants[InDiscounts[i - 9]]--;
}

return Answer;
}
Loading