Sorting two times - view data in the different ways
Intuition\nTo maximize the selection of two non-overlapping events, we can sort the events in two different ways:\n- Sort by Increasing End Time: This allows us
# Intuition\nTo maximize the selection of two non-overlapping events, we can sort the events in two different ways:\n- Sort by Increasing End Time: This allows us to find the maximum value for a specific end time.\n- Sort by Decreasing Start Time: This helps us determine the maximum value for a specific start time.\nBy comparing the results from these two sorted lists, we can identify the maximum overall result.\n\n# Complexity\n\nTime Complexity: O(N log N) ?\nSpace Complexity: O(1) ?\n\n# Code\n```golang []\n\nfunc maxTwoEvents(events [][]int) int {\n\tsort.Slice(events, func(i, j int) bool {\n\t\treturn events[i][1] < events[j][1]\n\t})\n\n\tr1Max := [][]int{{events[0][1], events[0][2]}}\n\tfor i := 1; i < len(events); i++ {\n\t\tif events[i][1] == r1Max[len(r1Max)-1][0] {\n\t\t\tr1Max[len(r1Max)-1][1] = max(events[i][2], r1Max[len(r1Max)-1][1])\n\t\t} else if events[i][2] > r1Max[len(r1Max)-1][1] {\n\t\t\tr1Max = append(r1Max, []int{events[i][1], events[i][2]})\n\t\t}\n\t}\n\tsort.Slice(events, func(i, j int) bool {\n\t\treturn events[i][0] > events[j][0]\n\t})\n\tr2Min := [][]int{{events[0][0], events[0][2]}}\n\tfor i := 1; i < len(events); i++ {\n\t\tif events[i][0] == r2Min[len(r2Min)-1][0] {\n\t\t\tr2Min[len(r2Min)-1][1] = max(events[i][2], r2Min[len(r2Min)-1][1])\n\t\t} else if events[i][2] > r2Min[len(r2Min)-1][1] {\n\t\t\tr2Min = append(r2Min, []int{events[i][0], events[i][2]})\n\t\t}\n\t}\n\tmax := 0\n\tfor i := 0; i < len(r1Max); i++ {\n\t\tif max < r1Max[i][1] {\n\t\t\tmax = r1Max[i][1]\n\t\t}\n\t\tfor j := 0; j < len(r2Min); j++ {\n\t\t\tif r2Min[j][0] <= r1Max[i][0] {\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif r1Max[i][1]+r2Min[j][1] > max {\n\t\t\t\tmax = r1Max[i][1] + r2Min[j][1]\n\t\t\t}\n\t\t}\n\t}\n\treturn max\n}\n```
['Go']