This file contains 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
class Solution { | |
public boolean validTree(int n, int[][] edges) { | |
int[] roots = new int[n]; | |
int count = n; | |
for(int i=0; i<roots.length; i++) { | |
roots[i] = i; | |
} | |
for(int i=0; i<edges.length; i++) { | |
int rootX = findRoot(edges[i][0], roots); |
This file contains 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
class Solution { | |
public int longestConsecutive(int[] nums) { | |
if (nums.length == 0) return 0; | |
Set<Integer> set = new HashSet<>(); | |
for (int i = 0; i < nums.length; i++) { | |
set.add(nums[i]); | |
} | |
int res = 1; | |
for (int i = 0; i < nums.length; i++) { | |
if(set.contains(nums[i]-1)) continue; |
This file contains 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
class Solution { | |
int r, c; | |
int[][] heights; | |
public List<List<Integer>> pacificAtlantic(int[][] heights) { | |
r = heights.length; | |
c = heights[0].length; | |
this.heights = heights; | |
boolean[][] p = new boolean[r][c]; | |
boolean[][] a = new boolean[r][c]; | |
This file contains 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
class Solution { | |
public int numIslands(char[][] grid) { | |
if(grid.length == 0 || grid == null) | |
return 0; | |
System.out.println("hi"); | |
int row = grid.length; | |
int col = grid[0].length; | |
int count =0; |
This file contains 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
class Solution { | |
public boolean canFinish(int numCourses, int[][] prerequisites) { | |
ArrayList[] graph = new ArrayList[numCourses]; | |
boolean[] visited = new boolean[numCourses]; | |
boolean[] dp = new boolean[numCourses]; | |
for(int i=0;i<numCourses;i++) | |
graph[i] = new ArrayList(); | |
This file contains 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
/* | |
// Definition for a Node. | |
class Node { | |
public int val; | |
public List<Node> neighbors; | |
public Node() { | |
val = 0; | |
neighbors = new ArrayList<Node>(); | |
} | |
public Node(int _val) { |
This file contains 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
class Solution { | |
public boolean canJump(int[] nums) { | |
int index = nums.length - 1; | |
for (int i = nums.length - 1; i >= 0; --i) | |
if (i + nums[i] >= index) | |
index = i; | |
return index == 0; | |
} | |
} |
This file contains 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
class Solution { | |
public int uniquePaths(int r, int c) { | |
if(r == 1 || c == 1){ | |
return 1; | |
} | |
int[][] dp = new int[r][c]; | |
for (int i = 0; i < dp.length; i++) { | |
for (int j = 0; j < dp[0].length; j++) { | |
if(i == 0 || j == 0){ |
This file contains 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
class Solution { | |
private int numDecodings_helper(int i,String s,int[] dp){ | |
if(i==s.length()) | |
return 1; | |
if(s.charAt(i)=='0') | |
return 0; | |
if(dp[i]!=-1) | |
return dp[i]; | |
int temp=0,j=i,ans=0; |
This file contains 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
class Solution { | |
public int rob(int[] nums) { | |
int n = nums.length; | |
if(n==1) return nums[0]; | |
if(n==2) return Math.max(nums[0],nums[1]); | |
return Math.max(rob(nums,0,nums.length-2),rob(nums,1,nums.length-1)); | |
} | |
public int rob(int[] nums,int s,int e) { | |
if (nums.length == 1) return nums[0]; | |
int[] dp = new int[nums.length]; |
NewerOlder