给定一个整数数组,它可以包含+ ve和-ve数字.我要最大化数组中任何3个元素的乘积.元素可以是不连续的.
一些例子:
int[] arr = {-5, -7, 4, 2, 1, 9}; // Max Product of 3 numbers = -5 * -7 * 9
int[] arr2 = {4, 5, -19, 3}; // Max Product of 3 numbers = 4 * 5 * 3
Run Code Online (Sandbox Code Playgroud)
我尝试使用动态编程解决它,但我没有得到预期的结果.它在乘法中返回通常涉及相同数字两次的结果.因此,对于数组 - {4, 2, 1, 9}它正在返回 - 32,即4 * 4 * 2.
这是我的代码:
public static int maxProduct(int[] arr, int count) {
return maxProduct(arr, 0, arr.length - 1, count);
}
private static …Run Code Online (Sandbox Code Playgroud) 鉴于两个字符串:
str1 = "abcdefacbccbagfacbacer"
str2 = "abc"
Run Code Online (Sandbox Code Playgroud)
我要找到其中最长的子串str1由字符子集组成str2,在这种情况下它将是 - 7 (acbccba).什么是解决这个问题的方法,至少复杂.首先我想到了DP.但是,我认为DP确实不是必需的,因为我们必须搜索子字符串,而不是子序列.然后我虽然后缀树.但这需要额外的预处理时间.
最好的方法是什么?实际上,这个问题是否适用于后缀树或DP?