Search This Blog

Tuesday, February 6, 2018

Permutations

Given a collection of distinct numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
Algorithm:
See Combinations. Simply without rule 2. But the IO settings is a little bit different than Combination, which give you n numbers from 1~n and k buckets. In this problem, it gives you a set of predefined numbers. So instead of start with the first number, you start with nums[0]. And you need an additional array to mark the numbers that already show up.
Solution:


class Solution {
  
    void DFS(int[] visited, int[] nums, List<Integer> ans, List<List<Integer>> res)
    {
      if( ans.size() == nums.length ){
        List<Integer> tmp = new ArrayList<Integer>(ans);
        res.add(tmp);
        return;
      }
      for(int i = 0; i < nums.length; i++){
        if( visited[i] == 0 ){
          visited[i] = 1;
          ans.add(nums[i]);
          DFS(visited, nums, ans, res);
          ans.remove(ans.size()-1);
          visited[i] = 0;
        }
      }
    }
    
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> result = new ArrayList<List<Integer>>();
        List<Integer> answer = new ArrayList<Integer>();
        int[] visited = new int[nums.length]; 
        
        DFS(visited, nums, answer, result);
      
        return result;
    }
}


Combinations

Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

Algorithm:

Imagine you have n cards on your hand and each card has a unique number between 1~n. There are k buckets in front of you. You need to drop your cards into the buckets to come up with a combination.

There's only one rule to follow:

Each time you step in front of the ith bucket, you have to increase the number to drop in from last time.

For example, you drop in 1 in the first bucket, next time you step in front of the first bucket, you have to drop in 2.

Say n = 3, k = 2
          
i = 1 : [ 1 ] [ ]      Drop one in the first bucket
i = 2 : [ 1 ] [ 2 ]   => one combination (1, 2)
i = 2 : [ 1 ] [ ]     When the bucket is full, retrieve the card back to your hand
i = 2 : [ 1 ] [ 3 ]   Because you drop in 2 last time, so you drop in 3  => 2nd combination (1, 3)
i = 2 : [ 1 ] [ ]
i = 1 : [ ] [ ]         You can't drop in 4, so step to the first bucket(rest count on i = 2),
                          and retrieve 1 back to your hand
i = 1 : [ 2 ] [ ]      Because you drop in 1 last time at i = 1, so you drop in 2
i = 2 : [ 2 ] [ 1 ]   Since the count were reset, drop in 1 => (2, 1)
i = 2 : [ 2 ] [ ]
i = 2 : [ 2 ] [ 3 ]   Since 2 is not available, drop in 3 => (2, 3)
i = 2 : [ 2 ] [ ]
i = 1 : [ ] [ ]         Reset count for i = 2
i = 1 : [ 3 ] [ 1 ]   => (3, 1)
i = 2 : [ 3 ] [ ]     
i = 2 : [ 3 ] [ 2 ]   => (3, 2)

We notice that the result is actually the permutation of 3 out of 2 elements. So the combination has to divide by k! which is 2 in this case. To achieve the requirement, we have to add one more rule to the algorithm: The number in the bucket has to be bigger than the one in the previous bucket.

Hence, (3, 1), (3, 2) and (2, 1) is not valid

The mathematical calculation for only one rule is Cn_k * k! = n! / (n-k)! = 3! / 1! = 6
The mathematical calculation for two rules is Cn_k = n! / ((n-k)! * k!) = 3! / (2! * 1!) = 3

Solution:

class Solution {
  
    void DFS(int n, int k, int step, List<Integer> ans, List<List<Integer>> res){
      if( ans.size() == k ){
        ArrayList<Integer> temp = new ArrayList<Integer>(ans);
        res.add(temp);
        return;
      }
  
      // Start with step, which implies the 2nd rule
      // i.e. You will get (1,2) but not (2,1)
      // If you want to get all the permutations, you start with i = 1
      for(int i=step; i <= n; i++){
          ans.add(i);
          DFS(n, k, i+1, ans, res);
          ans.remove(ans.size()-1);
      }
      
    }
  
    public List<List<Integer>> combine(int n, int k) {
      List<List<Integer>> result = new ArrayList<List<Integer>>();
      List<Integer> answer = new ArrayList<Integer>();
      
      DFS(n, k, 1, answer, result);
      
      return result;
        
    }
}

Performance:


Friday, February 2, 2018

Maximum movies watched

Alex and Tom share a common passion for movies, but unfortunately they do not really like each other. A movie festival is taking place in another town, and both of them want to participate in it. The problem is the neither of them is willing to meet the other during the event.

After some not very pleasant negotiations, they agree that the fairest way to fulfill both goals is to pick two intervals of time so as to maximize the total number of movies that they can both see on their own. Keeping in mind that Alex and Tom each want to spend several days in a row at the festival(as the town in which the event is taking place is not close by, so they do not want to travel there more than once), help them count the maximal number of movies that can be seen by both of them independently.

int getMaxMoviesWatched(int[] movies, int K, int L)

movies is an array of size N which specifies the number of movies on each day of the festival
K is the number of days for Alex
L is the number of days for Tom

Assume N is an integer within the range of [2,600]
K and L are integers within the range [1,N-1]
Each element of array movies is an integer within the range [1,500]

return the maximal number of movies that can be seen by both of them or -1 if a solution is not possible.

Examples:

movies = [6, 1, 4, 6, 3, 2, 7, 4], K = 3, L = 2

Alex can participate 4+6+3 = 13 movies
Tom                          7+4 = 11 movies

Return 13+11=24

movies = [10, 19, 15], K=2, L=2

Return -1

My Solution: (Might be wrong, please advice) 


  static int getMaxMoviesWatched(int[] movies, int K, int L){
    int length = movies.length;
    if ( length < K+L )
      return -1;

    int maxK = 0;
    int indK = 0;
    for(int i = 0; i <= length-K; i++){
      int value = 0;
      for(int j = i; j < K+i; j++)
        value += movies[j];

      if ( value > maxK ){
        maxK = value;
        indK = i;
      }
    }

    int maxL = 0;
    for(int i = 0; i <= length-L; i++){
      int value = 0;
      if( i == indK )
        i += K;

      for(int j = i; j < L+i; j++)
        value += movies[j];

      if ( value > maxL ){
        maxL = value;
      }
    }

    return maxK + maxL;

  }


Maximum squared distance

Four integers A, B, C, D are given. The integers can be used to describe two points on a plane by assigning their values to the coordinates of the points. Each integer has to be assigned to exactly one coordinate. Your task is to assign the integers A, B, C and D to the coordinates of two points in such a way as to maximize the squared distance between those points.

int getMaxDistanceSquare(int A, int B, int C, int D)

Assume that:

A, B, C and D are integers within the range [-5000, 5000]

For example, let's consider the following values:
A = 1
B = 1
C = 2
D = 3

We have:
(1, 1) <=> (2, 3)  or (3, 2) squared distance = 5 or 5      MAX = 5
(1, 2) <=> (1, 3)      (3, 1)                                1     5
(1, 3) <=> (1, 2)      (2, 1)                                1     5

For Example,

A = 2
B = 4
C = 2
D = 4

We have:
(2, 4) <=> (2, 4) or (4, 2) squared distance = 0 or 8      MAX = 8
(2, 2) <=> (4, 4)                                              8
(2, 4) <=> (4, 2) or (2, 4)                                8     0

For Example,
A = 1
B = 2
C = 3
D = 4

We have:
(1, 2) <=> (3, 4) or (4, 3) squared distance = 8 or 10    MAX = 10
(1, 3) <=> (2, 4) or (4, 2)                                1     10
(1, 4) <=> (2, 3) or (3, 2)                                1      8


My Solution: (Might be wrong, please advice) 


  static int getMaxDistanceSquare(int A, int B, int C, int D){
    int max = -1; 
    int[] dist = new int[3];
    dist[0] = (A-B)*(A-B) + (C-D)*(C-D); 
    dist[1] = (A-C)*(A-C) + (B-D)*(B-D);
    dist[2] = (A-D)*(A-D) + (B-C)*(B-C);

    for(int i = 0; i < 2; i++)
      max = dist[i] > dist[i+1] ? dist[i] : dist[i+1];

    return max;
  }

Sunday, January 28, 2018

Linked List Cycle

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

Algorithm:

The classic fast & slow pointer problem
Just like the clock has 3 hands with different speed.
If there's a cycle, they will meet eventually.

Solutions:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode slow = head, fast = head;
        while (fast != null && fast.next != null){
            slow = slow.next;
            fast = fast.next.next;
            
            if (slow == fast) return true;
        }
        
        return false;
        
    }
}

Performance:

More intuitively, is to save each node reference in a HashSet (Sorted unique elements hash table).
Return true if found a duplicate. But this method takes more time(Around 10 ms) and need extra space.

Solution 2:


public class Solution {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> mySet = new HashSet<ListNode>();
        while(head != null){
            if(mySet.contains(head)){
                return true;
            }
            else{
                mySet.add(head);
                head = head.next;
            }
        }
        return false;
    }
}

Sunday, January 21, 2018

Rotate Image

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOTallocate another 2D matrix and do the rotation.
Example 1:
Given input matrix = 
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
],

rotate the input matrix in-place such that it becomes:
[
  [7,4,1],
  [8,5,2],
  [9,6,3]
]
Example 2:
Given input matrix =
[
  [ 5, 1, 9,11],
  [ 2, 4, 8,10],
  [13, 3, 6, 7],
  [15,14,12,16]
], 

rotate the input matrix in-place such that it becomes:
[
  [15,13, 2, 5],
  [14, 3, 4, 1],
  [12, 6, 8, 9],
  [16, 7,10,11]
]

Algorithm:

It's simply moving numbers around, and you only need to find the rules.

for N = 3, you only need to move the outside edge twice :

1 2 3    a   7 2 1     b     7 4 1
4 5 6  ==> 4 5 6  ==>   8 5 2
7 8 9         9 8 3           9 6 3  

To achieve step a :                                                Note that if we want to rotate left:

tmp = (0, 0)          // Throw 1 in tmp                       tmp = (0, 0)
(0, 0) = (2, 0)       // Replace 1 with 7                     (0, 0) = (0, 2)
(2, 0) = (2, 2)       // Replace 7 with 9                     (0, 2) = (2, 2)
(2, 2) = (0, 2)       // Replace 9 with 3                     (2, 2) = (2, 0)
(0, 2) = tmp         // Replace 3 with 1                     (2, 0) = tmp

Same idea you can get step b :

With these coordinates mapping from step a & b, you can get something like this:
1
2
3
4
5
6
7
      for(int i=0; i < nn-1; i++) {
        int tmp = matrix[0][i];
        matrix[0][i] = matrix[nn-1-i][0];
        matrix[nn-1-i][0] = matrix[nn-1][nn-1-i];
        matrix[nn-1][nn-1-i] = matrix[i][nn-1];
        matrix[i][nn-1] = tmp;
      }   
But then you realize that this only rotate the outside rim.

For N=4 :

1    2    3   4                 13   9   5    1           c          13   9  5  1
5    6    7   8       =>     14   6   7    2         ===>     14  10  6  2
9   10  11 12                15  10  11  3                       15  11  7  3
13 14  15 16                16  12  8    4                       16  12  8  4

We need extra loop to move the highlighted area (Step c) :

tmp   =  (1, 1)
(1, 1) =  (2, 1)
(2, 1) =  (2, 2)
(2, 2) =  (1, 2)
(1, 2) =  tmp

It's not hard to figure out the final from here.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public void rotate(int[][] matrix) {
      int nn = matrix[0].length;
      for(int j=0; j < nn/2; j++ ) { 
        for(int i=j; i < nn-1-j; i++) {
          int tmp = matrix[j][i];
          matrix[j][i] = matrix[nn-1-i][j];
          matrix[nn-1-i][j] = matrix[nn-1-j][nn-1-i];
          matrix[nn-1-j][nn-1-i] = matrix[i][nn-1-j];
          matrix[i][nn-1-j] = tmp;
        }   
      }   
    }
}

For comparison, this is the solution for rotate left by 90 degree:


  static void rotateLeft2D(int[][] matrix)
  {
    int nn = matrix[0].length;
    for(int j=0; j < nn/2; j++ ) {
      for(int i=j; i < nn-1-j; i++) {
        int tmp = matrix[j][i];
        matrix[j][i] = matrix[i][nn-1-j];
        matrix[i][nn-1-j] = matrix[nn-1-j][nn-1-i];
        matrix[nn-1-j][nn-1-i] = matrix[nn-1-i][j];
        matrix[nn-1-i][j] = tmp;
      }
    }
  }

Performance:

Saturday, January 20, 2018

Rotate Array

Rotate an array of n elements to the right by k steps.

For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]

Algorithm

If you draw a diagram like this and check for the change of indexes, it is fairly simple:

0 1 2 3 4 5 6   -> index                
1 2 3 4 5 6 7   -> Original ( n = 7 )
^^^^^^
5 6 7 1 2 3 4   -> rotate right by 3 (k = 3)
         ^^^^^^

This operation is equivalent to three operations
1. Create a destination array with the same length
1. Copy from position '0' of original to destination '3' of length '4' // 0 -> position k of length n-k
2. Copy from position '4' of original to destination '0' of length '3' // n-k -> position 0 of length k

Solution (Rotate right)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public void rotate(int[] nums, int k) {
        if (k > nums.length)
            k = k % nums.length;

        int[] result = new int[nums.length];

        System.arraycopy(nums, nums.length - k, result, 0, k);
        System.arraycopy(nums, 0, result, k, nums.length - k);


        System.arraycopy(result, 0, nums, 0, nums.length);
    }
}

Performance



Same algorithm apply to rotation left
0 1 2 3 4 5 6   -> index
1 2 3 4 5 6 7   -> Original
^^^^
4 5 6 7 1 2 3   -> rotate left by 3
            ^^^^

Solution (Rotate left)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution {
    public void rotate(int[] nums, int k) {
        if (k > nums.length)
            k = k % nums.length;

        int[] result = new int[nums.length];

        System.arraycopy(nums, k, result, 0, nums.length - k);
        System.arraycopy(nums, 0, result, nums.length - k, k);


        System.arraycopy(result, 0, nums, 0, nums.length);
    }
}

Although this solution has space complexity O(n) because I make a copy of the original array.

Here's an solution of space O(1):

Algorithm:

We make use the following properties for rotation right

1 2 3 4 5 6 7
4 3 2 1 5 6 7      1. reverse the first n-k elements
4 3 2 1 7 6 5      2. reverse the last k elements
5 6 7 1 2 3 4      3. reverse the whole array

How about rotate left ?

1 2 3 4 5 6 7
3 2 1 4 5 6 7      1. reverse the first k elements
3 2 1 7 6 5 4      2. reverse the last n-k elements
4 5 6 7 1 2 3      3. reverse the whole array

Solution for rotate right:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution 
{
    public void rotate(int[] nums, int k) 
    {
        k %= nums.length;
        
        reverse(nums, 0, nums.length - 1);
        
        reverse(nums, 0, k - 1);
        
        reverse(nums, k, nums.length - 1);
        

    }
    
    private void reverse(int[] nums, int start, int end)
    {
        int temp;
        while(start < end)
        {
            temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start ++;
            end --;
        }
    }
}

The shortcoming is that this algorithm runs quite slow in Java