diff --git a/matrix/count_paths.py b/matrix/count_paths.py index 4861ad5fd0aa..1cb21529678a 100644 --- a/matrix/count_paths.py +++ b/matrix/count_paths.py @@ -19,55 +19,17 @@ 0 1 * * 0 1 * * """ - -def depth_first_search(grid: list[list[int]], row: int, col: int, visit: set) -> int: - """ - Recursive Backtracking Depth First Search Algorithm - - Starting from top left of a matrix, count the number of - paths that can reach the bottom right of a matrix. - 1 represents a block (inaccessible) - 0 represents a valid space (accessible) - - 0 0 0 0 - 1 1 0 0 - 0 0 0 1 - 0 1 0 0 - >>> grid = [[0, 0, 0, 0], [1, 1, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0]] - >>> depth_first_search(grid, 0, 0, set()) - 2 - - 0 0 0 0 0 - 0 1 1 1 0 - 0 1 1 1 0 - 0 0 0 0 0 - >>> grid = [[0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 0, 0, 0]] - >>> depth_first_search(grid, 0, 0, set()) - 2 - """ - row_length, col_length = len(grid), len(grid[0]) - if ( - min(row, col) < 0 - or row == row_length - or col == col_length - or (row, col) in visit - or grid[row][col] == 1 - ): - return 0 - if row == row_length - 1 and col == col_length - 1: - return 1 - - visit.add((row, col)) - - count = 0 - count += depth_first_search(grid, row + 1, col, visit) - count += depth_first_search(grid, row - 1, col, visit) - count += depth_first_search(grid, row, col + 1, visit) - count += depth_first_search(grid, row, col - 1, visit) - - visit.remove((row, col)) - return count - +intervals = [(4, 5), (0, 2), (2, 7), (1, 3), (0, 4)] +intervals.sort(key=lambda x: x[1]) +count = 0 +end = 0 +answer = [] + +for interval in intervals: + if end <= interval[0]: + end = interval[1] + count += 1 + answer.append(interval) if __name__ == "__main__": import doctest diff --git a/scheduling/Iinterval_scheduling_algorithm.py b/scheduling/Iinterval_scheduling_algorithm.py new file mode 100644 index 000000000000..d831381e1f39 --- /dev/null +++ b/scheduling/Iinterval_scheduling_algorithm.py @@ -0,0 +1,36 @@ +""" +interval scheduling is a class of problems. The programs take a number of tasks into account. Every task is represented by an interval that indicates the amount of time it should take a machine to complete it. If there is no overlap between any two intervals on the system or resource, a subset of intervals is compatible. + +The goal of the interval scheduling maximization problem is to identify the largest compatible set or a collection of intervals with the least possible overlap. The idea is to optimize throughput by completing as many tasks as you can. + +""" + + +def interval_scheduling(stimes, ftimes): + index = list(range(len(stimes))) + # sort according to finish times + index.sort(key=lambda item: ftimes[item]) + + maximal_set = set() + prev_finish_time = 0 + for item in index: + if stimes[item] >= prev_finish_time: + maximal_set.add(item) + prev_finish_time = ftimes[item] + + return maximal_set + + +n = int(input("Enter number of activities: ")) +stimes = input("Enter the start time of the {} activities in order: .{n}").split() +stimes = [int(st) for st in stimes] +ftimes = input("Enter the finish times of the {} activities in order:.{n}").split() +ftimes = [int(ft) for ft in ftimes] + +ans = interval_scheduling(stimes, ftimes) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()