Cerinta completa

In this challenge, you will be given an array and must determine an array . There is a special rule: For all , . That is, can be any number you choose such that . Your task is to select a series of given such that the sum of the absolute difference of consecutive pairs of is maximized. This will be the array’s cost, and will be represented by the variable below.

The equation can be written:

For example, if the array , we know that , , and . Arrays meeting those guidelines are:

[1,1,1], [1,1,2], [1,1,3]
[1,2,1], [1,2,2], [1,2,3]

Our calculations for the arrays are as follows:

|1-1| + |1-1| = 0	|1-1| + |2-1| = 1	|1-1| + |3-1| = 2
|2-1| + |1-2| = 2	|2-1| + |2-2| = 1	|2-1| + |3-2| = 2

The maximum value obtained is .

Function Description

Complete the cost function in the editor below. It should return the maximum value that can be obtained.

cost has the following parameter(s):

  • B: an array of integers

Input Format

The first line contains the integer , the number of test cases.

Each of the next pairs of lines is a test case where:
– The first line contains an integer , the length of
– The next line contains space-separated integers

Constraints

Output Format

For each test case, print the maximum sum on a separate line.

Sample Input

1
5
10 1 10 1 10

Sample Output

36

Explanation

The maximum sum occurs when A[1]=A[3]=A[5]=10 and A[2]=A[4]=1. That is .


Limbajul de programare folosit: java8

Cod:

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

  // Complete the cost function below.
  static int cost(int[] B) {
    int lowP = 0;
    int highP = 0;

    for (int i = 1; i < B.length; i++) {
      int l = Math.max(lowP, highP + Math.abs(B[i - 1]  - 1));
      int h = Math.max(lowP + Math.abs(B[i] - 1), highP + Math.abs(B[i] - B[i - 1]));
      lowP = l;
      highP = h;
    }

    return Math.max(lowP, highP);
  }

  private static final Scanner scanner = new Scanner(System.in);

  public static void main(String[] args) throws IOException {
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

    int t = scanner.nextInt();
    scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

    for (int tItr = 0; tItr < t; tItr++) {
      int n = scanner.nextInt();
      scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

      int[] B = new int[n];

      String[] BItems = scanner.nextLine().split(" ");
      scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

      for (int i = 0; i < n; i++) {
        int BItem = Integer.parseInt(BItems[i]);
        B[i] = BItem;
      }

      int result = cost(B);

      bufferedWriter.write(String.valueOf(result));
      bufferedWriter.newLine();
    }

    bufferedWriter.close();

    scanner.close();
  }
}

Scor obtinut: 1.0

Submission ID: 464604596

Link challenge: https://www.hackerrank.com/challenges/sherlock-and-cost/problem

Sherlock and Cost