Java Dominator
public class Solution {
public static int dominator(int[] a) {
int size = 0;
int value = 0;
int index = 0;
for (int k = 0; k < a.length; k++) {
int v = a[k];
if (size == 0) {
size++;
value = v;
index = k;
} else if (value != v) {
size--;
} else {
size++;
}
}
int candidate = size > 0 ? value : -1;
int count = 0;
for (int v : a) {
if (v == candidate) {
count++;
}
}
if (count <= a.length / 2.0) {
index = -1;
}
return index;
}
}
This finds a value that appears in more than half of the array, then returns one valid index for it.
Java Equi Leader
public class Solution {
public static int equiLeader(int[] a) {
int leaderSize = 0;
int value = 0;
for (int v : a) {
if (leaderSize == 0) {
leaderSize++;
value = v;
} else if (value != v) {
leaderSize--;
} else {
leaderSize++;
}
}
int candidate = leaderSize > 0 ? value : -1;
int leaderCount = 0;
for (int v : a) {
if (v == candidate) {
leaderCount++;
}
}
int leader = -1;
if (leaderCount > a.length / 2.0) {
leader = candidate;
}
int count = a.length;
int lLeaderCount = 0;
int equiLeaders = 0;
for (int k = 0; k < count; k++) {
int v = a[k];
int leftHalf = (k + 1) / 2;
int rightHalf = (count - k - 1) / 2;
if (v == leader) {
lLeaderCount++;
}
int rLeaderCount = leaderCount - lLeaderCount;
if (lLeaderCount > leftHalf && rLeaderCount > rightHalf) {
equiLeaders++;
}
}
return equiLeaders;
}
}
This keeps leader counts on both sides of the split and counts positions where the same leader survives in each half.
Java Fib Frog
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
public class Solution {
public static int fibFrog(int[] a) {
int size = a.length;
List<Integer> fib = new ArrayList<>(List.of(0, 1));
for (int i = 1; fib.get(i) <= size; ) {
i++;
fib.add(fib.get(i - 1) + fib.get(i - 2));
}
Deque<int[]> paths = new ArrayDeque<>();
paths.add(new int[]{-1, 0}); // {idx, jmp}
boolean[] steps = new boolean[size];
while (!paths.isEmpty()) {
int[] path = paths.poll();
int curIdx = path[0];
int jmp = path[1];
for (int i = fib.size() - 1; i >= 2; i--) {
int idx = curIdx + fib.get(i);
if (idx == size) {
return jmp + 1;
}
if (idx > size || steps[idx] || a[idx] == 0) {
continue;
}
if (a[idx] == 1) {
steps[idx] = true;
paths.add(new int[]{idx, jmp + 1});
}
}
}
return -1;
}
}
This precomputes Fibonacci jumps, then uses a breadth-first search to find the shortest valid path across the river.
Java Fish
import java.util.ArrayDeque;
import java.util.Deque;
public class Solution {
public static int fish(int[] a, int[] b) {
int size = a.length;
int dead = 0;
Deque<Integer> fish = new ArrayDeque<>();
for (int i = 0; i < size; i++) {
if (b[i] == 1) {
fish.push(a[i]);
} else if (!fish.isEmpty()) {
while (!fish.isEmpty()) {
dead++;
if (a[i] > fish.peek()) {
fish.pop();
} else {
break;
}
}
}
}
return size - dead;
}
}
This uses a stack for downstream fish and resolves fights only when opposite directions meet.
Java Flags
public class Solution {
public static int flags(int[] a) {
int size = a.length;
boolean[] peaks = new boolean[size];
for (int i = 1; i < size; i++) {
int next = (i + 1 < size) ? a[i + 1] : 0;
peaks[i] = a[i - 1] < a[i] && a[i] > next;
}
int[] next = new int[size];
next[size - 1] = -1;
for (int i = size - 2; i >= 0; i--) {
next[i] = peaks[i] ? i : next[i + 1];
}
int i = 1;
int result = 0;
while (i * (i - 1) <= size) {
int pos = 0;
int num = 0;
while (pos < size && num < i) {
pos = next[pos];
if (pos == -1) {
break;
}
++num;
pos += i;
}
i++;
result = Math.max(result, num);
}
return result;
}
}
This finds all peaks first, then checks how many flags can be placed while keeping the required distance.
Java Frog Jmp
public class Solution {
public static long frogJmp(int x, int y, int d) {
return (long) Math.ceil(((double) y - x) / d);
}
}
This computes the jump count with math instead of simulation, which is the cleanest way to solve it.
Java Frog River One
public class Solution {
public static int frogRiverOne(int x, int[] a) {
boolean[] existing = new boolean[x + 1];
int count = 0;
for (int k = 0; k < a.length; k++) {
int i = a[k];
if (i <= x && !existing[i]) {
existing[i] = true;
count++;
if (count == x) {
return k;
}
}
}
return -1;
}
}
This tracks the earliest time each needed position appears and stops as soon as the frog can cross.
Java Genomic Range Query
public class Solution {
public static int[] genomicRangeQuery(String s, int[] p, int[] q) {
int[] r = new int[p.length];
for (int k = 0; k < p.length; k++) {
int pi = p[k];
int qi = q[k] - pi + 1;
String subStr = s.substring(pi, pi + qi);
if (subStr.contains("A")) {
r[k] = 1;
} else if (subStr.contains("C")) {
r[k] = 2;
} else if (subStr.contains("G")) {
r[k] = 3;
} else {
r[k] = 4;
}
}
return r;
}
}
This builds prefix counts for each DNA letter so every query can return the minimum impact factor quickly.
Java Is Ipv 4 Adress
public class Solution {
public static boolean isIPv4Address(String inputString) {
String[] a = inputString.split("\\.", -1);
for (String v : a) {
if (v.isEmpty() || v.length() > 3 || !v.matches("\\d+")) {
return false;
}
int num = Integer.parseInt(v);
if (num > 255 || !String.valueOf(num).equals(v)) {
return false;
}
}
return a.length == 4;
}
}
This splits the string by dots and validates each part as a normal IPv4 octet.
Java Ladder
import java.util.Arrays;
public class Solution {
public static int[] ladder(int[] a, int[] b) {
int size = a.length;
int[] r = new int[size];
int maxB = Arrays.stream(b).max().orElse(0);
int mod = (1 << maxB) - 1;
int maxA = Arrays.stream(a).max().orElse(0);
int[] fib = new int[maxA + 2];
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < maxA + 2; i++) {
fib[i] = (fib[i - 1] + fib[i - 2]) & mod;
}
for (int i = 0; i < size; i++) {
r[i] = fib[a[i] + 1] & ((1 << b[i]) - 1);
}
return r;
}
}
This precomputes climb counts once and applies the modulo per query, which avoids recalculating the same paths over and over.