package jz.bm;import javax.crypto.MacSpi;
import java.util.ArrayList;
import java.util.Arrays;public class bm7 {public int Fibonacci(int n) {if (n <= 2) {return 1;}int[] dp = new int[n + 1];dp[1] = 1;dp[2] = 1;for (int i = 3; i <= n; i++) {dp[i] = dp[i - 1] + dp[i - 2];}return dp[n];}public int jumpFloor (int number) {int[] dp = new int[number + 1];dp[0] = 1;dp[1] = 1;for (int i = 2; i <= number; i++) {dp[i] = dp[i - 2] + dp[i - 1];}return dp[number];}public int minCostClimbingStairs (int[] cost) {int[] dp = new int[cost.length + 1];
for (int i = 2; i <= cost.length; i++) {dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]);}return dp[cost.length];}public String LCS (String s1, String s2) {if (s1 == null || s1.equals("") || s2 == null || s2.equals("")) {return "";}int m = s1.length(), n = s2.length();String[][] dp = new String[m + 1][n + 1]; for (int i = 0; i <= m; i++) {dp[i][0] = "";}for (int i = 0; i <= n; i++) {dp[0][i] = "";}for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (s1.charAt(i - 1) == s2.charAt(j - 1)) {dp[i][j] = dp[i - 1][j - 1] + s1.charAt(i - 1);} else {dp[i][j] = dp[i - 1][j].length() > dp[i][j - 1].length() ? dp[i - 1][j] : dp[i][j - 1];}}}return dp[m][n].equals("") ? "-1" : dp[m][n];}public String LCS66 (String str1, String str2) {if (str1 == null || str1.equals("") || str2 == null || str2.equals("")) {return "";}int max = 0; int maxIndex = 0;int m = str1.length(), n = str2.length();int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (str1.charAt(i - 1) == str2.charAt(j - 1)) {dp[i][j] = dp[i - 1][j - 1] + 1;if (dp[i][j] > max) {max = dp[i][j];maxIndex = i;}} else {dp[i][j] = 0; }}}return str1.substring(maxIndex - max, maxIndex);}public int uniquePaths (int m, int n) {int[][] dp = new int[m + 1][n + 1];for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (i == 1) {dp[i][j] = 1;continue;}if (j == 1) {dp[i][j] = 1;continue;}dp[i][j] = dp[i - 1][j] + dp[i][j - 1];}}return dp[m][n];}public int minPathSum (int[][] matrix) {int row = matrix.length;int col = matrix[0].length;int[][] dp = new int[row][col];dp[0][0] = matrix[0][0];for (int i = 1; i < row; i++) {dp[i][0] = dp[i - 1][0] + matrix[i][0];}for (int i = 1; i < col; i++) {dp[0][i] = dp[0][i - 1] + matrix[0][i];}for (int i = 1; i < row; i++) {for (int j = 1; j < col; j++) {dp[i][j] = Math.min(dp[i - 1][j] + matrix[i][j], dp[i][j] + matrix[i][j]);}}return dp[row - 1][col - 1];}public int solve (String nums) {if (nums == null | "".equals(nums) || nums.startsWith("0")) {return 0;}int n = nums.length();
int[] dp = new int[n + 1];
dp[0] = 1; dp[1] = nums.charAt(0) == '0' ? 0 : 1;for (int i = 2; i <= n; i++) {if(nums.charAt(i-1) == '0' && (nums.charAt(i-2) == '0' || nums.charAt(i-2) > '2')){return 0;}else if(nums.charAt(i-1)=='0'){ dp[i] = dp[i-2];}else if(nums.charAt(i-2)=='0' || nums.charAt(i-2)>'2' || nums.charAt(i-2)=='2'&& nums.charAt(i-1)>'6' ){ dp[i] = dp[i-1];}else{dp[i] = dp[i-1]+dp[i-2];}
}return dp[n];}public int minMoney (int[] arr, int aim) {int[] dp = new int[aim + 1];Arrays.fill(dp, aim + 1);dp[0] = 0;for (int i = 1; i <= aim; i++) {for (int j = 0; j < arr.length; j++) {if (i - arr[j] >= 0) { dp[i] = Math.min(dp[i], dp[i - arr[j]] + 1);}}}return dp[aim] > aim ? -1 : dp[aim];}public int LIS (int[] arr) {int m = arr.length;if (m == 0) {return 0;}int[] dp = new int[m]; Arrays.fill(dp, 1);int res = 1;for (int i = 1; i < m; i++) {for (int j = 0; j < i; j++) {if (arr[i] > arr[j]) {dp[i] = Math.max(dp[i], dp[j] + 1);}}res = Math.max(dp[i], res);}return res;}public int FindGreatestSumOfSubArray (int[] array) {int n = array.length;int[] dp = new int[n + 1];dp[0] = 0;int max = array[0];for (int i = 1; i <= n; i++) {dp[i] = Math.max(dp[i - 1] + array[i - 1], array[i - 1]);max = Math.max(max, dp[i]);}return max;}public int getLongestPalindrome (String A) {int maxlen = 1;for(int i = 0; i < A.length() - 1; i++)maxlen = Math.max(maxlen, Math.max(getPalindrome(A, i, i), getPalindrome(A, i, i + 1)));return maxlen;}public int getPalindrome(String A, int left, int right) {while (left >= 0 && right < A.length() && A.charAt(left) == A.charAt(right)) {left--;right++;}return right - left - 1;}public ArrayList<String> restoreIpAddresses (String s) {ArrayList<String> res = new ArrayList<>();dfs(s, 0, res, new ArrayList<String>());return res;}private void dfs(String s, int begin, ArrayList<String> res, ArrayList<String> cur) {System.out.println(cur.size());if (cur.size() == 4 && begin == s.length()) {res.add(cur.get(0) + "." + cur.get(1) + "." + cur.get(2) + "." + cur.get(3));return;}if ((s.length() - begin > 3 * (4 - cur.size())) || (s.length() - begin < (4 - cur.size()))) {return;}for (int i = begin; i < begin + 3 && i < s.length(); i++) {String s1 = s.substring(begin, i + 1);int num = Integer.parseInt(s1);if (num >= 0 && num <= 255 && (s1.length() == 1 || s1.charAt(0) != '0')) {cur.add(s1);dfs(s,i + 1, res, cur);cur.remove(cur.size() - 1);}}}public int editDistance (String str1, String str2) {int m = str1.length(), n = str2.length();int[][] dp = new int[m + 1][n + 1];for (int i = 1; i <= n; i++) {dp[0][i] = dp[0][i - 1] + 1;}for (int i = 1; i <= m; i++) {dp[i][0] = dp[i - 1][0] + 1;}for (int i = 1; i <= m; i++) {for (int j = 1; j <= n; j++) {if (str1.charAt(i - 1) == str2.charAt(j - 1)) {dp[i][j] = dp[i - 1][j - 1];} else {dp[i][j] = Math.min(dp[i - 1][j], Math.min(dp[i][j - 1], dp[i - 1][j - 1])) + 1;}}}return dp[m][n];}public int rob (int[] nums) {int n = nums.length;int[] dp = new int[n + 1];dp[1] = nums[0];for (int i = 2; i <= n; i++) {dp[i] = Math.max(nums[i - 1] + dp[i - 2], dp[i - 1]);}return dp[n];}public int rob2 (int[] nums) {int n = nums.length;int[] dp = new int[n + 1];dp[1] = nums[0];for (int i = 2; i < n; i++) {dp[i] = Math.max(nums[i - 1] + dp[i - 2], dp[i - 1]);}int res = dp[n - 1];Arrays.fill(dp, 0);dp[1] = 0;for (int i = 2; i <= n; i++) {dp[i] = Math.max(nums[i - 1] + dp[i - 2], dp[i - 1]);}return Math.max(res, dp[n]);}public int maxProfit (int[] prices) {int n = prices.length;if (n == 0) return 0;int res = 0;int min = prices[0];for (int i = 1; i < n; i++) {res = Math.max(prices[i] - min, res);min = Math.min(min, prices[i]);}return res;}public int maxProfit1 (int[] prices) {int n = prices.length;if (n <= 1) return 0;int res = 0;for (int i = 1; i < n; i++) {if (prices[i] - prices[i - 1] > 0) {res += prices[i] - prices[i - 1];}}return res;}
}