Showing posts with label leetcode. Show all posts
Showing posts with label leetcode. Show all posts

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;
     }
}

3. [Technical Interview] [Leetcode] - Maximal Square

Key Points
- This one is a bit tricky, but the basic idea is that at the current element i and j, if its top, left, and top-left neighbours are all 1s, then you know the current element i and j is part of a square and we increment the current count.

For example:
11
11

becomes
11
12

and 2 means the area of the square is 2x2 so 4.

Another example:
111
111
111

after dynamic programming becomes:
111
122
123

this one would be 3 as we can see, and so 3x3 = 9;

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
61
62
63
64
65
66
import java.util.*;

public class HelloWorld{

    int count = 0;
    int result = Integer.MIN_VALUE;
    
     public static void main(String []args){
        char[][] multi = new char[][]{
          { '1', '1'},
          { '1', '1'},
        };
         
        System.out.println(maximalSquare(multi));
     }
     
public static int maximalSquare(char[][] a) {
  if (a == null || a.length == 0 || a[0].length == 0)
    return 0;

  int max = 0, n = a.length, m = a[0].length;

  // dp(i, j) represents the length of the square 
  // whose lower-right corner is located at (i, j)
  // dp(i, j) = min{ dp(i-1, j-1), dp(i-1, j), dp(i, j-1) }
  int[][] dp = new int[n + 1][m + 1];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
        System.out.println("looking at i: " + (i - 1));
        System.out.println("looking at j: " + (j - 1));
      if (a[i - 1][j - 1] == '1') {
          System.out.println("dp[i - 1][j] is: " + dp[i - 1][j]);
          System.out.println("dp[i][j - 1] is: " + dp[i][j - 1]);
          int innerMin = Math.min(dp[i - 1][j], dp[i][j - 1]);
          
          System.out.println("the innerMin was: " + innerMin);
          
          System.out.println("dp[i - 1][j - 1] is: " + dp[i - 1][j - 1]);
          
          int outerMin = Math.min(dp[i - 1][j - 1], innerMin);
          
          System.out.println("the outerMin was: " + outerMin);
          int replacement = outerMin + 1;
          
          System.out.println("the replacement was: " + (outerMin + 1));
          
        dp[i][j] = replacement;
        
        System.out.println("the max was: " + max);
        
        int maxthing = Math.max(max, dp[i][j]);
        
        System.out.println("the maxthing was: " + maxthing);
        
        max = maxthing;
      }
    }
  }

// print the result of our dynamic programming 2d array with the result
System.out.println(Arrays.deepToString(dp));
  // return the area
  return max * max;
}
}

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...