借楼,请问大家这种解法是否可行? public static int dfsSolution(int n, int m, int[] birthYear, int x){ if(birthYear==null || birthYear.length==0) return x>m?0:n; //for later O(1) find HashSet<Integer> set=new HashSet<>(); for(int b: birthYear){ set.add(b); } return dfs(n,m,set,0,0, x); } public static int dfs(int n, int m, HashSet<Integer> set, int cur_year, int age, int x){ if(cur_year>=x) { if(age<=m){ return set.contains(age)?2*n:n; } return 0; } if(age>m) return 0; //if it is in the birth year if(set.contains(age)){ //这一批海豚今年会生n个宝宝,从0岁开始算.另开一个分支计算 return dfs(n,m,set,cur_year+1,age+1, x)+dfs(n,m,set, cur_year+1,1,x); } return dfs(n,m,set,cur_year+1,age+1, x); }
点赞 评论

相关推荐

嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务