일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Python Code
- Convert Sorted List to Binary Search Tree
- attribute
- 컴퓨터의 구조
- t1
- 315. Count of Smaller Numbers After Self
- LeetCode
- 프로그래머스
- data science
- shiba
- Protocol
- 밴픽
- Decorator
- 30. Substring with Concatenation of All Words
- 운영체제
- 43. Multiply Strings
- 109. Convert Sorted List to Binary Search Tree
- Python Implementation
- iterator
- Class
- Regular Expression
- kaggle
- concurrency
- 시바견
- Generator
- Python
- 파이썬
- 715. Range Module
- Substring with Concatenation of All Words
- DWG
Archives
- Today
- Total
Scribbling
[Programmers] 방의 개수 본문
https://school.programmers.co.kr/learn/courses/30/lessons/49190
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 |