Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.List;
  6.  
  7. public class CourseTest {
  8.  
  9.     private static class Course {
  10.         public int start;
  11.         public int end;
  12.         public int size;
  13.  
  14.         public int getStart() {
  15.             return start;
  16.         }
  17.  
  18.         public int getEnd() {
  19.             return end;
  20.         }
  21.  
  22.         public int getSize() {
  23.             return size;
  24.         }
  25.  
  26.         public Course(int start, int end) {
  27.             this.start = start;
  28.             this.end = end;
  29.             this.size = end - start;
  30.         }
  31.  
  32.         @Override
  33.         public String toString() {
  34.             return "Course{" +
  35.                     "start=" + start +
  36.                     ", end=" + end +
  37.                     '}';
  38.         }
  39.     }
  40.  
  41.  
  42.     private List<Course> getMaxCourse(List<Course> courses) {
  43.         courses.sort(Comparator.comparing(Course::getStart));
  44.         List<List<Course>> coursesVariants = new ArrayList<>();
  45.  
  46.         for(int i =0; i < courses.size(); i++){
  47.             coursesVariants.add(getVariantStartFrom(i, courses));
  48.         }
  49.         coursesVariants.sort(Comparator.comparing(List::size));
  50.         return coursesVariants.get(coursesVariants.size() - 1);
  51.     }
  52.  
  53.     private List<Course> getVariantStartFrom(int i, List<Course> sortedCourses) {
  54.         List<Course> variant = new ArrayList<>();
  55.         Course minimumSizeCourseWithThisStartDate = sortedCourses.get(i);
  56.         for(; i < sortedCourses.size(); i++) {
  57.             if(sortedCourses.size() - 1  == i){
  58.                 if(minimumSizeCourseWithThisStartDate.start < sortedCourses.get(i).start) {
  59.                     variant.add(minimumSizeCourseWithThisStartDate);
  60.                     minimumSizeCourseWithThisStartDate = sortedCourses.get(i);
  61.                     variant.add(sortedCourses.get(i));
  62.                 }else {
  63.                     if(minimumSizeCourseWithThisStartDate.size >  sortedCourses.get(i).size) {
  64.                         variant.add(sortedCourses.get(i));
  65.                     }
  66.                 }
  67.  
  68.                 continue;
  69.             }
  70.  
  71.  
  72.             if(minimumSizeCourseWithThisStartDate.size >  sortedCourses.get(i).size) {
  73.                 minimumSizeCourseWithThisStartDate = sortedCourses.get(i);
  74.             }
  75.  
  76.             if(minimumSizeCourseWithThisStartDate.start < sortedCourses.get(i).start) {
  77.                 variant.add(minimumSizeCourseWithThisStartDate);
  78.                 minimumSizeCourseWithThisStartDate = sortedCourses.get(i);
  79.                 continue;
  80.             }
  81.  
  82.  
  83. //            while(sortedCourses.get(i).start < ){
  84. //            }
  85.         }
  86.         return variant;
  87.     }
  88.  
  89.  
  90.     @Test
  91.     public void test() {
  92.         List<Course> course = new ArrayList<>(List.of(new Course(1,2),
  93.                 new Course(1,4),
  94.                 new Course(2,3),
  95.                 new Course(3,4),
  96.                 new Course(1,3)));
  97.  
  98.         List<Course> maxVariant = getMaxCourse(course);
  99.         System.out.println(maxVariant.toString());
  100.     }
  101.  
  102.     @Test
  103.     public void test2() {
  104.         List<Course> course = new ArrayList<>(List.of(
  105.                 new Course(1,4),
  106.                 new Course(2,3),
  107.                 new Course(3,4)));
  108.  
  109.         List<Course> maxVariant = getMaxCourse(course);
  110.         System.out.println(maxVariant.toString());
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement