Find Islands

  • Difficulty
  • Java
Description:

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water
and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid
are all surrounded by water.

 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
    //Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water
    // and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid
    // are all surrounded by water.

    public static void main(String[] a) {
        char[][] grid = {
                {'1', '1', '1', '1', '0'},
                {'1', '1', '0', '1', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '0', '0', '0'}};

        System.out.println(numIslands(grid));
    }

    public static int numIslands(char[][] grid) {
        int islandCount = 0;
        for (int row = 0; row < grid.length; row++) {
            for (int col = 0; col < grid[0].length; col++) {

                if (!isVisited(grid[row][col]) && isIsland(grid[row][col])) {
                    islandCount++;
                    markIsland(grid, row, col);
                }
            }
        }
        return islandCount;
    }

    public static boolean isIsland(char l) {
        return l == '1';
    }

    public static boolean isVisited(char l) {
        return l == 'v';
    }

    public static void markIsland(char[][] grid, int row, int col) {
        if (row > -1 && row < grid.length &&
                col > -1 && col < grid.length) {

            if (grid[row][col] == '1') {
                grid[row][col] = 'v';
                markIsland(grid, row + 1, col);
                markIsland(grid, row - 1, col);
                markIsland(grid, row, col + 1);
                markIsland(grid, row, col - 1);
            }
        }
    }

}

Permutations

  • Difficulty
  • Java

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

 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
    //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]
    // ]


    public static void main(String[] args){

        int[] nums = {1, 2, 3};
        System.out.println(permute(nums));

    }

    public static List<List<Integer>> permute(int[] nums) {

        List<List<Integer>> list = new ArrayList<>();
        backtrack(list, new ArrayList<>(), nums);
        return list;
    }

    public static void backtrack(List<List<Integer>> list, List<Integer> tempList, int[] nums){

        // If the tempList is full then we can add it to the list because it's a complete list
        if(tempList.size() == list.size()){
            list.add(new ArrayList<>(tempList));
            return;
        }

        for(int i = 0; i < nums.length; i++){ // (i = 0, 1, 2 (i = 0, (i = 0, 1, 2)))

            if(tempList.contains(nums[i])) continue;
            tempList.add(nums[i]); // [ 3, 1]
            backtrack(list, tempList, nums); //
            tempList.remove(tempList.size() - 1); //
        }
    }

}

Longest Increasing Subsequence

  • Difficulty
  • Java
Description:

 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
public static void main(String[] argv){
        //int[] arr = {5, 2, 8, 6, 3, 6, 9, 7};
        int[] arr = {10,9,2,5,3,7,101,18};

        System.out.println(longestIncreaingSubsequence(arr));
    }

    public static int longestIncreaingSubsequence(int[] arr) {
        HashMap<Integer, Integer> m = new HashMap<>();

        int max = 0;
        for (int i = arr.length - 1; i >= 0; i--) {

            for (int j = i + 1; j < arr.length; j++) {

                if (arr[i] < arr[j]) {
                    int val = m.get(arr[j]);
                    max = Math.max(max, val);
                }
            }
            m.put(arr[i], max + 1);
        }

        return max + 1;
    }

Median of two sorted arrays

  • Difficulty
  • Java
Description:

 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
    public static void main(String[] a){

        int[] nums1 = {};
        int[] nums2 = {1, 2, 3, 4};

        System.out.println(findMedianSortedArrays(nums1, nums2));
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {

        if(nums2.length == 0) {
            int mid = (nums1.length - 1)/2;
            return findMiddle(nums1, nums1, 0, mid, mid + 1, nums1.length - 1);
        }
        if(nums1.length == 0) {
            int mid = (nums2.length - 1)/2;
            return findMiddle(nums2, nums2, 0, mid, mid + 1, nums2.length - 1);
        }

        return findMiddle(nums1, nums2, 0, nums1.length - 1, 0, nums2.length - 1);
    }

    public static double findMiddle(int[] nums1, int[] nums2, int s1, int e1, int s2, int e2) {

        int len1 = (e1 - s1);
        int len2 = (e2 - s2);

        if (len1 >= 0 && len2 < 0) return nums1[s1];
        if (len2 >= 0 && len1 < 0) return nums2[s2];

        if ((len1 + len2) % 2 == 0) {
            return (double) (nums1[s1 + len1] + nums2[s2]) / 2;
        }

        int mid1 = len1 / 2;
        int mid2 = len2 / 2;
        return (findMiddle(nums1, nums1, s1, mid1, mid1 + 1, e1) +
                findMiddle(nums2, nums2, s2, mid2, mid2 + 1, e2)) / 2;

    }