Skip to main content
Tweeted twitter.com/#!/StackCodeReview/status/455574992307384320
deleted 257 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This is solution to the Assemblyassembly line problem.[Here][1] Here is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper linkthe hyperlink. 

Note,: I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so I request that you don’tnot consider that in your feedback. Looking

I'm looking for request code review, optimizations and best practices.

    final class Station {
        
        private final int stationTime;
        private final int transtionToTime; // time to transition`to` this station
        private final int transitionFromTime; // time to transitition `from` this station.
        
        public Station(int stationTime, int transitionTime, int transitionFromTime) {
            this.stationTime = stationTime;
            this.transtionToTime = transitionTime;
            this.transitionFromTime = transitionFromTime;
        }
        
        public int getStationTime() {
            return stationTime;
        }
        
        public int getTransitionToTime() {
            return transtionToTime;
        }
    
        public int getTransitionFromTime() {
            return transitionFromTime;
        }
    }
    
    public final class AssemblyLine {
        
        private AssemblyLine() {}
    
        /**
         * Given an assembly line, outputs the shortest time needed to go through it.
         * The input data structures are not expected to be changed by client.
         * 
         * @param line1     the assembly line 1
         * @param line2     the assumbly line 2
         * @return          the minumum time needed to process the item.
         */
   public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevLine1Min = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevLine2Min = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentLine1Min = line1.get(i).getStationTime() + Math.min(prevLine1Min, prevLine2Min + line1.get(i).getTransitionToTime());
            int currentLine2Min = line2.get(i).getStationTime() + Math.min(prevLine1Min + line2.get(i).getTransitionToTime(), prevLine2Min);

            prevLine1Min = currentLine1Min;
            prevLine2Min = currentLine2Min;
        }
        return Math.min(prevLine1Min + line1.get(line1.size() - 1).getTransitionFromTime(), prevLine2Min + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }
    
        public static void main(String[] args) { 
    
            List<Station> line1 = new ArrayList<Station>(); 
            line1.add(new Station(4, 10, 7));
            line1.add(new Station(5,  9, 4));
            line1.add(new Station(3,  2, 5));
            line1.add(new Station(2,  8, 18));
            
            List<Station> line2 = new ArrayList<Station>(); 
            line2.add(new Station( 2, 12, 9));
            line2.add(new Station(10,  7, 2));
            line2.add(new Station( 1,  4, 8));
            line2.add(new Station( 4,  5, 7)); 
    
            assertEquals(35, assemblyLineShortestTime(line1, line2));
        }
    }


  [1]: http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/

This is solution to the Assembly line problem.[Here][1] is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper link. Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so request you don’t consider that in your feedback. Looking for request code review, optimizations and best practices.

    final class Station {
        
        private final int stationTime;
        private final int transtionToTime; // time to transition`to` this station
        private final int transitionFromTime; // time to transitition `from` this station.
        
        public Station(int stationTime, int transitionTime, int transitionFromTime) {
            this.stationTime = stationTime;
            this.transtionToTime = transitionTime;
            this.transitionFromTime = transitionFromTime;
        }
        
        public int getStationTime() {
            return stationTime;
        }
        
        public int getTransitionToTime() {
            return transtionToTime;
        }
    
        public int getTransitionFromTime() {
            return transitionFromTime;
        }
    }
    
    public final class AssemblyLine {
        
        private AssemblyLine() {}
    
        /**
         * Given an assembly line, outputs the shortest time needed to go through it.
         * The input data structures are not expected to be changed by client.
         * 
         * @param line1     the assembly line 1
         * @param line2     the assumbly line 2
         * @return          the minumum time needed to process the item.
         */
   public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevLine1Min = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevLine2Min = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentLine1Min = line1.get(i).getStationTime() + Math.min(prevLine1Min, prevLine2Min + line1.get(i).getTransitionToTime());
            int currentLine2Min = line2.get(i).getStationTime() + Math.min(prevLine1Min + line2.get(i).getTransitionToTime(), prevLine2Min);

            prevLine1Min = currentLine1Min;
            prevLine2Min = currentLine2Min;
        }
        return Math.min(prevLine1Min + line1.get(line1.size() - 1).getTransitionFromTime(), prevLine2Min + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }
    
        public static void main(String[] args) { 
    
            List<Station> line1 = new ArrayList<Station>(); 
            line1.add(new Station(4, 10, 7));
            line1.add(new Station(5,  9, 4));
            line1.add(new Station(3,  2, 5));
            line1.add(new Station(2,  8, 18));
            
            List<Station> line2 = new ArrayList<Station>(); 
            line2.add(new Station( 2, 12, 9));
            line2.add(new Station(10,  7, 2));
            line2.add(new Station( 1,  4, 8));
            line2.add(new Station( 4,  5, 7)); 
    
            assertEquals(35, assemblyLineShortestTime(line1, line2));
        }
    }


  [1]: http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/

This is solution to the assembly line problem. Here is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of the hyperlink. 

Note: I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so I request that you not consider that in your feedback.

I'm looking for request code review, optimizations and best practices.

final class Station {
    
    private final int stationTime;
    private final int transtionToTime; // time to transition`to` this station
    private final int transitionFromTime; // time to transitition `from` this station.
    
    public Station(int stationTime, int transitionTime, int transitionFromTime) {
        this.stationTime = stationTime;
        this.transtionToTime = transitionTime;
        this.transitionFromTime = transitionFromTime;
    }
    
    public int getStationTime() {
        return stationTime;
    }
    
    public int getTransitionToTime() {
        return transtionToTime;
    }

    public int getTransitionFromTime() {
        return transitionFromTime;
    }
}

public final class AssemblyLine {
    
    private AssemblyLine() {}

    /**
     * Given an assembly line, outputs the shortest time needed to go through it.
     * The input data structures are not expected to be changed by client.
     * 
     * @param line1     the assembly line 1
     * @param line2     the assumbly line 2
     * @return          the minumum time needed to process the item.
     */
public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
    int prevLine1Min = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
    int prevLine2Min = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
    for (int i = 1; i < line1.size(); i++) {
        int currentLine1Min = line1.get(i).getStationTime() + Math.min(prevLine1Min, prevLine2Min + line1.get(i).getTransitionToTime());
        int currentLine2Min = line2.get(i).getStationTime() + Math.min(prevLine1Min + line2.get(i).getTransitionToTime(), prevLine2Min);

        prevLine1Min = currentLine1Min;
        prevLine2Min = currentLine2Min;
    }
    return Math.min(prevLine1Min + line1.get(line1.size() - 1).getTransitionFromTime(), prevLine2Min + line2.get(line2.size() - 1).getTransitionFromTime()); 
}

    public static void main(String[] args) { 

        List<Station> line1 = new ArrayList<Station>(); 
        line1.add(new Station(4, 10, 7));
        line1.add(new Station(5,  9, 4));
        line1.add(new Station(3,  2, 5));
        line1.add(new Station(2,  8, 18));
        
        List<Station> line2 = new ArrayList<Station>(); 
        line2.add(new Station( 2, 12, 9));
        line2.add(new Station(10,  7, 2));
        line2.add(new Station( 1,  4, 8));
        line2.add(new Station( 4,  5, 7)); 

        assertEquals(35, assemblyLineShortestTime(line1, line2));
    }
}
added 49 characters in body
Source Link
JavaDeveloper
  • 8.5k
  • 29
  • 93
  • 162

This is solution to the Assembly line problem.Here[Here][1] is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper link. Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so request you don’t consider that in your feedback. Looking for request code review, optimizations and best practices.

    final class Station {
        
        private final int stationTime;
        private final int transtionToTime; // time to transition`to` this station
        private final int transitionFromTime; // time to transitition `from` this station.
        
        public Station(int stationTime, int transitionTime, int transitionFromTime) {
            this.stationTime = stationTime;
            this.transtionToTime = transitionTime;
            this.transitionFromTime = transitionFromTime;
        }
        
        public int getStationTime() {
            return stationTime;
        }
        
        public int getTransitionToTime() {
            return transtionToTime;
        }
    
        public int getTransitionFromTime() {
            return transitionFromTime;
        }
    }
    
    public final class AssemblyLine {
        
        private AssemblyLine() {}
    
        /**
         * Given an assembly line, outputs the shortest time needed to go through it.
         * The input data structures are not expected to be changed by client.
         * 
         * @param line1     the assembly line 1
         * @param line2     the assumbly line 2
         * @return          the minumum time needed to process the item.
         */
    public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevAprevLine1Min = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevBprevLine2Min = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentAcurrentLine1Min = line1.get(i).getStationTime() + Math.min(prevAprevLine1Min, prevBprevLine2Min + line1.get(i).getTransitionToTime());
            int currentBcurrentLine2Min = line2.get(i).getStationTime() + Math.min(prevAprevLine1Min + line2.get(i).getTransitionToTime(), prevBprevLine2Min);

            prevAprevLine1Min = currentA;currentLine1Min;
            prevBprevLine2Min = currentB;currentLine2Min;
        }
        return Math.min(prevAprevLine1Min + line1.get(line1.size() - 1).getTransitionFromTime(), prevBprevLine2Min + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }
    
        public static void main(String[] args) { 
    
            List<Station> line1 = new ArrayList<Station>(); 
            line1.add(new Station(4, 10, 7));
            line1.add(new Station(5,  9, 4));
            line1.add(new Station(3,  2, 5));
            line1.add(new Station(2,  8, 18));
            
            List<Station> line2 = new ArrayList<Station>(); 
            line2.add(new Station( 2, 12, 9));
            line2.add(new Station(10,  7, 2));
            line2.add(new Station( 1,  4, 8));
            line2.add(new Station( 4,  5, 7)); 
    
            assertEquals(35, assemblyLineShortestTime(line1, line2));
        }
    }


  [1]: http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/

This is solution to the Assembly line problem.Here is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper link. Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so request you don’t consider that in your feedback. Looking for request code review, optimizations and best practices.

final class Station {
    
    private final int stationTime;
    private final int transtionToTime; // time to transition`to` this station
    private final int transitionFromTime; // time to transitition `from` this station.
    
    public Station(int stationTime, int transitionTime, int transitionFromTime) {
        this.stationTime = stationTime;
        this.transtionToTime = transitionTime;
        this.transitionFromTime = transitionFromTime;
    }
    
    public int getStationTime() {
        return stationTime;
    }
    
    public int getTransitionToTime() {
        return transtionToTime;
    }

    public int getTransitionFromTime() {
        return transitionFromTime;
    }
}

public final class AssemblyLine {
    
    private AssemblyLine() {}

    /**
     * Given an assembly line, outputs the shortest time needed to go through it.
     * The input data structures are not expected to be changed by client.
     * 
     * @param line1     the assembly line 1
     * @param line2     the assumbly line 2
     * @return          the minumum time needed to process the item.
     */
    public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevA = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevB = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentA = line1.get(i).getStationTime() + Math.min(prevA, prevB + line1.get(i).getTransitionToTime());
            int currentB = line2.get(i).getStationTime() + Math.min(prevA + line2.get(i).getTransitionToTime(), prevB);

            prevA = currentA;
            prevB = currentB;
        }
        return Math.min(prevA + line1.get(line1.size() - 1).getTransitionFromTime(), prevB + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }

    public static void main(String[] args) { 

        List<Station> line1 = new ArrayList<Station>(); 
        line1.add(new Station(4, 10, 7));
        line1.add(new Station(5,  9, 4));
        line1.add(new Station(3,  2, 5));
        line1.add(new Station(2,  8, 18));
        
        List<Station> line2 = new ArrayList<Station>(); 
        line2.add(new Station( 2, 12, 9));
        line2.add(new Station(10,  7, 2));
        line2.add(new Station( 1,  4, 8));
        line2.add(new Station( 4,  5, 7)); 

        assertEquals(35, assemblyLineShortestTime(line1, line2));
    }
}

This is solution to the Assembly line problem.[Here][1] is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper link. Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so request you don’t consider that in your feedback. Looking for request code review, optimizations and best practices.

    final class Station {
        
        private final int stationTime;
        private final int transtionToTime; // time to transition`to` this station
        private final int transitionFromTime; // time to transitition `from` this station.
        
        public Station(int stationTime, int transitionTime, int transitionFromTime) {
            this.stationTime = stationTime;
            this.transtionToTime = transitionTime;
            this.transitionFromTime = transitionFromTime;
        }
        
        public int getStationTime() {
            return stationTime;
        }
        
        public int getTransitionToTime() {
            return transtionToTime;
        }
    
        public int getTransitionFromTime() {
            return transitionFromTime;
        }
    }
    
    public final class AssemblyLine {
        
        private AssemblyLine() {}
    
        /**
         * Given an assembly line, outputs the shortest time needed to go through it.
         * The input data structures are not expected to be changed by client.
         * 
         * @param line1     the assembly line 1
         * @param line2     the assumbly line 2
         * @return          the minumum time needed to process the item.
         */
   public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevLine1Min = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevLine2Min = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentLine1Min = line1.get(i).getStationTime() + Math.min(prevLine1Min, prevLine2Min + line1.get(i).getTransitionToTime());
            int currentLine2Min = line2.get(i).getStationTime() + Math.min(prevLine1Min + line2.get(i).getTransitionToTime(), prevLine2Min);

            prevLine1Min = currentLine1Min;
            prevLine2Min = currentLine2Min;
        }
        return Math.min(prevLine1Min + line1.get(line1.size() - 1).getTransitionFromTime(), prevLine2Min + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }
    
        public static void main(String[] args) { 
    
            List<Station> line1 = new ArrayList<Station>(); 
            line1.add(new Station(4, 10, 7));
            line1.add(new Station(5,  9, 4));
            line1.add(new Station(3,  2, 5));
            line1.add(new Station(2,  8, 18));
            
            List<Station> line2 = new ArrayList<Station>(); 
            line2.add(new Station( 2, 12, 9));
            line2.add(new Station(10,  7, 2));
            line2.add(new Station( 1,  4, 8));
            line2.add(new Station( 4,  5, 7)); 
    
            assertEquals(35, assemblyLineShortestTime(line1, line2));
        }
    }


  [1]: http://www.geeksforgeeks.org/dynamic-programming-set-34-assembly-line-scheduling/
Source Link
JavaDeveloper
  • 8.5k
  • 29
  • 93
  • 162

Assembly line scheduling

This is solution to the Assembly line problem.Here is complete description, which is too big and verbose to re-describe. I apologize for the inconvenience of hyper link. Note, I do understand merits of unit testing in separate files. But deliberately added it to main method for personal convenience, so request you don’t consider that in your feedback. Looking for request code review, optimizations and best practices.

final class Station {
    
    private final int stationTime;
    private final int transtionToTime; // time to transition`to` this station
    private final int transitionFromTime; // time to transitition `from` this station.
    
    public Station(int stationTime, int transitionTime, int transitionFromTime) {
        this.stationTime = stationTime;
        this.transtionToTime = transitionTime;
        this.transitionFromTime = transitionFromTime;
    }
    
    public int getStationTime() {
        return stationTime;
    }
    
    public int getTransitionToTime() {
        return transtionToTime;
    }

    public int getTransitionFromTime() {
        return transitionFromTime;
    }
}

public final class AssemblyLine {
    
    private AssemblyLine() {}

    /**
     * Given an assembly line, outputs the shortest time needed to go through it.
     * The input data structures are not expected to be changed by client.
     * 
     * @param line1     the assembly line 1
     * @param line2     the assumbly line 2
     * @return          the minumum time needed to process the item.
     */
    public static int assemblyLineShortestTime(List<Station> line1, List<Station> line2) {
        int prevA = line1.get(0).getStationTime() + line1.get(0).getTransitionToTime(); 
        int prevB = line2.get(0).getStationTime() + line2.get(0).getTransitionToTime(); 
        for (int i = 1; i < line1.size(); i++) {
            int currentA = line1.get(i).getStationTime() + Math.min(prevA, prevB + line1.get(i).getTransitionToTime());
            int currentB = line2.get(i).getStationTime() + Math.min(prevA + line2.get(i).getTransitionToTime(), prevB);

            prevA = currentA;
            prevB = currentB;
        }
        return Math.min(prevA + line1.get(line1.size() - 1).getTransitionFromTime(), prevB + line2.get(line2.size() - 1).getTransitionFromTime()); 
    }

    public static void main(String[] args) { 

        List<Station> line1 = new ArrayList<Station>(); 
        line1.add(new Station(4, 10, 7));
        line1.add(new Station(5,  9, 4));
        line1.add(new Station(3,  2, 5));
        line1.add(new Station(2,  8, 18));
        
        List<Station> line2 = new ArrayList<Station>(); 
        line2.add(new Station( 2, 12, 9));
        line2.add(new Station(10,  7, 2));
        line2.add(new Station( 1,  4, 8));
        line2.add(new Station( 4,  5, 7)); 

        assertEquals(35, assemblyLineShortestTime(line1, line2));
    }
}