Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/main/java/algorithms/sprint7/EqualSums.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,30 @@
Пространственная сложность — O(S / 64).
*/
public class EqualSums {
private static final int MAX_GAMES = 300;
private static final int MAX_TOTAL = MAX_GAMES * 300;

static boolean solve(int[] points) {
int total = 0;
if (points.length > MAX_GAMES) {
throw new IllegalArgumentException("Too many games");
}

long total = 0;
for (int point : points) {
if (point < 0) {
throw new IllegalArgumentException("Point value is out of range");
}
total += point;
if (total > MAX_TOTAL) {
throw new IllegalArgumentException("Total points are out of range");
}
}

if ((total & 1) == 1) {
if ((total & 1L) == 1L) {
return false;
}

int target = total / 2;
int target = (int) (total / 2);
long[] reachable = new long[(target >> 6) + 1];
reachable[0] = 1L;

Expand Down Expand Up @@ -168,10 +180,17 @@ private static void run() throws Exception {
FastOut out = new FastOut(System.out);

int n = in.nextInt();
if (n < 0 || n > MAX_GAMES) {
throw new IOException("Number of games is out of range");
}
int[] points = new int[n];

for (int i = 0; i < n; i++) {
points[i] = in.nextInt();
int point = in.nextInt();
if (point < 0) {
throw new IOException("Point value is out of range");
}
points[i] = point;
}

out.writeAscii(solve(points) ? "True" : "False");
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/algorithms/sprint7/LevenshteinDistance.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
Пространственная сложность: O(min(n, m)), потому что хранится только две строки динамики.
*/
public class LevenshteinDistance {
private static final int MAX_LINE_LENGTH = 1000;

static int solve(String first, String second) {
if (first.length() > MAX_LINE_LENGTH || second.length() > MAX_LINE_LENGTH) {
throw new IllegalArgumentException("Input line is too long");
}
String s = first;
String t = second;

Expand Down Expand Up @@ -94,8 +98,8 @@ private int read() throws IOException {
return buf[ptr++];
}

String nextLine() throws IOException {
byte[] tmp = new byte[1024];
String nextLine(int maxLength) throws IOException {
byte[] tmp = new byte[Math.min(1024, maxLength + 1)];
int size = 0;
int c = read();

Expand All @@ -105,8 +109,11 @@ String nextLine() throws IOException {

while (c != -1 && c != '\n') {
if (c != '\r') {
if (size == maxLength) {
throw new IOException("Input line is too long");
Comment thread
krotname marked this conversation as resolved.
}
if (size == tmp.length) {
byte[] grown = new byte[tmp.length * 2];
byte[] grown = new byte[Math.min(tmp.length * 2, maxLength)];
System.arraycopy(tmp, 0, grown, 0, tmp.length);
tmp = grown;
}
Expand Down Expand Up @@ -169,8 +176,8 @@ private static void run() throws Exception {
FastIn in = new FastIn(System.in);
FastOut out = new FastOut(System.out);

String s = in.nextLine();
String t = in.nextLine();
String s = in.nextLine(MAX_LINE_LENGTH);
String t = in.nextLine(MAX_LINE_LENGTH);

out.writeInt(solve(s, t));
out.writeByte('\n');
Expand Down
Loading