Skip to content

Instantly share code, notes, and snippets.

@zooyf
Last active June 21, 2017 01:40
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zooyf/a0e6b9e301d37e4e60ea06b0f80e60be to your computer and use it in GitHub Desktop.
import Foundation
func numberOfTasksRuningWithStart(start:Array<Int>, end:Array<Int>, query:Array<Int>) -> Array<Int> {
// 1.fill empty array with zero
var numberOfTasksRuning = Array<Int>.init(repeating: 0, count: query.count)
// 2.search data
for i in 0..<query.count {
// query i times for each item in query[]
for j in 0..<start.count {
// find if each array in startToEndTimeArray[] contains the i'th query
let queryItem = query[i]
if queryItem >= start[j] && queryItem < end[j] {
numberOfTasksRuning[i] += 1
}
}
}
return numberOfTasksRuning
}
print(numberOfTasksRuningWithStart(start: [0,5,2],end: [4,7,8],query: [1,9,4,3]))
#include <stdio.h>
void running_tasks(int runningTasks[], int start[], int end[], int n, int query[], int m) {
int i;
for (i = 0; i < m; i++) {
int j;
for (j = 0; j < n; j++) {
int queryItem = query[i];
if (queryItem >= start[j] && queryItem < end[j]) {
runningTasks[i]++;
}
}
}
}
int main(int argc, char *argv[])
{
const int n = 3;
int start[] = {0,5,2};
int end[] = {4,7,8};
const int m = 4;
int query[] = {1,9,4,3};
int runningTasks[m] = {0};
running_tasks(runningTasks, start, end, n, query, m);
// 输出
int i;
for (i = 0; i < m; i++) {
printf("%d ", runningTasks[i]);
}
putchar('\n');
}
'''
algorithm of python3 version
'''
def task_running(start, end, query):
# 1.fill empty array with zero
runningTasks = [0] * len(query)
# 2.search data
for i in range(0, len(query)):
for j in range(0, len(start)):
queryItem = query[i]
if queryItem < end[j] and queryItem >= start[j]:
runningTasks[i] += 1
return runningTasks
start = [0,5,2]
end = [4,7,8]
query = [1,9,4,3]
print task_running(start, end, query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment