算法实践应用
数据结构与算法

需求背景:
有问题的jira 关联模块信息:
    jira1:关联模块A、B;
    jira2:关联模块A、C;
    jira3:关联模块C、D;
    jira4:关联模块D;
    jira5:关联模块E;

在日常版本迭代时,根据当前 sprint 的中的 jira 号捞出涉及到的模块,那么此次 sprint 迭代中 模块 A、B、C、D、E都不做为升级,主要的考虑是牵扯到 jira3 实际上是 通过 jira2 作为中间状态关联到的。





根据以上背景直接撸代码吧:

package src.daily;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Jira {

   // 构建对象
    class JiraDemo{
        String name;
        List<String> modules;

        public JiraDemo(final String name,List<String> modules){
            this.name = name;
            this.modules = modules;
        }

        public String getName(){
            return name;
        }

        public List<String> getModules(){
            return modules;
        }
    }
    
    //BFS 处理
    public List<String> getJira(JiraDemo jiraDemo, List<JiraDemo> jiraDemos){
        List<String> ans = new ArrayList<>();
        //初始化,前提是需求侧已经传入一个涉及的jira,看具体需求
        ans.add(jiraDemo.getName());
        LinkedList<String> modules = new LinkedList<>();
        for(String module:jiraDemo.getModules()){
            modules.add(module);
        }
        
        while(!modules.isEmpty()){
            int size = modules.size();
            while(size-- >0){
                String md = modules.removeFirst();
                for(JiraDemo jdDemo:jiraDemos){
                    if(jdDemo.getModules().contains(md) && !ans.contains(jdDemo.getName())){
                        ans.add(jdDemo.getName());
                        // 这一步还可以继续优化,存在优化空间。
                        for(String jdModule: jdDemo.getModules()){
                            modules.add(jdModule);
                        }
                    }
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Jira jira = new Jira();

        Jira.JiraDemo jiraA = jira.new JiraDemo("jiraA",new ArrayList<String>(Arrays.asList("user","volume")));
        Jira.JiraDemo jiraB = jira.new JiraDemo("jiraB",new ArrayList<String>(Arrays.asList("user","openstack")));
        Jira.JiraDemo jiraC = jira.new JiraDemo("jiraC",new ArrayList<String>(Arrays.asList("openstack","vue")));
        Jira.JiraDemo jiraD = jira.new JiraDemo("jiraD",new ArrayList<String>(Arrays.asList("vue")));
        Jira.JiraDemo jiraE = jira.new JiraDemo("jiraE",new ArrayList<String>(Arrays.asList("js")));
        List<JiraDemo> lists = new ArrayList<JiraDemo>(Arrays.asList(jiraA,jiraB,jiraC,jiraD,jiraE));
        jira.getJira(jiraA,lists);
    }
}
暂无评论