classSolution: defbusyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int: returnsum(s <= queryTime <= e for s, e inzip(startTime, endTime))
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { public: intbusyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime){ int n = startTime.size(); int ans = 0; for (int i = 0; i < n; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { ans++; } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution { publicintbusyStudent(int[] startTime, int[] endTime, int queryTime) { intn= startTime.length; intans=0; for (inti=0; i < n; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { ans++; } } return ans; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12
publicclassSolution { publicintBusyStudent(int[] startTime, int[] endTime, int queryTime) { int n = startTime.Length; int ans = 0; for (int i = 0; i < n; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { ans++; } } return ans; } }
[sol1-C]
1 2 3 4 5 6 7 8 9
intbusyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime){ int ans = 0; for (int i = 0; i < startTimeSize; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { ans++; } } return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10
var busyStudent = function(startTime, endTime, queryTime) { const n = startTime.length; let ans = 0; for (let i = 0; i < n; i++) { if (startTime[i] <= queryTime && endTime[i] >= queryTime) { ans++; } } return ans; };
[sol1-Golang]
1 2 3 4 5 6 7 8
funcbusyStudent(startTime []int, endTime []int, queryTime int) (ans int) { for i, s := range startTime { if s <= queryTime && queryTime <= endTime[i] { ans++ } } return }
classSolution { public: intbusyStudent(vector<int>& startTime, vector<int>& endTime, int queryTime){ int n = startTime.size(); int maxEndTime = *max_element(endTime.begin(), endTime.end()); if (queryTime > maxEndTime) { return0; } vector<int> cnt(maxEndTime + 2); for (int i = 0; i < n; i++) { cnt[startTime[i]]++; cnt[endTime[i] + 1]--; } int ans = 0; for (int i = 0; i <= queryTime; i++) { ans += cnt[i]; } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintbusyStudent(int[] startTime, int[] endTime, int queryTime) { intn= startTime.length; intmaxEndTime= Arrays.stream(endTime).max().getAsInt(); if (queryTime > maxEndTime) { return0; } int[] cnt = newint[maxEndTime + 2]; for (inti=0; i < n; i++) { cnt[startTime[i]]++; cnt[endTime[i] + 1]--; } intans=0; for (inti=0; i <= queryTime; i++) { ans += cnt[i]; } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintBusyStudent(int[] startTime, int[] endTime, int queryTime) { int n = startTime.Length; int maxEndTime = endTime.Max(); if (queryTime > maxEndTime) { return0; } int[] cnt = newint[maxEndTime + 2]; for (int i = 0; i < n; i++) { cnt[startTime[i]]++; cnt[endTime[i] + 1]--; } int ans = 0; for (int i = 0; i <= queryTime; i++) { ans += cnt[i]; } return ans; } }
intbusyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime){ int maxEndTime = 0; for (int i = 0; i < endTimeSize; i++) { maxEndTime = MAX(maxEndTime, endTime[i]); } if (queryTime > maxEndTime) { return0; } int *cnt = (int *)malloc(sizeof(int) * (maxEndTime + 2)); memset(cnt, 0, sizeof(maxEndTime) * (maxEndTime + 2)); for (int i = 0; i < startTimeSize; i++) { cnt[startTime[i]]++; cnt[endTime[i] + 1]--; } int ans = 0; for (int i = 0; i <= queryTime; i++) { ans += cnt[i]; } free(cnt); return ans; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var busyStudent = function(startTime, endTime, queryTime) { const n = startTime.length; const maxEndTime = _.max(endTime); if (queryTime > maxEndTime) { return0; } const cnt = newArray(maxEndTime + 2).fill(0); for (let i = 0; i < n; i++) { cnt[startTime[i]]++; cnt[endTime[i] + 1]--; } let ans = 0; for (let i = 0; i <= queryTime; i++) { ans += cnt[i]; } return ans; };
funcbusyStudent(startTime []int, endTime []int, queryTime int) (ans int) { maxEndTime := 0 for _, e := range endTime { maxEndTime = max(maxEndTime, e) } if queryTime > maxEndTime { return } cnt := make([]int, maxEndTime+2) for i, s := range startTime { cnt[s]++ cnt[endTime[i]+1]-- } for _, c := range cnt[:queryTime+1] { ans += c } return }
funcmax(a, b int)int { if b > a { return b } return a }
publicstaticintUpperbound(int[] arr, int l, int r, int target) { int ans = r + 1; while (l <= r) { int mid = l + ((r - l) >> 1); if (arr[mid] > target) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; }
publicstaticintLowerbound(int[] arr, int l, int r, int target) { int ans = r + 1; while (l <= r) { int mid = l + ((r - l) >> 1); if (arr[mid] >= target) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; } }
staticintupperbound(constint *arr, int l, int r, int target) { int ans = r + 1; while (l <= r) { int mid = l + ((r - l) >> 1); if (arr[mid] > target) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; }
staticintlowerbound(constint *arr, int l, int r, int target) { int ans = r + 1; while (l <= r) { int mid = l + ((r - l) >> 1); if (arr[mid] >= target) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; }
intbusyStudent(int* startTime, int startTimeSize, int* endTime, int endTimeSize, int queryTime){ qsort(startTime, startTimeSize, sizeof(int), cmp); qsort(endTime, endTimeSize, sizeof(int), cmp); int lessStart = upperbound(startTime, 0, startTimeSize - 1, queryTime); int lessEnd = lowerbound(endTime, 0, endTimeSize - 1, queryTime); return lessStart - lessEnd; }