This commit is contained in:
2026-04-18 14:37:54 -05:00
parent 6cd3aca9a0
commit 3936b37e29
2 changed files with 130 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
// General imports
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Island {
static int N, M;
static int[][] grid;
static boolean[][] visited;
static Map<Integer, Integer> islands = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
grid = new int[N][M];
visited = new boolean[N][M];
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < M; j++) {
int cell = Integer.parseInt(st.nextToken());
grid[i][j] = cell;
}
}
br.close();
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (visited[i][j]) continue;
if (grid[i][j] > 0) {
islands.put(grid[i][j], islands.getOrDefault(grid[i][j], 0) + 1);
search(grid[i][j], i, j);
} else {
visited[i][j] = true;
}
}
}
if (islands.size() == 0) {
System.out.println(0);
return;
}
for (Map.Entry<Integer, Integer> e : islands.entrySet()) {
int terrainNumber = e.getKey();
int islandNum = e.getValue();
System.out.printf("%d %d%n", terrainNumber, islandNum);
}
}
static void search(int val, int i, int j) {
if (visited[i][j]) return;
visited[i][j] = true;
for (int dir = 0; dir < 4; dir++) {
switch(dir) {
case 0:
if (j + 1 < M && grid[i][j + 1] == val) {
search(val, i, j + 1);
}
break;
case 1:
if (i + 1 < N && grid[i + 1][j] == val) {
search(val, i + 1, j);
}
break;
case 2:
if (j - 1 >= 0 && grid[i][j - 1] == val) {
search(val, i, j - 1);
}
break;
case 3:
if (i - 1 >= 0 && grid[i - 1][j] == val) {
search(val, i - 1, j);
}
break;
}
}
}
}

View File

@@ -0,0 +1,50 @@
// General imports
import java.util.Scanner;
public class MaximumAura {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Write code here
int a = sc.nextInt();
int[]input = new int[a];
int[] left = new int[a];
int[] right = new int[a];
left[0] = 1;
right[a-1] =1;
for (int i = 0; i < a; i++) {
input[i] =sc.nextInt();
}
for (int i = 1; i < input.length; i++) {
int max = 0;
for (int j =0; j < i; j++) {
if (input[j]< input[i]) {
max = Math.max(max, left[j]);
}
}
left[i] = max +1;
}
for (int i = a-2; i >= 0; i--) {
right[i] = 1;
int max = 0;
for (int j = a-1; j > i; j--) {
if (input[j]< input[i]) {
max = Math.max(max, right[j]);
}
}
right[i] += max;
}
for (int i = 0; i < input.length; i++) {
System.out.print((left[i]+right[i]-1)+" ");
}
sc.close();
}
}