#Java Code optimization- running out of time

1 messages Ā· Page 1 of 1 (latest)

orchid orioleBOT
#

<@&987246399047479336> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question šŸ¤–. Maybe it helps! In any case, a human is on the way šŸ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

In order to optimize the code and improve its performance, you can consider the following suggestions:

  1. Use more efficient data structures: Instead of using ArrayLists, consider using more efficient data structures such as LinkedList or HashSet, depending on your specific requirements. These data structures can provide faster insertion and retrieval times.

  2. Preallocate ArrayList capacity: If you know the expected size of your ArrayLists in advance, use the ArrayList(int initialCapacity) constructor to preallocate memory for the lists. This can help avoid unnecessary resizing operations during insertion.

  3. Use enhanced for loop: Instead of using traditional for loops with index variables, use enhanced for loops (also known as "for-each" loops) when iterating over collections. This can provide better performance and cleaner code.

  4. Avoid unnecessary object creation: Be mindful of creating unnecessary objects within loops or methods that are called frequently. For example, if you don't need to modify the elements in your ArrayLists, consider using an immutable collection like Collections.unmodifiableList() instead.

  5. Consider parallel processing: If your code allows it, consider using parallel processing techniques such as Java 8's parallel streams or multi-threading to process the ArrayLists concurrently. This can help leverage multiple CPU cores and improve overall performance.

  6. Profile your code: Use a profiler tool to identify any bottlenecks or areas of improvement in your code. Profiling will help you pinpoint specific methods or operations that are consuming excessive time or resources.

Remember that optimization is a trade-off between speed and readability/maintainability of the code. Make sure to measure and test the performance improvements after implementing these suggestions to ensure they have a positive impact on your specific use case.

obtuse oasis
#

I need help to optimise the code as this code is not passing the test cases, it is taking too long with larger data set.

Especially, I have 2 ArrayLists area and price which needs to be sent to a method (called isOutlier). However, before sending both the lists, I want to remove its first element and then send. In the second iteration, I want the first elements back in both the lists and this time I want to remove the second elements from both and then send. In the third iteration, I want the first two elements intact and I want to remove the third element and so on. That is why I have made a copy of the lists. But I think, making a copy in every iteration is using too much time.

Please please help me in optimising this code, the timer is running.

vital spruce
#

"the timer is running"

#

What timer

obtuse oasis
#

I have to submit this code

vital spruce
#

When

obtuse oasis
#

within 1 hour

vital spruce
#

Is this an interview screening?

#

Or a school assignment

#

Also I'm maybe 15 minutes from my laptop

obtuse oasis
#

there are 3 test hidden cases remaining which are not passing

#

school assignmnet

#

because of the optimisation, the compiler is stopping the code and I cant even see if the output is correct or not

#

rest 5 test cases have passed, so maybe if it is optimised

#

please helpplease

crisp pike
#

and try to code clean spoderman

strong glen
#

so given a list of 10 elements, the first loop you want elements 2-10, 2nd time 1, 3-10, 3th time 1,2,4-10, ... ?

obtuse oasis
#

I need to use some kind of efficient data structure for it

crisp pike
#

ohh

#

u can just ignore that index

#

rather than copying into new

obtuse oasis
#

Also, in general,(not only that removing number problem) the code needs to perform faster

crisp pike
#

just pass the index into the func and skip it

#

can u share me full code and ur input, so i can debug it out?

#

and see where it taking much time

obtuse oasis
#

that is the full code

crisp pike
#

there is no main func?

obtuse oasis
#

I dont have to edit the main part of the code, I can only edit the method

crisp pike
#

gotcha

#

can u give me any input?

obtuse oasis
#

wait I'll share the question

#

A real estate agent is advising a seller on the price to ask for a home. To do this, the agent will look at homes that have sold in the area and base the valuation on this data. The only factors that will be considered are square footage and sale price.
The agent starts by removing any outliers from the list of comparable homes. To determine the outliers:
• Select the home to test.
• Create a list of prices of other homes of the same size. It will be called compList in the examples.
• If there are no other homes of the same size, the house being tested is not an butlier.
• Otherwise:
• Calculate the mean price, P[m], and the standard deviation, o, for the homes compList.
° If | price[i] - P[m ]/ > 3 * o, the house is an outlier.

#

so, when I select a home to test for outlier, I dont have to consider that home, that is why I need to remove it from the list

crisp pike
#

is there any link to check this problem online?

obtuse oasis
#

No

#

I tried

crisp pike
#

no input too?

#

is there any example to understand?

obtuse oasis
#

yes, input I'll give

#

yes

#

1 minute

#

this is the example

#

the logic of the code is correct, but all the copying and removing is making it slow

crisp pike
#

got it now

obtuse oasis
#

please help me optimise it then

crisp pike
#

give me some mins, lemme see if i can see anything

obtuse oasis
#

sure

strong glen
#

you could use a stream, and filter it based on the index

obtuse oasis
#

how

#

but stream is also not very efficient, right?

crisp pike
#

nvm, i will just optimize this part of urs

strong glen
#

Based on the question it also sounds like you don't need to remove a single element, but rather fetch houses of the same size?

obtuse oasis
#

yes

#

that is what I'm doing in the isOuliers method

#

if there are 3 houses of 1200 area, of prices 15k, 16k and 17k respectively. When considering the first house, I need the 16k and 17k house. When considering the second house, i need the 15k and 17k house. but the problem is that prices are in different arraylist

crisp pike
obtuse oasis
#

no, no it is working as intended

#

in the outliers method, it is only adding those houses as need, according to the example

#

need to find a better way of doing it

#

sorry for being nagging but please please hurryplease

strong glen
#

you should use area.get(i).equals(currentArea) by the way. You're using Long and not long.

crisp pike
orchid orioleBOT
obtuse oasis
#

😭

crisp pike
#

wait, i can do more

#

this is it, i don't understand anything in if-else structure

orchid orioleBOT
obtuse oasis
#

still exceeding the time😭

crisp pike
#

wow then, what u did after storing outliers?

obtuse oasis
#

should I send the second half of the question?

crisp pike
#

yup and how much time remains?

#

u used many streams to calc something, which can be done inside any prev loops

obtuse oasis
#

After removing outliers, The valuation is then calculated against the resulting list using the following:
If there are no houses in the list, use 1000 per square foot as the price.
• If there is only 1 house in the list, its square foot price is used.
• If there are 1 or more houses in the list with the exact square footage of the house to price, use the mean of those prices.
• If the required square footage is between the square footage of two houses in the list, interpolate the square foot price using the means of the closestĀ higher,Ā and

#

the required square footage is outside of the range of houses listed, extrapolate the price based on the means of the two square footage values that are closest to the home to value.In all cases, if the final price is less than 1000 or greater than 10^6, the price will be 103 and 106 , respectively. For any square footage, the square foot price is the mean of the prices at that square footage. Return an integer that represents the valuation of the seller's house.
For example, there are n = 6 houses with area = [1200, 1300, 1250, 1300, 1200,
2000], price = [12000, 24000, 14000, 22000, 13000, 30000] and the house to value has reqArea = 1500 square feet. The following table showsĀ theĀ testĀ for.The 1300 square foot houses are both outliers, so they are discarded. The new
arrays are area' = [1200, 1200, 1200, 2000] and price' = [12000, 14000, 13000,
30000]. Interpolate the price between the two house sizes remaining. The interpolated price is 13000 + (30000 - 13000)/(2000-1200) * (1500-1200)Ā =
19375.

crisp pike
#

newArea.contains(reqArea)
You used this which is itself O(N)
u could have used set to do it in O(1)

#

there can be many optimizations possible

#

Now, if i change it to a hashset, i need to change it everywhere peepo_sad

obtuse oasis
#

this is the entire question

crisp pike
#

for(long val: newArea) if((secondMax<val) && (val<max)) secondMax=val;
Same here, u are looping around for calc secondMax, which could be calc in O(1) above

crisp pike
#

u used list, which takes O(N) to find an element

obtuse oasis
#

please do in sets😭

#

the main area and price will remain in Arraylist

#

please optimise it as much as possible, the last three test cases are not passing😭

crisp pike
#

I am trying, there is so much to do thisisfine

#
            long closestMax=newArea.stream().filter(val->val>reqArea).min(Comparator.comparingLong(val->Math.abs(reqArea-val))).orElseThrow(NoSuchElementException::new);```
#

What is these two lines doing? can u explain it

obtuse oasis
crisp pike
#

ohh nearest ones

obtuse oasis
#

I also have loop for closest min and max, but I thought streams was efficient

#

for(int i=0; i< n; i++){
long val=newArea.get(i);
// long difference= Math.abs(reqArea-val);
if ((val< reqArea)&&((Math.abs(reqArea-val))< Math.abs(reqArea-closestMin))){
closestMin=val;
}
else if ((val>reqArea)&&(Math.abs(reqArea-val))< Math.abs(reqArea-closestMax)){
closestMax=val;
Ā Ā Ā Ā Ā Ā Ā Ā }

strong glen
#

you're also looping an awfull lot over newArea

#

so maybe see if you can optimize that a bit

#

you could also set up a map<Long, List<Long>> <= where the key is area, and the latter is the list of prices

crisp pike
orchid orioleBOT
obtuse oasis
#

give me 2 mins to implement and run the code

strong glen
#

Also just a tip since I see a decent bit of min/maxing. As of Java 21 there's a Math.clamp function

crisp pike
#

I am still running java 17 lol peepo_christ

#

can u give me constraints of the problem @obtuse oasis ??

#

is it more than 10^4 ?

#

I optimized it to run in O(N^2) now i guess, running it in lower complexing needs time to think then and its 2 am for me thisisfine

obtuse oasis
crisp pike
# obtuse oasis

bro, then this will not work, we need to do in O(N) or O(NlogN)

obtuse oasis
#

oh

#

I'm sorry for troubling you so much

crisp pike
#

I can do that, but I am not in my mind cause its midnight

obtuse oasis
#

please

#

please

#

do it

#

it will take a couple of minutes for you

#

please

crisp pike
#

if i can somehow make outlier check func to be O(1), its solved

#

but the Q is... how?

obtuse oasis
#

I tried my best to do it and you did a better job in 20 mins

crisp pike
#

need to do some maths

#

long sd= Math.round(Math.sqrt(complist.stream().mapToDouble(val->Math.pow(val-mean,2)).sum()/(complist.size())));
Can u optimize it somehow, i will try other looop

obtuse oasis
#

yes

crisp pike
#

i will reduce mean

obtuse oasis
#

private static long computeSd(List<Long> data) {
double sum = 0.0;
long mean = computeMean(data);

for (long val : data) {
    sum = sum + Math.pow(val - mean, 2);
}
return Math.round(Math.sqrt(sum / data.size()));

}

#

instead of calling the mean method, passing the value of mean would be better

crisp pike
#

u need to implement it in O(1) for one

#

or O(N) for all

obtuse oasis
#

but standard deviation will always be O(N)

#

yeah, that's what

crisp pike
obtuse oasis
crisp pike
#

but its giving O(N) for one value of list right, so for N values, there will be O(N*N) time

strong glen
#

and this also doesn't help size wise: long closestMin=newArea.stream().filter(val->val<reqArea).min(Comparator.comparingLong(val->Math.abs(reqArea-val))).orElseThrow(NoSuchElementException::new);
long closestMax=newArea.stream().filter(val->val>reqArea).min(Comparator.comparingLong(val->Math.abs(reqArea-val))).orElseThrow(NoSuchElementException::new);

#

but also a tad late for me to rewrite that section

#

but try to reduce the amount of times you loop through your collections, try to group a bit. And indeed some things could be sets/hashmaps

obtuse oasis
#

I'm trying to implement the code you sent last @crisp pike

#

and both distA and distB are not initialised, should I initialise them with MAX_VALUE?

obtuse oasis
#

oh yeah, sorry

crisp pike
#

        Map<Long, Integer> map = new HashMap<>();
        Map<Long, Long> prices = new HashMap<>();

        for(int i=0; i<size; ++i) {
            map.put(area.get(i), map.getOrDefault(area.get(i), 0)+1);
            prices.put(area.get(i), prices.getOrDefault(area.get(i), 0L) + price.get(i));
        }

        for(int i=0; i<size; ++i)
            mean[i] = map.get(area.get(i)) == 1 ? 0 : Math.round((prices.get(area.get(i)) - price.get(i)) / (map.get(area.get(i))-1));```
orchid orioleBOT
crisp pike
#

I implemented and calc all mean in O(N) like this

#

now need to think about standard deviation

obtuse oasis
#

ok

obtuse oasis
#

we are keeping the outlier method same?

obtuse oasis
crisp pike
#

my mind is not working peepo_sad

obtuse oasis
#

please😭

crisp pike
obtuse oasis
crisp pike
#

i am thinking of optimizing the sd

#

but

#

mean is working good, i checked but sd .................... howwww peepoG

obtuse oasis
#

I dont know😭 😭

#

I'll have a look at the logic

#

I got where the logic wrong

#

if the homes are of exact same area, we have to take mean of that

#

else if(newArea.contains(reqArea)){
// int c=0;
// double sum=0;
// for(int i=0; i<n;i++){
// if(newArea.get(i)==reqArea){
// c++;
// sum+= (newPrice.get(i));
// }
// }
// return Math.max(1000, Math.min(1000000, Math.round(sum/c)));
Ā Ā Ā Ā //Ā }

#

this one

#

got that 1 test case back, but the three optimisation one's are still there😭 😭

#

@crisp pike

crisp pike
#

i am trying to think but its 2:30 am now -_-

obtuse oasis
#

please

#

please

#

please

#

only standard deviation for O(N) is left?

#

we are not making any changes to isOutlier method?

crisp pike
obtuse oasis
#

okay, I'm waiting

crisp pike
#

got an idea, lets test it

obtuse oasis
#

yayyy

#

came up with something?

crisp pike
orchid orioleBOT
crisp pike
#

it should run in O(N)

#

i optimized the isOutlier() method to run in O(1)

obtuse oasis
#

let me try and run it

crisp pike
#

I don't think, there should be time problem now thisisfine

obtuse oasis
#

just wait 5 mins

#

please

crisp pike
#

There can be a wrong answer, but not time limit if i am right, optimized it too much!
I even ask, why ur school assignment needs too much of cleansing of code?

#

if this gives tle, I am ded for sure

obtuse oasis
#

is there a 0L in the second line of first for loop of the program?

crisp pike
#

yeah

#

second* loop

#

umm, imma go sleep now šŸ™‚

obtuse oasis
#

no no

#

wait

#

2 mins

#

I'm getting some small errors

#

let me run at altest once

#

please

crisp pike
#

this won't give u tle, maybe wrong answer if i changed any logic in between, that u can change, like i removed round from mean and sd, u can do that

obtuse oasis
#

wait

#

it says

#

method outlier cannot be aplied to given types

#

Oh i made a mistake

#

please stay up for 2 more mins

#

yes

#

wrong output

#

but no ttle

#

but idk anything about this code

#

how do i debug????

#

still 3 test cases remaining😭

#

@crisp pike

#

help me 1 last time

crisp pike
#

so no tle

obtuse oasis
#

there are 2 error in interpolation

#

@crisp pike

#

please help me one last time, i would have debug, but I dont understand the map part

#

or at least explain it to me, so I can debug

crisp pike
obtuse oasis
#

sure

#

One of the test cases failing is not hidden, its this one

#

the expected output is 30625, but the output is 31667

#

How do I find the complist ?

crisp pike
orchid orioleBOT
obtuse oasis
#

okay

#

give me 2 mins

crisp pike
crisp pike
obtuse oasis
#

fingers crossed

crisp pike
#

if it works or does not works, I am going to sleep peepo_sad

obtuse oasis
#

wait for 2 mins

crisp pike
#

I shut my laptop down peepo_comfy

#

It's 3:26 am fat_laugh

obtuse oasis
#

nooo

#

1 more min just

#

its doneeeeee

#

yayyyyyyyyy

crisp pike
#

Correct?

obtuse oasis
#

thank you sooooooo much

#

yeah

crisp pike
#

Send me a ss

#

I wanna see

obtuse oasis
#

yes

crisp pike
#

I will explain this code then tomorrow okay? I will go to sleep u know peepo_heart

obtuse oasis
#

yeah okay

crisp pike
obtuse oasis
#

you came in the form of an angelplease

crisp pike
#

U know, the difficulty of this Q is around medium-hard

#

Why ur school needs this?

crisp pike
#

Difficulty is that because of the optimisation

obtuse oasis
#

yes, logic, I had sorted

crisp pike
#

We implemented mean and variance in O(1) for every element like bruhh

obtuse oasis
#

but optimisation, OMG

#

You're an expert

#

what do you do?

crisp pike
#

Yup, ur logic was correct, I did not changed any logic.. nor i understand.. I just optimized lol pepekek

obtuse oasis
#

😭 thank you so much

crisp pike
obtuse oasis
#

really?

#

where

crisp pike
#

In an Asian University

obtuse oasis
#

cool

crisp pike
#

I guess so lol

obtuse oasis
#

Well, thank you soooooooooooo much

crisp pike
#

U did not answer my Q tho?

obtuse oasis
#

my school?

#

huh

crisp pike
#

Why ur school needs u to solve this hard prob?

obtuse oasis
#

stupid teacher is challenging

crisp pike
#

And are u in school for real? Or university?

obtuse oasis
#

he taught big (o) from Sumita Arora book

#

school

#

DPS

crisp pike
#

Wait, which country u are from?

obtuse oasis
#

India

crisp pike
obtuse oasis
#

šŸ˜‚

crisp pike
#

U do this stuff in dps ?

obtuse oasis
#

what did you think?

crisp pike
#

I thought u are like some American guy but u are just local to me

obtuse oasis
#

where are you from?

#

in india

crisp pike
#

U should complain .. this is not a problem for school students

crisp pike
obtuse oasis
#

I think, I'll be the only one to do

crisp pike
#

Well, it means this is also close to 4 am for u

obtuse oasis
#

yes

crisp pike
#

Good night then peepo_heart

obtuse oasis
#

Good night

#

and thanks again

crisp pike
#

I will explain the code tomorrow, ping me again on this thread if I forget

obtuse oasis
#

oki

#

byeeeeeee

#

thanksssss

crisp pike
#

Byee peepo_comfy

orchid orioleBOT
#

Closed the thread.

crisp pike
#

hey @obtuse oasis

obtuse oasis
#

Hey

crisp pike
orchid orioleBOT
crisp pike
#

So, lets see how i calculated mean without loop

#

at that time, i did many things lol, i need to see ur code first so i can see where i changed stuff lol

orchid orioleBOT
crisp pike
# crisp pike u did that hmm

@obtuse oasis in the part, where u were calc mean in isOutlier function, u looped through array to add values in compList which are same as the currentArea and later sum them

#

So, what u are doing is... u were calc sum of all x num in the list... but not the x itself

#

x + x + x + x + ......... to (n-1) times

#

which can be calculated as x*(n-1) right?

crisp pike
# crisp pike here it is

So, using hashmap named "map" here, i store the freq count of an area, let say, i got from the hashmap thay currentArea came 7 times in the "area" list

obtuse oasis
#

yes

#

okay

crisp pike
#

In "prices" map, this includes, all the prices of that area came

#

but on "prices" map, any area will contains a list of prices from index 1, and at index 0, it contains all the sum of the total prices in this area

#
        Map<Long, List<Long>> prices = new HashMap<>();

        for(int i=0; i<size; ++i) {

            map.put(area.get(i), map.getOrDefault(area.get(i), 0)+1);

            if(prices.containsKey(area.get(i))) {
                prices.get(area.get(i)).add(price.get(i));
                prices.get(area.get(i)).set(0, prices.get(area.get(i)).get(0) + price.get(i));
            }
            else {
                List<Long> list = new ArrayList<>();
                list.add(price.get(i));
                list.add(price.get(i));

                prices.put(area.get(i), list);
            }
        }```
orchid orioleBOT
crisp pike
#

Here, i am updating frequency or count of an area in "map"

#

and prices also, if it contains, add the new price, also put the current sum of whole on index 0

#

if not contains, make a new list with price of that on index 1, and on index 0, sum of all the prices in that area

#

Now, lets take an example!
area -> price
12 -> 100
13 -> 200
12 -> 300
12 -> 400
14 -> 500

#

Map -> [12, 3], [13, 1], [14, 1]

#

Prices -> [12, [800, 100, 300, 400]], [13, [200, 200]], [14, [500, 500]]

#

did u understand how, these maps filled? @obtuse oasis

#

lets say, for area 12, we need to find mean, then

#

mean = map.get(12) == 1 ? 0 : (prices.get(12).get(0) - (price[12])) / (map.get(12)-1);

#

it means, if the map only has one 12, then it means there are no more 12 other than 12, so out compList is empty and mean is 0

#

and if there is, then,
mean = sumOfAllOther12 / cntOfAllOther12

#

where

#
cntOfAllOther12 = cntOfAll12 - cntOfThis12(1) = 3 - 1 = 2
Hence, mean[0] = 700 / 2 = 350
mean[2] = (800-300)/(3-1) = 250```
orchid orioleBOT
obtuse oasis
#

1 min, I'm still trying to understand

crisp pike
#

alr, take ur time

#

see example, u will get it, how i calc mean

obtuse oasis
#

oki

obtuse oasis
crisp pike
#

Using this, I am updating the sum of all prices for that area at index 0

obtuse oasis
#

oh okayyy, and the first element is the sum

crisp pike
#

yup!

obtuse oasis
#

okay understood till here

crisp pike
#

So, now we can find mean in O(1) for every element of the array right?

obtuse oasis
#

yep

#

and the standard deviation

crisp pike
#
        
        // if there is only one element, then mean is 0, so this can't be an outlier so, return false, no need to check standard deviation
        if(map.get(currentArea) == 1)
            return false;
        
      // intializing sd and trev ( a variable which checks, if i traversed currentPrice or not, because we won't be adding it)
        double sd = 0;
        boolean trev = false;

        // traversing in map for currentArea (starting with index 1, cause index 0 is sum, not a individual price)
        for(int i = 1; i < prices.get(currentArea).size(); ++i) {
          
            // if i found a match, then put trev to true and we won't be adding it to sd
            if(prices.get(currentArea).get(i) == currentPrice && !trev) {
                trev = true;
                continue;
            }

            // standard deviation = summationOf(x - mean)^2 / n
            sd += Math.pow((prices.get(currentArea).get(i) - mean[index]), 2);
        }
        
        // Later dividing with numberOfN, which is stored in map ( but we are not taking the one with currentPrice, so map value -1 )
        sd /= (map.get(currentArea)-1);
        
        // Now we can check condition for outlier, where mean[index] is already calculated
        return (Math.abs(currentPrice-mean[index]) > (3*Math.sqrt(sd)));
    }```

```summationOf(x - mean)^2/n = ((x1 - mean)^2 + (x2 - mean)^2 + ........ (xn - mean)^2) / n```
orchid orioleBOT
crisp pike
#

i change some things too, like u were looping around for calc max and min also second max, which i concluded in above loop

obtuse oasis
#

Oki understood !!

#

took some timešŸ˜…

#

so much for just checking if its an outlier or not

crisp pike