java中如何在子数组中查找最小值
我有一次考试,但没能显示出来 请帮助我们如何做到这一点 给定一个大小为N的数组A[]和一个整数k。任务是打印大小为k的每个子数组的最小元素java中如何在子数组中查找最小值,java,arrays,Java,Arrays,我有一次考试,但没能显示出来 请帮助我们如何做到这一点 给定一个大小为N的数组A[]和一个整数k。任务是打印大小为k的每个子数组的最小元素 For Each valid index i(0<=i <=N -K) Have to print min(A[i],A[i+1],A[i+2]...A[i+k]). 输出: 0 0 2 2 但我尝试的是找到最大元素: 我知道这是错误的。但我只知道这一点 public static int maxSum(int arr[], int n, i
For Each valid index i(0<=i <=N -K) Have to print min(A[i],A[i+1],A[i+2]...A[i+k]).
输出:
0 0 2 2
但我尝试的是找到最大元素:
我知道这是错误的。但我只知道这一点
public static int maxSum(int arr[], int n, int k)
{
// k must be greater
if (n < k)
{
System.out.println("Invalid");
return -1;
}
// Compute sum of first window of size k
int res = 0;
for (int i=0; i<k; i++)
res += arr[i];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
int curr_sum = res;
for (int i=k; i<n; i++)
{
curr_sum += arr[i] - arr[i-k];
res = Math.max(res, curr_sum);
}
return res;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {5,2,10,0,3,2,5};
int k = 7;
int n = arr.length;
System.out.println(maxSum(arr, n, k));
}
}
公共静态整数最大和(整数arr[],整数n,整数k)
{
//k必须更大
if(n 对于(inti=0;i这是我在大约5分钟内编写的一个超级简单的解决方案。
注:我不执行输入,n
、k
、array
值仅在main
方法中硬编码
package stackoverflow;
public class MinimumSubArray {
public static void main(String[] args) {
solve(5, 2, new int[]{ 10, 0, 3, 2, 5 }); // expect 0 0 2 2
solve(5, 2, new int[]{ 10, 0, 3, 2, 1 }); // expect 0 0 2 1
solve(1, 1, new int[]{ 6 }); // expect 6
solve(3, 3, new int[]{ 3, 2, 1 }); // expect 1
solve(3, 1, new int[]{ 3, 2, 1 }); // expect 3 2 1
}
private static void solve(final int n, final int k, final int[] array) {
if (n != array.length)
throw new IllegalArgumentException( String.format("Array length must be %d.", n) );
if (k > n)
throw new IllegalArgumentException( String.format("K = %d is bigger than n = %d.", k, n) );
int currentStartIndex = 0;
while (currentStartIndex <= (n - k)) {
int min = array[currentStartIndex];
for (int i = currentStartIndex + 1; i < currentStartIndex + k; i++) {
if (array[i] < min) {
min = array[i];
}
}
System.out.printf("%d ", min); // print minimum of the current sub-array
currentStartIndex++;
}
System.out.println();
}
}
包堆栈溢出;
公共类最小子阵{
公共静态void main(字符串[]args){
solve(5,2,newint[]{10,0,3,2,5});//预期为0,2
solve(5,2,newint[]{10,0,3,2,1});//预期为0,2,1
求解(1,1,新int[]{6});//期望6
solve(3,3,newint[]{3,2,1});//期望值为1
solve(3,1,newint[]{3,2,1});//预期3,2,1
}
私有静态void solve(final int n,final int k,final int[]数组){
if(n!=array.length)
抛出新的IllegalArgumentException(String.format(“数组长度必须为%d.”,n));
如果(k>n)
抛出新的IllegalArgumentException(String.format(“K=%d大于n=%d.”,K,n));
int currentStartIndex=0;
while(currentStartIndex)您自己的尝试在哪里?已更新,但我知道我编写的方法是错误的。我猜您必须找到的不是求和,而是最小元素?是的,它完全错误。它甚至与任务有关吗?似乎您试图找到的是最大和而不是最小元素。请在发布之前阅读。
public static int maxSum(int arr[], int n, int k)
{
// k must be greater
if (n < k)
{
System.out.println("Invalid");
return -1;
}
// Compute sum of first window of size k
int res = 0;
for (int i=0; i<k; i++)
res += arr[i];
// Compute sums of remaining windows by
// removing first element of previous
// window and adding last element of
// current window.
int curr_sum = res;
for (int i=k; i<n; i++)
{
curr_sum += arr[i] - arr[i-k];
res = Math.max(res, curr_sum);
}
return res;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int arr[] = {5,2,10,0,3,2,5};
int k = 7;
int n = arr.length;
System.out.println(maxSum(arr, n, k));
}
}
package stackoverflow;
public class MinimumSubArray {
public static void main(String[] args) {
solve(5, 2, new int[]{ 10, 0, 3, 2, 5 }); // expect 0 0 2 2
solve(5, 2, new int[]{ 10, 0, 3, 2, 1 }); // expect 0 0 2 1
solve(1, 1, new int[]{ 6 }); // expect 6
solve(3, 3, new int[]{ 3, 2, 1 }); // expect 1
solve(3, 1, new int[]{ 3, 2, 1 }); // expect 3 2 1
}
private static void solve(final int n, final int k, final int[] array) {
if (n != array.length)
throw new IllegalArgumentException( String.format("Array length must be %d.", n) );
if (k > n)
throw new IllegalArgumentException( String.format("K = %d is bigger than n = %d.", k, n) );
int currentStartIndex = 0;
while (currentStartIndex <= (n - k)) {
int min = array[currentStartIndex];
for (int i = currentStartIndex + 1; i < currentStartIndex + k; i++) {
if (array[i] < min) {
min = array[i];
}
}
System.out.printf("%d ", min); // print minimum of the current sub-array
currentStartIndex++;
}
System.out.println();
}
}