├── .gitattributes ├── .gitignore ├── .idea ├── .gitignore ├── compiler.xml ├── description.html ├── encodings.xml ├── misc.xml ├── modules.xml ├── project-template.xml ├── uiDesigner.xml └── vcs.xml ├── contributing.md ├── leetcode.iml ├── license ├── readme.md └── src └── com └── company ├── Algorithms └── easy │ └── TwoSum │ └── Solution.java ├── Main.java └── thirty_day_leetcoding_challenge ├── week1 ├── BestTimeToBuyAndSellStockII │ └── Solution.java ├── CountingElements │ └── Solution.java ├── GroupAnagrams │ └── Solution.java ├── HappyNumber │ └── Solution.java ├── MaximumSubarray │ └── Solution.java ├── MoveZeroes │ └── Solution.java └── SingleNumber │ └── Solution.java ├── week2 ├── BackspaceStringCompare │ └── Solution.java ├── ContiguousArray │ └── Solution.java ├── DiameterOfBinaryTree │ └── Solution.java ├── LastStoneWeight │ └── Solution.java ├── MiddleOfTheLinkedList │ └── Solution.java ├── MinStack │ └── Solution.java └── PerformStringShifts │ └── Solution.java ├── week3 ├── ConstructBinarySearchTreeFromPreorderTraversal │ └── Solution.java ├── LeftMostColumnWithAtLeastAOne │ └── Solution.java ├── MinimumPathSum │ └── Solution.java ├── NumberOfIslands │ └── Solution.java ├── ProductOfArrayExceptSelf │ └── Solution.java ├── SearchInRotatedSortedArray │ └── Solution.java └── ValidParenthesisString │ └── Solution.java └── week4 ├── BitwiseANDofNumbersRange └── Solution.java ├── JumpGame └── Solution.java ├── LRUCache └── Solution.java └── SubarraySumEqualsK └── Solution.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /workspace.xml -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/description.html: -------------------------------------------------------------------------------- 1 | Simple Java application that includes a class with main() method -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/project-template.xml: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /contributing.md: -------------------------------------------------------------------------------- 1 | ### Pull Requests / Contributions 2 | 3 | this repo is currently accepting contributions or pull requests. 4 | 5 | ### Adding a solution 6 | 7 | If you want to add a solution, feel free to fork the repo and add the solution in your new forked repo and make pull request. 8 | -------------------------------------------------------------------------------- /leetcode.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Abdelrahman Saed 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 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |
for coding interview preparation 6 |

7 | 8 | * [30-day-leetcoding-challenge](#30-day-leetcoding-challenge) 9 | * [Algorithms](#Algorithms) 10 | 11 | 12 | # 30-day-leetcoding-challenge 13 | 14 | | week | Challenge | Solution | 15 | |:---:|:----------------------------------------------------------------------------------------------------:|:------------------------------------------------------------------------------------------------------------------------------------------------------------:| 16 | | 1 | [Single Number](https://leetcode.com/explore/other/card/30-day-leetcoding-challenge/528/week-1/3283/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/SingleNumber/Solution.java) | 17 | | 1 | [Happy Number](https://leetcode.com/explore/other/card/30-day-leetcoding-challenge/528/week-1/3284/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/HappyNumber/Solution.java) | 18 | | 1 | [Maximum Subarray](https://leetcode.com/explore/other/card/30-day-leetcoding-challenge/528/week-1/3285/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/MaximumSubarray/Solution.java) | 19 | | 1 | [Move Zeroes](https://leetcode.com/explore/other/card/30-day-leetcoding-challenge/528/week-1/3286/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/MoveZeroes/Solution.java) | 20 | | 1 | [Best Time to Buy and Sell Stock II](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3287/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/BestTimeToBuyAndSellStockII/Solution.java) | 21 | | 1 | [Group Anagrams](https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/528/week-1/3288/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/GroupAnagrams/Solution.java) | 22 | | 1 | [Counting Elements](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/528/week-1/3289/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week1/CountingElements/Solution.java) | 23 | |---| ------------------------|-----| 24 | | 2 | [Middle of the Linked List](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/MiddleOfTheLinkedList/Solution.java) | 25 | | 2 | [Backspace String Compare](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3291/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/BackspaceStringCompare/Solution.java) | 26 | | 2 | [Min Stack](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3292/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/DiameterOfBinaryTree/Solution.java) | 27 | | 2 | [Diameter of Binary Tree](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3293/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/MinStack/Solution.java) | 28 | | 2 | [Last Stone Weight](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3297/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/LastStoneWeight/Solution.java) | 29 | | 2 | [Contiguous Array](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3298/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/ContiguousArray/Solution.java) | 30 | | 2 | [Perform String Shifts](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3299/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week2/PerformStringShifts/Solution.java) | 31 | |---| ------------------------|-----| 32 | | 3 | [Product of Array Except Self](https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3300/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/ProductOfArrayExceptSelf/Solution.java) | 33 | | 3 | [Valid Parenthesis String](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3301/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/ValidParenthesisString/Solution.java) | 34 | | 3 | [Number of Islands](https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3302/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/NumberOfIslands/Solution.java) | 35 | | 3 | [Minimum Path Sum](https://leetcode.com/explore/featured/card/30-day-leetcoding-challenge/530/week-3/3303/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/MinimumPathSum/Solution.java) | 36 | | 3 | [Search in Rotated Sorted Array](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3304/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/SearchInRotatedSortedArray/Solution.java) | 37 | | 3 | [Construct Binary Search Tree from Preorder Traversal](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3305/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/ConstructBinarySearchTreeFromPreorderTraversal/Solution.java) | 38 | | 3 | [Leftmost Column with at Least a One](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3306/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week3/LeftMostColumnWithAtLeastAOne/Solution.java) | 39 | |---| ------------------------|-----| 40 | | 4 | [Subarray Sum Equals K](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3307/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week4/SubarraySumEqualsK/Solution.java) | 41 | | 4 | [Bitwise AND of Numbers Range](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3308/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week4/BitwiseANDofNumbersRange/Solution.java) | 42 | | 4 | [LRU Cache](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3309/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week4/LRUCache/Solution.java) | 43 | | 4 | [Jump Game](https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/531/week-4/3310/) | [java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company/thirty_day_leetcoding_challenge/week4/JumpGame/Solution.java) | 44 | 45 | 46 | 47 | 48 | 49 | # Algorithms 50 | | n | Question | Solution | Difficulty| 51 | |:---:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------------------------------:|:-----------------------------:| 52 | | 1 | [Two Sum](https://leetcode.com/problems/two-sum/)|[java](https://github.com/AbdOoSaed/leetcode/blob/master/src/com/company//Users/abdo/dev/java-projects/leetcode/src/com/company/Algorithms/easy/TwoSum/Solution.java) |Easy| 53 | | 2 | [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [not-yet](#) |Easy| 54 | | 3 | [Add Binary](https://leetcode.com/problems/add-binary/) | [not-yet](#) |Easy| 55 | | 4 | [Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/) | [not-yet](#) |Easy| 56 | | 5 | [Reverse Integer](https://leetcode.com/problems/reverse-integer/) | [not-yet](#) |Easy| 57 | | 6 | [Palindrome Number](https://leetcode.com/problems/palindrome-number/) | [not-yet](#) |Easy| 58 | | 7 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [not-yet](#) |Easy| 59 | | 8 | [Largest Perimeter Triangle](https://leetcode.com/problems/largest-perimeter-triangle/) | [not-yet](#) |Easy| 60 | | 9 | [Roman to Integer](https://leetcode.com/problems/roman-to-integer/) | [not-yet](#) |Easy| 61 | | 10 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | [not-yet](#) |Easy| 62 | | 11 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [not-yet](#) |Easy| 63 | | 12 | [Merge Two Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | [not-yet](#) |Easy| 64 | | 13 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | [not-yet](#) |Easy| 65 | | 14 | [Remove Element](https://leetcode.com/problems/remove-element/) | [not-yet](#) |Easy| 66 | | 15 | [Implement strStr()](https://leetcode.com/problems/implement-strstr/) | [not-yet](#) |Easy| 67 | | 16 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [not-yet](#) |Easy| 68 | | 17 | [Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | [not-yet](#) |Easy| 69 | | 18 | [Plus One](https://leetcode.com/problems/plus-one/) | [not-yet](#) |Easy| 70 | | 19 | [Sqrt(x)](https://leetcode.com/problems/sqrtx/) | [not-yet](#) |Easy| 71 | | 20 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [not-yet](#) |Easy| 72 | | 21 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/) | [not-yet](#) |Easy| 73 | | 22 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array/) | [not-yet](#) |Easy| 74 | | 23 | [Same Tree](https://leetcode.com/problems/same-tree/) | [not-yet](#) |Easy| 75 | | 24 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [not-yet](#) |Easy| 76 | | 25 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [not-yet](#) |Easy| 77 | | 26 | [Binary Tree Level Order Traversal II](https://leetcode.com/problems/binary-tree-level-order-traversal-ii/) | [not-yet](#) |Easy| 78 | | 27 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [not-yet](#) |Easy| 79 | | 28 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/) | [not-yet](#) |Easy| 80 | | 29 | [Min Stack](https://leetcode.com/problems/min-stack/) | [not-yet](#) |Easy| 81 | | 30 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists/) | [not-yet](#) |Easy| 82 | | 31 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [not-yet](#) |Easy| 83 | | 32 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/) | [not-yet](#) |Easy| 84 | | 33 | [Excel Sheet Column Number](https://leetcode.com/problems/excel-sheet-column-number/) | [not-yet](#) |Easy| 85 | | 34 | [Factorial Trailing Zeroes](https://leetcode.com/problems/factorial-trailing-zeroes/) | [not-yet](#) |Easy| 86 | | 35 | [Unique Number of Occurrences](https://leetcode.com/problems/unique-number-of-occurrences/) | [not-yet](#) |Easy| 87 | | 36 | [Print in Order](https://leetcode.com/problems/print-in-order/) | [not-yet](#) |Easy| 88 | | 37 | [Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/) | [not-yet](#) |Easy| 89 | | 38 | [Single Number](https://leetcode.com/problems/single-number/) | [not-yet](#) |Easy| 90 | | 39 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [not-yet](#) |Easy| 91 | | 40 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [not-yet](#) |Easy| 92 | | 41 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [not-yet](#) |Easy| 93 | | 42 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [not-yet](#) |Easy| 94 | | 43 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) | [not-yet](#) |Easy| 95 | | 44 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [not-yet](#) |Easy| 96 | | 45 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [not-yet](#) |Easy| 97 | | 46 | [Count and Say](https://leetcode.com/problems/count-and-say/) | [not-yet](#) |Easy| 98 | | 47 | [Reverse String](https://leetcode.com/problems/reverse-string/) | [not-yet](#) |Easy| 99 | | 48 | [Fizz Buzz](https://leetcode.com/problems/fizz-buzz/) | [not-yet](#) |Easy| 100 | | 49 | [Delete Node in a Linked List](https://leetcode.com/problems/delete-node-in-a-linked-list) | [not-yet](#) |Easy| 101 | | 50 | [Majority Element](https://leetcode.com/problems/majority-element) | [not-yet](#) |Easy| 102 | | 51 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate) | [not-yet](#) |Easy| 103 | | 52 | [Missing Number](https://leetcode.com/problems/missing-number/) | [not-yet](#) |Easy| 104 | | 53 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [not-yet](#) |Easy| 105 | | 54 | [Intersection of Two Arrays II](https://leetcode.com/problems/intersection-of-two-arrays-ii/) | [not-yet](#) |Easy| 106 | | 55 | [Count Primes](https://leetcode.com/problems/count-primes/) | [not-yet](#) |Easy| 107 | | 56 | [Rotate Array](https://leetcode.com/problems/rotate-array/) | [not-yet](#) |Easy| 108 | | 57 | [Reverse Bits](https://leetcode.com/problems/reverse-bits/) | [not-yet](#) |Easy| 109 | | 58 | [Shortest Unsorted Continuous Subarray](https://leetcode.com/problems/shortest-unsorted-continuous-subarray) | [not-yet](#) |Easy| 110 | | 59 | [Merge Two Binary Tree](https://leetcode.com/problems/merge-two-binary-trees/) | [not-yet](#) |Easy| 111 | | 60 | [Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree) | [not-yet](#) |Easy| 112 | | 61 | [Find All Numbers Disappeared in an Array](https://leetcode.com/problems/find-all-numbers-disappeared-in-an-array/) | [not-yet](#) |Easy| 113 | | 62 | [Path Sum](https://leetcode.com/problems/path-sum/) | [not-yet](#) |Easy| 114 | | 63 | [Path Sum II](https://leetcode.com/problems/path-sum-ii/) | [not-yet](#) |Easy| 115 | | 64 | [Path Sum III](https://leetcode.com/problems/path-sum-iii) | [not-yet](#) |Easy| 116 | | 65 | [House Robber](https://leetcode.com/problems/house-robber/) | [not-yet](#) |Easy| 117 | | 66 | [Palindrome Linked List](https://leetcode.com/problems/palindrome-linked-list) | [not-yet](#) |Easy| 118 | | 67 | [ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [not-yet](#) |Easy| 119 | | 68 | [Remove Nth Node From End of List](https://leetcode.com/problems/remove-nth-node-from-end-of-list/solution/) | [not-yet](#) |Easy| 120 | | 69 | [Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/) | [not-yet](#) |Easy| 121 | | 70 | [Valid Sudoku](https://leetcode.com/problems/valid-sudoku/) | [not-yet](#) |Easy| 122 | | 72 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4/) | [not-yet](#) |Easy| 123 | | 73 | [Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/) | [not-yet](#) |Easy| 124 | | 74 | [Implement Queue using Stacks](https://leetcode.com/problems/implement-queue-using-stacks/) | [not-yet](#) |Easy| 125 | | 75 | [Bulls and Cows](https://leetcode.com/problems/bulls-and-cows/) | [not-yet](#) |Easy| 126 | | 76 | [Strobogrammatic Number](https://leetcode.com/problems/Strobogrammatic-Number/) | [not-yet](#) |Easy| 127 | | 77 | [Number of Boomerangs](https://leetcode.com/problems/number-of-boomerangs/) | [not-yet](#) |Easy| 128 | | 78 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [not-yet](#) |Easy| 129 | | 79 | [Binary Tree Paths](https://leetcode.com/problems/binary-tree-paths/) | [not-yet](#) |Easy| 130 | | 80 | [Longest Palindrome](https://leetcode.com/problems/longest-palindrome/) | [not-yet](#) |Easy| 131 | | 81 |[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/description/) | [not-yet](#) |Easy| 132 | | 82 | [Power of Four](https://leetcode.com/problems/power-of-four/) | [not-yet](#) |Easy| 133 | | 83 | [Power of Two](https://leetcode.com/problems/power-of-two/description/) | [not-yet](#) |Easy| 134 | | 84 | [Add Strings](https://leetcode.com/problems/add-strings/) | [not-yet](#) |Easy| 135 | | 85 | [Nested List Weight Sum II](https://leetcode.com/discuss/interview-question/algorithms/125258/nested-list-weight-sum-ii) | [not-yet](#) |Easy| 136 | | 86 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/) | [not-yet](#) |Easy| 137 | | 87 | [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/) | [not-yet](#) |Easy| 138 | | 88 | [Ransom Note](https://leetcode.com/problems/ransom-note/) | [not-yet](#) |Easy| 139 | | 89 | [Find the Difference](https://leetcode.com/problems/find-the-difference/) | [not-yet](#) |Easy| 140 | | 90 | [Binary Watch](https://leetcode.com/problems/binary-watch/) | [not-yet](#) |Easy| 141 | | 91 | [Sum of Left Leaves](https://leetcode.com/problems/sum-of-left-leaves/) | [not-yet](#) |Easy| 142 | | 92 | [Convert a Number to Hexadecimal](https://leetcode.com/problems/convert-a-number-to-hexadecimal/) | [not-yet](#) |Easy| 143 | | 93 | [willAdd](url) | [not-yet](#) |Easy| 144 | | 94 |[willAdd](url) | [not-yet](#) |Easy| 145 | | 95 | [willAdd](url) | [not-yet](#) |Easy| 146 | | 96 | [willAdd](url) | [not-yet](#) |Easy| 147 | | 97 | [willAdd](url) | [not-yet](#) |Easy| 148 | | 98 | [willAdd](url) | [not-yet](#) |Easy| 149 | | 99 | [willAdd](url) | [not-yet](#) |Easy| 150 | | 100 | [willAdd](url) | [not-yet](#) |Easy| 151 | |---| ------------------------|------|-----| 152 | | 101 | [Find K Closest Elements](https://leetcode.com/problems/find-k-closest-elements/) | [not-yet](#) |Medium| 153 | | 102 | [Find All Anagrams in a String](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | [not-yet](#) |Medium| 154 | | 103 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [not-yet](#) |Medium| 155 | | 104 | [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/) | [not-yet](#) |Medium| 156 | | 105 | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | [not-yet](#) |Medium| 157 | | 106 | [Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/) | [not-yet](#) |Medium| 158 | | 107 |[Course Schedule](https://leetcode.com/problems/course-schedule/) | [not-yet](#) |Medium| 159 | | 108 |[Continuous Subarray Sum](https://leetcode.com/problems/continuous-subarray-sum/) | [not-yet](#) |Medium| 160 | | 109 | [Battleships in a Board](https://leetcode.com/problems/battleships-in-a-board/) | [not-yet](#) |Medium| 161 | | 110 | [Number of Islands](https://leetcode.com/problems/number-of-islands/description/) | [not-yet](#) |Medium| 162 | | 111 | [Largest Sum of Averages](https://leetcode.com/problems/largest-sum-of-averages/) | [not-yet](#) |Medium| 163 | | 112 | [willAdd](url) | [not-yet](#) |Medium| 164 | | 113 | [willAdd](url) | [not-yet](#) |Medium| 165 | | 114 | [willAdd](url) | [not-yet](#) |Medium| 166 | | 115 | [willAdd](url) | [not-yet](#) |Medium| 167 | | 116 | [willAdd](url) | [not-yet](#) |Medium| 168 | | 117 | [willAdd](url) | [not-yet](#) |Medium| 169 | | 118 | [willAdd](url) | [not-yet](#) |Medium| 170 | | 119 | [willAdd](url) | [not-yet](#) |Medium| 171 | | 120 | [willAdd](url) | [not-yet](#) |Medium| 172 | | 121 | [willAdd](url) | [not-yet](#) |Medium| 173 | | 122 | [willAdd](url) | [not-yet](#) |Medium| 174 | | 123 | [willAdd](url) | [not-yet](#) |Medium| 175 | | 124 | [willAdd](url) | [not-yet](#) |Medium| 176 | | 125 | [willAdd](url) | [not-yet](#) |Medium| 177 | | 126 | [willAdd](url) | [not-yet](#) |Medium| 178 | | 127 | [willAdd](url) | [not-yet](#) |Medium| 179 | | 128 | [willAdd](url) | [not-yet](#) |Medium| 180 | | 129 | [willAdd](url) | [not-yet](#) |Medium| 181 | | 130 | [willAdd](url) | [not-yet](#) |Medium| 182 | | 131 | [willAdd](url) | [not-yet](#) |Medium| 183 | | 132 | [willAdd](url) | [not-yet](#) |Medium| 184 | | 133 | [willAdd](url) | [not-yet](#) |Medium| 185 | | 134 | [willAdd](url) | [not-yet](#) |Medium| 186 | | 135 | [willAdd](url) | [not-yet](#) |Medium| 187 | | 136 | [willAdd](url) | [not-yet](#) |Medium| 188 | | 137 | [willAdd](url) | [not-yet](#) |Medium| 189 | | 138 | [willAdd](url) | [not-yet](#) |Medium| 190 | | 139 | [willAdd](url) | [not-yet](#) |Medium| 191 | | 140 | [willAdd](url) | [not-yet](#) |Medium| 192 | | 141 | [willAdd](url) | [not-yet](#) |Medium| 193 | | 142 | [willAdd](url) | [not-yet](#) |Medium| 194 | | 143 | [willAdd](url) | [not-yet](#) |Medium| 195 | | 144 | [willAdd](url) | [not-yet](#) |Medium| 196 | | 145 | [willAdd](url) | [not-yet](#) |Medium| 197 | | 146 | [willAdd](url) | [not-yet](#) |Medium| 198 | | 147 | [willAdd](url) | [not-yet](#) |Medium| 199 | | 148 | [willAdd](url) | [not-yet](#) |Medium| 200 | | 149 | [willAdd](url) | [not-yet](#) |Medium| 201 | | 150 | [willAdd](url) | [not-yet](#) |Medium| 202 | | 151 | [willAdd](url) | [not-yet](#) |Medium| 203 | | 152 | [willAdd](url) | [not-yet](#) |Medium| 204 | | 153 | [willAdd](url) | [not-yet](#) |Medium| 205 | | 154 | [willAdd](url) | [not-yet](#) |Medium| 206 | | 155 | [willAdd](url) | [not-yet](#) |Medium| 207 | | 156 | [willAdd](url) | [not-yet](#) |Medium| 208 | | 157 | [willAdd](url) | [not-yet](#) |Medium| 209 | | 158 | [willAdd](url) | [not-yet](#) |Medium| 210 | | 159 | [willAdd](url) | [not-yet](#) |Medium| 211 | | 160 | [willAdd](url) | [not-yet](#) |Medium| 212 | | 161 | [willAdd](url) | [not-yet](#) |Medium| 213 | | 162 | [willAdd](url) | [not-yet](#) |Medium| 214 | | 163 | [willAdd](url) | [not-yet](#) |Medium| 215 | | 164 | [willAdd](url) | [not-yet](#) |Medium| 216 | | 165 | [willAdd](url) | [not-yet](#) |Medium| 217 | | 166 | [willAdd](url) | [not-yet](#) |Medium| 218 | | 167 | [willAdd](url) | [not-yet](#) |Medium| 219 | | 168 | [willAdd](url) | [not-yet](#) |Medium| 220 | | 169 | [willAdd](url) | [not-yet](#) |Medium| 221 | | 170 | [willAdd](url) | [not-yet](#) |Medium| 222 | |---| ------------------------|------|-----| 223 | | 171 | [willAdd](url) | [not-yet](#) |Hard| 224 | | 172 | [willAdd](url) | [not-yet](#) |Hard| 225 | | 173 | [willAdd](url) | [not-yet](#) |Hard| 226 | | 174 | [willAdd](url) | [not-yet](#) |Hard| 227 | | 175 | [willAdd](url) | [not-yet](#) |Hard| 228 | | 176 | [willAdd](url) | [not-yet](#) |Hard| 229 | | 177 | [willAdd](url) | [not-yet](#) |Hard| 230 | | 178 | [willAdd](url) | [not-yet](#) |Hard| 231 | | 179 | [willAdd](url) | [not-yet](#) |Hard| 232 | | 180 | [willAdd](url) | [not-yet](#) |Hard| 233 | -------------------------------------------------------------------------------- /src/com/company/Algorithms/easy/TwoSum/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.Algorithms.easy.TwoSum; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | public class Solution { 8 | public int[] twoSum(int[] nums, int target) { 9 | //Time complexity : O(n) 10 | //Space complexity : O(n) 11 | HashMap valueAndIndex = new HashMap<>(); 12 | for (int i = 0; i < nums.length; i++) { 13 | int complement = target - nums[i]; 14 | if (valueAndIndex.containsKey(complement)) 15 | return new int[]{ valueAndIndex.get(complement),i}; 16 | valueAndIndex.put(nums[i], i); 17 | } 18 | return null; 19 | } 20 | //Brute Force Solution 21 | // public int[] twoSum(int[] nums, int target) { 22 | // //loop over all nums 23 | // //Time complexity : O(n^2) 24 | // //Space complexity : O(1) 25 | // for (int i = 0; i < nums.length; i++) 26 | // for (int j = i + 1; j < nums.length; j++) 27 | // if (nums[j] == target - nums[i]) return new int[]{i, j}; 28 | // return new int[0]; 29 | // } 30 | 31 | 32 | // public int[] twoSum(int[] nums, int target) { 33 | // //Time complexity : O(n) 34 | // //Space complexity : O(n) 35 | // HashMap valueAndIndex = new HashMap<>(); 36 | // for (int i = 0; i < nums.length; i++) valueAndIndex.put(nums[i], i); 37 | // for (int i = 0; i < nums.length; i++) { 38 | // int complement = target - nums[i]; 39 | // if (valueAndIndex.containsKey(complement) && valueAndIndex.get(complement) != i) 40 | // return new int[]{i, valueAndIndex.get(complement)}; 41 | // } 42 | // return null; 43 | // } 44 | 45 | 46 | public static void main(String[] args) { 47 | //Given an array of integers, return indices of the two numbers such that they add up to a specific target. 48 | // 49 | //You may assume that each input would have exactly one solution, and you may not use the same element twice. 50 | // 51 | //Example: 52 | // 53 | //Given nums = [2, 7, 11, 15], target = 9, 54 | // 55 | //Because nums[0] + nums[1] = 2 + 7 = 9, 56 | //return [0, 1]. 57 | 58 | //Input: 59 | //[3,3] 60 | //6 61 | //Output: 62 | //[1,1] 63 | //Expected: 64 | //[0,1] 65 | 66 | //[3,2,4] 67 | //6 68 | Solution s = new Solution(); 69 | System.out.println(Arrays.toString(s.twoSum(new int[]{2, 4, 4}, 6))); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/com/company/Main.java: -------------------------------------------------------------------------------- 1 | package com.company; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | // TODO: 4/19/20 start learning in leetcode: https://leetcode.com/explore/learn/ 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/BestTimeToBuyAndSellStockII/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.BestTimeToBuyAndSellStockII; 2 | 3 | import java.util.stream.IntStream; 4 | 5 | public class Solution { 6 | public int maxProfit(int[] prices) { 7 | int profit = 0; 8 | if (prices == null || prices.length == 0) return profit; 9 | if (isSorted(prices)) return (prices[prices.length - 1] - prices[0]); 10 | for (int i = 0; i < prices.length; i++) 11 | if (canBuy(prices, i) && prices[i] < prices[i + 1]) 12 | profit += (prices[i + 1] - prices[i]); 13 | return profit; 14 | } 15 | 16 | private boolean isSorted(int[] array) { 17 | return IntStream.range(0, array.length - 1).allMatch(i -> array[i] <= array[i + 1]); 18 | } 19 | 20 | private boolean canBuy(int[] array, int index) { 21 | return array.length > index + 1; 22 | } 23 | 24 | public static void main(String[] args) { 25 | //main Method for test only :) 26 | 27 | //Input: [1,2,3,4,5] 28 | //Output: 4 29 | //Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. 30 | // Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are 31 | // engaging multiple transactions at the same time. You must sell before buying again. 32 | 33 | // more Input: [6,1,3,2,4,7]->7 , [7,6,4,3,1]->0 , [1,2,3,4,5]->4 34 | Solution s = new Solution(); 35 | System.out.println(s.maxProfit(new int[]{1, 2, 3, 4, 5})); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/CountingElements/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.CountingElements; 2 | 3 | import java.util.HashSet; 4 | 5 | public class Solution { 6 | public int countElements(int[] arr) { 7 | HashSet hashArrWithoutDuplicate = new HashSet<>(); 8 | int count = 0; 9 | for (int i : arr) hashArrWithoutDuplicate.add(i); 10 | for (int i : arr) if (hashArrWithoutDuplicate.contains((i + 1))) count++; 11 | return count; 12 | } 13 | 14 | public static void main(String[] args) { 15 | 16 | //main Method for test only :) 17 | 18 | //Example 1: 19 | // 20 | //Input: arr = [1,2,3] 21 | //Output: 2 22 | //Explanation: 1 and 2 are counted cause 2 and 3 are in arr. 23 | //Example 2: 24 | // 25 | //Input: arr = [1,1,3,3,5,5,7,7] 26 | //Output: 0 27 | //Explanation: No numbers are counted, cause there's no 2, 4, 6, or 8 in arr. 28 | //Example 3: 29 | // 30 | //Input: arr = [1,3,2,3,5,0] 31 | //Output: 3 32 | //Explanation: 0, 1 and 2 are counted cause 1, 2 and 3 are in arr. 33 | //Example 4: 34 | // 35 | //Input: arr = [1,1,2,2] 36 | //Output: 2 37 | //Explanation: Two 1s are counted cause 2 is in arr. 38 | 39 | Solution s = new Solution(); 40 | System.out.println(s.countElements(new int[]{1, 2, 3})); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/GroupAnagrams/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.GroupAnagrams; 2 | 3 | import java.util.*; 4 | 5 | public class Solution { 6 | public List> groupAnagrams(String[] strs) { 7 | var hashMap = new HashMap>(); 8 | for (String str : strs) { 9 | var chars = str.toCharArray(); 10 | Arrays.sort(chars); 11 | String sortedStringKey = String.valueOf(chars); 12 | if (!hashMap.containsKey(sortedStringKey)) 13 | hashMap.put(sortedStringKey, new ArrayList<>()); 14 | hashMap.get(sortedStringKey).add(str); 15 | } 16 | return new ArrayList(hashMap.values()); 17 | } 18 | 19 | public static void main(String[] args) { 20 | 21 | //main Method for test only :) 22 | 23 | //Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 24 | //Output: 25 | //[ 26 | // ["ate","eat","tea"], 27 | // ["nat","tan"], 28 | // ["bat"] 29 | //] 30 | 31 | Solution s = new Solution(); 32 | System.out.println(s.groupAnagrams(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"})); 33 | } 34 | } 35 | 36 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/HappyNumber/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.HappyNumber; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | public class Solution { 8 | private Set numsVisited = new HashSet<>();//to return false if has a cycle 9 | 10 | public boolean isHappy(int n) { 11 | if (n == 1) return true;//return true when number = 1 "number is Happy :). 12 | 13 | int sum = sumOfSquaresAllNumInArray(intToArray(n)); 14 | if (!numsVisited.contains(sum)) { 15 | numsVisited.add(sum); 16 | return isHappy(sum); 17 | } 18 | 19 | return false; // if num not happy return false ;( 20 | } 21 | 22 | public static void main(String[] args) { 23 | //main Method for test only :) 24 | 25 | //Input: 19 26 | //Output: true 27 | //Explanation: 28 | //12 + 92 = 82 29 | //82 + 22 = 68 30 | //62 + 82 = 100 31 | //12 + 02 + 02 = 1 32 | 33 | Solution s = new Solution(); 34 | System.out.println(s.isHappy(200));//print ur result 35 | } 36 | 37 | private int sumOfSquaresAllNumInArray(int[] nums) { 38 | int sum = 0; 39 | for (int i : nums) sum += i * i; 40 | System.out.println(sum); 41 | return sum; 42 | } 43 | 44 | private int[] intToArray(int input) { 45 | return Arrays.stream(Integer.toString(input).split("")).mapToInt(Integer::parseInt).toArray(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/MaximumSubarray/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.MaximumSubarray; 2 | 3 | 4 | public class Solution { 5 | 6 | public int maxSubArray(int[] nums) { 7 | if (nums.length == 0) return 0; 8 | 9 | if (nums.length == 1) return nums[0]; 10 | 11 | int sum = 0; 12 | int max = Integer.MIN_VALUE; 13 | for (int num : nums) { 14 | max = Math.max(max, sum += num); 15 | sum = Math.max(sum, 0); 16 | } 17 | return max; 18 | } 19 | 20 | public static void main(String[] args) { 21 | //main Method for test only :) 22 | 23 | //Input: [-2,1,-3,4,-1,2,1,-5,4], 24 | //Output: 6 25 | //Explanation: [4,-1,2,1] has the largest sum = 6. 26 | 27 | Solution s = new Solution(); 28 | 29 | System.out.println(s.maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4})); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/MoveZeroes/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.MoveZeroes; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Solution { 6 | 7 | public void moveZeroes(int[] nums) { 8 | var arrayWithoutZeros = Arrays.stream(nums).filter(value -> value != 0).toArray(); 9 | for (int i = 0; i < nums.length; i++) nums[i] = (i < arrayWithoutZeros.length) ? arrayWithoutZeros[i] : 0; 10 | } 11 | 12 | //Other Solution 13 | /*-------1-------*/ 14 | // public void moveZeroes(int[] nums) { 15 | // int length = nums.length; 16 | // int count = 0; 17 | // for (int i = 0; i < length; i++) if (nums[i] != 0) swapNumbers(nums, count++, i); 18 | // } 19 | // 20 | // void swapNumbers(int[] arr, int pos1, int pos2) { 21 | // int temp = arr[pos2]; 22 | // arr[pos2] = arr[pos1]; 23 | // arr[pos1] = temp; 24 | // } 25 | /*-------1-------*/ 26 | 27 | /*-------2-------*/ 28 | // public void moveZeroes(int[] nums) { 29 | //// I don't know why not accepted 30 | // int[] temp = new int[nums.length]; 31 | // int countOfNumberNotZero = 0; 32 | // for (int i : nums) if (i != 0) temp[countOfNumberNotZero++] = i; 33 | // nums = temp; 34 | // } 35 | /*-------2-------*/ 36 | 37 | public static void main(String[] args) { 38 | //main Method for test only :) 39 | 40 | //Input: [0,1,0,3,12] 41 | //Output: [1,3,12,0,0] 42 | Solution s = new Solution(); 43 | s.moveZeroes(new int[]{0, 1, 0, 3, 12}); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week1/SingleNumber/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week1.SingleNumber; 2 | 3 | import java.util.HashMap; 4 | 5 | class Solution { 6 | 7 | public int singleNumber(int[] nums) { 8 | 9 | HashMap numberAndAppears = new HashMap<>(); 10 | 11 | for (int i : nums) { 12 | int count = 1; 13 | if (numberAndAppears.containsKey(i)) 14 | count += numberAndAppears.get(i);//if num exist before increment the value 15 | 16 | numberAndAppears.put(i, count); 17 | } 18 | 19 | for (int i : nums) if (numberAndAppears.get(i) == 1) return i;// if found num appears one return it 20 | 21 | return -1;//if no item appears one time return -1 22 | } 23 | 24 | public static void main(String[] args) { 25 | //main Method for test only :) 26 | 27 | //Example 1: 28 | // 29 | //Input: [2,2,1] 30 | //Output: 1 31 | //Example 2: 32 | // 33 | //Input: [4,1,2,1,2] 34 | //Output: 4 35 | Solution s = new Solution(); 36 | System.out.println(s.singleNumber(new int[]{4, 1, 2, 1, 2})); 37 | } 38 | } -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/BackspaceStringCompare/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.BackspaceStringCompare; 2 | 3 | import java.util.Stack; 4 | 5 | public class Solution { 6 | public boolean backspaceCompare(String S, String T) { 7 | Stack charsS = new Stack<>(); 8 | Stack charsT = new Stack<>(); 9 | for (var ch : S.toCharArray()) 10 | if (ch != '#') charsS.add(ch); 11 | else { 12 | if (!charsS.isEmpty()) charsS.pop(); 13 | } 14 | for (var ch : T.toCharArray()) 15 | if (ch != '#') charsT.add(ch); 16 | else { 17 | if (!charsT.isEmpty()) charsT.pop(); 18 | } 19 | return charsS.equals(charsT); 20 | } 21 | 22 | public static void main(String[] args) { 23 | 24 | //main Method for test only :) 25 | 26 | //Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 27 | //Output: 28 | //[ 29 | // ["ate","eat","tea"], 30 | // ["nat","tan"], 31 | // ["bat"] 32 | //] 33 | 34 | Solution s = new Solution(); 35 | System.out.println(s.backspaceCompare("a##c", "#a#c")); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/ContiguousArray/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.ContiguousArray; 2 | 3 | import java.util.*; 4 | 5 | public class Solution { 6 | // TODO: 4/15/20 Read Solution https://leetcode.com/problems/contiguous-array/solution/ 7 | public int findMaxLength(int[] nums) { 8 | // Make all the 0 to -1 first 9 | Map map = new HashMap<>(); 10 | // Whenever sum get add up to 0, we know we should add 11 | map.put(0, -1); 12 | int max = 0, count = 0; 13 | for (int i = 0; i < nums.length; i++) { 14 | count += (nums[i] == 1 ? 1 : -1); 15 | if (map.containsKey(count)) max = Math.max(max, i - map.get(count)); 16 | else map.put(count, i); 17 | } 18 | return max; 19 | } 20 | 21 | public static void main(String[] args) { 22 | 23 | //Input: 24 | //[0,0,0,1,1,1,0] 25 | //Output: 26 | //4 27 | //Expected: 28 | //6 29 | 30 | 31 | //Input: 32 | //[0,1,0,1] 33 | //Output: 34 | //2 35 | //Expected: 36 | //4 37 | 38 | 39 | //Input: 40 | //[0,1,1,0,1,1,1,0] 41 | //Output: 42 | //6 43 | //Expected: 44 | //4 45 | 46 | //Input: 47 | //[0,0,1,0,0,0,1,1] 48 | //Output: 49 | //0 50 | //Expected: 51 | //6 52 | 53 | //Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. 54 | // 55 | //Example 1: 56 | //Input: [0,1] 57 | //Output: 2 58 | //Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1. 59 | //Example 2: 60 | //Input: [0,1,0] 61 | //Output: 2 62 | //Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. 63 | //Note: The length of the given binary array will not exceed 50,000. 64 | 65 | Solution s = new Solution(); 66 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 1}) + " Expected:2"); 67 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 1, 0}) + " Expected:2"); 68 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 1, 1, 0, 1, 1, 1, 0}) + " Expected:4"); 69 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 1, 0, 1}) + " Expected:4"); 70 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 0, 0, 1, 1, 1, 0}) + " Expected:6"); 71 | System.out.println("Output:" + s.findMaxLength(new int[]{0, 0, 1, 0, 0, 0, 1, 1}) + " Expected:6"); 72 | 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/DiameterOfBinaryTree/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.DiameterOfBinaryTree; 2 | 3 | class TreeNode { 4 | 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode(int x) { 10 | val = x; 11 | } 12 | } 13 | 14 | class Solution { 15 | 16 | public int diameterOfBinaryTree(TreeNode root) { 17 | return diameter(root); 18 | } 19 | 20 | private int diameter(TreeNode root) { 21 | if (root == null) return 0; 22 | return Math.max(height(root.left) + height(root.right), Math.max(diameter(root.left), diameter(root.right))); 23 | } 24 | 25 | private int height(TreeNode node) { 26 | if (node == null) return 0; 27 | return (1 + Math.max(height(node.left), height(node.right))); 28 | } 29 | 30 | //other solution 31 | //public class Solution { 32 | // int max = 0; 33 | // 34 | // public int diameterOfBinaryTree(TreeNode root) { 35 | // maxDepth(root); 36 | // return max; 37 | // } 38 | // 39 | // private int maxDepth(TreeNode root) { 40 | // if (root == null) return 0; 41 | // 42 | // int left = maxDepth(root.left); 43 | // int right = maxDepth(root.right); 44 | // 45 | // max = Math.max(max, left + right); 46 | // 47 | // return Math.max(left, right) + 1; 48 | // } 49 | //} 50 | public static void main(String[] args) { 51 | //Given a binary tree, you need to compute the length of the diameter of the tree. 52 | // The diameter of a binary tree is the length of the longest path between any two nodes in a tree. 53 | // This path may or may not pass through the root. 54 | // 55 | //Example:1 56 | //Given a binary tree 57 | // 1 58 | // / \ 59 | // 2 3 60 | // / \ 61 | // 4 5 62 | //Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3]. 63 | // 64 | //Example:2 65 | //Given a binary tree 66 | // 1 67 | // / \ 68 | // 2 3 69 | // / \ \ 70 | // 4 5 5 71 | //Return 4, which is the length of the path [4,2,1,3,5] or [5,2,1,3,5]. 72 | //Note: The length of path between two nodes is represented by the number of edges between them. 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/LastStoneWeight/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.LastStoneWeight; 2 | 3 | import java.util.*; 4 | 5 | public class Solution { 6 | public int lastStoneWeight(int[] stones) { 7 | return lastStoneWeight(stones, stones.length - 1); 8 | } 9 | 10 | private int lastStoneWeight(int[] stones, int length) { 11 | if (length < 0) return 0; 12 | if (length == 0) return stones[0];//if array has only one number return it 13 | Arrays.sort(stones);//sort array to push all large number to the end 14 | if (stones[length] == stones[length - 1]) length -= 2;//If x == y, both stones are totally destroyed; 15 | else 16 | stones[length - 1] = stones[length] - stones[--length];//If x != y, the stone of weight *x* is totally destroyed, and the stone of weight *y* has new weight y-x. 17 | return lastStoneWeight(stones, length); 18 | } 19 | 20 | public static void main(String[] args) { 21 | 22 | //We have a collection of stones, each stone has a positive integer weight. 23 | // 24 | //Each turn, we choose the two heaviest stones and smash them together. Suppose the stones have weights x and y with x <= y. The result of this smash is: 25 | // 26 | //If x == y, both stones are totally destroyed; 27 | //If x != y, the stone of weight x is totally destroyed, and the stone of weight y has new weight y-x. 28 | //At the end, there is at most 1 stone left. Return the weight of this stone (or 0 if there are no stones left.) 29 | // 30 | // 31 | // 32 | //Example 1: 33 | // 34 | //Input: [2,7,4,1,8,1] 35 | //Output: 1 36 | //Explanation: 37 | //We combine 7 and 8 to get 1 so the array converts to [2,4,1,1,1] then, 38 | //we combine 2 and 4 to get 2 so the array converts to [2,1,1,1] then, 39 | //we combine 2 and 1 to get 1 so the array converts to [1,1,1] then, 40 | //we combine 1 and 1 to get 0 so the array converts to [1] then that's the value of last stone. 41 | // 42 | // 43 | //Note: 44 | // 45 | //1 <= stones.length <= 30 46 | //1 <= stones[i] <= 1000 47 | 48 | Solution s = new Solution(); 49 | System.out.println(s.lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1})); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/MiddleOfTheLinkedList/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.MiddleOfTheLinkedList; 2 | 3 | import java.util.HashMap; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * public class ListNode { 8 | * int val; 9 | * ListNode next; 10 | * ListNode(int x) { val = x; } 11 | * } 12 | */ 13 | class Solution { 14 | public ListNode middleNode(ListNode head) { 15 | //slowPointer go to next node every loop +1 16 | //fastPointer go to next next node every loop +2 17 | //when fastPointer reach the end slowPointer will be in the middle 18 | ListNode slowPtr = head; 19 | ListNode fastPtr = head; 20 | if (head != null) while (fastPtr != null && fastPtr.next != null) { 21 | fastPtr = fastPtr.next.next; 22 | slowPtr = slowPtr.next; 23 | } 24 | return slowPtr; 25 | } 26 | //other Solution 27 | // public ListNode middleNode(ListNode head) { 28 | // //add all nodes in hashMap with the count in linkedList 29 | // //in the end return node in hashMap.size/2 30 | // HashMap nodeHashMap = new HashMap<>(); 31 | // int count = 0; 32 | // if (head != null) while (head != null) { 33 | // nodeHashMap.put(count++, head); 34 | // head = head.next; 35 | // } 36 | // return nodeHashMap.get(count / 2); 37 | // } 38 | 39 | //helper 40 | public class ListNode { 41 | int val; 42 | ListNode next; 43 | 44 | ListNode(int x) { 45 | val = x; 46 | } 47 | } 48 | 49 | public static void main(String[] args) { 50 | 51 | //main Method for test only :) 52 | 53 | //Example 1: 54 | // 55 | //Input: [1,2,3,4,5] 56 | //Output: Node 3 from this list (Serialization: [3,4,5]) 57 | //The returned node has value 3. (The judge's serialization of this node is [3,4,5]). 58 | //Note that we returned a ListNode object ans, such that: 59 | //ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, and ans.next.next.next = NULL. 60 | //Example 2: 61 | // 62 | //Input: [1,2,3,4,5,6] 63 | //Output: Node 4 from this list (Serialization: [4,5,6]) 64 | //Since the list has two middle nodes with values 3 and 4, we return the second one. 65 | // 66 | // 67 | //Note: 68 | // 69 | //The number of nodes in the given list will be between 1 and 100. 70 | 71 | } 72 | } -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/MinStack/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.MinStack; 2 | 3 | import java.util.Stack; 4 | 5 | public class Solution { 6 | 7 | private int minValue = Integer.MAX_VALUE; 8 | private Stack stack = new Stack(); 9 | 10 | public void push(int x) { 11 | //when Add to Stack if (newValue < minValue) minValue = newValue 12 | //then add To Stack 13 | if (stack.isEmpty() || x < minValue) minValue = x; 14 | stack.push(x); 15 | } 16 | 17 | public void pop() { 18 | //first if stack NotEmpty 19 | //if minValue equals top value delete top value and search for new min value in stack 20 | if (!stack.isEmpty()) if (minValue == stack.pop()) { 21 | minValue = Integer.MAX_VALUE; 22 | stack.forEach(pair -> { 23 | if (pair < minValue) minValue = pair; 24 | }); 25 | } 26 | } 27 | 28 | public int top() { 29 | return stack.peek(); 30 | } 31 | 32 | public int getMin() { 33 | return minValue; 34 | } 35 | 36 | public static void main(String[] args) { 37 | 38 | //Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. 39 | // 40 | //push(x) -- Push element x onto stack. 41 | //pop() -- Removes the element on top of the stack. 42 | //top() -- Get the top element. 43 | //getMin() -- Retrieve the minimum element in the stack. 44 | // 45 | // 46 | //Example: 47 | // 48 | //MinStack minStack = new MinStack(); 49 | //minStack.push(-2); 50 | //minStack.push(0); 51 | //minStack.push(-3); 52 | //minStack.getMin(); --> Returns -3. 53 | //minStack.pop(); 54 | //minStack.top(); --> Returns 0. 55 | //minStack.getMin(); --> Returns -2. 56 | 57 | Solution s = new Solution(); 58 | s.push(2147483646); 59 | s.push(2147483646); 60 | s.push(2147483647); 61 | 62 | System.out.println(s.top()); 63 | s.pop(); 64 | System.out.println(s.getMin()); 65 | 66 | s.pop(); 67 | System.out.println(s.getMin()); 68 | s.pop(); 69 | 70 | s.push(2147483647); 71 | System.out.println(s.top()); 72 | System.out.println(s.getMin()); 73 | 74 | s.push(-2147483648); 75 | System.out.println(s.top()); 76 | System.out.println(s.getMin()); 77 | s.pop(); 78 | System.out.println(s.getMin()); 79 | //Output: 80 | //[2147483647,2147483646,2147483646,2147483646,2147483646,2147483646,2147483646,2147483646] 81 | //Expected: 82 | //[2147483647,2147483646,2147483646,2147483647,2147483647,-2147483648,-2147483648,2147483647] 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week2/PerformStringShifts/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week2.PerformStringShifts; 2 | 3 | public class Solution { 4 | public String stringShift(String s, int[][] shift) { 5 | for (var i : shift) { 6 | if (i[0] == 0) s = leftShift(s, i[1]); 7 | else s = rightShift(s, i[1]); 8 | } 9 | return s; 10 | } 11 | 12 | private String leftShift(String input, int lastIndex) { 13 | return input.substring(lastIndex) + input.substring(0, lastIndex); 14 | } 15 | 16 | private String rightShift(String input, int lastIndex) { 17 | return input.substring(input.length() - lastIndex) + input.substring(0, input.length() - lastIndex); 18 | } 19 | 20 | public static void main(String[] args) { 21 | 22 | //You are given a string s containing lowercase English letters, and a matrix shift, where shift[i] = [direction, amount]: 23 | // 24 | //direction can be 0 (for left shift) or 1 (for right shift). 25 | //amount is the amount by which string s is to be shifted. 26 | //A left shift by 1 means remove the first character of s and append it to the end. 27 | //Similarly, a right shift by 1 means remove the last character of s and add it to the beginning. 28 | //Return the final string after all operations. 29 | // 30 | // 31 | // 32 | //Example 1: 33 | // 34 | //Input: s = "abc", shift = [[0,1],[1,2]] 35 | //Output: "cab" 36 | //Explanation: 37 | //[0,1] means shift to left by 1. "abc" -> "bca" 38 | //[1,2] means shift to right by 2. "bca" -> "cab" 39 | //Example 2: 40 | // 41 | //Input: s = "abcdefg", shift = [[1,1],[1,1],[0,2],[1,3]] 42 | //Output: "efgabcd" 43 | //Explanation: 44 | //[1,1] means shift to right by 1. "abcdefg" -> "gabcdef" 45 | //[1,1] means shift to right by 1. "gabcdef" -> "fgabcde" 46 | //[0,2] means shift to left by 2. "fgabcde" -> "abcdefg" 47 | //[1,3] means shift to right by 3. "abcdefg" -> "efgabcd" 48 | // 49 | // 50 | //Constraints: 51 | // 52 | //1 <= s.length <= 100 53 | //s only contains lower case English letters. 54 | //1 <= shift.length <= 100 55 | //shift[i].length == 2 56 | //0 <= shift[i][0] <= 1 57 | //0 <= shift[i][1] <= 100 58 | 59 | 60 | Solution s = new Solution(); 61 | System.out.println(s.stringShift("abcdefg", new int[][]{{1, 1}, {1, 1}, {0, 2}, {1, 3}})); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/ConstructBinarySearchTreeFromPreorderTraversal/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.ConstructBinarySearchTreeFromPreorderTraversal; 2 | 3 | class Solution { 4 | public class TreeNode { 5 | int val; 6 | TreeNode left; 7 | TreeNode right; 8 | 9 | TreeNode(int x) { 10 | val = x; 11 | } 12 | } 13 | 14 | public TreeNode bstFromPreorder(int[] preorder) { 15 | TreeNode root = new TreeNode(preorder[0]); 16 | for (int i = 1; i < preorder.length; i++) add(root, preorder[i]); 17 | return root; 18 | } 19 | 20 | private void add(TreeNode root, int value) { 21 | if (root == null) return; 22 | TreeNode newNode = new TreeNode(value); 23 | while (true) { 24 | if (value < root.val) { 25 | if (root.left == null) { 26 | root.left = newNode; 27 | break; 28 | } 29 | root = root.left; 30 | 31 | } else if (value > root.val) { 32 | if (root.right == null) { 33 | root.right = newNode; 34 | break; 35 | } 36 | root = root.right; 37 | 38 | } else break; 39 | } 40 | } 41 | 42 | public static void main(String[] args) { 43 | 44 | //Return the root node of a binary search tree that matches the given preorder traversal. 45 | // 46 | //(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val. Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) 47 | // 48 | // 49 | // 50 | //Example 1: 51 | // 52 | //Input: [8,5,1,7,10,12] 53 | //Output: [8,5,10,1,7,null,12] 54 | // 55 | // 56 | // 57 | //Note: 58 | // 59 | //1 <= preorder.length <= 100 60 | //The values of preorder are distinct. 61 | 62 | 63 | Solution s = new Solution(); 64 | s.bstFromPreorder(new int[]{8, 5, 1, 7, 10, 12}); 65 | } 66 | } -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/LeftMostColumnWithAtLeastAOne/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.LeftMostColumnWithAtLeastAOne; 2 | 3 | import java.util.List; 4 | 5 | 6 | // This is the BinaryMatrix's API interface. 7 | // You should not implement it, or speculate about its implementation 8 | interface BinaryMatrix { 9 | int get(int x, int y); 10 | List dimensions(); 11 | } 12 | 13 | 14 | public class Solution { 15 | public int leftMostColumnWithOne(BinaryMatrix binaryMatrix) { 16 | List dim = binaryMatrix.dimensions(); 17 | int rows = dim.get(0); 18 | int columns = dim.get(1); 19 | 20 | int result = -1; 21 | 22 | if (rows == 0 || columns == 0) return -1; 23 | 24 | int row = 0; 25 | int column = columns - 1; 26 | while (row < rows && column >= 0) { 27 | if (binaryMatrix.get(row, column) == 1) { 28 | result = column--; 29 | } else row++; 30 | } 31 | return result; 32 | } 33 | } -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/MinimumPathSum/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.MinimumPathSum; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Solution { 6 | public int minPathSum(int[][] grid) { 7 | //Todo 8 | //https://www.programcreek.com/2014/05/leetcode-minimum-path-sum-java/ 9 | //https://leetcode.com/problems/minimum-path-sum/discuss/23647/My-8-lines-simple-solution 10 | //https://leetcode.com/problems/minimum-path-sum/discuss/214867/Java-DP-Solution-with-explanation-and-picture-O(mn)-time-and-O(1)-space 11 | //https://leetcode.com/problems/minimum-path-sum/discuss/324419/Java-solution-using-BFS-%2B-PriorityQueue. 12 | //https://leetcode.com/problems/minimum-path-sum/discuss/23555/AC-Java-DP-solution-v.s.-TLE-Dijstra-solution 13 | //https://leetcode.com/problems/minimum-path-sum/discuss/23564/Java-Easy-to-understand-10-lines 14 | //https://leetcode.com/problems/minimum-path-sum/discuss/23609/11-line-DP-in-Java-using-4ms 15 | int m = grid.length, n = grid[0].length; 16 | for (int i = 0; i < m; i++) { 17 | for (int j = 0; j < n; j++) { 18 | if (i == 0 && j != 0) grid[i][j] += grid[i][j - 1]; 19 | if (i != 0 && j == 0) grid[i][j] += grid[i - 1][j]; 20 | if (i != 0 && j != 0) grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); 21 | } 22 | } 23 | return grid[m - 1][n - 1]; 24 | } 25 | 26 | 27 | public static void main(String[] args) { 28 | //Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 29 | // 30 | //Note: You can only move either down or right at any point in time. 31 | // 32 | //Example: 33 | // 34 | //Input: 35 | //[ 36 | // [1,3,1], 37 | // [1,5,1], 38 | // [4,2,1] 39 | //] 40 | //Output: 7 41 | //Explanation: Because the path 1→3→1→1→1 minimizes the sum. 42 | 43 | //base condition i==grid.length||j==grid[i].length 44 | Solution s = new Solution(); 45 | int[][] grid = new int[][]{ 46 | {1, 3, 1}, 47 | {1, 5, 1}, 48 | {4, 2, 1}, 49 | }; 50 | System.out.println(s.minPathSum(grid)); 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/NumberOfIslands/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.NumberOfIslands; 2 | 3 | public class Solution { 4 | public int numIslands(char[][] grid) { 5 | //https://www.programcreek.com/2014/04/leetcode-number-of-islands-java/ 6 | int numIslands = 0; 7 | for (int i = 0; i < grid.length; i++) 8 | for (int j = 0; j < grid[i].length; j++) 9 | if (grid[i][j] == '1') { 10 | numIslands++; 11 | changeOneToZero(grid, i, j); 12 | } 13 | return numIslands; 14 | } 15 | 16 | private void changeOneToZero(char[][] grid, int i, int j) { 17 | if (i < 0 || j < 0 || i >= grid.length || j >= grid[i].length || grid[i][j] == '0') return; 18 | grid[i][j] = '0'; 19 | changeOneToZero(grid, i - 1, j);//go up 20 | changeOneToZero(grid, i + 1, j);//go down 21 | changeOneToZero(grid, i, j - 1);//go left 22 | changeOneToZero(grid, i, j + 1);//go right 23 | } 24 | 25 | public static void main(String[] args) { 26 | //Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. 27 | // 28 | //Example 1: 29 | // 30 | //Input: 31 | //11110 32 | //11010 33 | //11000 34 | //00000 35 | // 36 | //Output: 1 37 | //Example 2: 38 | // 39 | //Input: 40 | //11000 41 | //11000 42 | //00100 43 | //00011 44 | // 45 | //Output: 3 46 | 47 | Solution s = new Solution(); 48 | char[][] grid = new char[][]{ 49 | {'1', '1', '1', '1', '0'}, 50 | {'1', '1', '0', '1', '0'}, 51 | {'1', '1', '0', '0', '0'}, 52 | {'0', '0', '0', '0', '0'} 53 | }; 54 | System.out.println(s.numIslands(grid)); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/ProductOfArrayExceptSelf/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.ProductOfArrayExceptSelf; 2 | 3 | import java.util.Arrays; 4 | 5 | public class Solution { 6 | public int[] productExceptSelf(int[] nums) { 7 | // nums =[2, 3, 4, 8] 8 | int length = nums.length; 9 | int[] newNums = new int[length]; 10 | 11 | int[] left = new int[length]; 12 | left[0] = 1; 13 | for (int i = 1; i < length; i++) left[i] = left[i - 1] * nums[i - 1]; 14 | // left =[1, 2, 6, 24] 15 | 16 | int[] right = new int[length]; 17 | right[length - 1] = 1; 18 | for (int i = length - 2; i >= 0; i--) right[i] = right[i + 1] * nums[i + 1]; 19 | // right =[96, 32, 8, 1] 20 | for (int i = 0; i < length; i++) newNums[i] = left[i] * right[i]; 21 | // newNums =[96, 64, 48, 24] 22 | return newNums; 23 | } 24 | //other solution 25 | // public int[] productExceptSelf(int[] nums) { 26 | // //nums =[2, 3, 4, 8] 27 | // int[] newNums = new int[nums.length]; 28 | // newNums[0] = 1; 29 | // for (int i = 1; i < nums.length; i++) newNums[i] = newNums[i - 1] * nums[i - 1];//start from right and calculate 30 | // int temp = 1; 31 | // for (int i = nums.length - 1; i >= 0; i--) { 32 | // newNums[i] = newNums[i] * temp; 33 | // temp *= nums[i]; 34 | // } 35 | // return newNums; 36 | // } 37 | 38 | //0,0 39 | //Input: 40 | //[1,0] 41 | //Output: 42 | //[0,0] 43 | //Expected: 44 | //[0,1] 45 | public static void main(String[] args) { 46 | Solution s = new Solution(); 47 | System.out.println(Arrays.toString(s.productExceptSelf(new int[]{2, 3, 4, 8}))); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/SearchInRotatedSortedArray/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.SearchInRotatedSortedArray; 2 | 3 | public class Solution { 4 | public int search(int[] nums, int target) { 5 | int center = nums.length ^ 2; 6 | if (center % 2 != 0) center++;// if odd +1 7 | 8 | for (int i = center; i < nums.length; i++) 9 | if (nums[i] == target) return i;//from center to end 10 | //if not found in first for 11 | for (int i = 0; i < center; i++) if (nums[i] == target) return i;//from start to center 12 | 13 | return -1;// if target not found return -1 14 | } 15 | 16 | public static void main(String[] args) { 17 | //Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. 18 | // 19 | //(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]). 20 | // 21 | //You are given a target value to search. If found in the array return its index, otherwise return -1. 22 | // 23 | //You may assume no duplicate exists in the array. 24 | // 25 | //Your algorithm's runtime complexity must be in the order of O(log n). 26 | // 27 | //Example 1: 28 | // 29 | //Input: nums = [4,5,6,7,0,1,2], target = 0 30 | //Output: 4 31 | //Example 2: 32 | // 33 | //Input: nums = [4,5,6,7,0,1,2], target = 3 34 | //Output: -1 35 | Solution s = new Solution(); 36 | System.out.println(s.search(new int[]{4, 5, 6, 7, 0, 1, 2}, 0)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week3/ValidParenthesisString/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week3.ValidParenthesisString; 2 | 3 | public class Solution { 4 | public boolean checkValidString(String s) { 5 | // TODO: 4/16/20 plzz return to it back. 6 | // Time: O(N) s.length 7 | // Space: O(1) 8 | if (s.trim().equals("") || s == null) return true;//An empty string is also valid. 9 | int min = 0; 10 | int max = 0; 11 | var chars = s.toCharArray(); 12 | for (var ch : chars) { 13 | 14 | // max += (ch == '(' || ch == '*') ? 1 : -1; 15 | // min += (ch == ')' || ch == '*') ? -1 : 1; 16 | 17 | if (ch == '(') { 18 | min++; 19 | } else { 20 | min--; 21 | } 22 | if (ch != ')') { 23 | max++; 24 | } else { 25 | max--; 26 | } 27 | 28 | // min += (ch == '(') ? 1 : -1; 29 | // max += (ch != ')') ? 1 : -1; 30 | if (max < 0) return false; 31 | min = Math.max(0, min); 32 | } 33 | return min == 0; 34 | } 35 | 36 | 37 | public static void main(String[] args) { 38 | //Let diff be count of left parenthesis minus count of right parenthesis. 39 | // 40 | //When we meet: 41 | // 42 | //(, we increment diff. 43 | //), we decrement diff. 44 | //*, we have three choices which makes the diff become a range -- [diff - 1, diff + 1]. 45 | //So we use maxDiff/minDiff to record the maximum/minimum diff we can get. 46 | // 47 | //When we meet: 48 | // 49 | //(, ++maxDiff and ++minDiff. 50 | //), --maxDiff and --minDiff. 51 | //*, ++maxDiff and --minDiff. 52 | //If maxDiff become negative, it means it's already invalid, we should return false. 53 | // 54 | //Whenever minDiff falls below 0, we should force it to be 0 because we never accept negative diff during the process. 55 | // 56 | //After scanning through string s, as long as minDiff is 0, this string can be a valid one. 57 | Solution s = new Solution(); 58 | System.out.println(s.checkValidString("((*)")); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week4/BitwiseANDofNumbersRange/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week4.BitwiseANDofNumbersRange; 2 | 3 | public class Solution { 4 | //https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html 5 | //https://www.tutorialspoint.com/java/java_basic_operators.htm 6 | //https://www.geeksforgeeks.org/bitwise-operators-in-java/?ref=rp 7 | //https://stackoverflow.com/questions/2311476/whats-the-reason-high-level-languages-like-c-java-mask-the-bit-shift-count-ope 8 | //>>> is logical shift, >> is arithmetic shift. 9 | public int rangeBitwiseAnd(int m, int n) { 10 | if (m == 0) return 0; 11 | while (n > m) n = n & n - 1; 12 | return m & n; 13 | } 14 | 15 | //other solution 16 | // public int rangeBitwiseAnd(int m, int n) { 17 | // if (m == 0) return 0; 18 | // int ans = Integer.MAX_VALUE; 19 | // 20 | // for (int i = m; i <= n && i >= 0; i++) { 21 | // int negate = Integer.MAX_VALUE - i; 22 | // if (negate < n && negate > m) return 0; 23 | // ans = ans & i; 24 | // } 25 | // return ans; 26 | // } 27 | 28 | public static void main(String[] args) { 29 | 30 | //Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. 31 | // 32 | //Example 1: 33 | // 34 | //Input: [5,7] 35 | //Output: 4 36 | //Example 2: 37 | // 38 | //Input: [0,1] 39 | //Output: 0 40 | 41 | Solution s = new Solution(); 42 | System.out.println(s.rangeBitwiseAnd(5, 7)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week4/JumpGame/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week4.JumpGame; 2 | 3 | public class Solution { 4 | public boolean canJump(int[] nums) { 5 | int lastPos = nums.length - 1; 6 | for (int i = nums.length - 1; i >= 0; i--) { 7 | if (i + nums[i] >= lastPos) { 8 | lastPos = i; 9 | } 10 | } 11 | return lastPos == 0; 12 | } 13 | 14 | public static void main(String[] args) { 15 | Solution s = new Solution(); 16 | System.out.println(s.canJump(new int[]{2,3,1,1,4})); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week4/LRUCache/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week4.LRUCache; 2 | 3 | import java.util.LinkedHashMap; 4 | import java.util.Map; 5 | 6 | public class Solution { 7 | static class LRUCache { 8 | private Map cache; 9 | private int capacity; 10 | 11 | public LRUCache(int capacity) { 12 | this.cache = new LinkedHashMap<>(capacity); 13 | this.capacity = capacity; 14 | } 15 | 16 | public int get(int key) { 17 | if (!cache.containsKey(key)) return -1; 18 | int valueTemp = cache.get(key); 19 | cache.remove(key); 20 | cache.put(key, valueTemp); 21 | return valueTemp; 22 | } 23 | 24 | public void put(int key, int value) { 25 | // If already present, then 26 | // remove it first. Note that 27 | // we are going to add later 28 | if (cache.containsKey(key)) cache.remove(key); 29 | 30 | // If cache size is full, remove the least 31 | // recently used. 32 | else if (cache.size() >= capacity) cache.remove(cache.keySet().iterator().next()); 33 | cache.put(key, value); 34 | } 35 | } 36 | 37 | public static void main(String[] args) { 38 | 39 | LRUCache cache = new LRUCache(2); 40 | // 41 | cache.put(1, 1); 42 | cache.put(2, 2); 43 | System.out.println(cache.get(1)); // returns 1 44 | cache.put(3, 3); // evicts key 2 45 | System.out.println(cache.get(2)); // returns -1 (not found) 46 | cache.put(4, 4); // evicts key 1 47 | System.out.println(cache.get(1)); // returns -1 (not found) 48 | System.out.println(cache.get(3)); // returns 3 49 | System.out.println(cache.get(4)); // returns 4 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/company/thirty_day_leetcoding_challenge/week4/SubarraySumEqualsK/Solution.java: -------------------------------------------------------------------------------- 1 | package com.company.thirty_day_leetcoding_challenge.week4.SubarraySumEqualsK; 2 | 3 | import java.util.HashMap; 4 | 5 | public class Solution { 6 | public int subarraySum(int[] nums, int k) { 7 | int count = 0; 8 | int sum = 0; 9 | HashMap map = new HashMap<>(); 10 | map.put(0, 1); 11 | for (int num : nums) { 12 | sum += num; 13 | if (map.containsKey(sum - k)) count += map.get(sum - k); 14 | map.put(sum, map.getOrDefault(sum, 0) + 1); 15 | } 16 | return count; 17 | } 18 | 19 | public static void main(String[] args) { 20 | //Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. 21 | // 22 | //Example 1: 23 | //Input:nums = [1,1,1], k = 2 24 | //Output: 2 25 | //Note: 26 | //The length of the array is in range [1, 20,000]. 27 | //The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. 28 | 29 | Solution s = new Solution(); 30 | System.out.println(s.subarraySum(new int[]{1, 1, 1}, 2)); 31 | } 32 | } 33 | --------------------------------------------------------------------------------