NC_126_MIN_MONEY NC_127_LCS NC_128_MAXWATER

package org.example.interview.practice;

import java.util.Arrays;

/**
 * @author xianzhe.ma
 * @date 2021/8/16
 */

public class NC_126_MIN_MONEY {
    public int minMoney (int[] arr, int aim) {
        // write code here
        int Max = aim + 1;//定一个全局最大值
        int []dp = new int[aim + 1];//dp[i]的含义是目标值为i的时候最少钱币数是多少。
        Arrays.fill(dp,Max);//把dp数组全部定为最大值
        dp[0] = 0;//总金额为0的时候所需钱币数一定是0
        for(int i = 1;i <= aim;i ++){// 遍历目标值
            for(int j = 0;j < arr.length;j ++){// 遍历钱币
                if(arr[j] <= i){//如果当前的钱币比目标值小就可以兑换
                    dp[i] = Math.min(dp[i],dp[i-arr[j]] + 1);
                }
            }
        }
        return dp[aim] > aim ? -1 : dp[aim];
    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/9/2
 */

public class NC_127_LCS {

    public String LCS (String str1, String str2) {
        // write code here
        //定义dp[i][j]表示字符串str1中第i个字符和str2种第j个字符为最后一个元素所构成的最长公共子串
        int maxLenth = 0;//记录最长公共子串的长度
        //记录最长公共子串最后一个元素在字符串str1中的位置
        int maxLastIndex = 0;
        int[][] dp = new int[str1.length() + 1][str2.length() + 1];
        for (int i = 0; i < str1.length(); i++) {
            for (int j = 0; j < str2.length(); j++) {
                //递推公式,两个字符相等的情况
                if (str1.charAt(i) == str2.charAt(j)) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                    //如果遇到了更长的子串,要更新,记录最长子串的长度,
                    //以及最长子串最后一个元素的位置
                    if (dp[i + 1][j + 1] > maxLenth) {
                        maxLenth = dp[i + 1][j+1];
                        maxLastIndex = i;
                    }
                } else {
                    //递推公式,两个字符不相等的情况
                    dp[i + 1][j+1] = 0;
                }
            }
        }
        //最字符串进行截取,substring(a,b)中a和b分别表示截取的开始和结束位置
        return str1.substring(maxLastIndex - maxLenth + 1, maxLastIndex + 1);

    }
}
package org.example.interview.practice;

/**
 * @author xianzhe.ma
 * @date 2021/7/15
 */

public class NC_128_MAXWATER {
    public static long maxWater(int[] height) {
        // write code here
        long result = 0;
        int maxValue = -1;
        int maxAdder = 0;
        //取数组最大的下标和它的值
        for (int i = 0; i < height.length; i++) {
            if (height[i] > maxValue) {
                maxValue = height[i];
                maxAdder = i;
            }
        }

        for (int left = 0; left < maxAdder; left++) {
            for (int i = left + 1; i <= maxAdder; i++) {
                if (height[i] < height[left]) {
                    result = result + (height[left] - height[i]);
                } else {
                    left = i;
                }
            }

        }

        for (int right = height.length - 1; right > maxAdder; right--) {
            for (int i = right - 1; i >= maxAdder; i--) {
                if (height[i] < height[right]) {
                    result = result + (height[right] - height[i]);
                } else {
                    right = i;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] waters = {10, 4, 2, 0, 8, 1, 3};
        System.out.println(maxWater2(waters));
    }

    public static long maxWater2(int[] height) {

        int maxLocation = 0;
        long result = 0;
        int maxValue = -1;

        for (int i = 0; i < height.length; i++) {
            if (height[i] > maxValue) {
                maxValue = height[i];
                maxLocation = i;
            }
        }

        for (int left = 0; left < maxLocation; left++) {
            for (int i = left + 1; i <= maxLocation; i++) {
                if (height[i] < height[left]) {
                    result = result + (height[left] - height[i]);
                } else {
                    left = i;
                }
            }
        }

        for (int right = height.length - 1; right > maxLocation; right--) {
            for (int i = right - 1; i >= maxLocation; i--) {
                if (height[i] < height[right]) {
                    result = result + (height[right] - height[i]);
                } else {
                    right = i;
                }
            }
        }

        return result;
    }
}

 

posted on 2022-02-11 16:53  MaXianZhe  阅读(33)  评论(0编辑  收藏  举报

导航