일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |
Tags
- 밴픽
- t1
- Regular Expression
- Decorator
- 프로그래머스
- 시바견
- Convert Sorted List to Binary Search Tree
- Python Implementation
- 30. Substring with Concatenation of All Words
- 파이썬
- Generator
- 43. Multiply Strings
- Python
- shiba
- Python Code
- Substring with Concatenation of All Words
- Protocol
- LeetCode
- kaggle
- DWG
- 715. Range Module
- Class
- concurrency
- data science
- 컴퓨터의 구조
- 315. Count of Smaller Numbers After Self
- iterator
- 운영체제
- 109. Convert Sorted List to Binary Search Tree
- attribute
Archives
- Today
- Total
Scribbling
[C++] find, deque, bfs, structured binding 본문
class Solution {
public:
int minMutation(string startGene, string endGene, vector<string>& bank) {
if (bank.empty() || find(bank.begin(), bank.end(), endGene) == bank.end()) {
return -1;
}
vector<string> genes{ bank };
genes.push_back(startGene);
genes.push_back(endGene);
vector<vector<int>> graph(genes.size(), vector<int>());
for (int i = 0; i < genes.size(); i++) {
for (int j = 0; j < genes.size(); j++) {
int delta = 0;
for (int k = 0; k < 8; k++) {
if (genes[i][k] != genes[j][k]) {
delta++;
}
}
if (delta == 1) {
graph[i].push_back(j);
}
}
}
deque<pair<string, int>> q;
q.push_back({ startGene, 0 });
unordered_set<string> visited;
visited.insert(startGene);
while (!q.empty()) {
auto [cur, t] = q.front();
q.pop_front();
if (cur == endGene) {
return t;
}
int i = find(genes.begin(), genes.end(), cur) - genes.begin();
for (int j : graph[i]) {
if (visited.count(genes[j]) == 0) {
q.push_back({ genes[j], t + 1 });
visited.insert(genes[j]);
}
}
}
return -1;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] Deque<int> (0) | 2024.02.06 |
---|---|
[C++] 전문가를 위한 C++ - Chapter 8 (0) | 2023.10.07 |
[C++] Smart pointers (0) | 2023.09.30 |
[C++] 전문가를 위한 C++ - Chapter 2 (1) | 2023.09.21 |
[C++] 2d DP table with vector (0) | 2023.09.19 |