8
public Seg(int val, int end, int color, boolean isStart) {12
this.isStart = isStart;15
public String toString() {16
return "[" + val + " " + end + " " + color + " " + isStart + "]";20
public List<List<Long>> splitPainting(int[][] segments) {21
List<List<Long>> res = new ArrayList();23
List<Seg> list = new ArrayList();25
// making a list of segments26
for (int[] segment : segments) {27
list.add(new Seg(segment[0], segment[1], segment[2], true));28
list.add(new Seg(segment[1], segment[1], segment[2], false));31
// Sorting the segments38
// System.out.println(list);40
// Iterating over list to combine the elements41
for (Seg curr : list) {44
// if the segment is starting there could be three ways45
if (res.size() > 0 && res.get(len - 1).get(0) == curr.val) {46
// if starting point of two things is same47
List<Long> temp = res.get(len - 1);48
temp.set(1, Math.max(temp.get(1), curr.end));49
temp.set(2, (long) (temp.get(2) + curr.color));50
} else if (res.size() > 0 && res.get(len - 1).get(1) > curr.val) {51
// if there is a start in between create a new segment of different color52
List<Long> prev = res.get(len - 1);53
prev.set(1, (long) curr.val);54
List<Long> temp = new ArrayList();55
temp.add((long) curr.val);56
temp.add((long) curr.end);57
temp.add((long) (prev.get(2) + curr.color));60
// Add a new value if nothing is present in result61
List<Long> temp = new ArrayList();62
temp.add((long) curr.val);63
temp.add((long) curr.end);64
temp.add((long) curr.color);68
if (res.size() > 0 && res.get(len - 1).get(0) == curr.val) {69
// if ending point of 2 segments is same70
Long prevColor = res.get(len - 1).get(2);71
res.get(len - 1).set(2, (long) (prevColor - curr.color));72
} else if (res.size() > 0 && res.get(len - 1).get(1) > curr.val) {73
// if there is a ending in between create a new segment of different color74
Long prevColor = res.get(len - 1).get(2);75
Long prevEnd = res.get(len - 1).get(1);76
res.get(len - 1).set(1, (long) curr.val);78
List<Long> temp = new ArrayList();79
temp.add((long) curr.val);80
temp.add((long) prevEnd);81
temp.add((long) (prevColor - curr.color));85
// System.out.println(res+" "+curr);88
// System.out.println(res);