How to calculate the minimum number of person present during two times
Date : March 29 2020, 07:55 AM
I hope this helps you . Take the start and end times for person number i, and name them s_i and e_i. Put all of these in a list, times. Then do the following: # start_time contains the start time of the event
# end_time contains the start time of the event
sort(times) # When doing this sort, make sure that if a s_i
# is at the same time as a e_j, put the s_i first.
current_number = number of people who're there at start_time
remove all times that match start_time from times
minimum_number = current_number
for time in times:
if time is an s_i: current_number += 1
if time is an e_i:
current_number -= 1
if current_number < minimum_number and time < end_time:
minimum_number = current_number
times = [(s_Bob, 8), (e_Bob, 10), (s_Alice, 10), (e_Alice, 12)]
times = [(s_Bob, 8), (s_Alice, 10), (e_Bob, 10), (e_Alice, 12)]
|
complexity in finding the number of times each character is present in a string
Tag : python , By : UnKnownUser
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Yes, O(n) is the best you can do. It is necessary to visit each character to count them all. from collections import Counter
input_string = "foobaarfoooobaaaarfo"
counter = Counter(input_string)
print(counter)
|
Find the minimum of a sorted and shifted array with better than O(n) time complexity
Date : March 29 2020, 07:55 AM
will help you We have an assignment to search for the minimum element of a sorted array that is shifted to the right afterwards. For example: [1, 5, 6, 19, 56, 101] becomes [19, 56, 101, 1, 5, 6]. The method should be implemented using a divide and conquer algorithm and it should have a better asymptotic time complexity than O(n). EDIT: I forgot to add that the elements int the array are unique. , In Java you could use a List because than you can create a Sublist. private Integer findMinimum(List<Integer> list) {
if (list.size() < 2)
return list.get(0);
int mid = list.size() / 2;
// create left and right list
List<Integer> leftList = list.subList(0, mid);
List<Integer> rightList = list.subList(mid, list.size());
if (leftList.get(leftList.size() - 1) <= rightList.get(rightList.size() - 1))
return findMin(leftList);
else
return findMin(rightList);
}
|
How can I find a number which occurs an odd number of times in a SORTED array in O(n) time?
Date : March 29 2020, 07:55 AM
|
given an array of integers in random order you have to find the minimum number of swaps to convert it to cyclic sorted a
Date : March 29 2020, 07:55 AM
|