Skip to content

Instantly share code, notes, and snippets.

@photizzo
Created May 27, 2021 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save photizzo/ebeca903d7dcd777f3968536ed59e60f to your computer and use it in GitHub Desktop.
Save photizzo/ebeca903d7dcd777f3968536ed59e60f to your computer and use it in GitHub Desktop.
package io.photizzo.station
fun optimalPoints(dataSet: Array<Pair<Int, Int>>): List<Int> {
// sort the pair according to the end points
var newDataSet = dataSet.sortedBy {
it.first < it.second
}
// create a list to store the common points in the segments
var points = mutableListOf<Int>()
var point =
newDataSet[0].second; // set the point to the first end point i-e shortest end point
points.add(point)
newDataSet.forEachIndexed { index, pair ->
if (point < newDataSet[index].first || point > newDataSet[index].second) { // if the point is not in the segment
point =
newDataSet[index].second // update the point to the end point of the current pair
points.add(point) // store it in the list
}
}
return points
}
fun minPoint(input1: Int, input2: Array<IntArray>): Int {
var sets = input2.map {
Pair(it[0], it[1])
}
var points = optimalPoints(sets.toTypedArray())
println("points: ${points.size}")
//return minimum point
return points.minOrNull() ?: 0
}
fun main() {
println(minPoint(1, arrayOf(intArrayOf(1,3), intArrayOf(2,5), intArrayOf(3,6))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment