LeetCode | Learn for Master - Part 7
  • Leetcode – paint house I solution (Java)

    Paint House I
    There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

    The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green,

    [Read More...]
  • Leetcode – Graph valid tree solution based on Union find set

    Graph Valid Tree
    Given n nodes labeled from 0 to n – 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.

    For example:

    Given n = 5 and edges = [[0, 1], [0, 2], [0, 3], [1, 4]], return true.

    Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [1, 3], [1, 4]], return false.


    Given n = 5 and edges = [[0, 1], [1, 2],

    [Read More...]
  • Leetcode – Meeting rooms solution in Java

    Meeting Rooms
    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],…] (si < ei), determine if a person could attend all meetings.

    For example, Given [[0, 30],[5, 10],[15, 20]], return false.


    We can sort the intervals using the start time. Then we check whether there is conflict.  For instance. for this two pairs,  [0, 30],[5, 10] As 5 is smaller than 30, the person cannot attend both. 

    When we do the check, we should record the latest end time before the current interval. For current interval,

    [Read More...]
  • Leetcode – Color Sort

    Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

    Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

    You are not suppose to use the library’s sort function for this problem.

    Follow up:
    A rather straight forward solution is a two-pass algorithm using counting sort.
    First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s,

    [Read More...]
  • LeetCode- Search in Rotated Sorted Array

    Leetcode- search in rotated sorted array

    Suppose a sorted array is rotated at some pivot unknown to you beforehand.

      (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

      You are given a target value to search. If found in the array return its index, otherwise return -1.

      You may assume no duplicate exists in the array.

    Leetcode- search in rotated sorted array

    Using the idea of binary search, we can reduce the search space based on which range the target value is located. 

    We first get the mid index of the array,

    [Read More...]
  • Leetcode problems classified by company


    1 Two Sum 23.0% Easy
    21 Merge Two Sorted Lists 35.4% Easy
    23 Merge k Sorted Lists 23.3% Hard
    33 Search in Rotated Sorted Array 30.2% Hard
    34 Search for a Range 29.1% Medium
    46 Permutations 35.7% Medium
    47 Permutations II 28.0% Medium
    50 Pow(x, n) 27.9% Medium
    53 Maximum Subarray 36.6% Medium
    56 Merge Intervals 25.3% Hard
    57 Insert Interval 23.8% Hard
    65 Valid Number 12.1% Hard
    68 Text Justification 16.1% Hard
    76 Minimum Window Substring 21.2% Hard
    101 Symmetric Tree 33.9% Easy
    102 Binary Tree Level Order Traversal 32.7% Easy
    103 Binary Tree Zigzag Level Order Traversal 28.6% Medium
    104 Maximum Depth of Binary Tree 47.8% Easy
    127 Word Ladder 19.6% Medium
    149 Max Points on a Line 14.2% Hard
    150 Evaluate Reverse Polish Notation 23.5% Medium
    152 Maximum Product Subarray 22.1% Medium
    156 Binary Tree Upside Down 38.3% Medium
    170 Two Sum III –

    [Read More...]
  • LeetCode – Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length.

    Do not allocate extra space for another array, you must do this in place with constant memory.

    The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

    Given input array nums = [3,2,2,3], val = 3

    Your function should return length = 2, with the first two elements of nums being 2.


    1. Try two pointers.
    [Read More...]
  • LeetCode – Count Numbers with Unique Digits (Python)

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.

    Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])


    1. A direct way is to use the backtracking approach.
    2. Backtracking should contains three states which are (the current number, number of steps to get that number and a bitmask which represent which number is marked as visited so far in the current number). Start with state (0,0,0) and count all valid number till we reach number of steps equals to 10n.
    [Read More...]
  • LeetCode – Water and Jug Problem

    leetcode Water and Jug Problem

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs.

    Operations allowed:

    • Fill any of the jugs completely.
    • Empty any of the jugs.
    • Pour water from one jug into another till the other jug is completely full or the first jug itself is empty.

    Example 1: 


    Input: x = 2,

    [Read More...]
  • LeetCode – Next Permutation (Python)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

    If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The replacement must be in-place, do not allocate extra memory.

    Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

    The solution is the Pandit’s algorithm

    1 Pandit’s algorithm
    The article in Wikipedia describes the algorithm invented by Narayana Pandit to changes the list in-place to generate the next permutation given a list or Array A.

    [Read More...]
Page 7 of 7« First...34567