This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MazeIIBFS{ | |
| public int shortestDistanceBFS(int[][] maze, int[] start, int[] dest) { | |
| int[][] distance = new int[maze.length][maze[0].length]; | |
| for (int[] row: distance) | |
| Arrays.fill(row, Integer.MAX_VALUE); | |
| distance[start[0]][start[1]] = 0; | |
| int[][] dirs={{0, 1} ,{0, -1}, {-1, 0}, {1, 0}}; | |
| Queue < int[] > queue = new LinkedList < > (); | |
| queue.add(start); | |
| while (!queue.isEmpty()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class MazeII{ | |
| public int shortestDistanceDFS(int[][] maze, int[] start, int[] dest) { | |
| int[][] distance = new int[maze.length][maze[0].length]; | |
| for (int[] row: distance) | |
| Arrays.fill(row, Integer.MAX_VALUE); | |
| distance[start[0]][start[1]] = 0; | |
| dfs(maze, start, distance); | |
| return distance[dest[0]][dest[1]] == Integer.MAX_VALUE ? -1 : distance[dest[0]][dest[1]]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| public class RearrangeStringKDistanceApart{ | |
| public static String rearrangeString(String str, int k) { | |
| if(k==0) | |
| return str; | |
| //initialize the counter for each character | |
| final HashMap<Character, Integer> map = new HashMap<Character, Integer>(); | |
| for(int i=0; i<str.length(); i++){ | |
| char c = str.charAt(i); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.*; | |
| class NumberOfDistinctIslands { | |
| public static int numDistinctIslands(int[][] grid) { | |
| if (grid == null || grid.length < 1 || grid[0].length < 1) return 0; | |
| int m = grid.length, n = grid[0].length; | |
| Set<String> res = new HashSet<>(); | |
| for (int i = 0; i < m; i++) { | |
| for (int j = 0; j < n; j++) { | |
| Set<String> set = new HashSet<>(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RangeSumQuery2DImmutable{ | |
| private int[][] dp; | |
| public NumMatrix(int[][] matrix) { | |
| if (matrix.length == 0 || matrix[0].length == 0) return; | |
| dp = new int[matrix.length + 1][matrix[0].length + 1]; | |
| for (int r = 0; r < matrix.length; r++) { | |
| for (int c = 0; c < matrix[0].length; c++) { | |
| dp[r + 1][c + 1] = dp[r + 1][c] + dp[r][c + 1] + matrix[r][c] - dp[r][c]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Your NumMatrix object will be instantiated and called as such: | |
| * NumMatrix obj = new NumMatrix(matrix); | |
| * obj.update(row,col,val); | |
| * int param_2 = obj.sumRegion(row1,col1,row2,col2); | |
| */ | |
| class NumMatrix{ | |
| public int[][] colSum; | |
| public int[][] matrix; | |
| public NumMatrix(int[][] matrix) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private int[] nums; | |
| public int sumRange(int i, int j) { | |
| int sum = 0; | |
| for (int l = i; l <= j; l++) { | |
| sum += data[l]; | |
| } | |
| return sum; | |
| } | |
| public int update(int i, int val) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RangeSumQueryMutable{ | |
| private int[] b; | |
| private int len; | |
| private int[] nums; | |
| public NumArray(int[] nums) { | |
| this.nums = nums; | |
| double l = Math.sqrt(nums.length); | |
| len = (int) Math.ceil(nums.length/l); | |
| b = new int [len]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CountofRangeSum{ | |
| public int countRangeSum(int[] nums, int lower, int upper) { | |
| int n = nums.length; | |
| long[] sums = new long[n + 1]; | |
| for (int i = 0; i < n; ++i) | |
| sums[i + 1] = sums[i] + nums[i]; | |
| return countWhileMergeSort(sums, 0, n + 1, lower, upper); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| aws ec2 import-image --description "Ubuntu OVA" --disk-containers file://containers.json |
NewerOlder