Course Schedule 2

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. Return the ordering of courses you should take to finish all courses....

April 7, 2022

Sliding Window Max

You are given an array of integers nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position. Return the max sliding window. Examples Example 1: Input: nums = [1,3,-1,-3,5,3,6,7], k = 3 Output: [3,3,5,5,6,7] Explanation: Window position Max --------------- ----- [1 3 -1] -3 5 3 6 7 3 1 [3 -1 -3] 5 3 6 7 3 1 3 [-1 -3 5] 3 6 7 5 1 3 -1 [-3 5 3] 6 7 5 1 3 -1 -3 [5 3 6] 7 6 1 3 -1 -3 5 [3 6 7] 7 Example 2: Input: nums = [1], k = 1 Output: [1] Brute force solution Time complexity : O(Nk), where N is number of elements in the array....

April 7, 2022

Design a Deck of Cards

from abc import ABCMeta, abstractmethod from enum import Enum import sys class Suit(Enum): HEART = 0 DIAMOND = 1 CLUBS = 2 SPADE = 3 class Card(metaclass=ABCMeta): def __init__(self, value, suit): self.value = value self.suit = suit self.is_available = True @property @abstractmethod def value(self): pass @value.setter @abstractmethod def value(self, other): pass class Deck(object): def __init__(self, cards): self.cards = cards self.deal_index = 0 def remaining_cards(self): return len(self.cards) - self.deal_index def deal_card(self): try: card = self....

April 7, 2022

Reconstruct Itinerary

You are given a list of airline tickets where tickets[i] = [fromi, toi] represent the departure and the arrival airports of one flight. Reconstruct the itinerary in order and return it. All of the tickets belong to a man who departs from “JFK”, thus, the itinerary must begin with “JFK”. If there are multiple valid itineraries, you should return the itinerary that has the smallest lexical order when read as a single string....

April 7, 2022

Text Justification

Given an array of strings words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly maxWidth characters. Extra spaces between words should be distributed as evenly as possible....

April 7, 2022

Find Median From Data Stream

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value and the median is the mean of the two middle values. MedianFinder() initializes the MedianFinder object. void addNum(int num) adds the integer num from the data stream to the data structure. double findMedian() returns the median of all elements so far. Answers within 10-5 of the actual answer will be accepted....

April 6, 2022

Subarray Sums Divisible by K

Given an array of integers nums and an integer k, return the total number of continuous subarrays whose sum is divisible by k. Example Input: nums = [4,5,0,-2,-3,1], k = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by k = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] Intuitions Brute Force Approach A naive but simple solution is to use the brute force approach....

April 6, 2022

Knight Dialer

The knight in chess has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). Given a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell 1 2 3 4 5 6 7 9 0 * 0 # Given an integer n, return how many distinct phone numbers of length n we can dial....

April 5, 2022

Meeting Rooms 2

Given an array of meeting time intervals intervals where intervals[i] = [start, end] return the minimum number of meeting rooms required. Examples Example 1 Input: intervals = [[0,30],[5,10],[15,20]] Output: 2 Example 2 Input: intervals = [[7,10],[2,4]] Output: 1 Intuition The first thought that comes to mind is that we should sort the intervals by their starting times. This would allow us to implement a priority queue, add intervals as they begin pop them off as they end and the size of the queue will represent our required meeting rooms....

April 5, 2022

Implementing a LRU Cache

Implement an LRUCache class. Functions should include: LRUCache(int capacity) Initialize the LRU cache with positive size capacity. int get(int key) Return the value of the key if the key exists, otherwise return -1. void put(int key, int value) Update the value of the key if the key exists. Otherwise, add the key-value pair to the cache. If the number of keys exceeds the capacity from this operation, evict the least recently used key....

April 5, 2022

Counting Islands

A 2D binary grid represents a land map. The map consists of ‘1’s and ‘0’s where the 1’s are land and the 0’s are water. Given this map, count the number of islands Example Input: map = [ ["1","1","0","0","0"], ["1","1","0","0","0"], ["0","0","1","0","0"], ["0","0","0","1","1"] ] Output: 3 Intuition If we treat the map as an undirected graph then we can walk the associated tree structure and count islands as we go. Algorithm Walk the map, if a node’s value is ‘1’ then we initiate a depth first traversal....

April 2, 2022