{
  "slug": "activity-selection",
  "name": "Activity Selection",
  "categories": [
    "others"
  ],
  "body": {},
  "implementations": {
    "python": {
      "dir": "other\\activity_selection.py",
      "url": "https://github.com/TheAlgorithms/python/tree/master/other\\activity_selection.py",
      "code": "<span class=\"hljs-comment\"># flake8: noqa</span>\r\n\r\n<span class=\"hljs-string\">&quot;&quot;&quot;The following implementation assumes that the activities\r\nare already sorted according to their finish time&quot;&quot;&quot;</span>\r\n\r\n<span class=\"hljs-string\">&quot;&quot;&quot;Prints a maximum set of activities that can be done by a\r\nsingle person, one at a time&quot;&quot;&quot;</span>\r\n<span class=\"hljs-comment\"># n --&gt; Total number of activities</span>\r\n<span class=\"hljs-comment\"># start[]--&gt; An array that contains start time of all activities</span>\r\n<span class=\"hljs-comment\"># finish[] --&gt; An array that contains finish time of all activities</span>\r\n\r\n\r\n<span class=\"hljs-keyword\">def</span> <span class=\"hljs-title function_\">printMaxActivities</span>(<span class=\"hljs-params\">start: <span class=\"hljs-built_in\">list</span>[<span class=\"hljs-built_in\">int</span>], finish: <span class=\"hljs-built_in\">list</span>[<span class=\"hljs-built_in\">int</span>]</span>) -&gt; <span class=\"hljs-literal\">None</span>:\r\n    <span class=\"hljs-string\">&quot;&quot;&quot;\r\n    &gt;&gt;&gt; start = [1, 3, 0, 5, 8, 5]\r\n    &gt;&gt;&gt; finish = [2, 4, 6, 7, 9, 9]\r\n    &gt;&gt;&gt; printMaxActivities(start, finish)\r\n    The following activities are selected:\r\n    0,1,3,4,\r\n    &quot;&quot;&quot;</span>\r\n    n = <span class=\"hljs-built_in\">len</span>(finish)\r\n    <span class=\"hljs-built_in\">print</span>(<span class=\"hljs-string\">&quot;The following activities are selected:&quot;</span>)\r\n\r\n    <span class=\"hljs-comment\"># The first activity is always selected</span>\r\n    i = <span class=\"hljs-number\">0</span>\r\n    <span class=\"hljs-built_in\">print</span>(i, end=<span class=\"hljs-string\">&quot;,&quot;</span>)\r\n\r\n    <span class=\"hljs-comment\"># Consider rest of the activities</span>\r\n    <span class=\"hljs-keyword\">for</span> j <span class=\"hljs-keyword\">in</span> <span class=\"hljs-built_in\">range</span>(n):\r\n\r\n        <span class=\"hljs-comment\"># If this activity has start time greater than</span>\r\n        <span class=\"hljs-comment\"># or equal to the finish time of previously</span>\r\n        <span class=\"hljs-comment\"># selected activity, then select it</span>\r\n        <span class=\"hljs-keyword\">if</span> start[j] &gt;= finish[i]:\r\n            <span class=\"hljs-built_in\">print</span>(j, end=<span class=\"hljs-string\">&quot;,&quot;</span>)\r\n            i = j\r\n\r\n\r\n<span class=\"hljs-keyword\">if</span> __name__ == <span class=\"hljs-string\">&quot;__main__&quot;</span>:\r\n    <span class=\"hljs-keyword\">import</span> doctest\r\n\r\n    doctest.testmod()\r\n\r\n    start = [<span class=\"hljs-number\">1</span>, <span class=\"hljs-number\">3</span>, <span class=\"hljs-number\">0</span>, <span class=\"hljs-number\">5</span>, <span class=\"hljs-number\">8</span>, <span class=\"hljs-number\">5</span>]\r\n    finish = [<span class=\"hljs-number\">2</span>, <span class=\"hljs-number\">4</span>, <span class=\"hljs-number\">6</span>, <span class=\"hljs-number\">7</span>, <span class=\"hljs-number\">9</span>, <span class=\"hljs-number\">9</span>]\r\n    printMaxActivities(start, finish)\r\n"
    }
  },
  "contributors": [
    {
      "name": "<NAME>",
      "email": "<EMAIL>",
      "commits": 1
    },
    {
      "name": "shellhub",
      "email": "<EMAIL>",
      "commits": 1
    },
    {
      "name": "<NAME>",
      "email": "<EMAIL>",
      "commits": 1
    },
    {
      "name": "<NAME>",
      "email": "<EMAIL>",
      "commits": 1
    },
    {
      "name": "<NAME>",
      "email": "<EMAIL>+<EMAIL>",
      "commits": 1
    }
  ],
  "explanationUrl": {}
}