Sort by

recency

|

52 Discussions

|

  • + 0 comments

    My approach to solve in O(n) was to iterate through each i student's extra time and in another array, keep track of the range of where the teacher could start grading in which student i would have enough time to complete their drawing. Ranges should be able to overlap. The result gives us an array with overlapping ranges of starting points, and the value indicated in each index of the array should be equivalent to the number of drawings that would be finished if the teacher were to start there.

    To calculate the range: We know that if given the maximum amount of extra time, the student will finish (unless t[i] == t.length, because the last student graded will only have t.length - 1 time to finish), so the range starts at i + 1, because that's a starting point where the student i will be graded last. The range continues up until the starting point where student i is graded with just the exact amount of extra time they need. Subtracting t[i] from i gives you the last spot in the range that a starting point would work. Since the students are in a circle, mod it by t.length to account for looping.

    To keep track of the ranges, instead of incrementing every value in between the start and end of the range (this would be very time- consuming with very large numbers of students), you can also just increment the value at the start of the range and decrement the same value at the index after the end of the range. This way, when iterating through the range array, you will know at what point you've entered or left the range, and if you keep track of the largest sum, it is effectively going to be the maximum value of overlapping ranges.

    Here's my solution in typescript: *note: Typescript mod operator is not a standard mod operation so I actually wrote up a method to perform what one would normally expect from mod, but for brevity I wrote this code up pretending that % works as expected

    solve(t[ ]: number[ ]) {
    		let currentId = 0;
        let max = Number.MIN_SAFE_INTEGER;
    		
    		// create an array of size t and make each index start at 0
        let ranges = Array<number>(t.length).fill(0);
        
    		// iterate through students and mark ranges in which the teacher gives enough time for that student to finish
        for (let i = 0; i<t.length; i++) {
            let startRange = i+1;
            let endRange = ((i - t[i]) % t.length) + 1;
            ranges[startRange]++;
            ranges[endRange]--;
        }
    		
    		// find the first student with largest overlapping of ranges
        let sum = 0;
        for (let i = 0; i<ranges.length; i++) {
            sum += ranges[i];
            if (sum > max) {
                max = sum;
                currentId = i;
    		}
        }
        return currentId+1
    		};
    
  • + 0 comments

    Here's my solution in Python. My basic idea is that when a student goes from being first to be visited by the teacher to being last, they gain len(t)-1 minutes, while all other students lose a minute. Instead of re-building a dictionary in every iteration, I use the fact that the student going from first to last gains len(t) minutes relative to all other students. Unless the student required 0 minutes, going from first to last guarantees that they will be able to finish their drawing, incrementing the count by 1. But then the count is decreased by the number of students who previously had just enough time. `from collections import deque def solve(t): completed = 0

    # create dictionary indicating how much extra time/excess time each student has if the teacher starts with student 1 (index 0 in Python)
    d = {}
    for i in range(len(t)):
        if (t[i]-i) not in d.keys():
            d[t[i]-i] = deque()
        d[t[i]-i].append(i)
        if t[i]-i <= 0:
            completed += 1
    
    # if all students already completed their drawing, no solution can be more optimal
    if completed == len(t):
        return 1
    
    max_completed = completed
    start_with = 1
    for i in range(len(t)-1):
        d[t[i]-i].popleft()
        if (t[i]-i-len(t)) not in d.keys():
            d[t[i]-i-len(t)] = deque()
        d[t[i]-i-len(t)].append(i)
        if t[i] > 0:
            completed += 1
        if -i in d.keys():
            completed -= len(d[-i])
        if completed > max_completed:
            max_completed = completed
            start_with = i + 2
    
    return start_with`
    
  • + 0 comments

    this problem easily could be awarded with 100 points or even promoted as hard.

    there are many things to consider:

    • build ranges of availability for every preschooler
    • think about situation where ranges are overlapping in cycles
    • walk twice around a table to close all cyclest
    def solve(t):
        ranges, n = [], len(t)
        for i, e in enumerate(t):
            if e >= n or e == 0:
                # skip those who need to much time
                # and those who had done their drawing
                continue
            # add range from start (second value is 0) to end (is 1)
            ranges.extend([((i-n+1+n)%n+1, 0), ((i-e+n)%n+1, 1)]) 
        ranges.sort(key=lambda e: (e[0], e[1]))
        # max opened ranges, currently open ranges
        # and first index of max opened range		
        max_, current, first_index = 0, 0, 1
        for _ in range(2): # walk twice arround a table
            for i, v in ranges:
                if max_ == current:
                    first_index = min(i, first_index) # get lowest index for all maxes
                if v == 0: # beginning of range
                    current += 1
                    if max_ < current:
                        max_ = current
                        first_index = i # set new first index
                else:
                    current -= 1
        return first_index
    
  • + 0 comments

    Overall the advantage of kindergarten learning is the lifelong effects on a child's behavior. Kindergarten learning is not all about the foundation of school but it also marks a significance on your child's lifestyle and manners. So instead of merely relying on a local kindergarten school, you should explore Worksheets by No Fuss Tutors or other of its kind to land the best place for your child's learning.

  • + 2 comments

    My O(n) solution in C++. Here is my idea: 1. For each value of t[i], count all sastified starting points then store in an array. 2. Search for max element in array then return the index. To store and search effciently, you may try the problem Array Manipulation in Data structure - hard - array

    int solve(vector<int> t) {
        int n = t.size();
        /* n < 10^5
        Must move to heap memory
        */
        int arr[n+1] = {0}; 
        
        for (int i = 0; i < n; ++i) {
            if (t[i] - i > 0) {
                /* t[i] > i
                The start point is included in (i+1,n - (t[i]-i))
                Add 1 in the count array when go into the interval
                Remove 1 when go out the interval
                */
                arr[i+1] += 1;
                arr[n - (t[i]-i) + 1] -= 1;
            }
            else {
                /* t[i] <= i
                The start point is included in (0,i - t[i]) and (i+1,n-1)
                Add 1 in the count array when go into the interval
                Remove 1 when go out the interval
                */
                arr[0] += 1;
                arr[i - t[i] + 1] -= 1;
                arr[i+1] += 1;
                arr[n] -= 1;
            } 
        }
        
        int index = 0;
        int count = 0;
        int max = 0;
        
        for (int i = 0; i < n; ++i) {
            count += arr[i];
            if (count > max) {
                max = count;
                index = i + 1;
            } 
        }
        
        return index;
    }