Is there a way to optimize my code more?

import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int J = in.nextInt() + 1;
int A = in.nextInt();

ArrayList<Integer>size[] = new ArrayList[3];

for (int i = 0; i < 3; i++) {
size[i] = new ArrayList<>();
}

for (int i = 1; i < J; i++) {
char x = in.next().charAt(0);
if (x == 'S') {
size[0].add(i);
} else if (x == 'M') {
size[1].add(i);
} else {
size[2].add(i);
}
}

ArrayList<Integer>arr[] = new ArrayList[J];

for (int i = 0; i < J; i++) {
arr[i] = new ArrayList<>();
}

for (int i = 0; i < A; i++) {
char x = in.next().charAt(0);
int num = in.nextInt();

if (num < J) {
if (x == 'S') {
arr[num].add(0);
} else if (x == 'M') {
arr[num].add(1);
} else {
arr[num].add(2);
}
}
}

int count = 0;
for (int i = 0; i < 3; i++) {
while (!size[i].isEmpty()) {
if (!arr[size[i].get(0)].isEmpty()) {
Iterator<Integer> iterator = arr[size[i].get(0)].iterator();
while (iterator.hasNext()) {
int adj = iterator.next();
if (adj <= i) {
count++;
iterator.remove();
break;
}
}
}
size[i].remove(0);
}
}

System.out.println(count);

}
}
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);

int J = in.nextInt() + 1;
int A = in.nextInt();

ArrayList<Integer>size[] = new ArrayList[3];

for (int i = 0; i < 3; i++) {
size[i] = new ArrayList<>();
}

for (int i = 1; i < J; i++) {
char x = in.next().charAt(0);
if (x == 'S') {
size[0].add(i);
} else if (x == 'M') {
size[1].add(i);
} else {
size[2].add(i);
}
}

ArrayList<Integer>arr[] = new ArrayList[J];

for (int i = 0; i < J; i++) {
arr[i] = new ArrayList<>();
}

for (int i = 0; i < A; i++) {
char x = in.next().charAt(0);
int num = in.nextInt();

if (num < J) {
if (x == 'S') {
arr[num].add(0);
} else if (x == 'M') {
arr[num].add(1);
} else {
arr[num].add(2);
}
}
}

int count = 0;
for (int i = 0; i < 3; i++) {
while (!size[i].isEmpty()) {
if (!arr[size[i].get(0)].isEmpty()) {
Iterator<Integer> iterator = arr[size[i].get(0)].iterator();
while (iterator.hasNext()) {
int adj = iterator.next();
if (adj <= i) {
count++;
iterator.remove();
break;
}
}
}
size[i].remove(0);
}
}

System.out.println(count);

}
}
15 Replies
JavaBot
JavaBot2d ago
This post has been reserved for your question.
Hey @Lantanggamer! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Lantanggamer
LantanggamerOP2d ago
I am doing a problem on DMOJ: https://dmoj.ca/problem/ccc15s2
DMOJ: Modern Online Judge
A school team is trying to assign jerseys numbered 1, 2, 3, \dots, J to student athletes. The size of each jersey is either small (S), medium (M) or large (L).
Lantanggamer
LantanggamerOP2d ago
ive been trying to figure out why its slow but I am unable to get to a solution
JavaBot
JavaBot2d ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Lantanggamer
LantanggamerOP2d ago
.
ayylmao123xdd
ayylmao123xdd2d ago
what are the current times vs how fast do you want it to be
Lantanggamer
LantanggamerOP23h ago
The last case is over 5 seconds It needs to be under
Lantanggamer
LantanggamerOP23h ago
Here’s the test cases
No description
ayylmao123xdd
ayylmao123xdd21h ago
hmmmmmmmmmmmmm i think you can simplify the code and maybe make it faster put all the jerseys in 1 hashmap and all the requirements in another hashmap and then you could loop over the players requirements table looking for the size and number and check in the other hashmap if its available if its available you set the value of the jersey to taken or however you want it and then you add +1 to the possibilities this could be quite fast cuz you can just check
map.get
or
map.containsKey
map.get
or
map.containsKey
Lantanggamer
LantanggamerOP20h ago
Wouldn’t that pretty much be the same as my code but using hashmap instead of ArrayList?
ayylmao123xdd
ayylmao123xdd20h ago
i think it would be less operations and only two loops to check the possibilities instead of 3 that you have
TonicBox
TonicBox16h ago
It will have more bytecode/processing overhead. Maps and lists only get faster when you get into really large sets of data Largely due to the boxing/unboxing that lists and maps necessitate as opposed to sticking with primatives Speed & Efficiency: Arrays & Primitives are super fast because they store data in a contiguous block of memory. This means quick access and better cache performance. HashMaps & Lists have extra layers (like hashing or node traversal) which add overhead and can slow things down. Memory Usage: Arrays use less memory since they hold data directly. Collections like HashMaps often use wrapper objects (e.g., Integer vs. int), which take up more space and can lead to autoboxing/unboxing overhead. Access & Operations: Arrays offer constant-time access with simple indexing. HashMaps require computing hash codes and handling collisions, while Lists (especially linked lists) might need to traverse elements, making operations slower. CPU & Optimization: Arrays benefit from better cache locality and allow the JVM’s JIT compiler to optimize code more effectively. HashMaps & Lists have less predictable memory access patterns, leading to cache misses and fewer optimization opportunities. Garbage Collection: Collections generate more temporary objects, increasing the garbage collector’s workload and potentially impacting performance. Arrays & Primitives are more lightweight, putting less strain on the GC. . So to optimise this, I'd recommend to rewrite it in a way that uses primatives and Arrays. Stear clear of boxing/unboxing as much as possible.
dan1st
dan1st15h ago
I'd say primitives vs arrays is probably not the issue here - more like that most of the code seems to do a lot of unnecessary stuff like having arrays of singleton lists for counts there are up to 10 to the power of 6 - as mentioned by ayylmao, your best chances are with a proper mapping from jersey numbers to information on what is available for that number and then you can iterate through all requests once and obtain the availability information for the jersey number and only check 3 values (S/M/L) per iteration - always using the smallest jersey possible or just use an array or list to store the sizes for each jersey number since you only have one and then process it request by request obtaining the jersey for any given number also fix your variable names
TonicBox
TonicBox15h ago
Yeah I was more responding to the suggestion of hashmaps. That's all true too though.
JavaBot
JavaBot10h ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?