433. Minimum Genetic Mutation

问题描述:

A gene string can be represented by an 8-character long string, with choices from "A""C""G""T".

Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.

For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.

Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.

Note:

  1. Starting point is assumed to be valid, so it might not be included in the bank.
  2. If multiple mutations are needed, all mutations during in the sequence must be valid.
  3. You may assume start and end string is not the same.

Example 1:

start: "AACCGGTT"
end:   "AACCGGTA"
bank: ["AACCGGTA"]

return: 1

Example 2:

start: "AACCGGTT"
end:   "AAACGGTA"
bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

return: 2

Example 3:

start: "AAAAACCC"
end:   "AACCCCCC"
bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

return: 3

解题思路:

这道题可以用bfs来解答,与word ladder十分相似。

不过这道题有更多的限制:

  1.gene从{‘A‘, ‘C‘, ‘G‘, ‘T‘ }中选择。

  2.每一个mutation的结果必须在bank中能够找到。

我们用queue来存储当前的gene string,并且用另一个queue来存储变化次数。

这两个queue要保持同步。

在最开始可以首先检查end是否在bank中,不在bank中的话,无论如何我们也到不达终点,可以直接返回-1.

在检查当前字符串是否可以加入队列时需要检查:

  1. 是否已经访问过

  2. 是否在bank中可以找到

代码:

class Solution {
public:
    int minMutation(string start, string end, vector<string>& bank) {
        vector<char> genes = {‘A‘, ‘T‘, ‘C‘, ‘G‘};
        unordered_set<string> s(bank.begin(), bank.end());

        if(s.count(end) == 0) return -1;

        unordered_set<string> visited;
        queue<string> q;
        queue<int> distance_q;
        q.push(start);
        distance_q.push(0);

        while(!q.empty()){
            string cur = q.front();
            q.pop();
            int dis = distance_q.front();
            distance_q.pop();
            for(int i = 0 ; i < 8; i++){
                for(int j = 0; j < 4; j++){
                    string temp = cur;
                    if(genes[j] == temp[i]) continue;
                    temp[i] = genes[j];

                    if(temp == end) return dis+1;
                    if(visited.count(temp) == 0 && s.count(temp) != 0){
                        visited.insert(temp);
                        q.push(temp);
                        distance_q.push(dis+1);
                    }
                }
            }
        }
        return -1;
    }
};

原文地址:https://www.cnblogs.com/yaoyudadudu/p/9393852.html

时间: 2024-10-13 01:08:38

433. Minimum Genetic Mutation的相关文章

(Java) LeetCode 433. Minimum Genetic Mutation —— 最小基因变化

A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T". Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE m

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【LeetCode】未分类(tag里面没有)(共题)

[419]Battleships in a Board (2018年11月25日)(谷歌的题,没分类.) 给了一个二维平面,上面有 X 和 . 两种字符. 一行或者一列连续的 X 代表一个战舰,问图中有多少个战舰.(题目要求one pass, 空间复杂度是常数) 题目说了每两个战舰之间起码有一个 . 作为分隔符,所以不存在正好交叉的情况. 题解:我提交了一个 floodfill 的题解,能过但是显然不满足要求. discuss里面说,我们可以统计战舰的最上方和最左方,从而来统计战舰的个数.(这个

几个mutation概念

Somatic mutations: A change in the genetic structure that is not inherited from a parent, and also not passed to offspring, is called a somatic cell genetic mutation or acquired mutation A germline mutation is any detectable and heritable variation i

苯氧乙醇Phenoxyethanol

Irritation (skin, eyes, or lungs), Occupational hazards, Use restrictions, Organ system toxicity (non-reproductive), Allergies/immunotoxicity 防腐剂.定香剂,未稀释前对眼睛刺激性很大,稀释至2.2%则不具刺激性. 属低度皮肤过敏危险性 美国食物及药物管理局(FDA)指,Phenoxyethanol能够抑制中央神经系统,有可能导致呕吐和腹泻. 资料来源:FD

L198

One of the most common birth defects throughout the world is a cleft lip. Babies born with a cleft lip may also have a cleft裂开的 palate, where the roof of the mouth is split. These birth defects can be repaired surgically. But unless that is done, it

使用遗传算法实现迷宫游戏(genetic maze)

强烈推荐一本书 <游戏编程中的人工智能技术>(AI.Techniques.for.Game.Programming).(美)Mat.Buckland 一.缘起 在之前的c印记系列当中有有一个迷宫小游戏,算是一个关于数组应用的例子. 其中有通过接收按键(人工操作)的方式来走出迷宫,也有使用递归算法或非递归算法的方式来实现自动(AI操作)走出迷宫. 后来我对近两三年比较火的人工智能,机器学习,深度学习之类的比较感兴趣了.于是乎,我找了很多书籍或网上的文章来看.但基本上都是两个类别的,其中一类就是一

2. Genetic Algorithm(1) ——进化算法

本篇博文讲述基因算法(Genetic Algorithm),基因算法是最著名的进化算法. 内容依然来自博主的听课记录和教授的PPT. Outline 简单基因算法 个体表示 变异 重组 1. 简单基因算法(Simple Genetic Algorithm) Holland's早期的基因算法被认为是“简单的基因算法”或是“权威的基因算法”.(simple genetic algorithm or canonical genetic algorithm) 1. 直接举例说明 问题描述:利用遗传算法求