{"id":6761,"date":"2020-05-16T21:11:55","date_gmt":"2020-05-17T04:11:55","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6761"},"modified":"2020-05-16T21:12:10","modified_gmt":"2020-05-17T04:12:10","slug":"leetcode-1450-number-of-students-doing-homework-at-a-given-time","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1450-number-of-students-doing-homework-at-a-given-time\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1450. Number of Students Doing Homework at a Given Time"},"content":{"rendered":"\n<p>Given two integer arrays&nbsp;<code>startTime<\/code>&nbsp;and&nbsp;<code>endTime<\/code>&nbsp;and given an integer&nbsp;<code>queryTime<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<code>ith<\/code>&nbsp;student started doing their homework at the time&nbsp;<code>startTime[i]<\/code>&nbsp;and finished it at time&nbsp;<code>endTime[i]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the number of students<\/em>&nbsp;doing their homework at time&nbsp;<code>queryTime<\/code>. More formally, return the number of students where&nbsp;<code>queryTime<\/code>&nbsp;lays in the interval&nbsp;<code>[startTime[i], endTime[i]]<\/code>&nbsp;inclusive.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [1,2,3], endTime = [3,2,7], queryTime = 4\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> We have 3 students where:\nThe first student started doing homework at time 1 and finished at time 3 and wasn't doing anything at time 4.\nThe second student started doing homework at time 2 and finished at time 2 and also wasn't doing anything at time 4.\nThe third student started doing homework at time 3 and finished at time 7 and was the only student doing homework at time 4.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [4], endTime = [4], queryTime = 4\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> The only student was doing their homework at the queryTime.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [4], endTime = [4], queryTime = 5\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [1,1,1,1], endTime = [1,3,2,4], queryTime = 7\n<strong>Output:<\/strong> 0\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> startTime = [9,8,7,6,5,4,3,2,1], endTime = [10,10,10,10,10,10,10,10,10], queryTime = 5\n<strong>Output:<\/strong> 5\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>startTime.length == endTime.length<\/code><\/li><li><code>1 &lt;= startTime.length &lt;= 100<\/code><\/li><li><code>1 &lt;= startTime[i] &lt;= endTime[i] &lt;= 1000<\/code><\/li><li><code>1 &lt;=&nbsp;queryTime &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int busyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime) {\n    int ans = 0;\n    for (int i = 0; i < startTime.size(); ++i)\n      if (startTime[i] <= queryTime &#038;&#038; queryTime <= endTime[i]) ++ans;\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:\n    return sum(s <= queryTime <= e for s, e in zip(startTime, endTime))\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given two integer arrays&nbsp;startTime&nbsp;and&nbsp;endTime&nbsp;and given an integer&nbsp;queryTime. The&nbsp;ith&nbsp;student started doing their homework at the time&nbsp;startTime[i]&nbsp;and finished it at time&nbsp;endTime[i]. Return&nbsp;the number of students&nbsp;doing their homework&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[106,222,128],"class_list":["post-6761","post","type-post","status-publish","format-standard","hentry","category-array","tag-brute-force","tag-easy","tag-interval","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6761","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6761"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6761\/revisions"}],"predecessor-version":[{"id":6763,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6761\/revisions\/6763"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}