Home > Enterprise >  get max time duration for max work done for single entity
get max time duration for max work done for single entity

Time:01-27

here is my data, trying to solve below puzzle

#ID      Date            GroupId      Code        Days        Lift     wt
#100     03-01-2020      LM100        500          30           5      kg
#100     03-15-2020      LM100        500          30           3      kg
#100     04-10-2020      LM100        500          20           20     kg
#100     05-15-2020      LM100        500          30           5      kg

want to calculate the longest duration with highest lift for every ID in data.

Lets say in first record, it says, ID 100 started on 1st march to lift 5kg everyday for next 30 days. But on 15th march ID100 started to lift 3kg too for next 30 days. So from 15th march to 30th march ID 100 is lifting 8 kg every day. SO that's how data is connected.

I'm not able to think of any logic to write for this.

Can anyone suggest any idea or logic for this ?

Update:

More definition on Data

Person started lifting 5kg/day from 1st march for next 30days (meaning until 30th march). But he started one more routine to lift 3kg/day too from 15th march for next 30 days (meaning until 14th April). So 1st and 2nd routine are being overlapped from 15th march to 30th March, that's why he's lifting 5 3 = 8 kg / day. That's how the data is for a person

CodePudding user response:

As this appears to be a schoolwork assignment I’ll be a little vague to let you work out the details.

Define record class to represent your inputs. Add an extra member field beyond what you mentioned in Question: the possible end date. Calculate this by making a LocalDate objectof the start date, then call plusDays passing your days count to get the end date.

record LiftSpan ( int participantId, LocalDate start, int days, LocalDate end, int groupId, int code, int lift, String unitOfWeight ) {}

Determine the first and last date of the overall date range. You neglected to define precisely the last date in your Question, so I’ll have to leave that to you to determine.

Start with the first date. Loop for each day in the overall range. For each day in that range, search the list of LiftSpan objects where the range contains that nth date. If there is only one object covering that date, continue looping. When you find more than one match, make a record from that first date to this date. Calculate elapsed days with ChronoUnit.DAYS. Copy over the appropriate fields for weight and code. Then replace your tracking of that first date with this date. Continue onwards, day by day, accumulating more LiftSpan objects. Stop looping when moving past the overall date range.

Optimizations are possible. If you sort the original set of objects, then you could compare their dates without going day by day. This alternative approach also eliminates needing to search the entire list repeatedly. But for a beginner programmer, the approach described first above would be more appropriate.

CodePudding user response:

You could start in the following way.

Mainly you need to compute and store aggregate result somewhere, here used a key-value data structure. After aggregation, grab the result based on your criteria.

In this case, it is just a simple scenarios based on one person which do some lift schemas (2 schemas for some days). The result should be the day in which the lift is maximum. Latter just adjust based on your requirements.(with max. days for each persons, etc)

public class TestLift
{
    static List<Record> list = new ArrayList<>();
    //default sort by data
    static Map<Date, Double> map = new TreeMap<>();
    public static void main(String args[])
    {
        run();
    }
    public static void run()
    {
        list.add(new Record(new GregorianCalendar(2022,0,10).getTime(),3,10));
        list.add(new Record(new GregorianCalendar(2022,0,12).getTime(),3,20));
        for(int i=0;i<list.size();i  )
        {
            //till no. of days 
            for(int j=0;j<list.get(i).days;j  )
            {
                Date d=list.get(i).date;
                Calendar c = new GregorianCalendar();
                c.setTime(d);
                //add extra days
                c.add(Calendar.DATE, j);
                d=c.getTime();
                //System.out.println(i ":" j "=" d);
                if(map.get(d)==null)
                {
                    map.put(d, list.get(i).lift);
                }
                else
                {
                    //add extra
                    map.put(d,map.get(d) list.get(i).lift);
                }
            }
            //look over aggregate map and find max lift
            for(Date d:map.keySet())
            {
                System.out.println(map.get(d) " kg at :" d);
            }
            System.out.println("###after add new record###");
            
        }
        
        Date result = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();
        System.out.println("\nresult=" map.get(result)  " kg at :"  result );
    }

    static class Record
    {
        public Date date;
        public int days;
        public double lift;
        public Record(Date date, int days, double lift)
        {
            this.date=date;
            this.days = days;
            this.lift = lift;
        }
    }   
}

Output

//computing aggregate step by step
10.0 kg at :Mon Jan 10 00:00:00 EET 2022
10.0 kg at :Tue Jan 11 00:00:00 EET 2022
10.0 kg at :Wed Jan 12 00:00:00 EET 2022
###after add new record###
10.0 kg at :Mon Jan 10 00:00:00 EET 2022
10.0 kg at :Tue Jan 11 00:00:00 EET 2022
30.0 kg at :Wed Jan 12 00:00:00 EET 2022
20.0 kg at :Thu Jan 13 00:00:00 EET 2022
20.0 kg at :Fri Jan 14 00:00:00 EET 2022
###after add new record###

result=30.0 kg at :Wed Jan 12 00:00:00 EET 2022
  •  Tags:  
  • Related