Tag: swe

  • Meeting Rooms II

    Given a list of meetings, that may or may not overlap, calculate the total number of rooms that are required to conduct all the meetings according to the schedule.

    Java

    class Solution {
        public int minMeetingRooms(int[][] intervals) {
            int[] start = new int[intervals.length];
            int[] end = new int[intervals.length];
            for (int i = 0; i < intervals.length; i++) {
                start[i] = intervals[i][0];
                end[i] = intervals[i][1];
            }
    
            Arrays.sort(start);
            Arrays.sort(end);
    
            int endPtr = 0, rooms = 0;
            for (int i = 0; i < intervals.length; i++) {
                if (start[i] < end[endPtr]) {
                    rooms++;
                } else {
                    endPtr++;
                }
            }
            return rooms;
        }
    }

    Runtime Complexity: O(nlog(n))

    Space Complexity: O(n)

    Javascript (ES6)

    /**
     * @param {number[][]} intervals
     * @return {number}
     */
    const minMeetingRooms = (intervals) => {
      let startList = [];
      let endList = [];
      let endPos = 0;
      let rooms = 0;
      for (const [start, end] of intervals) {
        startList.push(start);
        endList.push(end);
      }
      startList.sort((a, b) => a - b);
      endList.sort((a, b) => a - b);
      for (let i = 0; i < intervals.length; i++) {
        if (startList[i] < endList[endPos]) {
          rooms++;
        } else {
          endPos++;
        }
      }
      return rooms;
    };
    
    // console.log(minMeetingRooms([[7, 10],[2, 4]]));
    // 1

    Demo

  • Throttling function calls with a Queue in Javascript (ES6)

    The following ES6 Javascript code, helps to throttle function calls without discarding them. It uses a queue to keep track of all the function calls. We can use the reset method to reset the queue and the setTimeout() method.

    const throttle = (fn, delay) => {
      let timeout;
      let noDelay = true;
      let queue = [];
      const start = () => {
        if (queue.length) {
          const {context, args} = queue.shift();
          fn.apply(context, arguments);
          timeout = setTimeout(start, delay);
        } else {
          noDelay = true;
        }
      };
    
      const ret = (...args) => {
        queue.push({
          context: this,
          args,
        });
        if (noDelay) {
          noDelay = false;
          start();
        }
      };
    
      ret.reset = () => {
        clearTimeout(timeout);
        queue = [];
      };
      return ret;
    };
    
    /* Usage */
    const print = (number) => {
      console.log(`Hello World! ${number}`);
    }
    
    const test = throttle(print, 3000);
    
    test(1);
    test(2);
    test(3);
    
    // test.reset(); This will clear the queue and hence the above test case will only output the first line.
    // Output:
    // Hello World! 1
    // Hello World! 2
    // Hello World! 3
    

    Demo