├── .classpath ├── .gitattributes ├── .github └── workflows │ ├── codeql-analysis.yml │ └── greetings.yml ├── .gitignore ├── .project ├── .settings ├── org.eclipse.jdt.core.prefs └── org.eclipse.m2e.core.prefs ├── April Challenge └── com │ └── leetcode │ └── AprilChallenge │ ├── week3 │ ├── NumberOfIsland.java │ └── SearchInRotatedSortedArray.java │ └── week4 │ └── SubarraySumEqualToK.java ├── Assets ├── October Challenge │ ├── day15_189. Rotate Array.png │ └── day17_187. Repeated DNA Sequences.png └── September Challenge │ └── week2 │ └── Day13.JPG ├── August Challenge └── com │ └── leetcode │ └── AugustChallenge │ └── week1 │ ├── DetectCapital.java │ └── ValidPalindrome.java ├── Daily Coding Challenge └── FindAllDuplicatesInAnArray.java ├── July Challenge └── com │ └── leetcode │ └── JulyChallenge │ └── week1 │ ├── HammingDistance.java │ └── PlusOne.java ├── June Challenge └── com │ └── leetcode │ └── JuneChallenge │ ├── week1 │ ├── DeleteNodeInALinkedList.java │ ├── InvertBinaryTree.java │ ├── QueueReconstructionByHeight.java │ ├── ReverseString.java │ └── TwoCityScheduling.java │ └── week2 │ ├── InsertDeleteGetRandom.java │ ├── IsSubsequence.java │ ├── PowerOfTwo.java │ └── SearchInsertPosition.java ├── LICENSE ├── May Challenge └── com │ └── leetcode │ └── MayChallenge │ ├── week1 │ ├── CousinsInBinaryTree.java │ ├── FirstBadVersion.java │ ├── FirstUniqueCharacterInString.java │ ├── JewelsAndStones.java │ ├── MajorityElement.java │ ├── NumberComplement.java │ └── RansomNote.java │ ├── week2 │ ├── CheckIfAStraightLine.java │ ├── FindTownJudge.java │ ├── FloodFill.java │ ├── RemoveKDigits.java │ ├── SingleElementInSortedArray.java │ ├── Trie.java │ └── ValidPerfectSquare.java │ ├── week3 │ ├── CountSquareSubmatricesWithAllOnes.java │ ├── FindAllAnagramsInString.java │ ├── KthSmallestElementInBST.java │ ├── MaximumSumCircularSubarray.java │ ├── OddEvenLinkedList.java │ ├── PermutationInString.java │ └── StockSpanner.java │ ├── week4 │ ├── ConstructBSTFromPreorderTraversal.java │ ├── ContigiousArray.java │ ├── CountingBits.java │ ├── IntervalListIntersections.java │ ├── PossibleBipartition.java │ ├── SortCharactersByFrequency.java │ └── UncrossedLines.java │ └── week5 │ ├── CourseSchedule.java │ ├── EditDistance.java │ └── KClosestPointsToOrigin.java ├── November Challenge ├── FindSmallestDivisorGivenThreshold.java ├── FlippingAnImage.java └── PermutationTwo.java ├── October Challenge ├── week1 │ ├── day1 │ │ └── numberOfRecentCalls.cpp │ ├── day2 │ │ └── combinationSum.cpp │ ├── day3 │ │ └── KDiffPairsInArray.java │ ├── day4 │ │ └── removeCoveredIntervals.cpp │ ├── day5 │ │ └── complementOfBase10Integer.cpp │ └── day6 │ │ └── insertIntoBST.cpp └── week3 │ ├── day15 │ └── RotateArray.java │ └── day17 │ └── RepeatedDNASequences.java ├── README.md ├── September Challenge └── week2 │ └── Day13_InsertInterval.java ├── pom.xml └── target └── classes └── META-INF ├── MANIFEST.MF └── maven └── LeetCode-30-Day-Challenge └── LeetCode-30-Day-Challenge ├── pom.properties └── pom.xml /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | -------------------------------------------------------------------------------- /.github/workflows/codeql-analysis.yml: -------------------------------------------------------------------------------- 1 | # For most projects, this workflow file will not need changing; you simply need 2 | # to commit it to your repository. 3 | # 4 | # You may wish to alter this file to override the set of languages analyzed, 5 | # or to provide custom queries or build logic. 6 | # 7 | # ******** NOTE ******** 8 | # We have attempted to detect the languages in your repository. Please check 9 | # the `language` matrix defined below to confirm you have the correct set of 10 | # supported CodeQL languages. 11 | # ******** NOTE ******** 12 | 13 | name: "CodeQL" 14 | 15 | on: 16 | push: 17 | branches: [ master ] 18 | pull_request: 19 | # The branches below must be a subset of the branches above 20 | branches: [ master ] 21 | schedule: 22 | - cron: '38 18 * * 4' 23 | 24 | jobs: 25 | analyze: 26 | name: Analyze 27 | runs-on: ubuntu-latest 28 | 29 | strategy: 30 | fail-fast: false 31 | matrix: 32 | language: [ 'cpp', 'java' ] 33 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ] 34 | # Learn more... 35 | # https://docs.github.com/en/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#overriding-automatic-language-detection 36 | 37 | steps: 38 | - name: Checkout repository 39 | uses: actions/checkout@v2 40 | 41 | # Initializes the CodeQL tools for scanning. 42 | - name: Initialize CodeQL 43 | uses: github/codeql-action/init@v1 44 | with: 45 | languages: ${{ matrix.language }} 46 | # If you wish to specify custom queries, you can do so here or in a config file. 47 | # By default, queries listed here will override any specified in a config file. 48 | # Prefix the list here with "+" to use these queries and those in the config file. 49 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 50 | 51 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). 52 | # If this step fails, then you should remove it and run the build manually (see below) 53 | - name: Autobuild 54 | uses: github/codeql-action/autobuild@v1 55 | 56 | # ℹ️ Command-line programs to run using the OS shell. 57 | # 📚 https://git.io/JvXDl 58 | 59 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines 60 | # and modify them (or add more) to build your code if your project 61 | # uses a compiled language 62 | 63 | #- run: | 64 | # make bootstrap 65 | # make release 66 | 67 | - name: Perform CodeQL Analysis 68 | uses: github/codeql-action/analyze@v1 69 | -------------------------------------------------------------------------------- /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | name: Greetings 2 | 3 | on: [pull_request, issues] 4 | 5 | jobs: 6 | greeting: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/first-interaction@v1 10 | with: 11 | repo-token: ${{ secrets.GITHUB_TOKEN }} 12 | issue-message: 'Hey **#** , welcome to the repository, Thanks for Raising an Issue. Your contributions means a lot to us !!' 13 | pr-message: 'Hey **#** , welcome to the repository, Thanks for you active contributions !!' 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | /May Challenge/com/leetcode/test/ 3 | 4 | # Prerequisites 5 | *.d 6 | 7 | # Compiled Object files 8 | *.slo 9 | *.lo 10 | *.o 11 | *.obj 12 | 13 | # Precompiled Headers 14 | *.gch 15 | *.pch 16 | 17 | # Compiled Dynamic libraries 18 | *.so 19 | *.dylib 20 | *.dll 21 | 22 | # Fortran module files 23 | *.mod 24 | *.smod 25 | 26 | # Compiled Static libraries 27 | *.lai 28 | *.la 29 | *.a 30 | *.lib 31 | 32 | # Executables 33 | *.exe 34 | *.out 35 | *.app 36 | *.class 37 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Leetcode-challenge 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.m2e.core.maven2Builder 15 | 16 | 17 | 18 | 19 | 20 | org.eclipse.jdt.core.javanature 21 | org.eclipse.m2e.core.maven2Nature 22 | 23 | 24 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=13 3 | org.eclipse.jdt.core.compiler.compliance=13 4 | org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled 5 | org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning 6 | org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore 7 | org.eclipse.jdt.core.compiler.release=enabled 8 | org.eclipse.jdt.core.compiler.source=13 9 | -------------------------------------------------------------------------------- /.settings/org.eclipse.m2e.core.prefs: -------------------------------------------------------------------------------- 1 | activeProfiles= 2 | eclipse.preferences.version=1 3 | resolveWorkspaceProjects=true 4 | version=1 5 | -------------------------------------------------------------------------------- /April Challenge/com/leetcode/AprilChallenge/week3/NumberOfIsland.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.AprilChallenge.week3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.LinkedList; 5 | import java.util.List; 6 | import java.util.Queue; 7 | 8 | public class NumberOfIsland { 9 | 10 | 11 | class Node{ 12 | public int u; 13 | public int v; 14 | 15 | public Node(int u, int v) { 16 | this.u=u; 17 | this.v=v; 18 | } 19 | 20 | } 21 | 22 | public int numIslands(char[][] grid) { 23 | 24 | int row = grid.length; 25 | if(row ==0 )return 0; 26 | 27 | int col = grid[0].length; 28 | boolean marked[][]= new boolean[row][col]; 29 | Queue queue = new LinkedList(); 30 | int count=0; 31 | 32 | for(int i=0;i queue, int row, int col, boolean[][] marked, char[][] grid) { 44 | Node node = new Node(i,j); 45 | queue.add(node); 46 | marked[i][j]=true; 47 | 48 | while(!queue.isEmpty()) { 49 | Node top = queue.poll(); 50 | int topu=top.u; 51 | int topv=top.v; 52 | 53 | List adjacent = surround(topu,topv,row,col,grid,marked); 54 | for(Node adj:adjacent) { 55 | int u=adj.u; 56 | int v=adj.v; 57 | if(marked[u][v] == false) { 58 | queue.add(adj); 59 | marked[u][v]=true; 60 | } 61 | } 62 | } 63 | } 64 | 65 | private List surround(int i, int j, int row, int col, char[][] grid, boolean[][] marked) { 66 | 67 | List result = new ArrayList(); 68 | 69 | if(i-1>=0 && grid[i-1][j] =='1' && marked[i-1][j] == false) result.add(new Node(i-1,j)); 70 | if(i+1<=row-1 && grid[i+1][j] =='1' && marked[i+1][j] == false) result.add(new Node(i+1,j)); 71 | if(j-1>=0 && grid[i][j-1] =='1' && marked[i][j-1] == false) result.add(new Node(i,j-1)); 72 | if(j+1<=col-1 && grid[i][j+1] =='1' && marked[i][j+1] == false) result.add(new Node(i,j+1)); 73 | 74 | return result; 75 | } 76 | 77 | public static void main(String[] args) { 78 | 79 | char grid[][]= {{'1','1','1','1','0'}, 80 | {'1','1','0','1','0'}, 81 | {'1','1','0','0','0'}, 82 | {'0','0','0','0','0'}}; 83 | 84 | char grid1[][]= {{'1','1','0','0','0'}, 85 | {'1','1','0','0','0'}, 86 | {'0','0','1','0','0'}, 87 | {'0','0','0','1','1'}}; 88 | 89 | 90 | System.out.println(new NumberOfIsland().numIslands(grid1)); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /April Challenge/com/leetcode/AprilChallenge/week3/SearchInRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.AprilChallenge.week3; 2 | 3 | public class SearchInRotatedSortedArray { 4 | 5 | public static void main(String[] args) { 6 | 7 | int nums[]= {4,5,6,7,0,1,2}; 8 | int nums1[]= {4,5,6,7,0,1,2}; 9 | int m[]= {1,3}; 10 | int target=3; 11 | 12 | 13 | System.out.println(new SearchInRotatedSortedArray().search(nums 14 | ,0)); 15 | System.out.println(new SearchInRotatedSortedArray().search(nums1 16 | ,3)); 17 | System.out.println(new SearchInRotatedSortedArray().search(m 18 | ,target)); 19 | } 20 | 21 | private int search(int[] nums, int target) { 22 | 23 | if(nums.length == 0) return -1; 24 | 25 | int low = 0; 26 | int high = nums.length -1; 27 | 28 | while(low<=high) { 29 | int first=nums[low]; 30 | int mid = (low + high)/2; 31 | int value = nums[mid]; 32 | 33 | if(value == target) 34 | return mid; 35 | 36 | if(first<=value) { 37 | if(target>=first && target <=value) 38 | high=mid-1; 39 | else 40 | low=mid+1; 41 | } 42 | else 43 | { 44 | if(target>=value && target<=nums[high]) 45 | low=mid+1; 46 | else 47 | high=mid-1; 48 | 49 | } 50 | } 51 | return -1; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /April Challenge/com/leetcode/AprilChallenge/week4/SubarraySumEqualToK.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.AprilChallenge.week4; 2 | 3 | import java.util.HashMap; 4 | 5 | public class SubarraySumEqualToK { 6 | 7 | public static void main(String[] args) { 8 | int nums[]= {1,1,1}; 9 | int k=2; 10 | 11 | System.out.println(new SubarraySumEqualToK().subarraySum(nums,k)); 12 | } 13 | 14 | private int subarraySum(int[] nums, int k) { 15 | 16 | int n = nums.length; 17 | HashMap hash = new HashMap(); 18 | hash.put(0,1); 19 | int count=0; 20 | int currentSum=0; 21 | for(int i=0;i findDuplicates(int[] nums) { 18 | List result = new ArrayList(); 19 | 20 | for (int num : nums) { 21 | int number = Math.abs(num); 22 | if (nums[number - 1] < 0) 23 | result.add(number); 24 | else 25 | nums[number - 1] = -nums[number - 1]; 26 | } 27 | return result; 28 | } 29 | 30 | public static void main(String[] args) { 31 | int nums[] = { 4, 3, 2, 7, 8, 2, 3, 1 }; 32 | 33 | List result = new FindAllDuplicatesInAnArray().findDuplicates(nums); 34 | 35 | for (Integer num : result) 36 | System.out.println(num + " "); 37 | 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /July Challenge/com/leetcode/JulyChallenge/week1/HammingDistance.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JulyChallenge.week1; 2 | 3 | /** 4 | * Problem Statement :- 5 | * https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/ 6 | * 7 | * 8 | * Additional Reading:- 9 | * https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/ 10 | * 11 | * 12 | * 13 | */ 14 | 15 | public class HammingDistance { 16 | public int hammingDistance(int x, int y) { 17 | int count = 0; 18 | 19 | int result = x ^ y; 20 | while (result > 0) { 21 | result = (result & result - 1); 22 | count++; 23 | } 24 | return count; 25 | } 26 | 27 | public static void main(String[] args) { 28 | int x = 1; 29 | int y = 4; 30 | System.out.println(new HammingDistance().hammingDistance(x, y)); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /July Challenge/com/leetcode/JulyChallenge/week1/PlusOne.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JulyChallenge.week1; 2 | 3 | /* 4 | * Time complexity :- O(n) 5 | * Space complexity :- O(1) 6 | * 7 | * 8 | */ 9 | 10 | public class PlusOne { 11 | 12 | public int[] plusOne(int[] digits) { 13 | int n = digits.length; 14 | 15 | for (int i = n - 1; i >= 0; i--) { 16 | if (digits[i] < 9) { 17 | digits[i]++; 18 | return digits; 19 | } 20 | digits[i] = 0; 21 | } 22 | 23 | int edgeCase[] = new int[n + 1]; 24 | edgeCase[0] = 1; 25 | return edgeCase; 26 | 27 | } 28 | 29 | public static void main(String[] args) { 30 | int digits[] = { 4, 3, 2, 1 }; 31 | int digits1[] = { 9, 9, 9, 9 }; 32 | int digits2[] = { 1, 2, 3, 9 }; 33 | 34 | int result[] = new PlusOne().plusOne(digits); 35 | int result1[] = new PlusOne().plusOne(digits1); 36 | int result2[] = new PlusOne().plusOne(digits2); 37 | 38 | print(result); 39 | print(result1); 40 | print(result2); 41 | System.out.println(); 42 | 43 | } 44 | 45 | private static void print(int[] result) { 46 | for (int i : result) 47 | System.out.print(i + " "); 48 | 49 | System.out.println(); 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week1/DeleteNodeInALinkedList.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week1; 2 | 3 | class ListNode { 4 | int val; 5 | ListNode next; 6 | 7 | ListNode(int x) { 8 | val = x; 9 | } 10 | } 11 | 12 | /** 13 | * @author Shrinath Joshi 14 | * 15 | * Time complexity :- O(1) 16 | * 17 | * Space complexity :- O(1) 18 | * 19 | */ 20 | 21 | public class DeleteNodeInALinkedList { 22 | 23 | public void deleteNode(ListNode node) { 24 | ListNode curr = node; 25 | ListNode temp = node; 26 | curr.val = curr.next.val; 27 | temp = curr.next; 28 | curr.next = curr.next.next; 29 | temp.next = null; 30 | } 31 | 32 | public static void main(String[] args) { 33 | ListNode one = new ListNode(1); 34 | ListNode five = new ListNode(5); 35 | ListNode four = new ListNode(4); 36 | ListNode nine = new ListNode(9); 37 | 38 | one.next = five; 39 | five.next = four; 40 | four.next = nine; 41 | 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week1/InvertBinaryTree.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week1; 2 | 3 | import java.util.LinkedList; 4 | import java.util.Queue; 5 | 6 | /** 7 | * @author Shrinath Joshi 8 | * 9 | * 10 | */ 11 | class TreeNode { 12 | int val; 13 | TreeNode left; 14 | TreeNode right; 15 | 16 | TreeNode() { 17 | } 18 | 19 | TreeNode(int val) { 20 | this.val = val; 21 | } 22 | 23 | TreeNode(int val, TreeNode left, TreeNode right) { 24 | this.val = val; 25 | this.left = left; 26 | this.right = right; 27 | } 28 | } 29 | 30 | public class InvertBinaryTree { 31 | 32 | public TreeNode invertTree(TreeNode root) { 33 | // return invertTreeUsingRecursion(root); 34 | return invertTreeUsingIteration(root); 35 | } 36 | 37 | private TreeNode invertTreeUsingIteration(TreeNode root) { 38 | 39 | // Time complexity :- O(n) 40 | // Space complexity :- O(n) 41 | 42 | if (root == null) 43 | return root; 44 | 45 | Queue queue = new LinkedList(); 46 | queue.add(root); 47 | 48 | while (!queue.isEmpty()) { 49 | 50 | TreeNode current = queue.poll(); 51 | TreeNode temp = current.left; 52 | current.left = current.right; 53 | current.right = temp; 54 | 55 | if (current.left != null) 56 | queue.add(current.left); 57 | if (current.right != null) 58 | queue.add(current.right); 59 | } 60 | 61 | return root; 62 | } 63 | 64 | private TreeNode invertTreeUsingRecursion(TreeNode root) { 65 | 66 | // Time complexity :- O(n) 67 | // Space complexity :- O(h) h is the height of the tree (worst case h=n) 68 | 69 | // Base Case 1 70 | if (root == null) 71 | return root; 72 | // Base Case 2 73 | if (root.left == null && root.right == null) 74 | return root; 75 | 76 | TreeNode left = invertTree(root.left); 77 | TreeNode right = invertTree(root.right); 78 | root.right = left; 79 | root.left = right; 80 | 81 | return root; 82 | } 83 | 84 | public static void main(String[] args) { 85 | 86 | TreeNode one = new TreeNode(1); 87 | TreeNode three = new TreeNode(3); 88 | TreeNode six = new TreeNode(6); 89 | TreeNode nine = new TreeNode(9); 90 | TreeNode two = new TreeNode(2, one, three); 91 | TreeNode seven = new TreeNode(7, six, nine); 92 | 93 | TreeNode four = new TreeNode(4, two, seven); 94 | 95 | TreeNode root = new InvertBinaryTree().invertTree(four); 96 | 97 | inorder(root); 98 | System.out.println(); 99 | 100 | } 101 | 102 | private static void inorder(TreeNode root) { 103 | if (root != null) { 104 | inorder(root.left); 105 | System.out.print(root.val + " "); 106 | inorder(root.right); 107 | } 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week1/QueueReconstructionByHeight.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week1; 2 | 3 | import java.util.Arrays; 4 | 5 | public class QueueReconstructionByHeight { 6 | 7 | public int[][] reconstructQueue(int[][] people) { 8 | int n = people.length; 9 | int result[][] = new int[n][2]; 10 | 11 | for (int i = 0; i < n; i++) { 12 | result[i][0] = -1; 13 | result[i][1] = -1; 14 | } 15 | 16 | Arrays.sort(people, (a, b) -> (a[0] != b[0]) ? a[0] - b[0] : a[1] - b[1]); 17 | 18 | for (int i = 0; i < n; i++) { 19 | int count = people[i][1]; 20 | for (int j = 0; j < n; j++) { 21 | if (count == 0 && result[j][0] == -1) { 22 | result[j][0] = people[i][0]; 23 | result[j][1] = people[i][1]; 24 | break; 25 | } else if (result[j][0] >= people[i][0] || result[j][0] == -1) { 26 | count--; 27 | } 28 | } 29 | } 30 | 31 | return result; 32 | } 33 | 34 | public static void main(String[] args) { 35 | 36 | int people[][] = { { 7, 0 }, { 4, 4 }, { 7, 1 }, { 5, 0 }, { 6, 1 }, { 5, 2 } }; 37 | 38 | int result[][] = new QueueReconstructionByHeight().reconstructQueue(people); 39 | 40 | for (int i = 0; i < result.length; i++) { 41 | System.out.print("[" + result[i][0] + "," + result[i][1] + "]"); 42 | System.out.println(" "); 43 | } 44 | 45 | // {{5,0},{6,1},{5,2},{7,0},{4,4},{7,1}} 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week1/ReverseString.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week1; 2 | 3 | public class ReverseString { 4 | 5 | // Time complexity :- O(n) 6 | // space complexity :- O(1) 7 | public void reverseString(char[] s) { 8 | int left = 0; 9 | int right = s.length - 1; 10 | 11 | while (left < right) { 12 | s[left] = (char) (s[left] ^ s[right]); 13 | s[right] = (char) (s[right] ^ s[left]); 14 | s[left] = (char) (s[left] ^ s[right]); 15 | left++; 16 | right--; 17 | } 18 | } 19 | 20 | public static void main(String[] args) { 21 | char s[] = { 'h', 'e', 'l', 'l', 'o' }; 22 | char s1[] = { 'H', 'a', 'n', 'n', 'a', 'h' }; 23 | 24 | ReverseString reverseString = new ReverseString(); 25 | reverseString.reverseString(s); 26 | reverseString.reverseString(s1); 27 | 28 | for (char c : s) { 29 | System.out.print(c + " "); 30 | } 31 | 32 | System.out.println(); 33 | 34 | for (char c : s1) { 35 | System.out.print(c + " "); 36 | } 37 | 38 | System.out.println(); 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week1/TwoCityScheduling.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week1; 2 | 3 | import java.util.PriorityQueue; 4 | 5 | /** 6 | * @author Shrinath Joshi 7 | * 8 | * Time complexity :- O(nlogn) // 9 | * 10 | * Space complexity :- O(n) 11 | * 12 | */ 13 | public class TwoCityScheduling { 14 | public int twoCitySchedCost(int[][] costs) { 15 | int n = costs.length; 16 | int totalCost = 0; 17 | int currentCount = 0; 18 | 19 | // Building a Max-Heap 20 | PriorityQueue pq = new PriorityQueue((a, b) -> b.profit - a.profit); 21 | 22 | for (int i = 0; i < n; i++) 23 | pq.add(new CityNode(costs[i], (costs[i][1] - costs[i][0]))); 24 | 25 | while (currentCount < n && !pq.isEmpty()) { 26 | CityNode top = pq.poll(); 27 | int costOfCity[] = top.cityCost; 28 | 29 | if (currentCount < n / 2) 30 | totalCost += costOfCity[0]; 31 | else 32 | totalCost += costOfCity[1]; 33 | currentCount++; 34 | } 35 | 36 | return totalCost; 37 | } 38 | 39 | public static void main(String[] args) { 40 | int costs[][] = { { 10, 20 }, { 30, 200 }, { 400, 50 }, { 30, 20 } }; 41 | System.out.println(new TwoCityScheduling().twoCitySchedCost(costs)); 42 | } 43 | 44 | } 45 | 46 | class CityNode { 47 | public int cityCost[]; 48 | public int profit; 49 | 50 | public CityNode(int cityCost[], int profit) { 51 | this.cityCost = cityCost; 52 | this.profit = profit; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week2/InsertDeleteGetRandom.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.Random; 7 | 8 | class RandomizedSet { 9 | private List list; 10 | private HashMap map; 11 | private Random rand; 12 | 13 | /** Initialize your data structure here. */ 14 | public RandomizedSet() { 15 | list = new ArrayList(); 16 | map = new HashMap(); 17 | rand = new Random(); 18 | } 19 | 20 | /** 21 | * Inserts a value to the set. Returns true if the set did not already contain 22 | * the specified element. 23 | */ 24 | public boolean insert(int val) { 25 | if (map.containsKey(val)) 26 | return false; 27 | 28 | list.add(val); 29 | map.put(val, list.size() - 1); 30 | return true; 31 | 32 | } 33 | 34 | /** 35 | * Removes a value from the set. Returns true if the set contained the specified 36 | * element. 37 | */ 38 | public boolean remove(int val) { 39 | if (!map.containsKey(val)) 40 | return false; 41 | 42 | int index = map.get(val); 43 | int valueAtLastIndex = list.get(list.size() - 1); 44 | 45 | list.set(index, valueAtLastIndex); 46 | map.put(valueAtLastIndex, index); 47 | list.remove(list.size() - 1); 48 | map.remove(val); 49 | return true; 50 | } 51 | 52 | /** Get a random element from the set. */ 53 | public int getRandom() { 54 | 55 | return list.get(rand.nextInt(list.size())); 56 | } 57 | } 58 | 59 | /** 60 | * Your RandomizedSet object will be instantiated and called as such: 61 | * 62 | */ 63 | 64 | public class InsertDeleteGetRandom { 65 | 66 | public static void main(String[] args) { 67 | RandomizedSet obj = new RandomizedSet(); 68 | 69 | System.out.println(obj.insert(1)); 70 | System.out.println(obj.remove(2)); 71 | System.out.println(obj.insert(2)); 72 | System.out.println(obj.getRandom()); 73 | System.out.println(obj.remove(1)); 74 | System.out.println(obj.insert(2)); 75 | System.out.println(obj.getRandom()); 76 | 77 | } 78 | 79 | } 80 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week2/IsSubsequence.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week2; 2 | 3 | public class IsSubsequence { 4 | 5 | public boolean isSubsequence(String s, String t) { 6 | // Base Condition 7 | if (s.length() == 0 && t.length() != 0) 8 | return true; 9 | 10 | if (s.length() > t.length()) 11 | return false; 12 | 13 | int firstPointer = 0; 14 | int secondPointer = 0; 15 | 16 | while (firstPointer < s.length() && secondPointer < t.length()) { 17 | 18 | if (s.charAt(firstPointer) == t.charAt(secondPointer)) { 19 | firstPointer++; 20 | secondPointer++; 21 | } else 22 | secondPointer++; 23 | } 24 | 25 | return firstPointer == s.length(); 26 | } 27 | 28 | public static void main(String[] args) { 29 | 30 | String s = "abc", t = "ahbgdc"; 31 | System.out.println(new IsSubsequence().isSubsequence(s, t)); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week2/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week2; 2 | 3 | public class PowerOfTwo { 4 | 5 | public boolean isPowerOfTwo(int n) { 6 | return n > 0 && ((n & (n - 1)) == 0); 7 | } 8 | 9 | public static void main(String[] args) { 10 | System.out.println(new PowerOfTwo().isPowerOfTwo(12)); 11 | System.out.println(new PowerOfTwo().isPowerOfTwo(11)); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /June Challenge/com/leetcode/JuneChallenge/week2/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.JuneChallenge.week2; 2 | 3 | public class SearchInsertPosition { 4 | 5 | public int searchInsert(int[] nums, int target) { 6 | int index = 0; 7 | 8 | while (index < nums.length && nums[index] < target) 9 | index++; 10 | 11 | return index; 12 | } 13 | 14 | public static void main(String[] args) { 15 | int nums[] = { 1, 3, 5, 7 }; 16 | int target = 4; 17 | 18 | System.out.println(new SearchInsertPosition().searchInsert(nums, target)); 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Shrinath Joshi 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week1/CousinsInBinaryTree.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week1; 2 | 3 | /** 4 | * 5 | * @author Shrinath Joshi 6 | * 7 | * Time complexity :- O(n) 8 | * Space complexity :-O(log(n)) 9 | * 10 | */ 11 | 12 | 13 | class TreeNode { 14 | int val; 15 | TreeNode left; 16 | TreeNode right; 17 | 18 | TreeNode() { 19 | } 20 | 21 | TreeNode(int val) { 22 | this.val = val; 23 | } 24 | 25 | TreeNode(int val, TreeNode left, TreeNode right) { 26 | this.val = val; 27 | this.left = left; 28 | this.right = right; 29 | } 30 | } 31 | 32 | public class CousinsInBinaryTree { 33 | 34 | public boolean isCousins(TreeNode root, int x, int y) { 35 | 36 | int xLevel = getLevelOfNode(root, x, 0); 37 | int yLevel = getLevelOfNode(root, y, 0); 38 | 39 | TreeNode xParent = getParent(root, x, new TreeNode(-1)); 40 | TreeNode yParent = getParent(root, y, new TreeNode(-1)); 41 | 42 | return ((xLevel == yLevel) && ((xParent != null && yParent != null) && (xParent.val != yParent.val))); 43 | } 44 | 45 | private TreeNode getParent(TreeNode root, int x, TreeNode parent) { 46 | if (root == null) 47 | return null; 48 | if (root.val == x) 49 | return parent; 50 | else { 51 | TreeNode leftParent = getParent(root.left, x, root); 52 | if (leftParent != null) 53 | return leftParent; 54 | 55 | TreeNode rightParent = getParent(root.right, x, root); 56 | if (rightParent != null) 57 | return rightParent; 58 | } 59 | return null; 60 | } 61 | 62 | private int getLevelOfNode(TreeNode root, int x, int level) { 63 | 64 | if (root == null) 65 | return 0; 66 | if (root.val == x) 67 | return level; 68 | else { 69 | int leftLevel = getLevelOfNode(root.left, x, level + 1); 70 | if (leftLevel != 0) 71 | return leftLevel; 72 | 73 | return getLevelOfNode(root.right, x, level + 1); 74 | } 75 | 76 | } 77 | 78 | public static void main(String[] args) { 79 | 80 | TreeNode node3 = new TreeNode(5); 81 | TreeNode node2 = new TreeNode(4, null, null); 82 | TreeNode node = new TreeNode(2, null, node2); 83 | TreeNode node1 = new TreeNode(3, null, node3); 84 | TreeNode root = new TreeNode(1, node, node1); 85 | 86 | System.out.println(new CousinsInBinaryTree().isCousins(root, 5, 4)); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week1/FirstBadVersion.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week1; 2 | 3 | /* The isBadVersion API is defined in the parent class VersionControl. 4 | boolean isBadVersion(int version); */ 5 | 6 | public class Solution extends VersionControl { 7 | public int firstBadVersion(int n) { 8 | 9 | //Binary Search 10 | int low =1; 11 | int high =n; 12 | while(high>=low){ 13 | int mid = low + (high-low)/2; 14 | if(isBadVersion(mid)){ 15 | high=mid-1; 16 | } 17 | else low=mid+1; 18 | } 19 | 20 | return low; 21 | } 22 | } -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week1/FirstUniqueCharacterInString.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week1; 2 | 3 | import java.util.LinkedHashMap; 4 | 5 | 6 | /** 7 | * @author Shrinath Joshi 8 | * Time complexity :- O(n) 9 | * Space complexity :- O(n) 10 | */ 11 | 12 | public class FirstUniqueCharacterInString { 13 | 14 | public int firstUniqChar(String s) { 15 | 16 | LinkedHashMap cache = new LinkedHashMap(); 17 | int n = s.length(); 18 | 19 | for(int i=0;i cache = new HashMap(); 14 | 15 | for(int i=0;irightAnsCount?leftAns:rightAns; 58 | } 59 | 60 | } 61 | 62 | private int getCount(int ans, int left, int right, int[] nums) { 63 | int count=0; 64 | for(int i =left;i<=right;i++) { 65 | if(nums[i] == ans) 66 | count++; 67 | } 68 | return count; 69 | } 70 | 71 | private int majorityElementUsingSorting(int[] nums) { 72 | 73 | // Time complexity :- O(nlogn) 74 | // Space complexity : - O(1) 75 | Arrays.sort(nums); 76 | return nums[nums.length / 2]; 77 | } 78 | 79 | private int majorityElementUsingHashMap(int[] nums) { 80 | 81 | // Time complexity:- O(n) 82 | // Space complexity:- O(n) 83 | 84 | int n = nums.length; 85 | 86 | HashMap counts = new HashMap(); 87 | 88 | for (int i = 0; i < n; i++) { 89 | counts.put(nums[i], counts.getOrDefault(nums[i], 0) + 1); 90 | if (counts.get(nums[i]) > n / 2) 91 | return nums[i]; 92 | } 93 | 94 | return -1; 95 | } 96 | 97 | public static void main(String[] args) { 98 | int nums[] = { 2, 2, 1, 1, 1, 2, 2 }; 99 | System.out.println(new MajorityElement().majorityElement(nums)); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week1/NumberComplement.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week1; 2 | 3 | /** 4 | * @author Shrinath Joshi 5 | * 6 | * Time complexity:- 7 | * no of time while loop executes = log(v) , 8 | * therefore , TC = log(v) ,where v - value of number 9 | */ 10 | 11 | public class NumberComplement { 12 | 13 | public int findComplement(int num) { 14 | int n =num; 15 | int bit = 1; 16 | while(n>0) { 17 | num=num^bit; 18 | bit=bit<<1; 19 | n=n>>1; 20 | } 21 | 22 | return num; 23 | } 24 | 25 | public static void main(String[] args) { 26 | System.out.println(new NumberComplement().findComplement(12)); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week1/RansomNote.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week1; 2 | 3 | import java.util.HashMap; 4 | 5 | /*@author Shrinath Joshi 6 | * 7 | * 8 | * Time complexity:- O(n+m) 9 | * Space complexity - O(m) 10 | * n = length of ransomNote 11 | * m = length of magazine 12 | */ 13 | 14 | public class RansomNote { 15 | 16 | public static void main(String[] args) { 17 | String ransomeNote = "bg"; 18 | String magazine = "efjbdfbdgfjhhaiigfhbaejahgfbbgbjagbddfgdiaigdadhcfcj"; 19 | 20 | System.out.println(new RansomNote().canConstruct(ransomeNote, magazine)); 21 | System.out.println(new RansomNote().canConstructEfficent(ransomeNote, magazine)); 22 | 23 | } 24 | 25 | private boolean canConstruct(String ransomNote, String magazine) { 26 | 27 | // Time complexity:- O(n+m) 28 | // Space complexity - O(m) 29 | 30 | HashMap magazineCache = new HashMap(); 31 | 32 | for (char c : magazine.toCharArray()) { 33 | magazineCache.put(c, magazineCache.getOrDefault(c, 0) + 1); 34 | } 35 | 36 | for (char c : ransomNote.toCharArray()) { 37 | if (!magazineCache.containsKey(c) || magazineCache.get(c) <= 0) 38 | return false; 39 | else 40 | magazineCache.put(c, magazineCache.get(c) - 1); 41 | } 42 | 43 | return true; 44 | 45 | } 46 | 47 | private boolean canConstructEfficent(String ransomNote, String magazine) { 48 | 49 | // Time complexity:- O(n+m) 50 | // Space complexity - O(1) 51 | 52 | int count[] = new int[26]; 53 | 54 | for(char c : magazine.toCharArray()) 55 | count[c - 'a']++; 56 | 57 | for(char c: ransomNote.toCharArray()) { 58 | if(count[--c - 'a']<0) return false; 59 | } 60 | 61 | return true; 62 | } 63 | 64 | } 65 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week2/CheckIfAStraightLine.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week2; 2 | 3 | public class CheckIfAStraightLine { 4 | 5 | public boolean checkStraightLine(int[][] coordinates) { 6 | 7 | if(coordinates.length ==2 ) return true; 8 | 9 | for(int i=2;i= 1) 42 | depthFirstSearch(image, r - 1, c, color, newColor); 43 | if (c >= 1) 44 | depthFirstSearch(image, r, c - 1, color, newColor); 45 | if (r + 1 < image.length) 46 | depthFirstSearch(image, r + 1, c, color, newColor); 47 | if (c + 1 < image[0].length) 48 | depthFirstSearch(image, r, c + 1, color, newColor); 49 | } 50 | } 51 | 52 | private int[][] floodFillUsingDFS(int[][] image, int sr, int sc, int newColor) { 53 | row = image.length; 54 | col = image[0].length; 55 | this.image = image; 56 | marked = new boolean[row][col]; 57 | dfs(sr, sc, newColor); 58 | return image; 59 | } 60 | 61 | private void dfs(int sr, int sc, int newColor) { 62 | int oldColor = image[sr][sc]; 63 | image[sr][sc] = newColor; 64 | marked[sr][sc] = true; 65 | 66 | for (Edge i : getAdjacent(sr, sc, oldColor)) { 67 | if (!marked[i.getU()][i.getV()]) 68 | dfs(i.getU(), i.getV(), newColor); 69 | } 70 | 71 | } 72 | 73 | private int[][] floodFillUsingBFS(int[][] image, int sr, int sc, int newColor) { 74 | row = image.length; 75 | col = image[0].length; 76 | 77 | if (!validNode(sr, sc)) 78 | return image; 79 | 80 | marked = new boolean[row][col]; 81 | this.image = image; 82 | 83 | bfs(marked, newColor, sr, sc, image); 84 | 85 | return image; 86 | } 87 | 88 | private void bfs(boolean[][] marked, int newColor, int sr, int sc, int[][] image) { 89 | 90 | marked[sr][sc] = true; 91 | int oldColor = image[sr][sc]; 92 | image[sr][sc] = newColor; 93 | 94 | Queue queue = new LinkedList(); 95 | queue.add(new Edge(sr, sc, oldColor)); 96 | 97 | while (!queue.isEmpty()) { 98 | Edge top = queue.poll(); 99 | for (Edge i : getAdjacent(top.getU(), top.getV(), top.getOldColor())) { 100 | int u = i.getU(); 101 | int v = i.getV(); 102 | if (!marked[u][v]) { 103 | image[u][v] = newColor; 104 | marked[u][v] = true; 105 | queue.add(i); 106 | } 107 | } 108 | 109 | } 110 | } 111 | 112 | private List getAdjacent(int sr, int sc, int oldColor) { 113 | 114 | List adj = new ArrayList(); 115 | if (validNode(sr - 1, sc) && image[sr - 1][sc] == oldColor && !marked[sr - 1][sc]) 116 | adj.add(new Edge(sr - 1, sc, image[sr - 1][sc])); 117 | if (validNode(sr + 1, sc) && image[sr + 1][sc] == oldColor && !marked[sr + 1][sc]) 118 | adj.add(new Edge(sr + 1, sc, image[sr + 1][sc])); 119 | if (validNode(sr, sc - 1) && image[sr][sc - 1] == oldColor && !marked[sr][sc - 1]) 120 | adj.add(new Edge(sr, sc - 1, image[sr][sc - 1])); 121 | if (validNode(sr, sc + 1) && image[sr][sc + 1] == oldColor && !marked[sr][sc + 1]) 122 | adj.add(new Edge(sr, sc + 1, image[sr][sc + 1])); 123 | 124 | return adj; 125 | } 126 | 127 | private boolean validNode(int i, int j) { 128 | if (i >= 0 && i < row && j >= 0 && j < col) 129 | return true; 130 | return false; 131 | } 132 | 133 | public static void main(String[] args) { 134 | 135 | int image[][] = { { 1, 1, 1 }, { 1, 1, 0 }, { 1, 0, 1 } }; 136 | int image1[][] = { { 0, 0, 0 }, { 0, 0, 0 } }; 137 | 138 | int result[][] = new FloodFill().floodFill(image, 1, 1, 2); 139 | int result1[][] = new FloodFill().floodFill(image1, 0, 0, 2); 140 | 141 | for (int i = 0; i < result.length; i++) { 142 | for (int j = 0; j < result[0].length; j++) 143 | System.out.print(result[i][j] + " "); 144 | System.out.println(); 145 | } 146 | 147 | System.out.println(); 148 | for (int i = 0; i < result1.length; i++) { 149 | for (int j = 0; j < result1[0].length; j++) 150 | System.out.print(result1[i][j] + " "); 151 | System.out.println(); 152 | } 153 | } 154 | 155 | } 156 | 157 | class Edge { 158 | private int u; 159 | private int v; 160 | private int oldColor; 161 | 162 | public Edge(int u, int v, int oldColor) { 163 | this.u = u; 164 | this.v = v; 165 | this.oldColor = oldColor; 166 | } 167 | 168 | public int getU() { 169 | return u; 170 | } 171 | 172 | public void setU(int u) { 173 | this.u = u; 174 | } 175 | 176 | public int getV() { 177 | return v; 178 | } 179 | 180 | public int getOldColor() { 181 | return oldColor; 182 | } 183 | 184 | public void setOldColor(int oldColor) { 185 | this.oldColor = oldColor; 186 | } 187 | 188 | public void setV(int v) { 189 | this.v = v; 190 | } 191 | 192 | } 193 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week2/RemoveKDigits.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week2; 2 | 3 | /* 4 | * @author Shrinath Joshi 5 | * 6 | * Time complexity:- O(kn) 7 | * Space complexity :- O(1) 8 | * 9 | */ 10 | 11 | public class RemoveKDigits { 12 | public String removeKdigits(String num, int k) { 13 | return removeKdigit(num, k); 14 | } 15 | 16 | private String removeKdigit(String num, int k) { 17 | 18 | int n = num.length(); 19 | 20 | if (n == k) 21 | return "0"; 22 | 23 | StringBuilder sb = new StringBuilder(num); 24 | 25 | for (int i = 0; i < k; i++) { 26 | int index = 0; 27 | while (index < sb.length() - 1 && sb.charAt(index) <= sb.charAt(index + 1)) 28 | index++; 29 | 30 | sb.delete(index, index + 1); 31 | } 32 | 33 | while (sb.length() > 1 && sb.charAt(0) == '0') 34 | sb.delete(0, 1); 35 | 36 | if (sb.length() == 0) 37 | return "0"; 38 | 39 | return sb.toString(); 40 | 41 | } 42 | 43 | 44 | public static void main(String[] args) { 45 | 46 | String num = "1432219"; 47 | int k = 3; 48 | 49 | String num1 = "5337"; 50 | int k1 = 2; 51 | 52 | System.out.println(new RemoveKDigits().removeKdigits(num, k)); 53 | System.out.println(new RemoveKDigits().removeKdigits(num1, k1)); 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week2/SingleElementInSortedArray.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week2; 2 | 3 | public class SingleElementInSortedArray { 4 | 5 | public int singleNonDuplicate(int[] nums) { 6 | 7 | // return singleNonDuplicateUsingXOR(nums); 8 | 9 | return singleNonDuplicateUsingTwoPointer(nums); 10 | } 11 | 12 | private int singleNonDuplicateUsingTwoPointer(int[] nums) { 13 | 14 | if(nums.length == 1) return nums[0]; 15 | 16 | int low = 0 ; 17 | int high = nums.length-1; 18 | 19 | while(low<=high) { 20 | 21 | if(nums[low] != nums[low+1]) 22 | return nums[low]; 23 | if(nums[high] != nums[high-1]) 24 | return nums[high]; 25 | 26 | low=low+2; 27 | high=high-2; 28 | } 29 | return -1; 30 | } 31 | 32 | private int singleNonDuplicateUsingXOR(int[] nums) { 33 | //Time Complexity :- O(n) 34 | //Space Complexity :- O(1) 35 | 36 | int x =0; 37 | for(int i : nums) 38 | x=x^i; 39 | return x; 40 | } 41 | 42 | public static void main(String[] args) { 43 | int nums[] = { 1}; 44 | int nums1[] = { 3, 3, 7, 7, 10, 11, 11 }; 45 | 46 | 47 | System.out.println(new SingleElementInSortedArray().singleNonDuplicate(nums)); 48 | System.out.println(new SingleElementInSortedArray().singleNonDuplicate(nums1)); 49 | 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week2/Trie.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week2; 2 | 3 | 4 | /** 5 | * @author Shrinath Joshi 6 | * 7 | * Insert :- 8 | * Time complexity:- O(m) 9 | * Space complexity:- O(m) , m is length of String 10 | * 11 | * Search :- 12 | * Time complexity:- O(m) , m is length of String 13 | * Space complexity:- O(1) 14 | * 15 | * Search for key prefix :- 16 | * Time complexity:- O(m) , m is length of String 17 | * Space complexity:- O(1) 18 | * 19 | * 20 | */ 21 | class TrieNode{ 22 | public TrieNode[] children; 23 | public boolean isEndOfWord; 24 | 25 | public TrieNode() 26 | { 27 | children=new TrieNode[26]; 28 | } 29 | } 30 | 31 | public class Trie { 32 | private TrieNode root; 33 | 34 | public Trie() { 35 | root = new TrieNode(); 36 | } 37 | 38 | /** Inserts a word into the trie. */ 39 | public void insert(String word) { 40 | int n = word.length(); 41 | TrieNode curr = root; 42 | for(int i=0;i=low) { 22 | double mid = low + Math.floor((high - low)/2); 23 | double value = mid*mid; 24 | if(value == num) 25 | return true; 26 | else if(value > num) 27 | high=mid-1; 28 | else low = mid+1; 29 | } 30 | return false; 31 | } 32 | 33 | public static void main(String[] args) { 34 | System.out.println(new ValidPerfectSquare().isPerfectSquare(16)); 35 | System.out.println(new ValidPerfectSquare().isPerfectSquare(19)); 36 | System.out.println(new ValidPerfectSquare().isPerfectSquare(26)); 37 | System.out.println(new ValidPerfectSquare().isPerfectSquare(2147483647)); 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/CountSquareSubmatricesWithAllOnes.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | public class CountSquareSubmatricesWithAllOnes { 4 | 5 | public int countSquares(int[][] matrix) { 6 | int row = matrix.length; 7 | int column = matrix[0].length; 8 | int dp[][] = new int[row][column]; 9 | 10 | for (int i = 0; i < row; i++) 11 | dp[i][0] = matrix[i][0]; 12 | 13 | for (int i = 0; i < column; i++) 14 | dp[0][i] = matrix[0][i]; 15 | 16 | for (int i = 1; i < row; i++) { 17 | for (int j = 1; j < column; j++) { 18 | if (matrix[i][j] == 1) { 19 | int min = Math.min(dp[i - 1][j], dp[i][j - 1]); 20 | int finalMin = Math.min(min, dp[i - 1][j - 1]); 21 | dp[i][j] = finalMin + 1; 22 | } 23 | } 24 | } 25 | 26 | int result = 0; 27 | for (int i = 0; i < row; i++) 28 | for (int j = 0; j < column; j++) 29 | if (dp[i][j] >= 1) 30 | result += dp[i][j]; 31 | 32 | return result; 33 | 34 | } 35 | 36 | public static void main(String[] args) { 37 | int matrix[][] = { { 0, 1, 1, 1 }, { 1, 1, 1, 1 }, { 0, 1, 1, 1 } }; 38 | System.out.println(new CountSquareSubmatricesWithAllOnes().countSquares(matrix)); 39 | 40 | int matrix1[][] = { { 1, 0, 1 }, { 1, 1, 0 }, { 1, 1, 0 } }; 41 | System.out.println(new CountSquareSubmatricesWithAllOnes().countSquares(matrix1)); 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/FindAllAnagramsInString.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | public class FindAllAnagramsInString { 8 | 9 | public List findAnagrams(String s, String p) { 10 | int m = s.length(), n = p.length(); 11 | if (n > m) 12 | return new ArrayList<>(); 13 | 14 | int[] cnt_arr = new int[26]; 15 | for (int i = 0; i < n; i++) { 16 | cnt_arr[s.charAt(i) - 'a']++; 17 | cnt_arr[p.charAt(i) - 'a']--; 18 | } 19 | 20 | List list = new ArrayList<>(); 21 | for (int i = n; i < m; i++) { 22 | if (areAllZeroes(cnt_arr)) { 23 | list.add(i - n); 24 | } 25 | cnt_arr[s.charAt(i) - 'a']++; 26 | cnt_arr[s.charAt(i - n) - 'a']--; 27 | } 28 | if (areAllZeroes(cnt_arr)) { 29 | list.add(m - n); 30 | } 31 | 32 | return list; 33 | } 34 | 35 | boolean areAllZeroes(int[] cnt_arr) { 36 | for (int i = 0; i < cnt_arr.length; i++) { 37 | if (cnt_arr[i] != 0) 38 | return false; 39 | } 40 | return true; 41 | } 42 | 43 | 44 | public static void main(String[] args) { 45 | 46 | String s = "cbaebabacd"; 47 | String p = "abc"; 48 | 49 | String s1 = "eklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmoueklpyqrbgjdwtcaxzsnifvhmou"; 50 | String p1 = "yqrbgjdwtcaxzsnifvhmou"; 51 | 52 | System.out.println(new FindAllAnagramsInString().findAnagrams(s, p)); 53 | System.out.println(new FindAllAnagramsInString().findAnagrams(s1, p1)); 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/KthSmallestElementInBST.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | class TreeNode { 4 | int val; 5 | TreeNode left; 6 | TreeNode right; 7 | 8 | TreeNode() { 9 | } 10 | 11 | TreeNode(int val) { 12 | this.val = val; 13 | } 14 | 15 | TreeNode(int val, TreeNode left, TreeNode right) { 16 | this.val = val; 17 | this.left = left; 18 | this.right = right; 19 | } 20 | } 21 | 22 | class TreeValue{ 23 | int noOfSmallestSeen; 24 | int value; 25 | public TreeValue(int noOfSmallestSeen, int value) { 26 | super(); 27 | this.noOfSmallestSeen = noOfSmallestSeen; 28 | this.value = value; 29 | } 30 | } 31 | 32 | public class KthSmallestElementInBST { 33 | 34 | public int kthSmallest(TreeNode root, int k) { 35 | int nums[] = new int[2]; 36 | inorder(root, nums, k); 37 | return nums[1]; 38 | } 39 | 40 | private void inorder(TreeNode root, int[] nums, int k) { 41 | if(root == null) return ; 42 | 43 | inorder(root.left, nums,k); 44 | if(++nums[0] == k) { 45 | nums[1]=root.val; 46 | return; 47 | } 48 | inorder(root.right, nums, k); 49 | } 50 | 51 | public static void main(String[] args) { 52 | 53 | //Input: root = [5,3,6,2,4,null,null,1], k = 3 54 | 55 | TreeNode node4 = new TreeNode(4); 56 | TreeNode node1 = new TreeNode(1); 57 | TreeNode node6 = new TreeNode(6); 58 | TreeNode node2 = new TreeNode(2,node1,null); 59 | TreeNode node3 = new TreeNode(3,node2,node4); 60 | TreeNode root = new TreeNode(5,node3,node6); 61 | 62 | System.out.println(new KthSmallestElementInBST().kthSmallest(root, 3)); 63 | 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/MaximumSumCircularSubarray.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | /* 4 | * @author Shrinath Joshi 5 | * 6 | * Time complexity :-O(n) 7 | * Space complexity :- O(1) 8 | * 9 | * 10 | */ 11 | 12 | public class MaximumSumCircularSubarray { 13 | 14 | public int maxSubarraySumCircular(int[] A) { 15 | 16 | int n = A.length; 17 | int maxSoFar = Integer.MIN_VALUE; 18 | int maxEndingHere = 0; 19 | int minSoFar = Integer.MAX_VALUE; 20 | int minEndingHere = 0; 21 | int totalSum = 0; 22 | for (int i = 0; i < n; i++) { 23 | maxEndingHere = maxEndingHere + A[i]; 24 | if (maxSoFar < maxEndingHere) 25 | maxSoFar = maxEndingHere; 26 | if (maxEndingHere < 0) 27 | maxEndingHere = 0; 28 | 29 | totalSum += A[i]; 30 | 31 | minEndingHere = minEndingHere + A[i]; 32 | if (minSoFar > minEndingHere) 33 | minSoFar = minEndingHere; 34 | 35 | if (minEndingHere > 0) 36 | minEndingHere = 0; 37 | 38 | } 39 | 40 | if (totalSum == minSoFar) 41 | return maxSoFar; 42 | else 43 | return Math.max(maxSoFar, totalSum - minSoFar); 44 | } 45 | 46 | public static void main(String[] args) { 47 | int A[] = { 1, -2, 3, -2 }; 48 | int B[] = { 5, -3, 5 }; 49 | int C[] = { 3, -1, 2, -1 }; 50 | int D[] = { 3, -2, 2, -3 }; 51 | int E[] = { -2, -3, -1 }; 52 | 53 | System.out.println(new MaximumSumCircularSubarray().maxSubarraySumCircular(A)); 54 | System.out.println(new MaximumSumCircularSubarray().maxSubarraySumCircular(B)); 55 | System.out.println(new MaximumSumCircularSubarray().maxSubarraySumCircular(C)); 56 | System.out.println(new MaximumSumCircularSubarray().maxSubarraySumCircular(D)); 57 | System.out.println(new MaximumSumCircularSubarray().maxSubarraySumCircular(E)); 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/OddEvenLinkedList.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | /** 4 | * 5 | * @author Shrinath Joshi 6 | * 7 | * Time complexity :- O(n) 8 | * Space complexity :- O(1) 9 | */ 10 | 11 | class ListNode { 12 | int val; 13 | ListNode next; 14 | 15 | ListNode() { 16 | } 17 | 18 | ListNode(int val) { 19 | this.val = val; 20 | } 21 | 22 | ListNode(int val, ListNode next) { 23 | this.val = val; 24 | this.next = next; 25 | } 26 | } 27 | 28 | public class OddEvenLinkedList { 29 | 30 | public ListNode oddEvenList(ListNode head) { 31 | 32 | if (head == null) 33 | return null; 34 | 35 | if (head.next == null) 36 | return head; 37 | 38 | ListNode odd = head; 39 | ListNode even = head.next; 40 | ListNode evenStart = even; 41 | 42 | while (odd.next != null && odd.next.next != null && even != null) { 43 | odd.next = odd.next.next; 44 | odd = odd.next; 45 | even.next = even.next.next; 46 | even = even.next; 47 | } 48 | 49 | odd.next = evenStart; 50 | 51 | return head; 52 | 53 | } 54 | 55 | public static void main(String[] args) { 56 | // [1,2,3,4,5] 57 | ListNode head = new ListNode(1); 58 | head.next = new ListNode(2); 59 | head.next.next = new ListNode(3); 60 | head.next.next.next = new ListNode(4); 61 | head.next.next.next.next = new ListNode(5); 62 | 63 | OddEvenLinkedList ll = new OddEvenLinkedList(); 64 | 65 | ListNode result = ll.oddEvenList(head); 66 | while (result != null) { 67 | System.out.print(result.val + " "); 68 | result = result.next; 69 | } 70 | 71 | System.out.println(); 72 | 73 | ListNode head1 = new ListNode(2); 74 | head1.next = new ListNode(1); 75 | head1.next.next = new ListNode(3); 76 | head1.next.next.next = new ListNode(5); 77 | head1.next.next.next.next = new ListNode(6); 78 | head1.next.next.next.next.next = new ListNode(4); 79 | head1.next.next.next.next.next.next = new ListNode(7); 80 | 81 | ListNode result1 = ll.oddEvenList(head1); 82 | while (result1 != null) { 83 | System.out.print(result1.val + " "); 84 | result1 = result1.next; 85 | } 86 | 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/PermutationInString.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | 3 | import java.util.Arrays; 4 | 5 | /** 6 | * @author Shrinath Joshi 7 | * 8 | * Time complexity:- O(n) 9 | * Space complexity :- O(1) 10 | * 11 | */ 12 | 13 | 14 | public class PermutationInString { 15 | 16 | public boolean checkInclusion(String s1, String s2) { 17 | 18 | if (s1.length() == 0 || s2.length() == 0 || s1.length() > s2.length()) 19 | return false; 20 | 21 | int freqCountS1[] = new int[26]; 22 | int freqCountS2[] = new int[26]; 23 | 24 | for (int i : s1.toCharArray()) { 25 | freqCountS1[i - 'a']++; 26 | } 27 | 28 | int n = s1.length(); 29 | int m = s2.length(); 30 | 31 | for (int i = 0; i <=(m - n); i++) { 32 | int left = i; 33 | int right = i + n; 34 | for (int j = left; j < right; j++) { 35 | char currentChar = s2.charAt(j); 36 | freqCountS2[currentChar - 'a']++; 37 | 38 | if (checkIfEqual(freqCountS1, freqCountS2)) { 39 | return true; 40 | } 41 | } 42 | Arrays.fill(freqCountS2,0); 43 | } 44 | 45 | return false; 46 | } 47 | 48 | private boolean checkIfEqual(int[] freqCountS1, int[] freqCountS2) { 49 | for (int i = 0; i < freqCountS1.length; i++) if (freqCountS1[i] != freqCountS2[i])return false; 50 | return true; 51 | } 52 | 53 | public static void main(String[] args) { 54 | 55 | String s1 = "ab";String s2 = "eidbaooo"; 56 | System.out.println(new PermutationInString().checkInclusion(s1, s2)); 57 | 58 | String s3 = "ab"; 59 | String s4 = "eidboaoo"; 60 | 61 | System.out.println(new PermutationInString().checkInclusion(s3, s4)); 62 | 63 | String s5 = "adc"; 64 | String s6 = "dcda"; 65 | 66 | System.out.println(new PermutationInString().checkInclusion(s5, s6)); 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week3/StockSpanner.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week3; 2 | import java.util.Stack; 3 | 4 | /** 5 | * @author Shrinath Joshi 6 | * 7 | * Time complexity :- O(n) 8 | * Space complexity :- O(n) 9 | * 10 | */ 11 | 12 | 13 | 14 | class StockNode { 15 | int price; 16 | int span; 17 | 18 | public StockNode(int price, int span) { 19 | this.price = price; 20 | this.span = span; 21 | } 22 | 23 | } 24 | 25 | public class StockSpanner { 26 | private Stack stack; 27 | 28 | public StockSpanner() { 29 | stack = new Stack(); 30 | } 31 | 32 | public int next(int price) { 33 | int span = 1; 34 | 35 | while (!stack.isEmpty() && price >= stack.peek().price) { 36 | span += stack.peek().span; 37 | stack.pop(); 38 | } 39 | 40 | stack.add(new StockNode(price, span)); 41 | return span; 42 | } 43 | 44 | public static void main(String[] args) { 45 | 46 | StockSpanner sp = new StockSpanner(); 47 | System.out.println(sp.next(100)); 48 | System.out.println(sp.next(80)); 49 | System.out.println(sp.next(60)); 50 | System.out.println(sp.next(70)); 51 | System.out.println(sp.next(60)); 52 | System.out.println(sp.next(75)); 53 | System.out.println(sp.next(85)); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/ConstructBSTFromPreorderTraversal.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | /** 4 | * @author Shrinath Joshi Time complexity:- O(nlogn) Space complexity:- O(1) 5 | * 6 | */ 7 | 8 | class TreeNode { 9 | int val; 10 | TreeNode left; 11 | TreeNode right; 12 | 13 | TreeNode() { 14 | } 15 | 16 | TreeNode(int val) { 17 | this.val = val; 18 | } 19 | 20 | TreeNode(int val, TreeNode left, TreeNode right) { 21 | this.val = val; 22 | this.left = left; 23 | this.right = right; 24 | } 25 | } 26 | 27 | public class ConstructBSTFromPreorderTraversal { 28 | private TreeNode root; 29 | 30 | public TreeNode bstFromPreorder(int[] preorder) { 31 | 32 | int n = preorder.length; 33 | if (n == 0) 34 | return null; 35 | 36 | for (int node : preorder) 37 | buildBST(node); 38 | 39 | return root; 40 | } 41 | 42 | private void buildBST(int node) { 43 | 44 | if (root == null) { 45 | root = new TreeNode(node); 46 | return; 47 | } 48 | TreeNode current = root; 49 | while (current != null) { 50 | if (current.val > node) { 51 | // Left Subtree 52 | if (current.left == null) { 53 | current.left = new TreeNode(node); 54 | return; 55 | } else 56 | current = current.left; 57 | } else { 58 | // Right Subtree 59 | if (current.right == null) { 60 | current.right = new TreeNode(node); 61 | return; 62 | } else 63 | current = current.right; 64 | } 65 | } 66 | } 67 | 68 | public static void main(String[] args) { 69 | int preorder[] = { 8, 5, 1, 7, 10, 12 }; 70 | System.out.println(new ConstructBSTFromPreorderTraversal().bstFromPreorder(preorder)); 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/ContigiousArray.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * @author Shrinath Joshi 7 | * 8 | * Time complexity :- O(n) - all the elements are traversed only once 9 | * 10 | * Space complexity :- O(n) - additional space for storing count and 11 | * index, the maximum value for count in worst case will be n(i.e either 12 | * all 0 , or all 1) 13 | * 14 | */ 15 | 16 | public class ContigiousArray { 17 | public int findMaxLength(int[] nums) { 18 | int count = 0; 19 | int currentIndex = 0; 20 | int maxSize = 0; 21 | HashMap hash = new HashMap(); 22 | 23 | for (currentIndex = 0; currentIndex < nums.length; currentIndex++) { 24 | 25 | if (nums[currentIndex] == 1) 26 | count++; 27 | else 28 | count--; 29 | if (count == 0) { 30 | maxSize = currentIndex + 1; 31 | } 32 | if (hash.containsKey(count)) { 33 | int firstIndex = hash.get(count); 34 | if (maxSize < (currentIndex - firstIndex)) 35 | maxSize = currentIndex - firstIndex; 36 | } else 37 | hash.put(count, currentIndex); 38 | 39 | } 40 | return maxSize; 41 | 42 | } 43 | 44 | public static void main(String[] args) { 45 | int nums[] = { 0, 1, 0 }; 46 | System.out.println(new ContigiousArray().findMaxLength(nums)); 47 | 48 | int nums1[] = { 0, 1 }; 49 | System.out.println(new ContigiousArray().findMaxLength(nums1)); 50 | 51 | int nums2[] = { 0, 1, 0, 0, 1, 1, 0, 1, 0, 1 }; 52 | System.out.println(new ContigiousArray().findMaxLength(nums2)); 53 | 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/CountingBits.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | public class CountingBits { 4 | public int[] countBits(int num) { 5 | int result[] = new int[num + 1]; 6 | 7 | for (int i = 1; i <= num + 1; i++) 8 | result[i] = result[i / 2] + i % 2; 9 | 10 | return result; 11 | } 12 | 13 | public static void main(String[] args) { 14 | int[] result = new CountingBits().countBits(12); 15 | 16 | for (int i : result) 17 | System.out.print(i + " "); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/IntervalListIntersections.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class IntervalListIntersections { 7 | 8 | public int[][] intervalIntersection(int[][] A, int[][] B) { 9 | 10 | int a = 0; 11 | int b = 0; 12 | 13 | List result = new ArrayList(); 14 | 15 | while (a < A.length && b < B.length) { 16 | int a0 = A[a][0]; 17 | int a1 = A[a][1]; 18 | 19 | int b0 = B[b][0]; 20 | int b1 = B[b][1]; 21 | 22 | if (a0 <= b1 && a1 >= b0) { 23 | int u = Math.max(a0, b0); 24 | int v = Math.min(a1, b1); 25 | result.add(new int[] { u, v }); 26 | } else if (a0 >= b1 && a1 <= b0) { 27 | int u = Math.max(a0, b0); 28 | int v = Math.min(a1, b1); 29 | result.add(new int[] { u, v }); 30 | } 31 | 32 | if (a1 < b1) 33 | a++; 34 | else 35 | b++; 36 | } 37 | 38 | return result.toArray(new int[result.size()][]); 39 | } 40 | 41 | public static void main(String[] args) { 42 | 43 | int[][] A = { { 0, 2 }, { 5, 10 }, { 13, 23 }, { 24, 25 } }, 44 | B = { { 1, 5 }, { 8, 12 }, { 15, 24 }, { 25, 26 } }; 45 | 46 | int result[][] = new IntervalListIntersections().intervalIntersection(A, B); 47 | 48 | for (int i = 0; i < result.length; i++) { 49 | for (int j = 0; j < result[0].length; j++) { 50 | System.out.print(result[i][j] + " "); 51 | } 52 | System.out.println(); 53 | } 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/PossibleBipartition.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashMap; 5 | import java.util.List; 6 | import java.util.PriorityQueue; 7 | import java.util.Queue; 8 | 9 | /** 10 | * @author Shrinath Joshi 11 | * 12 | * Similar to Graph Bipartite 13 | * 14 | */ 15 | public class PossibleBipartition { 16 | public boolean possibleBipartition(int N, int[][] dislikes) { 17 | HashMap> adj = new HashMap<>(); 18 | HashMap color = new HashMap(); 19 | boolean visited[] = new boolean[N + 1]; 20 | 21 | // create adj list 22 | for (int i = 0; i < dislikes.length; i++) { 23 | int u = dislikes[i][0]; 24 | int v = dislikes[i][1]; 25 | List ad = null; 26 | if (adj.containsKey(u)) { 27 | ad = adj.get(u); 28 | } else 29 | ad = new ArrayList<>(); 30 | ad.add(v); 31 | adj.put(u, ad); 32 | } 33 | 34 | Queue queue = new PriorityQueue<>(); 35 | 36 | for (int i = 1; i <= N; i++) { 37 | if (!visited[i]) { 38 | color.put(i, "RED"); 39 | queue.add(i); 40 | 41 | while (!queue.isEmpty()) { 42 | int top = queue.poll(); 43 | if (visited[top]) 44 | continue; 45 | visited[top] = true; 46 | List adjList = adj.get(top); 47 | if (adjList != null) 48 | for (int v : adjList) { 49 | if (!visited[v]) { 50 | if (color.get(top).equals(color.get(v))) 51 | return false; 52 | if (color.get(top) == "RED") 53 | color.put(v, "BLUE"); 54 | else 55 | color.put(v, "RED"); 56 | 57 | queue.add(v); 58 | } 59 | } 60 | } 61 | 62 | } 63 | } 64 | 65 | return true; 66 | } 67 | 68 | public static void main(String[] args) { 69 | 70 | int N = 5, dislikes[][] = { { 1, 2 }, { 2, 3 }, { 3, 4 }, { 4, 5 }, { 1, 5 } }; 71 | System.out.println(new PossibleBipartition().possibleBipartition(N, dislikes)); 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/SortCharactersByFrequency.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | import java.util.HashMap; 4 | import java.util.PriorityQueue; 5 | 6 | /** 7 | * @author Shrinath Joshi 8 | * 9 | * Problem statement :- 10 | * https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3337/ 11 | * 12 | * Time complexity :- O(nlogn) Space complexity :-O(n) 13 | */ 14 | 15 | public class SortCharactersByFrequency { 16 | 17 | public String frequencySort(String s) { 18 | 19 | HashMap hash = new HashMap(); 20 | 21 | for (char c : s.toCharArray()) 22 | hash.put(c, hash.getOrDefault(c, 0) + 1); 23 | 24 | PriorityQueue pq = new PriorityQueue((a, b) -> hash.get(b) - hash.get(a)); 25 | pq.addAll(hash.keySet()); 26 | 27 | StringBuilder sb = new StringBuilder(); 28 | while (!pq.isEmpty()) { 29 | Character current = pq.remove(); 30 | int count = hash.get(current); 31 | while (count-- > 0) 32 | sb.append(current); 33 | } 34 | 35 | return sb.toString(); 36 | } 37 | 38 | public static void main(String[] args) { 39 | String str = "Aabb"; 40 | String str1 = "cccaaa"; 41 | 42 | System.out.println(new SortCharactersByFrequency().frequencySort(str)); 43 | System.out.println(new SortCharactersByFrequency().frequencySort(str1)); 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week4/UncrossedLines.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week4; 2 | 3 | /** 4 | * @author Shrinath Joshi 5 | * 6 | * Time complexity :- O(n*m) 7 | * Space complexity :- O(n*m) 8 | * 9 | */ 10 | public class UncrossedLines { 11 | 12 | public int maxUncrossedLines(int[] A, int[] B) { 13 | int aSize = A.length; 14 | int bSize = B.length; 15 | 16 | if (aSize == 0 || bSize == 0) 17 | return 0; 18 | 19 | int dp[][] = new int[aSize + 1][bSize + 1]; 20 | 21 | for (int i = 1; i < aSize + 1; i++) { 22 | for (int j = 1; j < bSize + 1; j++) { 23 | if (A[i - 1] == B[j - 1]) 24 | dp[i][j] = dp[i - 1][j - 1] + 1; 25 | else 26 | dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); 27 | } 28 | } 29 | 30 | return dp[aSize][bSize]; 31 | } 32 | 33 | public static void main(String[] args) { 34 | int A[] = { 2, 5, 1, 2, 5 }; 35 | int B[] = { 10, 5, 2, 1, 5, 2 }; 36 | System.out.println(new UncrossedLines().maxUncrossedLines(A, B)); 37 | 38 | int A1[] = { 1, 3, 7, 1, 7, 5 }; 39 | int B1[] = { 1, 9, 2, 5, 1 }; 40 | System.out.println(new UncrossedLines().maxUncrossedLines(A1, B1)); 41 | 42 | int A2[] = { 1, 1, 2, 1, 2 }; 43 | int B2[] = { 1, 3, 2, 3, 1 }; 44 | System.out.println(new UncrossedLines().maxUncrossedLines(A2, B2)); 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week5/CourseSchedule.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week5; 2 | 3 | import java.util.Stack; 4 | 5 | /** 6 | * @author Shrinath Joshi 7 | * 8 | * This question can be solved using Kahn's Algorithm 9 | * https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/ 10 | * 11 | * Time complexity:- O(V+E) Space complexity:- O(V) 12 | */ 13 | public class CourseSchedule { 14 | public boolean canFinish(int numCourses, int[][] prerequisites) { 15 | int[] inDegree = new int[numCourses]; 16 | int count = 0; 17 | 18 | for (int i = 0; i < prerequisites.length; i++) { 19 | inDegree[prerequisites[i][0]]++; 20 | } 21 | 22 | Stack stack = new Stack(); 23 | 24 | for (int i = 0; i < inDegree.length; i++) { 25 | if (inDegree[i] == 0) 26 | stack.push(i); 27 | } 28 | 29 | while (!stack.isEmpty()) { 30 | int curr = stack.pop(); 31 | count++; 32 | for (int i = 0; i < prerequisites.length; i++) { 33 | if (prerequisites[i][1] == curr) { 34 | inDegree[prerequisites[i][0]]--; 35 | if (inDegree[prerequisites[i][0]] == 0) 36 | stack.push(prerequisites[i][0]); 37 | } 38 | 39 | } 40 | } 41 | return count == numCourses; 42 | } 43 | 44 | public static void main(String[] args) { 45 | int numCourses = 2, prerequisites[][] = { { 1, 0 }, { 0, 1 } }; 46 | System.out.println(new CourseSchedule().canFinish(numCourses, prerequisites)); 47 | 48 | int numCourses1 = 2, prerequisites1[][] = { { 1, 0 } }; 49 | System.out.println(new CourseSchedule().canFinish(numCourses1, prerequisites1)); 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week5/EditDistance.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week5; 2 | 3 | /** 4 | * @author Shrinath Joshi 5 | * 6 | * Time Complexity :- O(n*m) 7 | * 8 | * Space Complexity :- O(n*m) 9 | * 10 | */ 11 | 12 | public class EditDistance { 13 | 14 | // Bottom Up Approach 15 | public int minDistance(String word1, String word2) { 16 | int len1 = word1.length(); 17 | int len2 = word2.length(); 18 | 19 | int dp[][] = new int[len1 + 1][len2 + 1]; 20 | for (int i = 0; i <= len1; i++) 21 | dp[i][0] = i; 22 | 23 | for (int i = 0; i <= len2; i++) 24 | dp[0][i] = i; 25 | 26 | for (int i = 1; i <= len1; i++) { 27 | for (int j = 1; j <= len2; j++) { 28 | if (word1.charAt(i - 1) == word2.charAt(j - 1)) 29 | dp[i][j] = dp[i - 1][j - 1]; 30 | else 31 | dp[i][j] = 1 + Math.min(dp[i - 1][j], Math.min(dp[i - 1][j - 1], dp[i][j - 1])); 32 | } 33 | } 34 | return dp[len1][len2]; 35 | 36 | } 37 | 38 | public static void main(String[] args) { 39 | 40 | String word1 = "intention", word2 = "execution"; 41 | System.out.println(new EditDistance().minDistance(word1, word2)); 42 | 43 | String word3 = "horse", word4 = "ros"; 44 | System.out.println(new EditDistance().minDistance(word3, word4)); 45 | 46 | String word5 = "", word6 = "r"; 47 | System.out.println(new EditDistance().minDistance(word5, word6)); 48 | 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /May Challenge/com/leetcode/MayChallenge/week5/KClosestPointsToOrigin.java: -------------------------------------------------------------------------------- 1 | package com.leetcode.MayChallenge.week5; 2 | 3 | import java.util.Comparator; 4 | import java.util.PriorityQueue; 5 | 6 | /* 7 | Time complexity :- O((n-k)logk) , n is the number of points 8 | Space complexity :- O(n) , n is the number of points 9 | 10 | */ 11 | 12 | public class KClosestPointsToOrigin { 13 | public int[][] kClosest(int[][] points, int K) { 14 | PriorityQueue maxHeap = new PriorityQueue(new PointComparator()); 15 | for (int[] i : points) { 16 | maxHeap.add(i); 17 | if (maxHeap.size() > K) 18 | maxHeap.poll(); 19 | } 20 | 21 | int result[][] = new int[K][2]; 22 | for (int i = 0; i < K; i++) { 23 | int res[] = maxHeap.poll(); 24 | result[i][0] = res[0]; 25 | result[i][1] = res[1]; 26 | } 27 | 28 | return result; 29 | } 30 | 31 | public static void main(String[] args) { 32 | int points[][] = { { 3, 3 }, { 5, -1 }, { -2, 4 } }, K = 2; 33 | int[][] result = new KClosestPointsToOrigin().kClosest(points, K); 34 | 35 | for (int i = 0; i < K; i++) { 36 | System.out.println("" + result[i][0] + result[i][1]); 37 | } 38 | } 39 | 40 | } 41 | 42 | class PointComparator implements Comparator { 43 | 44 | @Override 45 | public int compare(int[] p1, int[] p2) { 46 | return Double.compare(dist(p2), dist(p1)); 47 | } 48 | 49 | private double dist(int[] p) { 50 | return (p[0] * p[0] + p[1] * p[1]); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /November Challenge/FindSmallestDivisorGivenThreshold.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Shrinath Joshi 3 | * 4 | * Problem Statement 5 | * https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3521/ 6 | * 7 | */ 8 | public class FindSmallestDivisorGivenThreshold { 9 | public int smallestDivisor(int[] nums, int threshold) { 10 | int left = 1, right = (int) 1e6; 11 | 12 | while (left < right) { 13 | int m = (left + right) / 2; 14 | 15 | int sum = 0; 16 | for (int num : nums) 17 | sum += (num + m - 1) / m; 18 | 19 | // System.out.println("Sum "+sum+" when m "+m +" with left "+ left + " and right 20 | // "+right); 21 | if (sum <= threshold) 22 | right = m; 23 | else 24 | left = m + 1; 25 | } 26 | 27 | return left; 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /November Challenge/FlippingAnImage.java: -------------------------------------------------------------------------------- 1 | /** 2 | * @Author Shrinath Joshi 3 | * 4 | * Problem Statement:- 5 | * https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/565/week-2-november-8th-november-14th/3526/ 6 | * 7 | * Time complexity:- O(n2) Space complexity :- O(n2) 8 | * 9 | */ 10 | 11 | class FlippingAnImage { 12 | public int[][] flipAndInvertImage(int[][] A) { 13 | int column = A[0].length; 14 | 15 | for (int[] row : A) { 16 | for (int i = 0; i < (column + 1) / 2; i++) { 17 | 18 | if (row[i] == row[column - i - 1]) 19 | row[i] = row[column - i - 1] ^= 1; 20 | } 21 | } 22 | 23 | return A; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /November Challenge/PermutationTwo.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Arrays; 3 | import java.util.List; 4 | 5 | /** 6 | * @author Shrinath Joshi 7 | * 8 | * Problem Statement 9 | * :-https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/565/week-2-november-8th-november-14th/3528/ 10 | */ 11 | public class PermutationTwo { 12 | 13 | public List> permuteUnique(int[] nums) { 14 | Arrays.sort(nums); 15 | List> result = new ArrayList<>(); 16 | boolean used[] = new boolean[nums.length]; 17 | getPermutation(0, new ArrayList(), result, nums, used); 18 | return result; 19 | } 20 | 21 | private void getPermutation(int index, ArrayList partialSolution, List> result, int nums[], 22 | boolean used[]) { 23 | 24 | if (index > nums.length) 25 | return; 26 | 27 | if (partialSolution.size() == nums.length) { 28 | result.add(new ArrayList(partialSolution)); 29 | return; 30 | } 31 | 32 | for (int i = 0; i < nums.length; i++) { 33 | if (used[i] || i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) 34 | continue; 35 | 36 | used[i] = true; 37 | partialSolution.add(nums[i]); 38 | getPermutation(i + 1, partialSolution, result, nums, used); 39 | used[i] = false; 40 | partialSolution.remove(partialSolution.size() - 1); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /October Challenge/week1/day1/numberOfRecentCalls.cpp: -------------------------------------------------------------------------------- 1 | 2 | class RecentCounter { 3 | public: 4 | queue q; 5 | RecentCounter() { 6 | } 7 | 8 | int ping(int t) { // For one call to ping() worst case TC: O(N) but overall complexity of program does not exceed O(N) 9 | q.push(t); 10 | while (!q.empty() && q.front() + 3000 < t) // Remove all time units that are less than t-3000 11 | q.pop(); 12 | 13 | return q.size(); 14 | } 15 | 16 | }; 17 | 18 | /** 19 | * Your RecentCounter object will be instantiated and called as such: 20 | * RecentCounter* obj = new RecentCounter(); 21 | * int param_1 = obj->ping(t); 22 | */ 23 | -------------------------------------------------------------------------------- /October Challenge/week1/day2/combinationSum.cpp: -------------------------------------------------------------------------------- 1 | // TC: O(2^N) 2 | // SC: O(N^2) 3 | 4 | class Solution { 5 | public: 6 | 7 | 8 | void helper(vector> &ans, vector &v, int t, vector& can, int i=0, int sum=0) { 9 | if (sum==t) { 10 | ans.push_back(v); 11 | return; 12 | } 13 | if (i==can.size() || sum>t) 14 | return; 15 | 16 | int x=0; 17 | while (sum <= t) { 18 | helper(ans, v, t, can, i+1, sum); 19 | v.push_back(can[i]); 20 | sum += can[i]; 21 | x++; 22 | } 23 | while (x--) v.pop_back(); 24 | } 25 | 26 | vector> combinationSum(vector& candidates, int target) { 27 | vector> ans; 28 | vector v; 29 | sort(candidates.begin(), candidates.end()); 30 | helper(ans, v, target, candidates); 31 | return ans; 32 | } 33 | }; 34 | -------------------------------------------------------------------------------- /October Challenge/week1/day3/KDiffPairsInArray.java: -------------------------------------------------------------------------------- 1 | package week1.day3; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class KDiffPairsInArray { 8 | 9 | public int findPairs(int[] nums, int k) { 10 | 11 | // return findPairsUsingHashMap(nums, k); 12 | return findPairsUsingTwoPointer(nums, k); 13 | } 14 | 15 | private int findPairsUsingTwoPointer(int[] nums, int k) { 16 | // Time Complexity :- O(nlogn) 17 | // Space Complexity :- O(1) 18 | 19 | int result = 0; 20 | 21 | Arrays.sort(nums); 22 | int left = 0; 23 | int right = 0; 24 | 25 | for (left = 0, right = 0; left < nums.length; left++) { 26 | 27 | for (right = left + 1; right < nums.length && nums[right] - nums[left] < k; right++) 28 | ; 29 | if (right < nums.length && nums[right] - nums[left] == k) 30 | result++; 31 | while (left < nums.length - 1 && nums[left] == nums[left + 1]) 32 | left++; 33 | } 34 | 35 | return result; 36 | } 37 | 38 | public int findPairsUsingHashMap(int[] nums, int k) { 39 | 40 | // Time Complexity :- O(n) 41 | // Space Complexity :- O(n) 42 | 43 | int result = 0; 44 | 45 | Map map = new HashMap<>(); 46 | for (int num : nums) 47 | map.put(num, map.getOrDefault(num, 0) + 1); 48 | 49 | for (Integer num : map.keySet()) { 50 | if (k == 0 && map.get(num) > 2) { 51 | result++; 52 | } 53 | if (k > 0 && map.containsKey(num + k)) 54 | result++; 55 | } 56 | return result; 57 | } 58 | 59 | public static void main(String[] args) { 60 | int nums[] = { 1, 2, 4, 4, 3, 3, 0, 9, 2, 3 }, k = 3; 61 | 62 | System.out.println(new KDiffPairsInArray().findPairs(nums, k)); 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /October Challenge/week1/day4/removeCoveredIntervals.cpp: -------------------------------------------------------------------------------- 1 | // Basic implementation of Activity Selection problem (Greedy Algo) 2 | // TC: O(nlogn) 3 | // SC: O(n) 4 | 5 | class Solution { 6 | public: 7 | static bool comp(vector a, vector b) { // Comparator for sorting vector 8 | if (a[0]==b[0]) return a[1]>b[1]; 9 | return a[0]>& intervals) { 13 | sort(intervals.begin(), intervals.end(), comp); 14 | 15 | int l=0, r=-1; 16 | int ans=intervals.size(); 17 | for (auto i : intervals) { 18 | if (i[0]>=l && i[1]<=r) 19 | ans--; 20 | else 21 | l = i[0], r = i[1]; 22 | 23 | } 24 | return ans; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /October Challenge/week1/day5/complementOfBase10Integer.cpp: -------------------------------------------------------------------------------- 1 | // TC: O(logN) 2 | // SC: O(1) 3 | 4 | class Solution { 5 | public: 6 | int bitwiseComplement(int N) { 7 | if (N==0) 8 | return 1; 9 | int maxVal=1; 10 | while (maxVal<=N) 11 | maxVal = maxVal<<1; 12 | 13 | return maxVal-1-N; // To find complement of 5(101) -> 5 is subtracted from 7(111) resulting in 2(010) 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /October Challenge/week1/day6/insertIntoBST.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | //BST definition (nodeVal smaller than root is in left sub-tree and bigger is in right sub-tree. 15 | // TC: O(N) 16 | 17 | TreeNode* insertIntoBST(TreeNode* root, int val) { 18 | if (!root) return new TreeNode(val); 19 | 20 | if (root->val > val) 21 | root-> left = insertIntoBST(root->left, val); 22 | else 23 | root-> right = insertIntoBST(root->right, val); 24 | return root; 25 | } 26 | }; 27 | -------------------------------------------------------------------------------- /October Challenge/week3/day15/RotateArray.java: -------------------------------------------------------------------------------- 1 | package week3.day15; 2 | 3 | /** 4 | * @author Shrinath Joshi 5 | * 6 | * Problem Statement :- 7 | * https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3496/ 8 | * 9 | */ 10 | public class RotateArray { 11 | // Time Complexity :- O(N) 12 | // Space Complexity :- O(1) 13 | 14 | public void rotate(int[] nums, int k) { 15 | int n = nums.length; 16 | k = k % n; 17 | if (k < 0) 18 | k = k + n; 19 | reverse(nums, 0, n - k - 1); 20 | reverse(nums, n - k, n - 1); 21 | reverse(nums, 0, n - 1); 22 | } 23 | 24 | private void reverse(int nums[], int start, int end) { 25 | 26 | while (start < end) { 27 | int temp = nums[start]; 28 | nums[start] = nums[end]; 29 | nums[end] = temp; 30 | 31 | start++; 32 | end--; 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /October Challenge/week3/day17/RepeatedDNASequences.java: -------------------------------------------------------------------------------- 1 | package week3.day17; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashSet; 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | public class RepeatedDNASequences { 9 | // Time Complexity :- O(N) 10 | // Space Complexity :- O(N) 11 | 12 | public List findRepeatedDnaSequences(String s) { 13 | 14 | Set seen = new HashSet(); 15 | Set result = new HashSet(); 16 | 17 | for (int i = 0; i + 9 < s.length(); i++) { 18 | String temp = s.substring(i, i + 10); 19 | if (!seen.add(temp)) { 20 | result.add(temp); 21 | } 22 | } 23 | 24 | return new ArrayList<>(result); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Leetcode-30-day-challenge 2 | 3 | ### May Challenge ### 4 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | Ideal Time to Solve| 5 | |---|---|---|---|---|---|---|---|---| 6 | | 1 | [First Bad Version](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3316/)| [First Bad Version](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/FirstBadVersion.java) | Easy | Array | Binary Search | O(logn) | O(1) | 15-30 min | 7 | | 2 | [Jewels and Stones](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3317/)| [Jewels and Stones](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/JewelsAndStones.java) | Easy | HashMap | frequency counting | O(n) | O(n) | 15-30 min | 8 | | 3 | [Ransom Note](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3318/)| [Ransome Note ](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/RansomNote.java) | Easy | Array | Char Frequency Counting | O(n) | O(1) | 15-30 min | 9 | | 4 | [ Number Complement](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3319/) | [ Number Complement](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/NumberComplement.java) | Easy | NA | Bit Manipulation | O(log(v) , v= value of number | O(1) |15-30 min | 10 | | 5 | [First Unique Character in a String](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3320/) | [First Unique Character in a String](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/FirstUniqueCharacterInString.java) | Easy | HashMap | Frequency Counting | O(n) | O(n) | 10 min | 11 | | 6 | [Majority Element](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3321/) | [Majority Element](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/MajorityElement.java) | Easy | NA | Boyer Moore Algorithm | O(n) | O(1) | 30-45 min | 12 | | 7 | [Cousins in Binary Tree](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/534/week-1-may-1st-may-7th/3322/) | [Cousins in Binary Tree](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week1/CousinsInBinaryTree.java) | Easy | NA | NA | O(n) | O(log(n)) Resursive stack | 15-30 min | 13 | | 8 | [Check If It Is a Straight Line](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3323/) | [ Check If It Is a Straight Line](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/CheckIfAStraightLine.java) | Easy | NA | Slope of a line | O(n) | O(1) | 15-20 min | 14 | | 9 | [ Valid Perfect Square](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3324/) | [ Valid Perfect Square]() | Easy | NA | Binary Search | O(logn) | O(1) | 15-30 min | 15 | | 10 | [Find the Town Judge](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3325/) | [Find the Town Judge](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/FindTownJudge.java) | Easy | NA | NA | O(n) | O(n) | 15-30min | 16 | | 11 | [Flood Fill](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3326/) | [Flood Fill](github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/FloodFill.java) | Easy | Graph | Graph Traversal BFS/DFS | O(n) | O(n) | 30 min | 17 | | 12 | [Single Element in a Sorted Array](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3327/) | [Single Element in a Sorted Array](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/SingleElementInSortedArray.java) | Easy | NA | Two pointer Algo | O(logn) | O(1) | 15-30 min | 18 | | 13 | [ Remove K Digits](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3328/) | [ Remove K Digits](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/RemoveKDigits.java) | Easy | NA | Greedy Algorithm | O(kn) | O(1) | 30-45 min | 19 | | 14 | [ Implement Trie (Prefix Tree)](leetcode.com/explore/featured/card/may-leetcoding-challenge/535/week-2-may-8th-may-14th/3329/) | [ Implement Trie (Prefix Tree)](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week2/Trie.java) | Medium | Trie | NA | O(m) | O(m) | 30-45 min | 20 | | 15 | [ Maximum Sum Circular Subarray](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3330/) | [ Maximum Sum Circular Subarray](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/MaximumSumCircularSubarray.java) | Medium | NA | Kadane's Algorithm | O(n) | O(1) | 45 min | 21 | | 16 | [Odd Even Linked List](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3331/) | [Odd Even Linked List](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/OddEvenLinkedList.java) | Easy | Linked List | NA | O(n) | O(1) | 15-30 min | 22 | | 17 | [ Find All Anagrams in a String](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3332/) | [ Find All Anagrams in a String](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/FindAllAnagramsInString.java) | Medium | NA | Sliding Window | O(n) | O(1) | 45 min | 23 | | 18 | [ Permutation in String](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3333/) | [ Permutation in String](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/PermutationInString.java) | Easy | NA | Sliding Window | O(n) | O(1) | 30min | 24 | | 19 | [Online Stock Span](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3334/) | [Online Stock Span](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/StockSpanner.java) | Easy | Stack | NA | O(n) | O(n) | 30min | 25 | | 20 | [Kth Smallest Element in a BST](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3335/) | [Kth Smallest Element in a BST](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/KthSmallestElementInBST.java) | Medium | NA | Modified Inorder Traversal | O(n) | O(n) | 30min | 26 | | 21 | [Count Square Submatrices with All Ones](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/536/week-3-may-15th-may-21st/3336/ ) | [Count Square Submatrices with All Ones](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week3/CountSquareSubmatricesWithAllOnes.java) | Medium | NA | Dynamic Programming | O(n*m) | O(n*m) | 45min | 27 | | 22 | [ Sort Characters By Frequency](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3337/) | [ Sort Characters By Frequency](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/SortCharactersByFrequency.java) | Easy | HashMap, Priority Queue | NA | O(nlogn) | O(n) | 30min | 28 | | 23 | [Interval List Intersections](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3338/) | [Interval List Intersections](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/IntervalListIntersections.java) | Easy | NA | NA | O(n) | O(n) | 30min | 29 | | 24 | [Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3339/) | [Construct Binary Search Tree from Preorder Traversal](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/ConstructBSTFromPreorderTraversal.java) | Medium | NA | NA | O(nlog) | O(1) | 45min | 30 | | 25 | [Uncrossed Lines](https://leetcode.com/explore/challenge/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3340/) | [Uncrossed Lines](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/UncrossedLines.java) | Medium | NA | Dynamic Programming | O(nm) n and m are size of Array A & B | O(nm) n and m are size of Array A & B | 45 min | 31 | | 26 | [ Contiguous Array](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3341/) | [ Contiguous Array](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/ContigiousArray.java) | Medium | HashMap | NA | O(n) | O(n) | 45 min | 32 | | 27* | [Possible Bipartition](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3342/) | [Possible Bipartition](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/PossibleBipartition.java) | Medium | NA | Graph Colouring | O(V+E) | O(V) | 1 hr | 33 | | 28 | [Counting Bits](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/537/week-4-may-22nd-may-28th/3343/) | [Counting Bits](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week4/CountingBits.java) | Easy | NA | Bit Manipulation | O(n) | O(n) | 30min | 34 | | 29 | [Course Schedule](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/538/week-5-may-29th-may-31st/3344/) | [Course Schedule](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week5/CourseSchedule.java) | Medium | NA | [Kahn's Algorithm](https://www.geeksforgeeks.org/topological-sorting-indegree-based-solution/) | O(V+E) | O(V) | 45 min | 35 | | 30 | [K Closest Points to Origin](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/538/week-5-may-29th-may-31st/3345/) | [K Closest Points to Origin](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week5/KClosestPointsToOrigin.java) | Easy | Priority Queue | NA | O((n-k)logk) , n is the number of points | O(n) , n is the number of points | 30min | 36 | | 31 | [Edit Distance](https://leetcode.com/explore/featured/card/may-leetcoding-challenge/538/week-5-may-29th-may-31st/3346/) | [ Edit Distance](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/May%20Challenge/com/leetcode/MayChallenge/week5/EditDistance.java) | Hard | NA | String Edit Distance | O(m*n) where m and n are length of two string | O(m*n) where m and n are length of two string | 45 min | 37 | | | | | | | | | | | 38 | 39 | 40 | ### June Challenge ### 41 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | 42 | |---|---|---|---|---|---|---|---| 43 | | 1 | [Invert Binary Tree](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3347/) | [Invert Binary Tree](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/June%20Challenge/com/leetcode/JuneChallenge/week1/InvertBinaryTree.java) | Easy | Binary Tree | NA | O(n) | O(n) | 44 | | 2 | [Delete A Node in a LinkedList](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3348/) | [Delete A Node in a LinkedList](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/June%20Challenge/com/leetcode/JuneChallenge/week1/DeleteNodeInALinkedList.java) | Easy | LinkedList | NA | O(1) | O(1) | 45 | | 3* | [Two City Scheduling](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3349/) | [Two City Scheduling](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/June%20Challenge/com/leetcode/JuneChallenge/week1/TwoCityScheduling.java) | Easy | Priority Queue | NA | O(nlogn) | O(n) | 46 | | 4 | [Reverse String](https://leetcode.com/explore/challenge/card/june-leetcoding-challenge/539/week-1-june-1st-june-7th/3350/) | [Reverse String](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/June%20Challenge/com/leetcode/JuneChallenge/week1/ReverseString.java) | Easy | NA | Two Pointer | O(n) | O(1) | 47 | | | | | | | | | | 48 | | | | | | | | | | 49 | | | | | | | | | | 50 | | | | | | | | | | 51 | | | | | | | | | | 52 | | | | | | | | | | 53 | | | | | | | | | | 54 | 55 | 56 | ### July Challenge ### 57 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | Additional Reading 58 | |---|---|---|---|---|---|---|---|---| 59 | | 4 | [Hamming Distance](leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [Hamming Distance](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/July%20Challenge/com/leetcode/JulyChallenge/week1/HammingDistance.java) | Easy | NA | Bit Manipulation | O(1) | O(1) | https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/ | 60 | | 5 | [Plus One](leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [Plus One](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/July%20Challenge/com/leetcode/JulyChallenge/week1/PlusOne.java) | Easy | NA | NA | O(N) | O(1) | [Back To Back SWE](https://www.youtube.com/watch?v=vA0t42qwKO0) | 61 | 62 | ### August Challenge ### 63 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | Additional Reading 64 | |---|---|---|---|---|---|---|---|---| 65 | | 1 | [Hamming Distance](leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [Hamming Distance](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/July%20Challenge/com/leetcode/JulyChallenge/week1/HammingDistance.java) | Easy | NA | Bit Manipulation | O(1) | O(1) | https://www.techiedelight.com/brian-kernighans-algorithm-count-set-bits-integer/ | 66 | | 2 | [Plus One](leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [Plus One](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/July%20Challenge/com/leetcode/JulyChallenge/week1/PlusOne.java) | Easy | NA | NA | O(N) | O(1) | [Back To Back SWE](https://www.youtube.com/watch?v=vA0t42qwKO0) | 67 | | 3 | [Valid Palindrome](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3411/) | [Plus One](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/August%20Challenge/com/leetcode/AugustChallenge/week1/ValidPalindrome.java) | Easy | NA | NA | O(N) | O(1) | | 68 | | 4 | [Plus One](leetcode.com/explore/challenge/card/july-leetcoding-challenge/544/week-1-july-1st-july-7th/3381/) | [Plus One](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/July%20Challenge/com/leetcode/JulyChallenge/week1/PlusOne.java) | Easy | NA | NA | O(N) | O(1) | [Back To Back SWE](https://www.youtube.com/watch?v=vA0t42qwKO0) | 69 | 70 | ### September Challenge 71 | 72 | 73 | ### October Challenge 74 | 75 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | Additional Reading 76 | |---|---|---|---|---|---|---|---|---| 77 | | 15 | [189. Rotate Array](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3496/) | [Java](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/October%20Challenge/week3/day15/RotateArray.java) | Easy | String | | O(N) | O(1) | | 78 | | 17 | [187. Repeated DNA Sequences](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/561/week-3-october-15th-october-21st/3498/) | [Java](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/October%20Challenge/week3/day17/RepeatedDNASequences.java) | Easy | String | | O(N) | O(N) | | 79 | 80 | 81 | ### November Challenge 82 | 83 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | Additional Reading 84 | |---|---|---|---|---|---|---|---|---| 85 | | 6 | [1283. Find the Smallest Divisor Given a Threshold](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/564/week-1-november-1st-november-7th/3521/) | [Java](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/November%20Challenge/FindSmallestDivisorGivenThreshold.java) | Medium | | Binary Search | O(NlogM) | O(1) | [Discuss](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/446376/JavaC%2B%2BPython-Binary-Search) [Discuss](https://leetcode.com/problems/find-the-smallest-divisor-given-a-threshold/discuss/777019/Python-Clear-explanation-Powerful-Ultimate-Binary-Search-Template.-Solved-many-problems)[Binary Search 101](https://leetcode.com/problems/binary-search/discuss/423162/Binary-Search-101)| 86 | | 12 | [47. Permutations II](https://leetcode.com/explore/challenge/card/november-leetcoding-challenge/565/week-2-november-8th-november-14th/3528/) | [Java](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/November%20Challenge/PermutationTwo.java) | Medium | Backtracing | | | | [Discuss](https://leetcode.com/problems/permutations/discuss/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning))| 87 | 88 | 89 | 90 | 91 | 92 | ### Daily Coding Challenge ### 93 | | Day | Problem Statement | Solution | Difficulty | Data Structure Used | Algorithm used | Time Complexity | Space Complexity | 94 | |---|---|---|---|---|---|---|---| 95 | | 1 | [442. Find All Duplicates in an Array](https://leetcode.com/problems/find-all-duplicates-in-an-array/) | [Java](https://github.com/shrinathjoshi/Leetcode-30-day-challenge/blob/master/Daily%20Coding%20Challenge/FindAllDuplicatesInAnArray.java) | Medium | NA | NA | O(n) |O(1) | 96 | | 2 | [863. All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | | | | | | | 97 | | 3 | [938. Range Sum of BST](https://leetcode.com/problems/range-sum-of-bst/) | | | | | | | 98 | | 4 | [863. All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | | | | | | | 99 | | 5 | [863. All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | | | | | | | 100 | | 6 | [863. All Nodes Distance K in Binary Tree](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | | | | | | | 101 | 102 | 103 | ### If this repository was helpful to you then do star this repository, Also please feel free to contribute to this repo. Contibutions are always welcomed ### 104 | ### You can connect me on [LinkedIn](https://www.linkedin.com/in/shrinathjoshi/) ### 105 | 106 | 107 | 108 | 109 | -------------------------------------------------------------------------------- /September Challenge/week2/Day13_InsertInterval.java: -------------------------------------------------------------------------------- 1 | package week2; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | public class Day13_InsertInterval { 7 | public int[][] insert(int[][] intervals, int[] newInterval) { 8 | 9 | List result = new ArrayList<>(); 10 | int n = intervals.length; 11 | int i = 0; 12 | while (i < n && intervals[i][1] < newInterval[0]) { 13 | result.add(intervals[i++]); 14 | } 15 | 16 | while (i < n && intervals[i][0] <= newInterval[1]) { 17 | newInterval[0] = Integer.min(intervals[i][0], newInterval[0]); 18 | newInterval[1] = Integer.max(intervals[i][1], newInterval[1]); 19 | i++; 20 | } 21 | 22 | result.add(newInterval); 23 | 24 | while (i < n) { 25 | result.add(intervals[i++]); 26 | } 27 | 28 | return result.toArray(new int[result.size()][]); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 4 | 4.0.0 5 | LeetCode-30-Day-Challenge 6 | LeetCode-30-Day-Challenge 7 | 0.0.1-SNAPSHOT 8 | 9 | src 10 | 11 | 12 | maven-compiler-plugin 13 | 3.8.0 14 | 15 | 13 16 | 17 | 18 | 19 | 20 | maven-surefire-plugin 21 | 2.19 22 | 23 | 24 | org.junit.platform 25 | junit-platform-surefire-provider 26 | 1.0.0-M4 27 | 28 | 29 | org.junit.vintage 30 | junit-vintage-engine 31 | 4.12.0-M4 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | UTF-8 40 | 1.8 41 | ${maven.compiler.source} 42 | 5.5.2 43 | 1.5.2 44 | 45 | 46 | 47 | 48 | org.junit.jupiter 49 | junit-jupiter-engine 50 | ${junit.jupiter.version} 51 | test 52 | 53 | 54 | org.junit.platform 55 | junit-platform-runner 56 | ${junit.platform.version} 57 | test 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /target/classes/META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Built-By: Shrinath 3 | Build-Jdk: 13.0.2 4 | Created-By: Maven Integration for Eclipse 5 | 6 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/LeetCode-30-Day-Challenge/LeetCode-30-Day-Challenge/pom.properties: -------------------------------------------------------------------------------- 1 | #Generated by Maven Integration for Eclipse 2 | #Sat May 02 22:06:38 IST 2020 3 | m2e.projectLocation=C\:\\Users\\Shrinath\\Desktop\\Leetcode-challenge 4 | m2e.projectName=Leetcode-challenge 5 | groupId=LeetCode-30-Day-Challenge 6 | artifactId=LeetCode-30-Day-Challenge 7 | version=0.0.1-SNAPSHOT 8 | -------------------------------------------------------------------------------- /target/classes/META-INF/maven/LeetCode-30-Day-Challenge/LeetCode-30-Day-Challenge/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4.0.0 3 | LeetCode-30-Day-Challenge 4 | LeetCode-30-Day-Challenge 5 | 0.0.1-SNAPSHOT 6 | 7 | src 8 | 9 | 10 | maven-compiler-plugin 11 | 3.8.0 12 | 13 | 13 14 | 15 | 16 | 17 | 18 | --------------------------------------------------------------------------------