일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 109. Convert Sorted List to Binary Search Tree
- 315. Count of Smaller Numbers After Self
- 43. Multiply Strings
- concurrency
- data science
- Protocol
- Convert Sorted List to Binary Search Tree
- 715. Range Module
- 컴퓨터의 구조
- 시바견
- Python Code
- kaggle
- attribute
- Regular Expression
- t1
- LeetCode
- Python
- 운영체제
- shiba
- Substring with Concatenation of All Words
- Generator
- 30. Substring with Concatenation of All Words
- Decorator
- 밴픽
- Class
- 파이썬
- Python Implementation
- DWG
- 프로그래머스
- iterator
Archives
- Today
- Total
Scribbling
[Programmers] 방의 개수 본문
https://school.programmers.co.kr/learn/courses/30/lessons/49190
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. Python
def solution(arrows):
ret = 0
dy = [-1, -1, 0, 1, 1, 1, 0, -1]
dx = [0, 1, 1, 1, 0, -1, -1, -1]
y, x = 0, 0
nodes = set()
nodes.add((0, 0))
edges = set()
for arrow in arrows:
for _ in range(2):
ny, nx = y + dy[arrow], x + dx[arrow]
if (ny, nx) in nodes and (y, x, ny, nx) not in edges:
edges.add((y, x, ny, nx))
edges.add((ny, nx, y, x))
ret += 1
y, x = ny, nx
else:
nodes.add((ny, nx))
edges.add((y, x, ny, nx))
edges.add((ny, nx, y, x))
y, x = ny, nx
return ret
2. C++
#include <string>
#include <vector>
#include <set>
using namespace std;
int solution(vector<int> arrows) {
int ret = 0;
int dy[8] {-1, -1, 0, 1, 1, 1, 0, -1};
int dx[8] {0, 1, 1, 1, 0, -1, -1, -1};
int y = 0, x = 0;
set<vector<int>> nodes;
set<vector<int>> edges;
nodes.insert({0, 0});
for (int d : arrows) {
for (int i=0; i<2; i++) {
int ny = y + dy[d], nx = x + dx[d];
if (nodes.find({ny, nx}) != nodes.end() and edges.find({y, x, ny, nx}) == edges.end()) {
edges.insert({y, x, ny, nx});
edges.insert({ny, nx, y, x});
ret++;
y = ny;
x = nx;
} else {
nodes.insert({ny, nx});
edges.insert({y, x, ny, nx});
edges.insert({ny, nx, y, x});
y = ny;
x = nx;
}
}
}
return ret;
}
'Computer Science > Algorithms & Data Structures' 카테고리의 다른 글
[LeetCode] 2781. Length of the Longest Valid Substring (0) | 2024.07.10 |
---|---|
[LeetCode] 2340. Minimum Adjacent Swaps to Make a Valid Array (0) | 2024.07.02 |
[Programmers] 순위 (0) | 2024.06.28 |
[Programmers] 가장 먼 노드 (0) | 2024.06.28 |
[Programmers] 입국심사 (0) | 2024.06.27 |