每天刷一道牛客题霸-第22天- 最小编辑代价

题目

https://www.nowcoder.com/practice/05fed41805ae4394ab6607d0d745c8e4?tpId=190&&tqId=35213&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking

import java.util.*;


public class Solution {
    /**
     * min edit cost
     * @param str1 string字符串 the string
     * @param str2 string字符串 the string
     * @param ic int整型 insert cost
     * @param dc int整型 delete cost
     * @param rc int整型 replace cost
     * @return int整型
     */
    public static int minEditCost (String str1, String str2, int ic, int dc, int rc) {
        int[][] count = new int[str1.length()+1][str2.length()+1];
        for (int i = 0; i < count.length; i++) {
            count[i][0] = i*dc;
        }
        for (int i = 0; i < count[0].length; i++) {
            count[0][i] = i*ic;
        }
        for (int i = 1; i < count.length; i++) {
            for (int j = 1; j < count[i].length; j++) {
                if (str1.charAt(i-1) == str2.charAt(j-1)) {
                    count[i][j] = count[i-1][j-1];
                }else{
                    count[i][j] =Math.min(count[i][j-1]+ ic,  Math.min(count[i-1][j-1] + rc, count[i-1][j] + dc));
                }
            }
        }
        return count[str1.length()][str2.length()];
        // write code here
    }

}
#牛客题霸##题解#
全部评论

相关推荐

MomonKa:我拿Java简历投了pdd前端也给我简历过筛了
点赞 评论 收藏
分享
程序员小白条:简历有点拥挤,注意下排版和布局,奖项可以放项目上面,或者技术栈上面,主打一个学历+竞赛等方面,项目和技术栈这种大家包装太多了,但学历和竞赛这种实打实,虽然也有人造假,但 HR 喜欢具象化的东西
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务