Создан первый сервис.

This commit is contained in:
2025-03-27 23:18:35 +04:00
parent 7dee24b6ff
commit c5caa4d5ee
3 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
package ru.sguardian;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ContainerFirst {
public static void main(String[] args){
SpringApplication.run(ContainerFirst.class, args);
System.out.println("Server-1 started");
}
@PostMapping("/service-one")
public int[][] processMatrixPart(@RequestBody int[][] matrix){
long startTime = System.nanoTime();
MatrixProcessor process = new MatrixProcessor();
System.out.println("Service started work");
int[][] result = process.operationMatrix(matrix);
long endTime = System.nanoTime();
System.out.println("Service-1 end work " + (endTime - startTime) / 1000000 + " ms");
return result;
}
}

View File

@@ -0,0 +1,50 @@
package ru.sguardian;
import java.util.Arrays;
public class MatrixProcessor {
public int[][] operationMatrix(int[][] array) {
if (array.length <= 1) {
return array;
}
int mid = array.length / 2;
int[][] left = Arrays.copyOfRange(array, 0, mid);
int[][] right = Arrays.copyOfRange(array, mid, array.length);
left = operationMatrix(left);
right = operationMatrix(right);
return merge(left, right);
}
private int[][] merge(int[][] left, int[][] right) {
int[][] result = new int[left.length + right.length][];
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (findMaxElementRow(left[i]) >= findMaxElementRow(right[j])) {
result[k++] = left[i++];
} else {
result[k++] = right[j++];
}
}
while (i < left.length) {
result[k++] = left[i++];
}
while (j < right.length) {
result[k++] = right[j++];
}
return result;
}
public static int findMaxElementRow(int[] row) {
int max = Integer.MIN_VALUE;
for (int num : row) {
max = Math.max(max, num);
}
return max;
}
}

View File

@@ -0,0 +1 @@
server.port=8080