-
Notifications
You must be signed in to change notification settings - Fork 1
29-wnsmir #123
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
Conversation
9kyo-hwang
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3μμ μνμ¬κΈ° μνΈν΄μ ν΄λ¨κ΅°μ(λΏλ―)

μ λ λ¬Έμμ΄ λ°°μ΄λ‘ μ£Όμ΄μ§λ storage μ 보λ₯Ό ν¨λ©μν¨ int 2μ°¨μ λ°°μ΄λ‘ λ€ λ§€νμμΌ°μ΅λλ€. κ·Έλ¦¬κ³ 'νμΈ μλ£λ μΉΈ'μ -1λ‘ λμμ΅λλ€.
int N, M;
vector<vector<int>> GStorage;
int solution(vector<string> Storage, vector<string> Requests)
{
N = Storage.size(); M = Storage[0].size();
GStorage.assign(N + 2, vector<int>(M + 2, -1)); // κΈ°λ³Έκ°μ -1
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= M; ++j)
{
GStorage[i][j] = Storage[i - 1][j - 1] - 'A'; // A ~ Zλ₯Ό 0 ~ 25λ‘ λμ
}
}
...
}κ·Έλ¦¬κ³ requestλ₯Ό νμΈνλ©΄μ κΈΈμ΄κ° 1μ΄λ©΄ μ§κ²μ°¨, 2λΌλ©΄ ν¬λ μΈμ μ¬μ©νλ μμ²μ μννλλ‘ νμ΅λλ€. μ΄ λ μ΅μ΄ Answer λ³μ κ°μ Storage ν¬κΈ°λ‘ μ΄κΈ°νν λ€, κ° μμ²μμ 'κΊΌλΈ μ»¨ν μ΄λ κ°μ'λ₯Ό λ°ννκ² ν΄μ μ΄λ₯Ό κ³μ λΉΌλ μμΌλ‘ μ²λ¦¬νμ΅λλ€. μ΄λ¬λ©΄ μμ°μ€λ½κ² μμ²μ΄ μ’ λ£λλ©΄ 'λ¨μ 컨ν μ΄λ κ°μ'λ₯Ό μ¦μ λ°νν μ μκΈ° λλ¬Έμ΄μ£ .
int solution(vector<string> Storage, vector<string> Requests)
{
...
int Answer = N * M;
for(const string& Request : Requests)
{
if(Request.length() == 1)
{
Answer -= DeliveryByForklift(Request[0] - 'A');
}
else
{
Answer -= DeliveryByCrane(Request[0] - 'A');
}
}
return Answer;
}μ§κ²μ°¨ μ¬μ©νλ 건 μΈκΈνμ λλ‘ μΉμ¦ λ¬Έμ νΌ κ²κ³Ό κ±°μ λμΌνκ² ν΄κ²°ν μ μμ΅λλ€. λ¨ μνλ₯Ό μ§νν λ
- μΈμ μΉΈμ΄ μ°Ύλ 컨ν μ΄λλΌλ©΄ κ°μλ₯Ό μΉ΄μ΄ν νκ³ λ μ΄μ μ κ·Όνμ§ λͺ»νλλ‘ storage λ°°μ΄μ -1μ λ§νΉνκΈ°
- 컨ν μ΄λκ° κΊΌλ΄μ§ μΉΈμ΄λΌλ©΄ λ€μ μνλ νμν νλ³΄λ‘ μ§μ
μ΄ λ κ°μ§λ₯Ό μ μ ν μννλ κ² ν€μλ κ² κ°μ΅λλ€.
const vector<pair<int, int>> Offset
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};
bool OutOfBound(int x, int y)
{
return !(0 <= x && x <= N + 1 && 0 <= y && y <= M + 1);
}
int DeliveryByForklift(const int Container)
{
int NumDelivery = 0;
queue<pair<int, int>> Q;
Q.emplace(0, 0);
vector<vector<bool>> Visited(N + 2, vector<bool>(M + 2, false));
Visited[0][0] = true;
while(!Q.empty())
{
const auto [x, y] = Q.front();
Q.pop();
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny) || Visited[nx][ny])
{
continue;
}
Visited[nx][ny] = true;
if(GStorage[nx][ny] == Container) // μ°Ύλ 컨ν
μ΄λλΌλ©΄
{
NumDelivery++;
GStorage[nx][ny] = -1; // κΊΌλ΄κΈ°
}
else if(GStorage[nx][ny] == -1) // μ΄λ―Έ κΊΌλ΄μ§ μΉΈμ΄λΌλ©΄
{
Q.emplace(nx, ny); // λ€μ μνλ νμΈν΄μΌ ν μΉΈ
}
}
}
return NumDelivery;
}μ΄λ° μμΌλ‘ μ²λ¦¬νλ©΄ ν¬λ μΈμμλ κ·Έλ₯ 2μ€ λ°λ³΅λ¬Έ μννλ©΄μ ν΄λΉ 컨ν μ΄λ μΉΈλ§ μ²λ¦¬ν΄μ£Όλ©΄ λ©λλ€.
int DeliveryByCrane(const int Container)
{
int NumDelivery = 0;
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= M; ++j)
{
if(GStorage[i][j] == Container)
{
GStorage[i][j] = -1;
NumDelivery++;
}
}
}
return NumDelivery;
}μ 체 μ½λ
#include <string>
#include <vector>
#include <queue>
using namespace std;
int N, M;
vector<vector<int>> GStorage;
const vector<pair<int, int>> Offset
{
{-1, 0},
{0, 1},
{1, 0},
{0, -1}
};
bool OutOfBound(int x, int y)
{
return !(0 <= x && x <= N + 1 && 0 <= y && y <= M + 1);
}
int DeliveryByForklift(const int Container)
{
int NumDelivery = 0;
queue<pair<int, int>> Q;
Q.emplace(0, 0);
vector<vector<bool>> Visited(N + 2, vector<bool>(M + 2, false));
Visited[0][0] = true;
while(!Q.empty())
{
const auto [x, y] = Q.front();
Q.pop();
for(const auto& [dx, dy] : Offset)
{
int nx = x + dx, ny = y + dy;
if(OutOfBound(nx, ny) || Visited[nx][ny])
{
continue;
}
Visited[nx][ny] = true;
if(GStorage[nx][ny] == Container)
{
NumDelivery++;
GStorage[nx][ny] = -1;
}
else if(GStorage[nx][ny] == -1)
{
Q.emplace(nx, ny);
}
}
}
return NumDelivery;
}
int DeliveryByCrane(const int Container)
{
int NumDelivery = 0;
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= M; ++j)
{
if(GStorage[i][j] == Container)
{
GStorage[i][j] = -1;
NumDelivery++;
}
}
}
return NumDelivery;
}
int solution(vector<string> Storage, vector<string> Requests)
{
N = Storage.size(); M = Storage[0].size();
GStorage.assign(N + 2, vector<int>(M + 2, -1));
for(int i = 1; i <= N; ++i)
{
for(int j = 1; j <= M; ++j)
{
GStorage[i][j] = Storage[i - 1][j - 1] - 'A';
}
}
int Answer = N * M;
for(const string& Request : Requests)
{
if(Request.length() == 1)
{
Answer -= DeliveryByForklift(Request[0] - 'A');
}
else
{
Answer -= DeliveryByCrane(Request[0] - 'A');
}
}
return Answer;
}|
ν¨λ© λ§κ³ λ€λ₯Έ λ°©λ²μ΄ μμκΉ μ μ κ³ λ―Όνλλ° μ€μ μ΄μλ€λ©΄ λ§μ€μ μμ΄ ν¨λ©μ μΌμ κ² κ°λλΌκ³ μ,, κ²°κ΅ ν¨λ©μΌλ‘ νμλλ°, κ°μ λ°©μμΌλ‘ νΈμ ¨λ€μ! |
kangrae-jo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ¬λ°λ λ¬Έμ λ€μ.
μμ μ νμλκ² κ°κΈ°λ ν©λλ€.
μ λ ν¨λ©μ νμ§μκ³ νμμ΅λλ€.
λ§μ½ λ€μμΌλ‘ μ΄λν boardκ° λΉμ΄μκ±°λ, λ°©λ¬Έλμ§ μμμ κ²½μ°μλ§ νμ λ£κ³ ,
νμμ κΊΌλ΄μμ λ κ°μ₯μ리면 trueλ₯Ό λ°ννλ μμΌλ‘ 체ν¬λ₯Ό νμ΄μ.
κ·Έλ¦¬κ³ λ νκ°μ§ μ£Όμν μ μ΄λΌλ©΄,
μ§κ²μ°¨λ‘ μνλ²³μ μ§μΈλ, λ± κ·Έ μμ μμ μ§κ²μ°¨λ‘ κΊΌλΌ μ μλ λ
μλ€λ§ κΊΌλ΄μΌ νλ€λ μ μ΄μλ κ² κ°μμ.
μλ₯Ό λ€μ΄ Aνλκ° μΉμμ§λ©΄ κ·Έμμ Aλ₯Ό κΊΌλΌμ μλ μν©μ... λ¬Έμ μμ μꡬνλ λ°κ° μλκ±°μ£ .
μ λ κ·Έλμ κΊΌλΌμ μλ λ
μλ€μ μ’νλ₯Ό μ μ₯ν΄λκ³ κ° μΏΌλ¦¬μ λ§μ§λ§μ μ²λ¦¬ν΄μ£Όμμ΅λλ€.
μ’μλ¬Έμ κ°μ¬ν©λλ€.
#include <string>
#include <vector>
#include <queue>
using namespace std;
const int EMPTY = 0;
const int OFFSET[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
int n, m;
vector<vector<int>> board;
bool isIn(int y, int x) { return 0 <= y && y < n && 0 <= x && x < m; }
bool isOutLine(int y, int x) { return y == 0 || y == n - 1 || x == 0 || x == m - 1; }
bool canRemove(int y, int x, int c){
vector<vector<bool>> visited(n, vector<bool> (m, false));
queue<pair<int, int>> q;
q.push({y, x});
visited[y][x] = true;
while (!q.empty()) {
auto [curY, curX] = q.front();
q.pop();
if (isOutLine(curY, curX)) return true;
for (int dir = 0; dir < 4; dir++) {
int nextY = curY + OFFSET[dir][0];
int nextX = curX + OFFSET[dir][1];
if (isIn(nextY, nextX) && board[nextY][nextX] == EMPTY && !visited[nextY][nextX]) {
q.push({nextY, nextX});
visited[nextY][nextX] = true;
}
}
}
return false;
}
int solution(vector<string> storage, vector<string> requests) {
n = storage.size();
m = storage[0].size();
board = vector<vector<int>> (n, vector<int> (m));
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
board[y][x] = storage[y][x];
}
}
for (string request : requests) {
int c = request[0];
if (request.size() == 2) { // all
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] == c) board[y][x] = EMPTY;
}
}
} else { // outline
vector<pair<int, int>> result;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] == c && canRemove(y, x, c)) result.push_back({y, x});
}
}
for (auto [y, x] : result) board[y][x] = EMPTY;
}
}
int answer = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] != EMPTY) answer++;
}
}
return answer;
}|
μμ μ νμλ κΈ°μ΅μΌλ‘ λ λ¨Ήν κ² κ°μμ μλ°λ‘λ ν΄λ΄€μ΅λλ€. μλ°λ μ€νΈλ§μ λ€λ£¨κΈ°κΉλ€λ‘μμ char[][]λ‘ λ³νν λ€ λμΌν λ‘μ§μΌλ‘ λ¬Έμ λ₯Ό ν΄κ²°νμ΅λλ€. Java Codeimport java.util.*;
class Solution {
static final int[][] OFFSET = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static int n, m;
public int solution(String[] storage, String[] requests) {
n = storage.length;
m = storage[0].length();
char[][] board = new char[n][m];
for (int i = 0; i < n; i++) {
board[i] = storage[i].toCharArray();
}
for (String request : requests) {
char c = request.charAt(0);
// ν¬λ μΈ
if (request.length() == 2) {
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] == c) {
board[y][x] = '0';
}
}
}
}
// μ§κ²μ°¨
else {
Queue<Pos> q = new LinkedList<>();
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] == c && canRemove(board, y, x)) {
q.add(new Pos(y, x));
}
}
}
while (!q.isEmpty()) {
Pos p = q.poll();
board[p.y][p.x] = '0';
}
}
}
int answer = 0;
for (int y = 0; y < n; y++) {
for (int x = 0; x < m; x++) {
if (board[y][x] != '0') answer++;
}
}
return answer;
}
private static boolean canRemove(char[][] board, int y, int x) {
boolean[][] visited = new boolean[n][m];
Queue<Pos> q = new LinkedList<>();
q.add(new Pos(y, x));
visited[y][x] = true;
while (!q.isEmpty()) {
Pos cur = q.poll();
if (isOutLine(cur.y, cur.x)) return true;
for (int dir = 0; dir < 4; dir++) {
int nextY = cur.y + OFFSET[dir][0];
int nextX = cur.x + OFFSET[dir][1];
if (isIn(nextY, nextX) && !visited[nextY][nextX] && board[nextY][nextX] == '0') {
q.add(new Pos(nextY, nextX));
visited[nextY][nextX] = true;
}
}
}
return false;
}
private static boolean isOutLine(int y, int x) {
return y == 0 || y == n - 1 || x == 0 || x == m - 1;
}
private static boolean isIn(int y, int x) {
return 0 <= y && y < n && 0 <= x && x < m;
}
private static class Pos {
int y, x;
public Pos(int y, int x) {
this.y = y;
this.x = x;
}
}
} |
kokeunho
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ ν¨λ© μ²λ¦¬νμ¬ mapμ λ€μ ꡬμ±νμμ΅λλ€.
κ·Έλ¦¬κ³ requestμ κΈΈμ΄κ° 2, 1λ‘ λλμ΄
1μ΄λ©΄ (0, 0)μμ bfsλ₯Ό μμνμ¬ μΈλΆμ μ μ΄λ 컨ν
μ΄λ μ€ requestν΄ ν΄λΉνλ charλ₯Ό μ κ±°νμμ΅λλ€.
2λΌλ©΄ μ 체 mapμμ requestμ ν΄λΉνλ chλ₯Ό λͺ¨λ μ κ±°ν΄μ£Όμμ΅λλ€.
μκ³ νμ ¨μ΅λλ€.
Details
import java.util.*;
class Solution {
static char[][] map;
static int[] dx = {-1, 1, 0, 0};
static int[] dy = {0, 0, -1, 1};
public int solution(String[] storage, String[] requests) {
map = new char[storage.length+2][storage[0].length()+2];
for (int i = 0; i < map.length; i++) {
Arrays.fill(map[i], '-');
}
for (int i = 0; i < storage.length; i++) {
String line = storage[i];
for (int j = 0; j < line.length(); j++) {
map[i+1][j+1] = line.charAt(j);
}
}
processReq(requests);
return countContainer();
}
void processReq(String[] requests) {
for (String request : requests) {
char ch = request.charAt(0);
if (request.length() == 1) {
bfs(0, 0, ch);
} else {
removeCh(ch);
}
}
}
void bfs(int x, int y, char ch) {
Queue<int[]> queue = new LinkedList<>();
boolean[][] visited = new boolean[map.length][map[0].length];
queue.add(new int[]{x, y});
visited[x][y] = true;
while(!queue.isEmpty()) {
int[] current = queue.poll();
for (int i = 0; i < 4; i++) {
int nx = current[0] + dx[i];
int ny = current[1] + dy[i];
if (nx >= 0 && nx < map.length && ny >= 0 && ny < map[0].length && !visited[nx][ny]) {
if (map[nx][ny] == '-') {
queue.add(new int[]{nx, ny});
visited[nx][ny] = true;
}
if (map[nx][ny] == ch) {
map[nx][ny] = '-';
visited[nx][ny] = true;
}
}
}
}
}
void removeCh(char ch) {
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j] == ch) {
map[i][j] = '-';
}
}
}
}
int countContainer() {
int count = 0;
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[0].length; j++) {
if (map[i][j] != '-') count++;
}
}
return count;
}
}
g0rnn
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
νμ΄μ¬μ μ΄λ ΅κ΅°μ...γ γ γ μ μμ΄ μλλ€μ. μ λ ν¨λ©μ λ£κ³ μΈκ³½μμλΆν° bfsλ‘ νμνλλ° νμ΄μ¬μ μκ°μ΄κ³Όκ° λλ€μγ 1μκ°λμ 머리 μΈλ§€λ€κ° κ·Έλ₯ μλ°λ‘ κ°μνμ΅λλ€ :)
κ°μ λ‘μ§μ μλ°λ‘ ꡬννμλ 30λΆλ§μ ν μ μμμ΅λλ€. μμ μ΅μνκ² μ΅κ³ λ€μ
import java.util.*;
class Solution {
char[][] container;
int n, m;
int[][] offset = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public int solution(String[] storage, String[] requests) {
n = storage.length;
m = storage[0].length();
int answer = n * m;
container = padding(storage);
for (String req : requests) {
if (req.length() == 1){
answer -= one(req.charAt(0));
} else {
answer -= two(req.charAt(0));
}
}
return answer;
}
private char[][] padding(String[] storage) {
char[][] ret = new char[n+2][m+2];
Arrays.fill(ret[0], ' ');
Arrays.fill(ret[n+1], ' ');
for (int i = 0; i < n; i++) {
ret[i+1][0] = ' ';
ret[i+1][m+1] = ' ';
for (int j = 0; j < m; j++) {
ret[i+1][j+1] = storage[i].charAt(j);
}
}
return ret;
}
private int one(char target) {
int count = 0;
Queue<int[]> q = new ArrayDeque<>();
boolean[][] visited = new boolean[n+2][m+2];
q.add(new int[]{0, 0});
visited[0][0] = true;
while (!q.isEmpty()) {
int x = q.peek()[0];
int y = q.poll()[1];
for (int[] o : offset) {
int nx = x + o[0];
int ny = y + o[1];
if (nx < 0 || nx >= m+2 || ny < 0 || ny >= n + 2) continue;
if (visited[ny][nx]) continue;
if (container[ny][nx] == target) {
visited[ny][nx] = true;
container[ny][nx] = ' ';
count++;
}
else if (container[ny][nx] == ' ') {
visited[ny][nx] = true;
q.offer(new int[]{nx, ny});
}
}
}
//System.out.println("one: "+count);
return count;
}
private int two(char target) {
int count = 0;
for (int i = 0; i < n+2; i++) {
for (int j = 0; j < m+2; j++) {
if (container[i][j] == target) {
container[i][j] = ' ';
count++;
}
}
}
//System.out.println("two: "+count);
return count;
}
}
π λ¬Έμ λ§ν¬
https://school.programmers.co.kr/learn/courses/30/lessons/388353
βοΈ μμλ μκ°
30min
β¨ μλ μ½λ
μΌλ§μ μΉμ¦λ¬Έμ μλμ?
κ΅μ₯ν λΉμ·ν΄μ κΈλ°©ν μ μμμ΅λλ€.
λ€λ§ κ·Έλ¬Έμ μ λ€λ₯Έμ μ μ΄λ¬Έμ λ νλλ¦¬κ° μλ€λ κ²μ λλ€...γ
κ·Έλμ μ§μ ν¨λ©ν΄μ£Όμ΄ κ° λͺ λ Ήμ΄λ§λ€ bfsλ₯Ό μνν΄ μΈλΆλ₯Ό 0μΌλ‘ λ§λ€μμ΅λλ€.
μΉμ¦λ¬Έμ λ‘ μΉλ©΄ μΈλΆκ³΅κΈ°κ° 0μ΄λλκ²κ³Ό κ°μ΅λλ€.
κ° λͺ λ Ήλ¬Έμ λλ©° μνλ²³μ΄ λκ°λμ€λ©΄ κ·Έλ₯ λ°λ³΅λ¬Έ λλ² λλ©΄μ 1λ‘ λ°κΏμ£Όκ³ , 1μ λ€μ bfsλ₯Ό ν΅ν΄ μΈλΆμ μ μ΄νλ 1μ 0μΌλ‘ λ§λ€μ΄μ€λλ€.
λ€λ§ μ΄μ§ νκ°λ Έλ λΆλΆμ΄ μμλ€λ©΄, μ§κ²μ°¨λ‘μ§μ λ€μλ bfsλ₯Ό λ¬μμ£Όμ΄μΌ ν©λλ€.
ν¬λ μΈμ λΉμ°ν 1λ‘ λ°κΎΈμ΄μ€¬λ€κ° κ·Έ 1μ΄ 0κ³Ό λΆμ΄μλ€λ©΄ 0μΌλ‘ λ°κΏμ£Όλ bfsκ° νμνλ€λ©΄,
μ§κ²μ°¨λ λ§μ½ μΈλΆμ λΆμ΄μλ μ§μ μμ΄μλ λ΄λΆμ 1μ΄ μμλ€λ©΄ κ·Έ 1μ λ€λ₯Έ λͺ λ Ήμ΄κ° μ€νλκΈ°μ μ 0μΌλ‘ λ°κΏμ€μΌνκΈ° λλ¬Έμ λλ€.
μ¦ μ§κ²μ°¨λ ν¬λ μΈμ΄λ κ° λ‘μ§μ΄ λλκ³ bfsλ‘ μΈλΆμμ μ μ΄μ νμ μ λ°μ΄νΈν΄μ£Όμ΄μΌ ν©λλ€.
π μλ‘κ² μκ²λ λ΄μ©