| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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
- kaggle
- 컴퓨터의 구조
- 파이썬
- Convert Sorted List to Binary Search Tree
- Substring with Concatenation of All Words
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- Python Code
- Python Implementation
- Regular Expression
- t1
- data science
- 프로그래머스
- 운영체제
- Class
- iterator
- DWG
- Protocol
- concurrency
- Generator
- 시바견
- Python
- attribute
- LeetCode
- 30. Substring with Concatenation of All Words
- shiba
- 밴픽
- 43. Multiply Strings
- 315. Count of Smaller Numbers After Self
- Decorator
Archives
- Today
- Total
Scribbling
[C++] find, deque, bfs, structured binding 본문
LeetCode - The World's Leading Online Programming Learning Platform
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
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 |