Showing posts with label big. Show all posts
Showing posts with label big. Show all posts

Saturday, February 18, 2017

6. [Technical Interview] [Leetcode] - Minimum Path Sum

Key Points
- Any numbers not at the top row or left column of a int 2d array can be computed by taking the min of its top of left neighbour using this algorithmn from using dynamic programming: https://leetcode.com/discuss/17111/my-java-solution-using-dp-and-no-extra-space

Code
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;

public class HelloWorld{

    public static void main(String []args){
        int[][] multi = new int[][]{
          { 10, 10, 10},
          { 2, 2, 2},
          { 2, 2, 2},
        };
        System.out.println(minPathSum(multi));
    }
     
    public static int minPathSum(int[][] grid) {
        int m = grid.length;
        int n = grid[0].length;
        
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
            {
                // this is our starting top left corner, no need to change number in place
                // set it back to what it was and we will never enter here again
                if (i == 0 && j == 0)
                {
                    grid[i][j] = grid[i][j];
                }
                // if we are looking at the top row of numbers, compute a new number
                // for the current grid[0][x] by adding the left neighbour grid[0][j - 1] with current
                // grid[0][x]
                else if (i == 0 && j != 0)
                {
                    grid[i][j] = grid[i][j] + grid[i][j - 1];
                }
                // if we are looking at the left column of numbers, compute a new number
                // for the current grid[x][0] by adding the top neighbour grid[i - 1][0] with current
                // grid[x][0]
                else if (i != 0 && j == 0)
                {
                    grid[i][j] = grid[i][j] + grid[i - 1][j];
                }
                // Else we are looking at the numbers not at the topmost row or leftmost column.
                // This means these numbers can be computer by adding the current grid[i][j] with the
                // minimum of its top grid[i][j - 1] or left grid[i - 1][j] neighbour.
                else
                {
                    grid[i][j] = grid[i][j] + Math.min(grid[i][j - 1], grid[i - 1][j]);
                }
            }
        }
        
        // finally after all the addition and modifying each number of the original grid
        // in place, the answer of the least sum for the path from top left corner to
        // bottom right corner will be at grid[m - 1][n - 1] and so return the sum as int
        return grid[m - 1][n - 1];
    }
}

Friday, February 17, 2017

4. [Technical Interview] [Leetcode] - Kth Smallest Element in a BST

Key Points
- Since the left node of the current node is always the smaller one, and the right node of the current node is always the bigger one, the algorithm is to keep going down the left while keeping count of how many time we go down the left.
- Every time we go to a left node, we check if count == k. If yes, we found our kth smallest element in the BST. If not, we keep going down the left nodes and if there is no more left nodes to go to, we then try the right nodes.
- Eventually our count will equal the k and that node's value is the answer (this is the iterative solution).

Code
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.*;

public class HelloWorld{

static class TreeNode {
    private TreeNode left;
    private TreeNode right;
    private TreeNode parent;
    int val;

    public TreeNode (TreeNode left, TreeNode right, int item) {
        this.left = left;
        this.right = right;
        this.val = item;
    }
}

    int count = 0;
    int result = Integer.MIN_VALUE;
    
     public static void main(String []args){
         
         TreeNode node3 = new TreeNode(null, null, 5);
         TreeNode node2 = new TreeNode(null, null, 19);
         TreeNode node1 = new TreeNode(node3, node2, 10);
         
        System.out.println(kthSmallest(node1, 1));
     }
     
     public static int kthSmallest(TreeNode root, int k)
     {
         Stack<TreeNode> aStack = new Stack<TreeNode>();
         TreeNode p = root;
         int count = 0;
         
         while(!aStack.isEmpty() || p != null)
         {
             if (p != null)
             {
                 aStack.push(p);
                 p = p.left;
             }
             else
             {
                 TreeNode node = aStack.pop();
                 count++;
                 if (count == k)
                 {
                     return node.val;
                 }
                 else
                 {
                     p = node.right;
                 }
             }
         }
         
         return Integer.MIN_VALUE;
     }
}

2. [Technical Interview] [Java] - Reverse String in Place

Key Points
- Only traverse the string halfway and that is all that is needed to reverse the string in place.
- Use StringBuilder to reverse the string in place, not the same as making another copy of string.

 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.lang.Math; // headers MUST be above the first class
import java.util.*;
import java.lang.*;
import java.io.*;

// one class needs to have a main() method
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println(isPalindrome("appa"));
    System.out.println(reverse("hellop"));
  }
  
  private static boolean isPalindrome(String s)
  {
    int n = s.length();
    
    for (int i = 0; i < (n/2); i++)
    {
      if (s.charAt(i) != s.charAt(n - i - 1))
      {
        return false;
      }
    }
    
    return true;
  }
  
  private static String reverse(final String input)
  {
    final StringBuilder builder = new StringBuilder(input);
    int length = builder.length();
    
    for (int i = 0; i < length/2; i++)
    {
      final char current = builder.charAt(i);
      final int otherEnd = length - i - 1;
      
      builder.setCharAt(i, builder.charAt(otherEnd));
      builder.setCharAt(otherEnd, current);
    }
    
    return builder.toString();
  }
}

1. [Technical Interview] [Java] - Find Palindrome

Key Points
- Key is to stop checking halfway through the string to be efficient.

 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
import java.lang.Math; // headers MUST be above the first class
import java.util.*;
import java.lang.*;
import java.io.*;

// one class needs to have a main() method
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.print(isPalindrome("appa"));
  }
  
  private static boolean isPalindrome(String s)
  {
    int n = s.length();
    
    for (int i = 0; i < (n/2); i++)
    {
      if (s.charAt(i) != s.charAt(n - i - 1))
      {
        return false;
      }
    }
    
    return true;
  }
}

11. [Tutorial] How to Implement Android App with Node.js Server and MongoDB

Information - I followed this tutorial:  https://www.learn2crack.com/2014/04/android-login-registration-nodejs-server.html - But that link...