└── leetcode ├── Array Partition └── solution.java ├── Binary Search └── solution.java ├── Coin Change └── solution.java ├── Counting Bits └── solution.java ├── Course Schedule └── solution.java ├── Design Add and Search Words Data Structure └── solution.java ├── Fibonacci Number └── solution.java ├── Find Center of Star Graph └── solution.java ├── Find the Town Judge └── solution.java ├── Flood Fill └── solution.java ├── Generate Parentheses └── solution.java ├── House Robber II └── solution.java ├── Implement Trie (Prefix Tree) └── solution.java ├── Jump Game └── solution.java ├── Lexicographically Smallest Palindrome └── solution.java ├── Longest Common Subsequence └── solution.java ├── Longest Palindrome └── solution.java ├── Longest Palindromic Subsequence └── solution.java ├── Lowest common ancestor └── solution.java ├── Maximum Number of Coins You Can Get └── solution.java ├── Maximum Subarray └── solution.java ├── Minimum Path Sum └── solution.java ├── Move Zeroes └── solution.java ├── Number of 1 Bits └── solution.java ├── Number of Enclaves └── solution.java ├── Number of Islands └── solution.java ├── Number of Operations to Make Network Connected └── solution.java ├── Numbers With Same Consecutive Differences └── solution.java ├── Power of Two └── solution.java ├── Rotting Oranges └── solution.java ├── Same Tree └── solution.java ├── Search Insert Position └── solution.java ├── Search in Rotated Sorted Array └── solution.java ├── Single Number └── solution.java ├── Sqrt(x) └── solution.java ├── Subsets └── solution.java ├── Unique Paths II └── solution.java └── Validate Binary Search Tree └── solution.java /leetcode/Array Partition/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arrayPairSum(int[] nums) { 3 | Arrays.sort(nums); 4 | int n=0; 5 | for(int i=0;i= coins[n]){ 11 | pick = 1+ solve(coins,n,amount-coins[n],dp); 12 | } 13 | return dp[n][amount] = Math.min(pick,notpick); 14 | 15 | } 16 | public int coinChange(int[] coins, int amount) { 17 | int n= coins.length; 18 | int dp [][] = new int[n][amount+1]; 19 | for(int [] row : dp){ 20 | Arrays.fill(row,-1); 21 | } 22 | int ans = solve(coins,n-1,amount,dp); 23 | if(ans==Integer.MAX_VALUE-1) return -1; 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /leetcode/Counting Bits/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] countBits(int n) { 3 | int[] dp=new int[n+1]; 4 | int offset=1; 5 | for(int i=1;i<=n;i++){ 6 | if(offset*2==i){ 7 | offset=i; 8 | } 9 | dp[i]=1+dp[i-offset]; 10 | } 11 | return dp; 12 | } 13 | } -------------------------------------------------------------------------------- /leetcode/Course Schedule/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canFinish(int numCourses, int[][] prerequisites) { 3 | ArrayList> adj=new ArrayList<>(); 4 | for(int i=0;i()); 7 | } 8 | for(int i=0;i q=new LinkedList<>(); 20 | for(int i=0;in || row<0 || col>m || col<0 || image[row][col]!=startcolor) return; 14 | image[row][col]=color; 15 | recursiveColor(image,row+1,col,color,startcolor,n,m); 16 | recursiveColor(image,row-1,col,color,startcolor,n,m); 17 | recursiveColor(image,row,col+1,color,startcolor,n,m); 18 | recursiveColor(image,row,col-1,color,startcolor,n,m); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /leetcode/Generate Parentheses/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private List ans = new ArrayList<>(); 3 | public void generate(int open , int close , StringBuilder s){ 4 | if(open ==0 && close == 0){ 5 | ans.add(s.toString()); 6 | return ; 7 | } 8 | if(open > 0 ){ 9 | s.append('('); 10 | generate(open -1 ,close , s); 11 | s.deleteCharAt(s.length() -1); //backtracking 12 | } 13 | if(close >0 && close> open){ 14 | s.append(')'); 15 | generate(open , close -1 , s); 16 | s.deleteCharAt(s.length() - 1); //backtracking 17 | } 18 | } 19 | public List generateParenthesis(int n) { 20 | StringBuilder s = new StringBuilder(); 21 | generate(n,n,s); 22 | return ans; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /leetcode/House Robber II/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | if (nums.length < 2) 4 | return nums[0]; 5 | 6 | 7 | int[] skipLastHouse = new int[nums.length - 1]; 8 | int[] skipFirstHouse = new int[nums.length - 1]; 9 | 10 | for (int i = 0; i < nums.length - 1; i++) { 11 | skipLastHouse[i] = nums[i]; 12 | skipFirstHouse[i] = nums[i + 1]; 13 | } 14 | 15 | 16 | int lootSkippingLast = robHelper(skipLastHouse); 17 | int lootSkippingFirst = robHelper(skipFirstHouse); 18 | 19 | 20 | return Math.max(lootSkippingLast, lootSkippingFirst); 21 | } 22 | private int robHelper(int[] nums) { 23 | if (nums.length < 2) 24 | return nums[0]; 25 | 26 | int[] dp = new int[nums.length]; 27 | 28 | dp[0] = nums[0]; 29 | dp[1] = Math.max(nums[0], nums[1]); 30 | 31 | for (int i = 2; i < nums.length; i++) { 32 | dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]); 33 | } 34 | 35 | return dp[nums.length - 1]; 36 | } 37 | } -------------------------------------------------------------------------------- /leetcode/Implement Trie (Prefix Tree)/solution.java: -------------------------------------------------------------------------------- 1 | class TrieNode{ 2 | TrieNode[] child=new TrieNode[26]; 3 | boolean end; 4 | public TrieNode(){ 5 | end=false; 6 | for(int i=0;i<26;i++){ 7 | child[i]=null; 8 | } 9 | } 10 | } 11 | 12 | class Trie { 13 | static TrieNode root; 14 | public Trie(){ 15 | root=new TrieNode(); 16 | } 17 | public void insert(String word){ 18 | TrieNode current=root; 19 | for (int i = 0; i < word.length(); i++) { 20 | int index=word.charAt(i)-'a'; 21 | if(current.child[index]==null){ 22 | current.child[index]=new TrieNode(); 23 | } 24 | current=current.child[index]; 25 | } 26 | current.end=true; 27 | } 28 | 29 | public boolean search(String word){ 30 | TrieNode current=root; 31 | for (int i = 0; i < word.length(); i++) { 32 | int index=word.charAt(i)-'a'; 33 | if(current.child[index]==null){ 34 | return false; 35 | } 36 | current=current.child[index]; 37 | } 38 | return current.end; 39 | } 40 | public boolean startsWith(String prefix) { 41 | TrieNode current=root; 42 | for (int i = 0; i < prefix.length(); i++) { 43 | int index=prefix.charAt(i)-'a'; 44 | if(current.child[index]==null){ 45 | return false; 46 | } 47 | current=current.child[index]; 48 | } 49 | return true; 50 | } 51 | } -------------------------------------------------------------------------------- /leetcode/Jump Game/solution.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Solution { 3 | public boolean canJump(int[] nums) { 4 | int gas=0; 5 | for(int i=0;i ch[i]) { 9 | ch[j] = ch[i]; 10 | } else { 11 | ch[i] = ch[j]; 12 | } 13 | } 14 | i++; 15 | j--; 16 | } 17 | return String.valueOf(ch); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /leetcode/Longest Common Subsequence/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public static int solve(String text1 , String text2 , int i , int j,int [][] dp){ 3 | if(i<0 || j<0) return 0; 4 | if(dp[i][j]!=-1) return dp[i][j]; 5 | if(text1.charAt(i)==text2.charAt(j)){ 6 | return dp[i][j]= 1+solve(text1,text2,i-1,j-1,dp); 7 | } 8 | else{ 9 | return dp[i][j] = Math.max(solve(text1,text2,i-1,j,dp),solve(text1,text2,i,j-1,dp)); 10 | } 11 | } 12 | public int longestCommonSubsequence(String text1, String text2) { 13 | int n1 = text1.length(); 14 | int n2 = text2.length(); 15 | int dp [][] = new int[n1][n2]; 16 | for(int [] row : dp){ 17 | Arrays.fill(row,-1); 18 | } 19 | return solve(text1,text2,n1-1,n2-1,dp); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /leetcode/Longest Palindrome/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindrome(String s) { 3 | int[] freq = new int[58]; 4 | int len = 0; 5 | for(char ch: s.toCharArray()) freq[ch-'A']++; 6 | 7 | for(int num: freq) { 8 | if(num%2 == 0) { 9 | len+=num; 10 | } else { 11 | len += (num-1); 12 | } 13 | } 14 | 15 | return len==s.length()?len:len+1; 16 | } 17 | } -------------------------------------------------------------------------------- /leetcode/Longest Palindromic Subsequence/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindromeSubseq(String s) { 3 | String p = new StringBuilder(s).reverse().toString(); 4 | int m = s.length(); 5 | int dp[] = new int[m+1]; 6 | int de[] = new int[m+1]; 7 | for(int i=1;i<=m;i++) 8 | { 9 | for(int j=1;j<=m;j++) 10 | { 11 | if(s.charAt(i-1) == p.charAt(j-1)) 12 | { 13 | dp[j] = 1 + de[j-1]; 14 | } 15 | else 16 | { 17 | dp[j] = Math.max(dp[j-1] , de[j]); 18 | } 19 | } 20 | for(int j=1;j<=m;j++) 21 | { 22 | de[j] = dp[j]; 23 | } 24 | } 25 | return de[m]; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /leetcode/Lowest common ancestor/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { 3 | while(root !=null) 4 | if(p.val < root.val && q.val root.val && q.val > root.val) 8 | { 9 | root = root.right; 10 | } 11 | else 12 | { 13 | return root; 14 | } 15 | return null;} 16 | } -------------------------------------------------------------------------------- /leetcode/Maximum Number of Coins You Can Get/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxCoins(int[] piles) { 3 | Arrays.sort(piles); 4 | int a=piles.length/3; 5 | int b=0; 6 | for(int i=piles.length-2;i>=a;i-=2) 7 | { 8 | b+=piles[i]; 9 | } 10 | return b; 11 | } 12 | } -------------------------------------------------------------------------------- /leetcode/Maximum Subarray/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int sum=0; 4 | int ans =Integer.MIN_VALUE; 5 | for(int i=0;ians) ans=sum; 9 | if(sum<0) sum=0; 10 | } 11 | return ans; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /leetcode/Minimum Path Sum/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPathSum(int[][] grid) { 3 | int m=grid.length; 4 | int n=grid[0].length; 5 | int[][] a=new int[m][n]; 6 | for(int[] row: a) 7 | Arrays.fill(row,-1); 8 | a[0][0]=grid[0][0]; 9 | for(int i=1;i0){ 5 | if((n&1)==1) 6 | c++; 7 | n=n>>1; 8 | } 9 | return c; 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /leetcode/Number of Enclaves/solution.java: -------------------------------------------------------------------------------- 1 | class Pair{ 2 | int row; 3 | int col; 4 | Pair(int _row , int _col){ 5 | this.row=_row; 6 | this.col=_col; 7 | } 8 | } 9 | class Solution { 10 | public int numEnclaves(int[][] grid) { 11 | int m = grid.length; 12 | int n = grid[0].length; 13 | int [][] vis = new int[m][n]; 14 | Queue q = new LinkedList<>(); 15 | for(int i=0; i=0 && nrow=0 && ncol -1; i--){ 22 | if(grid[row][i] == '1' && !flag[row][i]){ 23 | flag[row][i] = true; 24 | incolumnchecker(grid,row,i); 25 | } 26 | else break; 27 | } 28 | 29 | for(int i=cols+1; i-1; i--){ 41 | if(grid[i][cols] == '1' && !flag[i][cols]){ 42 | flag[i][cols] = true; 43 | inrowchecker(grid,i,cols); 44 | } 45 | else break; 46 | } 47 | 48 | for(int i=row+1; i q = new LinkedList<>(); 4 | for (int i=1;i<=9;i++) 5 | { 6 | q.add(i); 7 | } 8 | for(int j=1;j= 0 && k != 0) 21 | { 22 | Integer g=(x*10)+(f-k); 23 | q.add(g); 24 | } 25 | } 26 | } 27 | int[] arr=new int[q.size()]; 28 | int index=0; 29 | while(!q.isEmpty()) 30 | { 31 | arr[index++]=q.poll(); 32 | } 33 | return arr; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /leetcode/Power of Two/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | if(n<=0) 4 | { 5 | return false; 6 | } 7 | return((n)&(n-1))==0?true:false; 8 | } 9 | } -------------------------------------------------------------------------------- /leetcode/Rotting Oranges/solution.java: -------------------------------------------------------------------------------- 1 | class Pair{ 2 | int row; 3 | int col; 4 | int time; 5 | Pair(int _row, int _col , int _time){ 6 | this.row=_row; 7 | this.col=_col; 8 | this.time=_time; 9 | } 10 | } 11 | class Solution { 12 | public int orangesRotting(int[][] grid) { 13 | int m = grid.length; 14 | int n = grid[0].length; 15 | int [][] vis = new int[m][n]; 16 | Queue q = new LinkedList<>(); 17 | int fresh=0; 18 | for(int i=0;i=0 && nrow= 0 && ncol = nums[left]) { 12 | if (nums[left] <= target && target <= nums[mid]) { 13 | right = mid - 1; 14 | } else { 15 | left = mid + 1; 16 | } 17 | } else { 18 | if (nums[mid] <= target && target <= nums[right]) { 19 | left = mid + 1; 20 | } else { 21 | right = mid - 1; 22 | } 23 | } 24 | } 25 | 26 | return -1; 27 | } 28 | } -------------------------------------------------------------------------------- /leetcode/Single Number/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int r=0; 4 | for(int num:nums) 5 | { 6 | r^=num; 7 | } 8 | return r; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /leetcode/Sqrt(x)/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int mySqrt(int x) { 3 | if(x==0){ 4 | return 0; 5 | } 6 | int low=1;int high=x;int result=0; 7 | while(low<=high){ 8 | int mid=low+(high-low)/2; 9 | if(mid<=x/mid){ 10 | result=mid; 11 | low=mid+1; 12 | }else{ 13 | high=mid-1; 14 | } 15 | } 16 | return result; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /leetcode/Subsets/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private List> ans = new ArrayList<>(); 3 | public void generate( int i , int [] nums , List a){ 4 | if(i==nums.length){ 5 | ans.add(new ArrayList<>(a)); 6 | return ; 7 | } 8 | generate(i+1,nums,a); //notpick 9 | a.add(nums[i]); //pick 10 | generate(i+1,nums,a); 11 | a.remove(a.size()-1); //backtracking 12 | } 13 | public List> subsets(int[] nums) { 14 | List a = new ArrayList<>(); 15 | generate(0,nums,a); 16 | return ans; 17 | } 18 | } -------------------------------------------------------------------------------- /leetcode/Unique Paths II/solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePathsWithObstacles(int[][] obstacleGrid) { 3 | if(obstacleGrid[0][0]==1) return 0; 4 | int m=obstacleGrid.length; 5 | int n=obstacleGrid[0].length; 6 | int [][] dp = new int[m][n]; 7 | dp[0][0]=1; 8 | for(int i=1;i= maxValue) { 11 | return false; 12 | } 13 | return isValidBST(root.left, minValue, root.val) && isValidBST(root.right, root.val, maxValue); 14 | } 15 | } --------------------------------------------------------------------------------