├── .vscode └── settings.json ├── .github └── workflows │ └── greetings.yml ├── November2022 ├── C++ │ ├── nearest-exit-from-entrance-in-maze.cpp │ ├── submissions.cpp │ ├── ugly-number.cpp │ ├── maximum-69-number.cpp │ ├── make-the-string-great.cpp │ └── orderly-queue.cpp └── Java │ ├── Maximum69Number.java │ └── UglyNumber.java ├── .DS_Store ├── img ├── Fork.png ├── Logo.png ├── Pull.png ├── Discord.png ├── Issues.png ├── Stars.png ├── Video.png ├── Youtube.png ├── Instagram.png ├── Linkedin.png ├── Subscribe.png ├── Telegram.png └── Contributors.png ├── .gitignore ├── January2021 ├── .DS_Store ├── Count Sorted Vowel Strings.java ├── concatenation-of-consecutive-binary-numbers.java ├── CheckIfAll1'sAreatLeastLengthKPlacesAway.java ├── smallest-string-with-a-given-numeric-value.java └── CheckIfTwoStringArraysareEquivalent.java ├── February2022 ├── C++ │ ├── Untitled-2.jpg │ ├── Add_Digits.cpp │ ├── Subarray_sum_equals_k.cpp │ ├── 4SUMII.cpp │ └── Compare_Version_Numbers.cpp ├── Kotlin │ ├── CompareVersionNumber.jar │ ├── ExcelSheetColumnNumber.jar │ ├── SingleNumber.kt │ ├── MaximumDepthofBinaryTree.kt │ ├── SwapPairs.kt │ ├── AddDigits.kt │ ├── RemoveCoveredIntervals.kt │ ├── ExcelSheetColumnNumber.kt │ └── ContiguousArray.kt ├── Python │ ├── AddDigits.py │ ├── SingleNumber.py │ ├── MaximumDepthofBinaryTree.py │ ├── ExcelSheetColumnNumber.py │ ├── BestTimetoBuyandSellStock.py │ ├── ContiguosArray.py │ ├── 4SumII.py │ ├── CombinationSum.py │ ├── FindtheDifference.py │ ├── SubarraySumEqualsK.py │ └── MajorityElement.py └── Java │ ├── MaximumDepthOfBinaryTree.java │ ├── MaximumDepthofBinaryTree.java │ ├── SingleNumber.java │ ├── SwapNodes.java │ └── BestTimetoBuyandSellStock.java ├── Learnings └── designPattern │ └── strategy │ ├── Operation.java │ └── AddOperation.java ├── December2021 ├── C++ │ ├── PowerOfTwo.cpp │ ├── Power_Of_Two.cpp │ ├── Smallest_Integer_Divisible_by_K.cpp │ ├── Number_Compliment.cpp │ ├── Number Complement.cpp │ ├── Min_Cost_to_move_chips_to_same_position.cpp │ ├── Consecuteive_Characters.cpp │ ├── Domino_and_Tromino_Tiling.cpp │ ├── MergeIntervel.cpp │ └── Maximum_Product_Subarray.cpp ├── MinCostToMoveChipsToSamePosition.java ├── SmallestIntegerDivisiblebyK.java ├── ConsecutiveCharacters.java ├── DominoAndTrominoTiling.java ├── MiddleoftheLinkedList.java ├── BinaryTreeTilt.java ├── MinimumAbsoluteDifference.java └── PowerOfTwo.java ├── June2021 ├── img │ └── PascalsTriangleCombinationApproach.png ├── C++ │ └── Readme.md ├── python │ ├── Remove All Adjacent Duplicates in String │ └── Pascal's Triangle-Day 21.py └── MinCostClimbingStairs.java ├── Concepts ├── readme.md └── LongestIncreasingSubsequence.java ├── SystemDesign └── Lowlevel │ └── Solid │ ├── openclosed │ ├── noncompliant │ │ ├── Shape.java │ │ ├── Square.java │ │ ├── Circle.java │ │ └── Rectangle.java │ └── compliant │ │ ├── Shape.java │ │ ├── FindShapeStats.java │ │ ├── Square.java │ │ └── Circle.java │ ├── LiskovSubstitutionPrinciple │ └── src │ │ ├── LSPCompliant │ │ ├── Perks.java │ │ ├── IEmployee.java │ │ ├── IEmployeeSpecialAllowances.java │ │ ├── ContractEmployee.java │ │ └── Employee.java │ │ └── LSPViolated │ │ ├── Perks.java │ │ ├── IEmployee.java │ │ ├── ContractEmployee.java │ │ └── Employee.java │ ├── DependencyInversion │ └── src │ │ ├── DIPCompliant │ │ ├── Network.java │ │ ├── Jio.java │ │ ├── Airtel.java │ │ └── CodingDecoded.java │ │ └── DIPViolated │ │ ├── Jio.java │ │ ├── Airtel.java │ │ └── CodingDecoded.java │ └── InterfaceSegregationPrinciple │ └── src │ ├── ISPViolated │ ├── FOODITEMS.java │ ├── IMenu.java │ └── TasteOfIndia.java │ └── ISPCompliant │ ├── Redesign │ ├── FOODITEMS.java │ ├── IVegetarianMenu.java │ ├── INonVegetarianMenu.java │ └── TasteOfIndia.java │ └── AdaptorPattern │ ├── FOODITEMS.java │ ├── IVegetarianMenu.java │ ├── IMenu.java │ ├── MenuManager.java │ └── VegMenuAdapter.java ├── .idea ├── vcs.xml ├── .gitignore ├── modules.xml ├── Coding-Decoded.iml ├── misc.xml └── runConfigurations.xml ├── April2022 ├── C++ │ ├── reverse_string.cpp │ ├── Container_With_Most_Water.cpp │ ├── Trim_a_Binary_Search_Tree.cpp │ ├── Kth_Smallest_In_BST.cpp │ ├── Top_K_Frequent_Elements.cpp │ └── conatiner_with_most_water.cpp ├── Kotlin │ ├── ReverseString.kt │ ├── DesignHashSet.kt │ ├── SearchinaBST.kt │ ├── TopKFrequentElements.kt │ ├── SortArrayByParity.kt │ ├── ContainerwithMostWater.kt │ ├── TrimBST.kt │ └── SwappingNodesInALinkedList.kt └── Java │ ├── ReverseString.java │ └── ContainerWithMostWater.java ├── DesignPatterns └── src │ └── CreationalDesignPattern │ ├── FactoryDesignPattern │ ├── Car.java │ ├── CarType.java │ ├── VehicleInterface.java │ ├── Breeza.java │ ├── WagonR.java │ ├── Ciaz.java │ ├── Test.java │ └── MarutiCarFactory.java │ ├── PrototypeDesignPattern │ ├── Clone.java │ ├── VehicleInterface.java │ └── Car.java │ ├── AbstractFactoryDesignPattern │ ├── Car.java │ ├── CarType.java │ ├── VehicleInterface.java │ ├── MarutiCarFactory.java │ ├── WagonRCarFactory.java │ ├── CiazCarFactory.java │ ├── Breeza.java │ ├── WagonR.java │ ├── Ciaz.java │ └── Test.java │ └── BuilderDesignPattern │ └── Test.java ├── Contest ├── ANumberAfteraDoubleReversal.java ├── CountingWordsWithaGivenPrefix.java ├── CountNumberofWaystoPlaceHouses.java ├── DestroyingAsteroids.java ├── KeepMultiplyingFoundValuesbyTwo.java ├── PercentageofLetterinString.java ├── CheckifAllAsAppearsBeforeAllBs.java ├── CountOperationstoObtainZero.java ├── MostFrequentEvenElement.java ├── SumofNumbersWithUnitsDigitK.java ├── BiWeekly Contest 75 │ └── FindTriangularSumofanArray.java ├── MinimumDeletionstoMakeArrayBeautiful.java ├── NodeWithHighestEdgeScore.java ├── DecodetheMessage.java ├── MaximizetheTopmostElementAfterKMoves.java ├── IntersectionofMultipleArrays.java ├── Minimum AmountOfTimeToFillCups.java ├── FindtheWinneroftheCircularGame.java ├── LargestCombinationWithBitwiseANDGreaterThanZero.java ├── RearrangeArrayElementsbySign.java ├── Find TheOriginalArrayofPrefixXor.java ├── RemovingMinimumNumberofMagicBeans.java ├── TotalAppealofAString.java ├── LongestUploadedPrefix.java ├── MinimumConsecutiveCardstoPickUp.java ├── CalculateAmountPaidinTaxes.java ├── MinimumMovesToReachTargetScore.java ├── NumberofSmoothDescentPeriodsofaStock.java ├── CalculateDigitSumOfAString.java ├── MinimumElementstoAddtoFormaGivenSum.java ├── CountLatticePointsInsideaCircle.java ├── RemoveDigitFromNumbertoMaximizeResult.java ├── CheckifMatrixIsXMatrix.java ├── BiWeeklyContest76 │ ├── Find_Closest_Number_to_Zero.java │ └── Number_of_Ways_to_Buy_Pens_and_Pencils.java ├── EvaluateBooleanBinaryTree.java ├── NumberOfZero-FilledSubarrays.java ├── NumberofLaserBeamsinaBank.java ├── PartitionArraySuchThatMaximumDifferenceIsK.java ├── ReplaceNonCoprimeNumbersinArray.java ├── GreatestEnglishLetterinUpperandLowerCase.java ├── RemovingStarsFromaString.java ├── MaximumConsecutiveFloorsWithoutSpecialFloors.java ├── Sqrt(x).java ├── MinimumAmountofTimetoFillCups.java ├── MinimumNumberofStepstoMakeTwoStringsAnagramII.java ├── CountCollisionsonaRoad.java ├── FirstBadVersion.java ├── Weekly Contest 315 │ └── LargestPositiveIntegerThatExistsWithItsNegative.py └── SolvingQuestionsWithBrainpower.java ├── November2021 ├── ArrangingCoins.java ├── C++ │ ├── Minimium_Value_to_get_Positive.cpp │ ├── Best_Time_To_Buy_And_Sell_Stocks.cpp │ ├── Maximum_Subarray.cpp │ ├── Hamming_Distance.cpp │ ├── Search_Insert_Position.cpp │ ├── Unique_Paths.cpp │ ├── Product_of_Array_Except_Self.cpp │ ├── Daily_Temperatures.cpp │ └── Arranging_Coins.cpp ├── HammingDistance.java ├── UniqueBinarySearchTrees.java ├── MinimumValueToGetPositiveStepbyStepSum.java ├── SearchInsertPosition.java ├── SingleElementinaSortedArray.java ├── UniquePaths.java ├── FindAllNumbersDisappearedinanArray.java ├── SingleNumbersIII.java ├── RemoveLinkedListElements.java └── KthNumberinMultiplicationTable.java ├── Leetcode2023 ├── April2023 │ ├── Java │ │ ├── BulbSwitcher.java │ │ ├── AddDigits.java │ │ ├── OptimalPartitionOfString.java │ │ ├── SmallestNumberinInfiniteSet.java │ │ └── BinarySearch.java │ ├── Python │ │ ├── OptimalPartitionOfString.py │ │ └── BinarySearch.py │ └── C++ │ │ ├── 2439_Minimize_Maximum_of_Array.cpp │ │ ├── 1046.LastStoneWeight.cpp │ │ ├── 2390RemovingStarsFromAString.cpp │ │ ├── 1431.KidsWiththeGreatestNumberofCandies.cpp │ │ ├── BinarySearch.cpp │ │ ├── 1768.MergeStringsAlternately.cpp │ │ └── 946.ValidateStackSequences.cpp ├── February2023 │ ├── Java │ │ └── ShuffleTheArray.java │ └── C++ │ │ └── InsertPosition.cpp ├── March2023 │ └── C++ │ │ └── can-place-flowers.cpp ├── January2023 │ ├── C++ │ │ ├── DetectCapital.cpp │ │ ├── detectCapital.cpp │ │ ├── delete-columns-to-make-sorted.cpp │ │ ├── maximum-ice-cream-bars.cpp │ │ └── MaximumIceCreamBars.cpp │ └── Java │ │ ├── DetectCapital.java │ │ └── GasStation.java └── May2023 │ └── Java │ └── MatrixDiagonalSum.java ├── March2022 ├── C++ │ ├── Finddups.cpp │ ├── BrokenCalculator.cpp │ ├── Simplify_Path.cpp │ ├── partition_labels.cpp │ └── ScoreOfParentheses.cpp ├── Java │ ├── CountAllValidPickup.java │ └── BrokenCalculator.java └── Kotlin │ ├── CountAllValid.kt │ ├── IsSubsequence.kt │ ├── CountingBits.kt │ └── BinarySearch.kt ├── July2022 ├── C++ │ ├── FibonacciNumber.cpp │ ├── searchA2DMatrix2.cpp │ ├── FlattenBinaryTreeToLinkedList.cpp │ ├── pascalsTriangle.cpp │ ├── kInversePairsArray.cpp │ ├── validAnagram.cpp │ └── jumpGame6.cpp ├── Java │ ├── FibonacciNumber.java │ └── PascalsTriangle.java └── Python │ └── Readme.md ├── May2022 ├── Java │ ├── RunningSumOf1DArray.java │ ├── MissingNumber.java │ ├── NumberOfStepsToReduceANumberToZero.java │ ├── NumberOf1Bits.java │ ├── BackspaceStringCompare.java │ └── CoinChange.java └── C++ │ └── Implementing_stack_using_queues.cpp ├── October2022 ├── Java │ ├── CheckIfTheSentenceIsPangram.java │ ├── LargestPerimeterTriangle.java │ ├── ToeplitzMatrix.java │ ├── DeleteNodeInALinkedList.java │ ├── ContainsDuplicateII.java │ └── CheckIfTwoStringArraysAreEquivalent.java └── C++ │ ├── 1662. Check If Two String Arrays are Equivalent - C++ │ └── 219. Contains Duplicate II ├── June2022 └── Java │ ├── RunningSumof1dArray.java │ ├── PartitioningIntoMinimumNumberOfDeciBinaryNumbers.java │ ├── MinimumMovesToEqualArrayElementsII.java │ ├── MergeSortedArray.java │ └── QueueReconstructionbyHeight.java ├── August2022 ├── Java │ ├── PowerOfFour.java │ └── RansomNote.java └── C++ │ ├── PowerOfFour.cpp │ ├── mirrorReflection.cpp │ ├── PowerOfThree.cpp │ ├── LIS.cpp │ ├── myCalendar1.cpp │ ├── FirstUniqueCharacterInAString.cpp │ └── ValidateBinarySearchTree.cpp ├── January2022 ├── Java │ ├── RichestCustomerWealth.java │ ├── CanPlaceFlowers.java │ ├── RotateArray.java │ ├── StoneGameIV.java │ ├── ValidMountain.java │ ├── CarPooling.java │ └── AddBinary.java ├── C++ │ ├── Pairs_of_Songs_With_Total_Durations_Divisible_by_60.cpp │ ├── sum_of_root_to_leaf_binary_number.cpp │ ├── insert_into_BST.cpp │ ├── 142.Linked_List_Cycle_II.cpp │ ├── Add_Binary.cpp │ ├── add_binary.cpp │ ├── Sum_Of_Root_Leaf │ ├── Max_distance_to_Closest_person.cpp │ ├── Insert_into_binary_search_Tree.cpp │ ├── Solving_Questions_With_Brainpower.cpp │ ├── Divide_a_String_Into_Groups_of_Size_k.cpp │ ├── Maximize_Distance_to_Closest_Person.cpp │ ├── Linked_List_Cycle_II.cpp │ ├── Can_Place_Flowers.cpp │ └── can_place_flowers.cpp └── Python │ ├── stone_game_IV.py │ ├── linkedListCycleII.py │ ├── richest_customer_health.py │ ├── validMountain.py │ ├── detect_capital.py │ ├── gasStation.py │ ├── rotate_array.py │ ├── RobotBoundedInCircle.py │ └── minimum_number_of_arrows_to_burst_balloons.py ├── Mission └── Facebook │ └── TargetFacebook.md ├── August2023 ├── Kth Largest Element In A Array.cpp ├── Excel Columns.cpp └── Sliding Windows Maximum.cpp ├── July2021 ├── python │ ├── README.md │ └── Reshape the matrix ├── FindPeakElement.java ├── GrayCode.java ├── C++ │ ├── KthSmallestElementInASortedMatrix.cpp │ └── ReshapeTheMatrix.cpp ├── MaximumLengthofRepeatedSubarray.java └── ValidTriangleNumber.java ├── August2021 ├── RangeAdditionII.java ├── FindMinimuminRotatedSortedArray.java ├── SumofSquareNumbers.java └── VerifyPreorderSerializationofaBinaryTree.java ├── June2023 ├── Java │ └── FindTheHighestAltitude.java └── C++ │ └── FindTheHighestAltitude.cpp ├── May2021 ├── PartitioningIntoMinimumNumberOfDeciBinaryNumbers.java └── ToLowerCase.java ├── September2021 ├── MaxConsecutiveOnes.java └── ShiftingLetters.java ├── October2021 ├── PerfectSquares.java ├── ClimbingStairs.java ├── JumpGame.java ├── FindMinimumInRotatedSortedArrayII.java ├── FindAllDuplicatesinanArray.java └── SortColors.java ├── August2020 ├── PowerOfFour.java ├── FIndAllDuplicate.java ├── ImplementRand10UsingRand7.java ├── LongestPalindrome.java ├── SortArrayByPartiy.java └── HIndex.java ├── February2023 └── C++ │ ├── 121. Best Time to Buy and Sell Stock.cpp │ └── 28. Find the Index of the First Occurrence in a String.cpp ├── November2020 └── SmallestIntegerDivisiblebyK.java ├── September2022 └── Java │ └── ReverseWordsInAStringIII.java ├── December2022 ├── C++ │ └── climbing-stairs.cpp └── Java │ ├── HouseRobber.java │ ├── DominoAndTrominoTiling.java │ └── ClimbingStairs.java ├── March2021 ├── check-if-a-string-contains-allbinary-codesofsize-K.java └── DistributeCandies.java ├── February2021 ├── ValidateStackSequences.java ├── NumberofStepstoReduceNumbertoZero.java ├── NoOf1Bits.java └── ContainerWithMostWater.java ├── December2020 ├── SmallestRangeII.java └── IncreasingTripletSequence.java └── April2021 ├── BeautifulArrangementII.java └── MinimumOperationstoMakeArrayEqual.java /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/workflows/greetings.yml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /November2022/C++/nearest-exit-from-entrance-in-maze.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/.DS_Store -------------------------------------------------------------------------------- /img/Fork.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Fork.png -------------------------------------------------------------------------------- /img/Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Logo.png -------------------------------------------------------------------------------- /img/Pull.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Pull.png -------------------------------------------------------------------------------- /img/Discord.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Discord.png -------------------------------------------------------------------------------- /img/Issues.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Issues.png -------------------------------------------------------------------------------- /img/Stars.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Stars.png -------------------------------------------------------------------------------- /img/Video.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Video.png -------------------------------------------------------------------------------- /img/Youtube.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Youtube.png -------------------------------------------------------------------------------- /img/Instagram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Instagram.png -------------------------------------------------------------------------------- /img/Linkedin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Linkedin.png -------------------------------------------------------------------------------- /img/Subscribe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Subscribe.png -------------------------------------------------------------------------------- /img/Telegram.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Telegram.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | January2022/C++/can_place_flowers.cpp 3 | January2022/C++/Word_Pattern.cpp 4 | -------------------------------------------------------------------------------- /img/Contributors.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/img/Contributors.png -------------------------------------------------------------------------------- /January2021/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/January2021/.DS_Store -------------------------------------------------------------------------------- /February2022/C++/Untitled-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/February2022/C++/Untitled-2.jpg -------------------------------------------------------------------------------- /Learnings/designPattern/strategy/Operation.java: -------------------------------------------------------------------------------- 1 | 2 | public Interface Operatioin{ 3 | public abstract void doOperation(); 4 | } -------------------------------------------------------------------------------- /February2022/Kotlin/CompareVersionNumber.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/February2022/Kotlin/CompareVersionNumber.jar -------------------------------------------------------------------------------- /February2022/Kotlin/ExcelSheetColumnNumber.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/February2022/Kotlin/ExcelSheetColumnNumber.jar -------------------------------------------------------------------------------- /December2021/C++/PowerOfTwo.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | return n > 0 && not (n & n - 1); 5 | } 6 | }; 7 | -------------------------------------------------------------------------------- /June2021/img/PascalsTriangleCombinationApproach.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Sunchit/Coding-Decoded/HEAD/June2021/img/PascalsTriangleCombinationApproach.png -------------------------------------------------------------------------------- /Concepts/readme.md: -------------------------------------------------------------------------------- 1 | ## General Concepts 2 | 3 | ### Java Solutions 4 | 1) [Longest Increasing Subsequence](/Concepts/LongestIncreasingSubsequence.java) 5 | -------------------------------------------------------------------------------- /Learnings/designPattern/strategy/AddOperation.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class AddOperation implements Operation { 4 | public void doOperation(){ 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/openclosed/noncompliant/Shape.java: -------------------------------------------------------------------------------- 1 | package com.codingdecoded.lowlevel.solid.openclosed.noncompliant; 2 | 3 | public interface Shape { 4 | } 5 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPCompliant/Perks.java: -------------------------------------------------------------------------------- 1 | package LSPCompliant; 2 | 3 | public enum Perks { 4 | CAR_ALLOWANCE, 5 | LTA, 6 | PF 7 | } 8 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPViolated/Perks.java: -------------------------------------------------------------------------------- 1 | package LSPViolated; 2 | 3 | public enum Perks { 4 | CAR_ALLOWANCE, 5 | LTA, 6 | PF 7 | } 8 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPCompliant/IEmployee.java: -------------------------------------------------------------------------------- 1 | package LSPCompliant; 2 | 3 | public interface IEmployee { 4 | 5 | Double getSalary(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /December2021/C++/Power_Of_Two.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | if(n<1) return false; 5 | n-=(n& (-n)); 6 | return n==0; 7 | } 8 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/DependencyInversion/src/DIPCompliant/Network.java: -------------------------------------------------------------------------------- 1 | package DIPCompliant; 2 | 3 | public interface Network { 4 | 5 | void makeCall(int stdCode, int no); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /April2022/C++/reverse_string.cpp: -------------------------------------------------------------------------------- 1 | //Problem Link: https://leetcode.com/problems/reverse-string/ 2 | class Solution { 3 | public: 4 | void reverseString(vector& s) { 5 | 6 | } 7 | }; 8 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/Car.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public abstract class Car implements VehicleInterface { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/PrototypeDesignPattern/Clone.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.PrototypeDesignPattern; 2 | 3 | public interface Clone { 4 | public Car getClone(Car car); 5 | } 6 | -------------------------------------------------------------------------------- /Contest/ANumberAfteraDoubleReversal.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(1) 4 | // SC : O(1) 5 | public boolean isSameAfterReversals(int num) { 6 | return num == 0 || !String.valueOf(num).endsWith("0"); 7 | } 8 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/Car.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public abstract class Car implements VehicleInterface { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/openclosed/compliant/Shape.java: -------------------------------------------------------------------------------- 1 | package com.codingdecoded.lowlevel.solid.openclosed.compliant; 2 | 3 | public interface Shape { 4 | Double getArea(); 5 | Double getPerimeter(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/CarType.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public enum CarType { 4 | HATCHBACK, SEDAN, SUV 5 | 6 | } 7 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/CarType.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public enum CarType { 4 | HATCHBACK, 5 | SEDAN, 6 | SUV 7 | 8 | } 9 | -------------------------------------------------------------------------------- /February2022/C++/Add_Digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | if(num<10) return num; 5 | 6 | int res=num%9; 7 | return res?res:9; 8 | 9 | } 10 | }; -------------------------------------------------------------------------------- /November2021/ArrangingCoins.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arrangeCoins(int n) { 3 | int rowCount = 0; 4 | 5 | while(n>0){ 6 | rowCount++; 7 | n = n - rowCount; 8 | } 9 | return n==0 ? rowCount : rowCount-1; 10 | } 11 | } -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPViolated/FOODITEMS.java: -------------------------------------------------------------------------------- 1 | package ISPViolated; 2 | 3 | public enum FOODITEMS { 4 | 5 | BUTTER_CHICKEN, 6 | PRAWNS, 7 | DAL_MAKNI, 8 | PANEER_PASANDA; 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Leetcode2023/April2023/Java/BulbSwitcher.java: -------------------------------------------------------------------------------- 1 | // Author: Mahesh Reddy B N 2 | 3 | // Problem Link: https://leetcode.com/problems/bulb-switcher/ 4 | 5 | class Solution { 6 | public int bulbSwitch(int n) { 7 | return (int)Math.sqrt(n); 8 | } 9 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/VehicleInterface.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public interface VehicleInterface { 4 | public int getEngineCC(); 5 | public int getPrice(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/PrototypeDesignPattern/VehicleInterface.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.PrototypeDesignPattern; 2 | 3 | public interface VehicleInterface { 4 | public int getEngineCC(); 5 | public int getPrice(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/Redesign/FOODITEMS.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.Redesign; 2 | 3 | public enum FOODITEMS { 4 | 5 | BUTTER_CHICKEN, 6 | PRAWNS, 7 | DAL_MAKNI, 8 | PANEER_PASANDA; 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/VehicleInterface.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public interface VehicleInterface { 4 | public int getEngineCC(); 5 | public int getPrice(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /February2022/Kotlin/SingleNumber.kt: -------------------------------------------------------------------------------- 1 | // Created by LeetCode : shobhitbruh 2 | class Solution { 3 | fun singleNumber(nums: IntArray): Int { 4 | var x = 0 5 | for (`val` in nums) { 6 | x = x xor `val` 7 | } 8 | return x 9 | } 10 | } -------------------------------------------------------------------------------- /February2022/Python/AddDigits.py: -------------------------------------------------------------------------------- 1 | # TC : O(1) 2 | # SC : O(1) 3 | 4 | class Solution: 5 | def addDigits(self, num: int) -> int: 6 | if num <= 9: 7 | return num 8 | if num % 9 == 0: 9 | return 9 10 | return num % 9 11 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/AdaptorPattern/FOODITEMS.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.AdaptorPattern; 2 | 3 | public enum FOODITEMS { 4 | 5 | BUTTER_CHICKEN, 6 | PRAWNS, 7 | DAL_MAKNI, 8 | PANEER_PASANDA; 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/Redesign/IVegetarianMenu.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.Redesign; 2 | 3 | import java.util.List; 4 | 5 | public interface IVegetarianMenu { 6 | 7 | List getVegetarianItems(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /December2021/C++/Smallest_Integer_Divisible_by_K.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int smallestRepunitDivByK(int K) { 4 | for (int r = 0, N = 1; N <= K; ++N) 5 | if ((r = (r * 10 + 1) % K) == 0) 6 | return N; 7 | return -1; 8 | } 9 | }; -------------------------------------------------------------------------------- /February2022/Python/SingleNumber.py: -------------------------------------------------------------------------------- 1 | # Time Complexity : O(N) 2 | # Space Complexity : O(1) 3 | 4 | class Solution: 5 | def singleNumber(self, nums: List[int]) -> int: 6 | result = 0 7 | for i in nums: 8 | result ^= i 9 | return result 10 | -------------------------------------------------------------------------------- /December2021/MinCostToMoveChipsToSamePosition.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostToMoveChips(int[] position) { 3 | int even = 0, odd = 0; 4 | 5 | for(int p : position){ 6 | if(p%2==0) even++; 7 | else odd++; 8 | } 9 | 10 | return Math.min(even,odd); 11 | } 12 | } -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/AdaptorPattern/IVegetarianMenu.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.AdaptorPattern; 2 | 3 | import java.util.List; 4 | 5 | public interface IVegetarianMenu { 6 | 7 | List getVegetarianItems(); 8 | 9 | } 10 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPViolated/IMenu.java: -------------------------------------------------------------------------------- 1 | package ISPViolated; 2 | 3 | import java.util.List; 4 | 5 | public interface IMenu { 6 | 7 | List getVegetarianItems(); 8 | 9 | List getNonVegetarianItems(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/Redesign/INonVegetarianMenu.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.Redesign; 2 | 3 | import java.util.List; 4 | 5 | 6 | public interface INonVegetarianMenu { 7 | 8 | List getNonVegetarianItems(); 9 | 10 | } 11 | -------------------------------------------------------------------------------- /December2021/C++/Number_Compliment.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | long long temp=1; 5 | while(num>=temp) 6 | { 7 | num=num^temp; 8 | temp=temp<<1; 9 | } 10 | return num; 11 | } 12 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPViolated/IEmployee.java: -------------------------------------------------------------------------------- 1 | package LSPViolated; 2 | 3 | import java.util.List; 4 | 5 | public interface IEmployee { 6 | 7 | Double calculateBonus(); 8 | 9 | Double getSalary(); 10 | 11 | List getPerks(); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /December2021/C++/Number Complement.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findComplement(int num) { 4 | long long temp=1; 5 | while(num>=temp) 6 | { 7 | num=num^temp; 8 | temp=temp<<1; 9 | } 10 | return num; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /March2022/C++/Finddups.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findDuplicate(vector& nums) { 4 | for(auto& i:nums) 5 | { 6 | if(nums[abs(i)]<0) return abs(i); 7 | else nums[abs(i)]=-nums[abs(i)]; 8 | } 9 | return -1; 10 | } 11 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPCompliant/IEmployeeSpecialAllowances.java: -------------------------------------------------------------------------------- 1 | package LSPCompliant; 2 | 3 | import java.util.List; 4 | 5 | public interface IEmployeeSpecialAllowances { 6 | 7 | Double calculateBonus(); 8 | 9 | List getPerks(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/MarutiCarFactory.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public abstract class MarutiCarFactory { 4 | 5 | public MarutiCarFactory() { 6 | } 7 | 8 | public abstract Car getCar(); 9 | } 10 | -------------------------------------------------------------------------------- /February2022/Kotlin/MaximumDepthofBinaryTree.kt: -------------------------------------------------------------------------------- 1 | //Author: Shobhit Behl (LeetCode:shobhitbruh) 2 | class Solution { 3 | fun maxDepth(root: TreeNode?): Int { 4 | return if (root == null) { 5 | 0 6 | } else Math.max(maxDepth(root.left), maxDepth(root.right)) + 1 7 | } 8 | } -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/DependencyInversion/src/DIPViolated/Jio.java: -------------------------------------------------------------------------------- 1 | package DIPViolated; 2 | 3 | public class Jio { 4 | 5 | public void makeCall(int stdCode, int no){ 6 | System.out.println("Making a call via jio network on " 7 | + stdCode + "-"+ no); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/WagonRCarFactory.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public class WagonRCarFactory extends MarutiCarFactory { 4 | 5 | public Car getCar() { 6 | return new WagonR(); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /July2022/C++/FibonacciNumber.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fib(int n) { 4 | 5 | int a = 0, b = 1, c = 0; 6 | for(int i=0;i getVegetarianItems(); 8 | 9 | List getNonVegetarianItems(); 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Contest/CountingWordsWithaGivenPrefix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(no of words * pref length) 3 | // SC : O(1) 4 | public int prefixCount(String[] words, String pref) { 5 | int count = 0; 6 | for(String word: words){ 7 | count += (word.startsWith(pref) == true) ? 1 :0 ; 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /November2021/C++/Minimium_Value_to_get_Positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) { 4 | int sum=0,min_val=0; 5 | for(auto& i:nums) 6 | { 7 | sum+=i; 8 | min_val=min(min_val,sum); 9 | } 10 | return 1-min_val; 11 | } 12 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/DependencyInversion/src/DIPCompliant/Jio.java: -------------------------------------------------------------------------------- 1 | package DIPCompliant; 2 | 3 | public class Jio implements Network { 4 | 5 | public void makeCall(int stdCode, int no){ 6 | System.out.println("Making a call via jio network on " 7 | + stdCode + "-"+ no); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /October2022/Java/CheckIfTheSentenceIsPangram.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public boolean checkIfPangram(String sentence) { 5 | for (char c = 'a'; c <= 'z'; c++) { 6 | if (sentence.indexOf(c) == -1) 7 | return false; 8 | } 9 | return true; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/DependencyInversion/src/DIPCompliant/Airtel.java: -------------------------------------------------------------------------------- 1 | package DIPCompliant; 2 | 3 | public class Airtel implements Network{ 4 | 5 | public void makeCall(int stdCode, int no){ 6 | System.out.println("Making a call via airtel network on " 7 | + stdCode + "-"+ no); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /February2022/Java/SingleNumber.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // Easy 4 | 5 | class Solution { 6 | public int singleNumber(int[] nums) { 7 | int l = nums.length; 8 | int res = 0; 9 | for (int i = 0; i < l; i++) { 10 | res = res ^ nums[i]; 11 | } 12 | return res; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /February2022/Python/MaximumDepthofBinaryTree.py: -------------------------------------------------------------------------------- 1 | # TC : O(N) 2 | # SC : O(N) 3 | class Solution: 4 | def maxDepth(self, root: Optional[TreeNode]) -> int: 5 | if not root: 6 | return 0 7 | left = 1+self.maxDepth(root.left) 8 | right = 1+self.maxDepth(root.right) 9 | return max(left, right) 10 | -------------------------------------------------------------------------------- /June2022/Java/RunningSumof1dArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(1) 4 | public int[] runningSum(int[] nums) { 5 | int rSum = 0; 6 | for(int i=0;i>i)&1)==1){ // right 9 | setBitCount++; 10 | } 11 | } 12 | return setBitCount; 13 | } 14 | } -------------------------------------------------------------------------------- /November2021/UniqueBinarySearchTrees.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n2) 3 | private int[] dp = new int[20]; 4 | public int numTrees(int n) { 5 | 6 | if(n <= 1) return 1; 7 | if(dp[n]>0) return dp[n]; 8 | for(int i = 1; i <= n; i++) 9 | dp[n] += numTrees(i-1) * numTrees(n-i); 10 | return dp[n]; 11 | 12 | } 13 | 14 | } -------------------------------------------------------------------------------- /June2021/C++/Readme.md: -------------------------------------------------------------------------------- 1 | ### June 2021 Leetcode C++ Solution 2 | 3 | 1. [SwiminRisingWater - Day 20]() 4 | 2. [OutOfBoundaryPaths - Day 24](/June2021/C++/OutOfBoundaryPaths.cpp) 5 | 3. [RedundantConnection - Day 25](/June2021/C++/RedundantConnection.cpp) 6 | 4. [Max Area of Island - Day 1](/June2021/C++/Max%20Area%20of%20Island.cpp) 7 | -------------------------------------------------------------------------------- /August2022/Java/PowerOfFour.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(log n base 4) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public boolean isPowerOfFour(int n) { 8 | double loggy = Math.log(n)/Math.log(4); 9 | int ilog = (int)loggy; 10 | loggy = Math.ceil(loggy); 11 | return (loggy==ilog); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /December2021/C++/Min_Cost_to_move_chips_to_same_position.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minCostToMoveChips(vector& position) { 4 | int even=0,odd=0; 5 | for(auto& i:position) 6 | { 7 | if (i%2) ++odd; 8 | else ++even; 9 | } 10 | return min(odd,even); 11 | } 12 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/openclosed/noncompliant/Square.java: -------------------------------------------------------------------------------- 1 | package com.codingdecoded.lowlevel.solid.openclosed.noncompliant; 2 | 3 | public class Square implements Shape { 4 | 5 | private int side; 6 | 7 | public int getSide() { 8 | return side; 9 | } 10 | 11 | public void setSide(int side) { 12 | this.side = side; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /November2021/MinimumValueToGetPositiveStepbyStepSum.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC: O(1) 4 | public int minStartValue(int[] nums) { 5 | int min = 0; 6 | int prefixSum = 0; 7 | for(int el: nums){ 8 | prefixSum += el; 9 | min = Math.min(min, prefixSum); 10 | } 11 | return min <0 ? Math.abs(min) + 1 : 1; 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode2023/April2023/Python/OptimalPartitionOfString.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def partitionString(self, s: str) -> int: 3 | char_set = set() 4 | count = 1 5 | for c in s: 6 | if c in char_set: 7 | count += 1 8 | char_set.clear() 9 | char_set.add(c) 10 | return count 11 | -------------------------------------------------------------------------------- /Contest/CountNumberofWaystoPlaceHouses.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(n) 4 | public int countHousePlacements(int n) { 5 | long mod=1000000007; 6 | long dp[]=new long[n+1]; 7 | dp[0]=1; 8 | dp[1]=2; 9 | for(int i=2;i<=n;i++){ 10 | dp[i]=(dp[i-1]+dp[i-2])%mod; 11 | } 12 | return (int)((dp[n]*dp[n])%mod); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/Breeza.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public class Breeza extends Car { 4 | @Override 5 | public int getEngineCC() { 6 | return 1500; 7 | } 8 | 9 | @Override 10 | public int getPrice() { 11 | return 900000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/WagonR.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public class WagonR extends Car{ 4 | @Override 5 | public int getEngineCC() { 6 | return 1200; 7 | } 8 | 9 | @Override 10 | public int getPrice() { 11 | return 500000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /January2022/Java/RichestCustomerWealth.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumWealth(int[][] accounts) { 3 | int max = Integer.MIN_VALUE; 4 | for(int[] account:accounts){ 5 | int currWealth = 0; 6 | for(int wealth : account) { 7 | currWealth += wealth; 8 | } 9 | max = Math.max(currWealth, max); 10 | } 11 | return max; 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /November2021/C++/Best_Time_To_Buy_And_Sell_Stocks.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | int profit=0; 5 | for(int i=1;iprices[i-1]) 8 | profit+=prices[i]-prices[i-1]; 9 | } 10 | return profit; 11 | } 12 | }; -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPCompliant/ContractEmployee.java: -------------------------------------------------------------------------------- 1 | package LSPCompliant; 2 | 3 | public class ContractEmployee extends Employee { 4 | 5 | public ContractEmployee(Integer id, String name) { 6 | super(id, name); 7 | } 8 | 9 | @Override 10 | public Double getSalary() { 11 | return 25000d; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/openclosed/noncompliant/Circle.java: -------------------------------------------------------------------------------- 1 | package com.codingdecoded.lowlevel.solid.openclosed.noncompliant; 2 | 3 | public class Circle implements Shape{ 4 | 5 | private int radius; 6 | 7 | public int getRadius() { 8 | return radius; 9 | } 10 | 11 | public void setRadius(int radius) { 12 | this.radius = radius; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Contest/DestroyingAsteroids.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Tc : O(nlogn) 3 | // SC : o(1) 4 | public boolean asteroidsDestroyed(int mass, int[] asteroids) { 5 | Arrays.sort(asteroids); 6 | long masss = mass; 7 | for (int asteroid : asteroids) { 8 | if ((long) asteroid > masss) return false; 9 | masss += asteroid; 10 | } 11 | return true; 12 | } 13 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/Ciaz.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public class Ciaz extends Car { 4 | 5 | @Override 6 | public int getEngineCC() { 7 | return 1500; 8 | } 9 | 10 | @Override 11 | public int getPrice() { 12 | return 1300000; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /November2021/C++/Maximum_Subarray.cpp: -------------------------------------------------------------------------------- 1 | // author:shyam2520 2 | class Solution { 3 | public: 4 | int maxSubArray(vector& nums) { 5 | int res=nums[0],sum=nums[0]; 6 | for(int i=1;i set = new HashSet<>(); 6 | for(int el: nums){ 7 | set.add(el); 8 | } 9 | 10 | while(set.contains(original)){ 11 | original = 2*original; 12 | } 13 | return original; 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /Leetcode2023/February2023/Java/ShuffleTheArray.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | class Solution { 3 | public int[] shuffle(int[] nums, int n) { 4 | int[] result = new int[2 * n]; 5 | for (int i = 0; i < n; ++i) { 6 | result[2 * i] = nums[i]; 7 | result[2 * i + 1] = nums[n + i]; 8 | } 9 | return result; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Mission/Facebook/TargetFacebook.md: -------------------------------------------------------------------------------- 1 | ### Mission Facebook 2022 2 | 3 | Below are the helpful links that for all those who are aspiring for Facebook/Meta 4 | 5 | 1) [DSA Sheet](./FacebookDSA.md) 6 | 2) [Facebook Interview Experience](https://www.youtube.com/watch?v=Dkd6GuUcD44) 7 | 3) [Facebook Useful Link by Adam Kniffin](./../../UsefulResources/ResourcesForMetaPrepByAdamKniffin.md) -------------------------------------------------------------------------------- /November2022/C++/submissions.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | 5 | if(n == 0){ 6 | return false; 7 | } 8 | 9 | for(int k =2; k <= 5; k++){ 10 | while(n%k==0){ 11 | n = n/k; 12 | } 13 | } 14 | 15 | return n==1; 16 | } 17 | }; -------------------------------------------------------------------------------- /November2022/C++/ugly-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isUgly(int n) { 4 | 5 | if(n == 0){ 6 | return false; 7 | } 8 | 9 | for(int k =2; k <= 5; k++){ 10 | while(n%k==0){ 11 | n = n/k; 12 | } 13 | } 14 | 15 | return n==1; 16 | } 17 | }; -------------------------------------------------------------------------------- /.idea/Coding-Decoded.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /August2023 /Kth Largest Element In A Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findKthLargest(vector& nums, int k) { 4 | priority_queue,greater> pq; 5 | for(auto i :nums){ 6 | pq.push(i); 7 | if(pq.size()>k) 8 | pq.pop(); 9 | } 10 | return pq.top(); 11 | 12 | } 13 | }; -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/Breeza.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public class Breeza extends Car { 4 | @Override 5 | public int getEngineCC() { 6 | return 1500; 7 | } 8 | 9 | @Override 10 | public int getPrice() { 11 | return 900000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/WagonR.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public class WagonR extends Car { 4 | @Override 5 | public int getEngineCC() { 6 | return 1200; 7 | } 8 | 9 | @Override 10 | public int getPrice() { 11 | return 500000; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /July2021/python/README.md: -------------------------------------------------------------------------------- 1 |

July 2021 Leetcode Python Solution

2 |

1.Find k closest elements-Day2

3 |

2.Reshape the matrix-Day5

4 | 5 | -------------------------------------------------------------------------------- /May2022/Java/MissingNumber.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | // TC : O(n) 3 | // SC : O(1) 4 | 5 | class Solution { 6 | public int missingNumber(int[] nums) { 7 | int l = nums.length; 8 | int tsum = l * (l+1) / 2; 9 | int csum = 0; 10 | for (int n : nums) { 11 | csum += n; 12 | } 13 | return tsum-csum; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /August2022/C++/PowerOfFour.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // TC: O(logn) 4 | // SC: O(logn) 5 | bool isPowerOfFour(int n) { 6 | if (n == 1) { 7 | return true; 8 | } 9 | if (n % 4 == 0 && n != 0) { 10 | return isPowerOfFour(n / 4); 11 | } else { 12 | return false; 13 | } 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /Contest/PercentageofLetterinString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | //TC : O(n) 3 | // SC : O(1) 4 | // 100* (count of letter in input string/length of string ) 5 | public int percentageLetter(String s, char letter) { 6 | int count = 0; 7 | for(char c: s.toCharArray()) { 8 | if(c == letter) { 9 | count++; 10 | } 11 | } 12 | return (100*count)/s.length(); 13 | } 14 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/Ciaz.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public class Ciaz extends Car { 4 | 5 | @Override 6 | public int getEngineCC() { 7 | return 1500; 8 | } 9 | 10 | @Override 11 | public int getPrice() { 12 | return 1300000; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /January2022/C++/Pairs_of_Songs_With_Total_Durations_Divisible_by_60.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numPairsDivisibleBy60(vector& time) { 4 | vector dict(60,0); 5 | int res=0; 6 | for(auto& i:time) 7 | { 8 | res+=dict[(60-i%60)%60]; 9 | ++dict[i%60]; 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /August2022/C++/mirrorReflection.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int mirrorReflection(int p, int q) { 4 | 5 | while(p%2==0 and q%2==0) 6 | { 7 | p>>=1; 8 | q>>=1; 9 | } 10 | if(p%2==0 and q%2==1) return 2; 11 | else if(p%2==1 and q%2==1) return 1; 12 | return 0; 13 | 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /January2022/C++/sum_of_root_to_leaf_binary_number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sumRootToLeaf(TreeNode* root, int num=0) { 4 | if(!root) 5 | return 0; 6 | num=2*num+root->val; 7 | if(!root->left&&!root->right) 8 | return num; 9 | return sumRootToLeaf(root->left,num)+sumRootToLeaf(root->right,num); 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /March2022/Java/CountAllValidPickup.java: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | public int countOrders(int n) { 4 | long[] dp=new long[n+1]; 5 | dp[1]=1; 6 | long mod=1000000007; 7 | for(int i=2;i 2 | 3 | 4 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /April2022/Kotlin/ReverseString.kt: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | fun reverseString(s: CharArray) { 4 | val sb = StringBuilder() 5 | for (x in s) { 6 | sb.append(x) 7 | } 8 | val y = sb.reverse().toString().toCharArray() 9 | for (i in s.indices) { 10 | s[i] = y[i] 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 9 | 10 | -------------------------------------------------------------------------------- /Contest/CheckifAllAsAppearsBeforeAllBs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Tc : O(len of string) 3 | // SC: O(1) 4 | public boolean checkString(String s) { 5 | boolean sawB = false; 6 | for(char c : s.toCharArray()){ 7 | if(c=='a' && sawB){ 8 | // voilation 9 | return false; 10 | } 11 | else if(c=='b'){ 12 | sawB = true; 13 | } 14 | } 15 | return true; 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /December2021/SmallestIntegerDivisiblebyK.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(K) 3 | // SC : O(1) 4 | public int smallestRepunitDivByK(int k) { 5 | if (k % 2 == 0 || k % 5 == 0) return -1; 6 | int remainder = 0; 7 | for (int counter = 1; counter <= k; counter++) { 8 | remainder = (remainder * 10 + 1) % k; 9 | if (remainder == 0) return counter; 10 | } 11 | return -1; 12 | } 13 | } -------------------------------------------------------------------------------- /June2022/Java/PartitioningIntoMinimumNumberOfDeciBinaryNumbers.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(n) 4 | // SC : O(1) 5 | 6 | // Max of all digits 7 | 8 | class Solution { 9 | public int minPartitions(String n) { 10 | int res = 0; 11 | for (char ch : n.toCharArray()) { 12 | res = Math.max(res, ch-48); 13 | } 14 | return res; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /November2021/SearchInsertPosition.java: -------------------------------------------------------------------------------- 1 | // TC : O(logn) 2 | // SC : O(1) 3 | class Solution { 4 | public int searchInsert(int[] nums, int target) { 5 | int low = 0; 6 | int high = nums.length; 7 | while(low=target){ 10 | high = mid; 11 | } else { 12 | low = mid +1; 13 | } 14 | } 15 | return low; 16 | } 17 | } -------------------------------------------------------------------------------- /April2022/C++/Container_With_Most_Water.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxArea(vector& height) { 4 | int l=0,r=height.size()-1; 5 | int res=0; 6 | while(l int: 6 | columnNumber = 0 7 | for ind in range(len(columnTitle)): 8 | currCharPos = ord(columnTitle[ind])-64 9 | columnNumber *= 26 10 | columnNumber += currCharPos 11 | return columnNumber 12 | -------------------------------------------------------------------------------- /July2022/C++/searchA2DMatrix2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool searchMatrix(vector>& a, int target) { 4 | 5 | int n = a.size(); 6 | int m = a[0].size(); 7 | for(int i=0;i= 0; i--) 8 | if (nums[i] + nums[i+1] > nums[i+2]) 9 | return nums[i] + nums[i+1] + nums[i+2]; 10 | return 0; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /September2021/MaxConsecutiveOnes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMaxConsecutiveOnes(int[] nums) { 3 | 4 | int mxCount = nums[0]; 5 | for(int i=1;ival) 7 | root->left = insertIntoBST(root->left,val); 8 | if(val>root->val) 9 | root->right = insertIntoBST(root->right,val); 10 | return root; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /April2022/Java/ReverseString.java: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | public void reverseString(char[] s) { 4 | StringBuilder sb=new StringBuilder(); 5 | for(char x:s){ 6 | sb.append(x); 7 | } 8 | char[] y=sb.reverse().toString().toCharArray(); 9 | for(int i=0;inums2){ 7 | count += nums1/nums2; 8 | nums1 = nums1 % nums2; 9 | } else { 10 | 11 | count += nums2/nums1; 12 | nums2 = nums2 % nums1; 13 | 14 | } 15 | 16 | } 17 | 18 | return count; 19 | } 20 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/BuilderDesignPattern/Test.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.BuilderDesignPattern; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | // method chaining 6 | Car.CarBuilder carBuilder = new Car.CarBuilder(2000, 2000000); 7 | carBuilder.setModelYear(2023); 8 | Car car = carBuilder.build(); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /July2022/Java/FibonacciNumber.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(n) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public int fib(int n) { 8 | int res = 0; 9 | int a = 0, b = 1; 10 | if (n == 1) return 1; 11 | for (int i = 2; i <= n; i++) { 12 | res = a + b; 13 | a = b; 14 | b = res; 15 | } 16 | return res; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /June2023/C++/FindTheHighestAltitude.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& gain) { 4 | int maxAltitude = 0; 5 | int currAltitude = 0; 6 | 7 | for (int it : gain) { 8 | currAltitude += it; 9 | maxAltitude = max(maxAltitude, currAltitude); 10 | } 11 | 12 | return maxAltitude; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode2023/March2023/C++/can-place-flowers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canPlaceFlowers(vector& nums, int n) { 4 | 5 | nums.insert(nums.begin(), 0); 6 | nums.push_back(0); 7 | 8 | for(int i=1;i& nums, int k) { 4 | unordered_map dict; 5 | int sum=0,res=0; 6 | dict[0]++; 7 | for(auto& i:nums) 8 | { 9 | sum+=i; 10 | if(dict.count(sum-k)) res+=dict[sum-k]; 11 | dict[sum]++; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /January2022/Python/stone_game_IV.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | class Solution: 4 | @lru_cache(None) 5 | def winnerSquareGame(self, n: int) -> bool: 6 | if n == 0: 7 | return False 8 | for i in range(1, int(n**0.5)+1): 9 | if not self.winnerSquareGame(n-(i*i)): 10 | return True 11 | return False 12 | -------------------------------------------------------------------------------- /April2022/Kotlin/DesignHashSet.kt: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class MyHashSet() { 3 | var hs: HashSet 4 | fun add(key: Int) { 5 | hs.add(key) 6 | } 7 | 8 | fun remove(key: Int) { 9 | hs.remove(key) 10 | } 11 | 12 | fun contains(key: Int): Boolean { 13 | return hs.contains(key) 14 | } 15 | 16 | init { 17 | hs = HashSet() 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /December2021/C++/Domino_and_Tromino_Tiling.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTilings(int n) { 4 | int mod=1e9+7; 5 | if(n<3) 6 | return n; 7 | vector dp(n+1,0); 8 | dp[1]=1; 9 | dp[2]=2; 10 | dp[3]=5; 11 | for(int i=4;i<=n;i++) 12 | dp[i]=(2*dp[i-1]%mod +dp[i-3]%mod)%mod; 13 | return dp[n]; 14 | } 15 | 16 | }; -------------------------------------------------------------------------------- /February2022/C++/4SUMII.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int fourSumCount(vector& nums1, vector& nums2, vector& nums3, vector& nums4) { 4 | unordered_map dict; 5 | int res=0; 6 | for(auto& i:nums1) for(auto& j:nums2) dict[i+j]++; 7 | for(auto& i:nums3) for(auto& j:nums4) if(dict[-(i+j)]) res+=dict[-(i+j)]; 8 | 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /Leetcode2023/April2023/C++/2439_Minimize_Maximum_of_Array.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimizeArrayValue(vector& nums) { 4 | 5 | int n = nums.size(); 6 | long sum = 0, maxi = 0; 7 | 8 | for(int i=0;i& nums, int target) { 4 | int l=0,r=nums.size()-1; 5 | int mid; 6 | while(l<=r) 7 | { 8 | mid=l+(r-l)/2; 9 | if(nums[mid]= 0; i-- ) { 7 | shift = (shift + shifts[i])%26; 8 | str[i] = (char)((str[i] - 'a' + shift) %26 + 'a'); 9 | } 10 | 11 | return new String(str); 12 | } 13 | } -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/AdaptorPattern/MenuManager.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.AdaptorPattern; 2 | 3 | public class MenuManager { 4 | 5 | public static void main(String[] args) { 6 | 7 | CombinedMenu combinedMenu = new CombinedMenu(); 8 | IVegetarianMenu tasteOfIndia = new VegMenuAdapter(combinedMenu); 9 | tasteOfIndia.getVegetarianItems(); 10 | 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /February2022/Kotlin/SwapPairs.kt: -------------------------------------------------------------------------------- 1 | // Created by LeetCode : shobhitbruh 2 | fun swapPairs(head: ListNode?): ListNode? { 3 | if (head == null || head.next == null) { 4 | return head 5 | } 6 | val one: ListNode = head 7 | val two: ListNode = head.next 8 | val three: ListNode? = two.next 9 | two.next = one 10 | one.next = swapPairs(three) 11 | return two 12 | } -------------------------------------------------------------------------------- /July2021/FindPeakElement.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(logn) 4 | // SC : O(1) 5 | public int findPeakElement(int[] nums) { 6 | int low = 0; 7 | int high = nums.length-1; 8 | 9 | while(low< high){ 10 | int mid = low + (high-low)/2; 11 | if(nums[mid]> dp(m,vector(n,1)); 7 | for(int i=1;i maxFreq){ 15 | maxFreq = freq[i]; 16 | ans = i; 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /January2022/C++/142.Linked_List_Cycle_II.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *detectCycle(ListNode *head) { 4 | ListNode* slow=head,*fast=head; 5 | while(fast && slow<=fast) 6 | { 7 | fast=fast->next; 8 | if(fast && fast<=slow) return fast; 9 | else fast=fast?fast->next:fast; 10 | slow=slow->next; 11 | } 12 | return fast; 13 | } 14 | }; -------------------------------------------------------------------------------- /Leetcode2023/January2023/C++/DetectCapital.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool detectCapitalUse(string word) { 4 | 5 | int n = word.length(); 6 | int c = 0; 7 | for(int i=0;i=65 and word[i]<=92) c++; 10 | } 11 | if(c==n or c==0 or ((word[0]>=65 and word[0]<=92) and c==1)) return true; 12 | return false; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /Leetcode2023/January2023/C++/detectCapital.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool detectCapitalUse(string word) { 4 | 5 | int n = word.length(); 6 | int c = 0; 7 | for(int i=0;i=65 and word[i]<=92) c++; 10 | } 11 | if(c==n or c==0 or ((word[0]>=65 and word[0]<=92) and c==1)) return true; 12 | return false; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /August2020/PowerOfFour.java: -------------------------------------------------------------------------------- 1 | class PowerOfFour { 2 | public boolean isPowerOfFour(int num) { 3 | int index = -1; 4 | if((num&(num-1))==0) { 5 | for(int i=0;i<32;i++){ 6 | if((((num >>i) &1) == 1)){ 7 | if(i%2==0){ 8 | return true; 9 | } 10 | } 11 | } 12 | } 13 | return false; 14 | } 15 | } -------------------------------------------------------------------------------- /Contest/SumofNumbersWithUnitsDigitK.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(1) 3 | // SC : O(1) 4 | public int minimumNumbers(int num, int k) { 5 | if(num==0) { 6 | return 0; 7 | } 8 | 9 | if(k==0) { 10 | return num%10==0 ? 1 : -1; 11 | } 12 | 13 | 14 | for(int i=1; i*k<=num && i<=10; i++) 15 | { 16 | if((num - i*k)%10==0) { 17 | 18 | return i; 19 | } 20 | } 21 | return -1; 22 | 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /January2022/C++/Add_Binary.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | string res=""; 5 | int c=0,i=a.length()-1,j=b.length()-1; 6 | while(c || i>=0 || j>=0) 7 | { 8 | c+= i>=0? a[i--]-'0':0; 9 | c+= j>=0? b[j--]-'0':0; 10 | res=char(c%2 +'0')+res; 11 | c/=2; 12 | } 13 | return res; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /January2022/C++/add_binary.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string addBinary(string a, string b) { 4 | string res=""; 5 | int c=0,i=a.length()-1,j=b.length()-1; 6 | while(c || i>=0 || j>=0) 7 | { 8 | c+= i>=0? a[i--]-'0':0; 9 | c+= j>=0? b[j--]-'0':0; 10 | res=char(c%2 +'0')+res; 11 | c/=2; 12 | } 13 | return res; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /May2022/Java/NumberOfStepsToReduceANumberToZero.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(logn) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public int numberOfSteps(int num) { 8 | int cnt = 0; 9 | while (num != 0) { 10 | if (num%2 == 0) 11 | num /= 2; 12 | else 13 | num -= 1; 14 | cnt += 1; 15 | } 16 | return cnt; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /November2021/SingleElementinaSortedArray.java: -------------------------------------------------------------------------------- 1 | // TC : O(logn) 2 | // SC : O(1) 3 | class Solution { 4 | public int singleNonDuplicate(int[] nums) { 5 | int low = 0, high = nums.length-1; 6 | 7 | while (low < high) { 8 | int mid = (low + high) / 2; 9 | if ((mid % 2 ==1 && nums[mid - 1] == nums[mid]) || ((mid % 2)==0 && nums[mid] == nums[mid + 1])) low = mid + 1; 10 | else high = mid; 11 | } 12 | return nums[low]; 13 | } 14 | } -------------------------------------------------------------------------------- /October2021/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(n) 4 | private int[] dp = null; 5 | public int climbStairs(int n) { 6 | dp = new int[n+1]; 7 | dp[1] = 1; 8 | if(n==1){ 9 | return 1; 10 | } 11 | dp[2] = 2; 12 | for(int i=3;i<=n;i++){ 13 | dp[i] = dp[i-1]+dp[i-2]; 14 | } 15 | 16 | return dp[n]; 17 | } 18 | } -------------------------------------------------------------------------------- /Contest/BiWeekly Contest 75/FindTriangularSumofanArray.java: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | public int triangularSum(int[] nums) { 4 | if(nums.length==1){ 5 | return nums[0]; 6 | } 7 | int[] nn=new int[nums.length-1]; 8 | for(int i=0;i Optional[ListNode]: 8 | s=set() 9 | while head: 10 | if head not in s: 11 | s.add(head) 12 | else: 13 | return head 14 | head=head.next -------------------------------------------------------------------------------- /August2020/FIndAllDuplicate.java: -------------------------------------------------------------------------------- 1 | class FindAllDuplicates { 2 | public List findDuplicates(int[] nums) { 3 | List ans = new ArrayList<>(); 4 | for(int i=0;i int: 6 | minToDate = prices[0] 7 | profit = 0 8 | for day in range(len(prices)): 9 | if prices[day] < minToDate: 10 | minToDate = prices[day] 11 | else: 12 | profit = max(profit, prices[day]-minToDate) 13 | return profit 14 | -------------------------------------------------------------------------------- /February2022/Java/BestTimetoBuyandSellStock.java: -------------------------------------------------------------------------------- 1 | // TC : O(n) 2 | // SC :O(1) 3 | class Solution { 4 | public int maxProfit(int[] prices) { 5 | int maxProfit = 0; 6 | int sellingPrice = 0; 7 | for(int i=prices.length-1;i>=0;i--){ 8 | sellingPrice = Math.max(sellingPrice, prices[i]); 9 | 10 | maxProfit = Math.max(maxProfit,sellingPrice - prices[i]); 11 | } 12 | return maxProfit; 13 | } 14 | } -------------------------------------------------------------------------------- /Leetcode2023/February2023/C++/InsertPosition.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int searchInsert(vector& nums, int target) { 4 | int mid,l=0,r=nums.size()-1; 5 | while(ltarget) r=mid; 10 | else l=mid+1; 11 | } 12 | 13 | return nums[l]==target?l:nums[l]>target?l:l+1; 14 | } 15 | }; -------------------------------------------------------------------------------- /May2022/Java/NumberOf1Bits.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(logn) 4 | // SC : O(1) 5 | // Do not do n&(n-1), specially need to be taken care of since java do not have unsigned integer type. 6 | // Alternative approach can be -> while (n > 0) { if (n&1) cnt++; n >>= 1; } 7 | 8 | public class Solution { 9 | // you need to treat n as an unsigned value 10 | public int hammingWeight(int n) { 11 | return Integer.bitCount(n); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /November2021/UniquePaths.java: -------------------------------------------------------------------------------- 1 | // TC : O(m*n) 2 | // SC : O(n*m) 3 | class Solution { 4 | public int uniquePaths(int m, int n) { 5 | int[][] dp = new int[m][n]; 6 | 7 | for(int i=0;i max) { 13 | max = countSum[i]; 14 | ans = i; 15 | } 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /February2022/Kotlin/AddDigits.kt: -------------------------------------------------------------------------------- 1 | // Author Shobhit Behl( LC : shobhitbruh) 2 | class Solution { 3 | fun addDigits(num: Int): Int { 4 | var n = Integer.toString(num).toCharArray() 5 | var sum = num 6 | while (n.size > 1) { 7 | sum = 0 8 | for (x in n) { 9 | sum += x - '0' 10 | } 11 | n = Integer.toString(sum).toCharArray() 12 | } 13 | return sum 14 | } 15 | } -------------------------------------------------------------------------------- /February2023/C++/121. Best Time to Buy and Sell Stock.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProfit(vector& prices) { 4 | 5 | int profit=0; 6 | int min_value=INT_MAX; 7 | for(int i=0;i grayCode(int n) { 5 | List list = new ArrayList<>(); 6 | list.add(0); 7 | 8 | if(n == 0) return list; 9 | list.add(1); 10 | 11 | int curr = 1; 12 | for(int i = 2;i<=n;i++){ 13 | curr *= 2; 14 | for(int j = list.size()-1;j>=0;j--){ 15 | list.add(curr+list.get(j)); 16 | } 17 | } 18 | return list; 19 | } 20 | } -------------------------------------------------------------------------------- /July2022/Python/Readme.md: -------------------------------------------------------------------------------- 1 | ## Python++ Solutions for July 2022 leetcode challenge 2 | 3 | Day 7 - Interleaving Strings 4 | 5 | Day 11 - Binary Tree Right Side View 6 | 7 | Day 14 - Construct Binary Tree from Preorder and Inorder Traversal 8 | 9 | Day 15 - Max Area of Island 10 | 11 | Day 16 - Out of Boundary Paths 12 | 13 | Day 21 - Partition List 14 | 15 | Day 24 - Search A 2D Matrix II 16 | 17 | Day 25 - Find First And Last Position Of Element Sorted Array 18 | -------------------------------------------------------------------------------- /Leetcode2023/April2023/Java/AddDigits.java: -------------------------------------------------------------------------------- 1 | // Author: Mahesh Reddy B N 2 | 3 | // Problem Link: https://leetcode.com/problems/add-digits/description/ 4 | 5 | class Solution { 6 | public int addDigits(int num) { 7 | while(num>=10){ 8 | int x = 0; 9 | while(num>0){ 10 | x+= num%10; 11 | num = num/10; 12 | } 13 | num = x; 14 | } 15 | return num; 16 | } 17 | } -------------------------------------------------------------------------------- /December2021/DominoAndTrominoTiling.java: -------------------------------------------------------------------------------- 1 | 2 | // TC : O(n) 3 | // SC : O(n) 4 | class Solution { 5 | public int numTilings(int n) { 6 | 7 | if(n == 1) return 1; 8 | else if(n == 2 ) return 2; 9 | 10 | int mod = 1000000007; 11 | int[] dp = new int[n + 1]; 12 | 13 | dp[1] = 1; 14 | dp[2] = 2; 15 | dp[3] = 5; 16 | for(int i = 4; i <= n; i++) { 17 | dp[i] = (2 * dp[i - 1] % mod + dp[i - 3] % mod) % mod; 18 | 19 | } 20 | return dp[n]; 21 | } 22 | } -------------------------------------------------------------------------------- /Leetcode2023/April2023/Java/OptimalPartitionOfString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int partitionString(String s) { 3 | Set set = new HashSet<>(); 4 | int count = 1; 5 | 6 | for (char c : s.toCharArray()) { 7 | if (set.contains(c)) { 8 | count++; 9 | set.clear(); 10 | } 11 | set.add(c); 12 | } 13 | 14 | return count; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /November2020/SmallestIntegerDivisiblebyK.java: -------------------------------------------------------------------------------- 1 | class SmallestIntegerDivisiblebyK { 2 | public int smallestRepunitDivByK(int K) { 3 | int remainder = 0; 4 | if(K ==2 || K==5 ){ 5 | return -1; 6 | } 7 | 8 | for(int i=0;ival; 7 | if(!root->left && !root->right) 8 | { 9 | res+=temp; 10 | return res; 11 | } 12 | sumRootToLeaf(root->left,temp); 13 | sumRootToLeaf(root->right,temp); 14 | return res; 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /Leetcode2023/April2023/Python/BinarySearch.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def search(self, nums: List[int], target: int) -> int: 3 | start, end = 0, len(nums) - 1 4 | while start <= end: 5 | mid = start + (end - start) // 2 6 | if nums[mid] == target: 7 | return mid 8 | elif nums[mid] < target: 9 | start = mid + 1 10 | else: 11 | end = mid - 1 12 | return -1 13 | -------------------------------------------------------------------------------- /September2022/Java/ReverseWordsInAStringIII.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | 4 | class Solution { 5 | public String reverseWords(String s) { 6 | StringBuilder sb = new StringBuilder(); 7 | String [] tokens = s.split("\\s+"); 8 | for (String token : tokens) { 9 | sb.append(new StringBuilder(token).reverse()); 10 | sb.append(" "); 11 | } 12 | return sb.toString().substring(0, sb.length()-1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /August2020/ImplementRand10UsingRand7.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class ImplementRand10UsingRand7 { 7 | public int rand10() { 8 | int noLesser40 = 41; 9 | while(noLesser40 >= 40) { 10 | noLesser40 = (rand7() -1)*7 + rand7() -1; 11 | } 12 | return noLesser40% 10 + 1; 13 | } 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Contest/DecodetheMessage.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String decodeMessage(String key, String message) { 3 | char[] map = new char[26]; 4 | int i = 0; 5 | for (char c : key.toCharArray()) { 6 | if (i < 26 && c != ' ' && map[c - 'a'] == 0) { 7 | map[c - 'a'] = (char) (i + 'a'); 8 | i++; 9 | } 10 | } 11 | String ans = ""; 12 | for (char c : message.toCharArray()) { 13 | ans += (c == ' ' ? ' ' : map[c - 'a']); 14 | } 15 | return ans; 16 | } 17 | } -------------------------------------------------------------------------------- /January2022/C++/Max_distance_to_Closest_person.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDistToClosest(vector& seats) { 4 | int dist=INT_MIN,res=0,prev=-1; 5 | for(int i=0;ivalright,low,high); 7 | if(root->val>high) 8 | return trimBST(root->left,low,high); 9 | root->left=trimBST(root->left,low,high); 10 | root->right=trimBST(root->right,low,high); 11 | return root; 12 | } 13 | }; -------------------------------------------------------------------------------- /Contest/MaximizetheTopmostElementAfterKMoves.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // Math.MAX([0...K-2], K) 4 | // TC : O(n) 5 | // SC : O(1) 6 | public int maximumTop(int[] nums, int k) { 7 | if (nums.length == 1 && k % 2 == 1) return -1; 8 | int max = 0; 9 | 10 | for (int i = 0; i < Math.min(k - 1 ,nums.length); i++){ 11 | max = Math.max(max, nums[i]); 12 | } 13 | 14 | if (k < nums.length) { 15 | max = Math.max(max, nums[k]); 16 | } 17 | return max; 18 | } 19 | } -------------------------------------------------------------------------------- /July2022/C++/FlattenBinaryTreeToLinkedList.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | TreeNode* prev = NULL; 4 | 5 | public: 6 | // TC: O(n) 7 | // SC: O(n) 8 | void flatten(TreeNode* root) { 9 | if (root == NULL) { 10 | return; 11 | } 12 | 13 | flatten(root->right); 14 | flatten(root->left); 15 | 16 | root->right = prev; 17 | root->left = NULL; 18 | prev = root; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /June2022/Java/MinimumMovesToEqualArrayElementsII.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(nlogn) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public int minMoves2(int[] nums) { 8 | Arrays.sort(nums); 9 | int l = 0, r = nums.length-1; 10 | int cnt = 0; 11 | while (l < r) { 12 | cnt += Math.abs(nums[l] - nums[r]); // avg of both 13 | l += 1; 14 | r -= 1; 15 | } 16 | return cnt; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /May2021/ToLowerCase.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | public String toLowerCase(String s) { 5 | char[] cList = s.toCharArray(); 6 | for(int i=0;i= 'A' && cList[i]<='Z') { 10 | 11 | cList[i] = (char)( cList[i] +32); // lower case 12 | 13 | } 14 | } 15 | return new String(cList); 16 | } 17 | } -------------------------------------------------------------------------------- /Contest/IntersectionofMultipleArrays.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // SC : O(1) 3 | // TC : O(n*m) 4 | public List intersection(int[][] nums) { 5 | int[] freq = new int[1001]; 6 | 7 | for(int[] num: nums){ 8 | for(int el: num){ 9 | freq[el]++; 10 | } 11 | } 12 | List ans = new ArrayList<>(); 13 | int len = nums.length; 14 | for(int i=1;i<1001;i++){ 15 | if(freq[i] == len){ 16 | ans.add(i); 17 | } 18 | } 19 | 20 | return ans; 21 | } 22 | } -------------------------------------------------------------------------------- /November2021/FindAllNumbersDisappearedinanArray.java: -------------------------------------------------------------------------------- 1 | // O(n) 2 | class Solution { 3 | public List findDisappearedNumbers(int[] nums) { 4 | List ans = new ArrayList<>(); 5 | for(int i=0;i0){ 8 | nums[el] = -nums[el]; 9 | } 10 | } 11 | 12 | for(int i=0;i0){ 15 | ans.add(i+1); 16 | } 17 | } 18 | return ans; 19 | } 20 | } 21 | 22 | -------------------------------------------------------------------------------- /October2022/C++/1662. Check If Two String Arrays are Equivalent - C++: -------------------------------------------------------------------------------- 1 | Problem Link: https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/ 2 | Solution: 3 | class Solution { 4 | public: 5 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 6 | string s1, s2; 7 | for(int i = 0; i < word1.size(); i++) s1 += word1[i]; 8 | for(int i = 0; i < word2.size(); i++) s2 += word2[i]; 9 | return s1 == s2; 10 | } 11 | }; 12 | -------------------------------------------------------------------------------- /October2022/Java/ToeplitzMatrix.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public boolean isToeplitzMatrix(int[][] matrix) { 5 | int m = matrix.length; 6 | int n = matrix[0].length; 7 | for (int i = 0; i < m; i++) { 8 | for (int j = 0; j < n; j++) { 9 | if (i > 0 && j > 0 && matrix[i][j] != matrix[i-1][j-1]) 10 | return false; 11 | } 12 | } 13 | return true; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /April2022/C++/Kth_Smallest_In_BST.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int count,n; 4 | int kthSmallest(TreeNode* root, int k) { 5 | n=k; 6 | count=0; 7 | return bst(root)->val; 8 | } 9 | TreeNode* bst(TreeNode* root) 10 | { 11 | if(!root) return NULL; 12 | TreeNode* node=bst(root->left); 13 | if(node ) return node; 14 | ++count; 15 | if(count==n) return root; 16 | return bst(root->right); 17 | } 18 | }; -------------------------------------------------------------------------------- /Contest/Minimum AmountOfTimeToFillCups.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/minimum-amount-of-time-to-fill-cups/ 2 | // @author: anuj0503 3 | 4 | class Solution { 5 | public int fillCups(int[] amount) 6 | { 7 | Arrays.sort(amount); 8 | int x=amount[0]; 9 | int y=amount[1]; 10 | int z=amount[2]; 11 | 12 | int sum=x+y+z; 13 | 14 | if(x+y>z) 15 | return sum/2 + sum%2; 16 | 17 | return z; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /January2022/C++/Insert_into_binary_search_Tree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode* insertIntoBST(TreeNode* root, int val) { 4 | if(!root) 5 | { 6 | TreeNode* newnode=new TreeNode(val); 7 | return newnode; 8 | } 9 | if(root->val>val) 10 | root->left=insertIntoBST(root->left,val); 11 | else 12 | root->right=insertIntoBST(root->right,val); 13 | return root; 14 | 15 | } 16 | }; -------------------------------------------------------------------------------- /March2022/Kotlin/CountingBits.kt: -------------------------------------------------------------------------------- 1 | //Author : Shobhit Behl (LC : shobhitbruh) 2 | class Solution { 3 | fun countBits(n: Int): IntArray { 4 | val arr = IntArray(n+1) 5 | 6 | for(i in arr.indices){ 7 | var j=i 8 | var c=0 9 | while(j>0){ 10 | c++ 11 | j = j xor (j and (j.inv()+1)) 12 | } 13 | arr[i]=c 14 | } 15 | 16 | 17 | return arr 18 | } 19 | } -------------------------------------------------------------------------------- /August2020/LongestPalindrome.java: -------------------------------------------------------------------------------- 1 | class LongestPalindrome { 2 | public int longestPalindrome(String s) { 3 | int[] freq = new int[256]; 4 | for(int el : s.toCharArray()){ 5 | freq[el]++; 6 | } 7 | 8 | int count =0; 9 | int odd = 0; 10 | for(int el: freq){ 11 | count += (2*(el/2)); 12 | if(el%2==1){ 13 | odd =1; 14 | } 15 | } 16 | 17 | return count+ odd; 18 | } 19 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/AbstractFactoryDesignPattern/Test.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.AbstractFactoryDesignPattern; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | 6 | WagonRCarFactory wagonRCarFactory = new WagonRCarFactory(); 7 | Car car = wagonRCarFactory.getCar(); 8 | System.out.println("Car " + car.getClass().getName() + " has " + car.getEngineCC() + " with price " + car.getPrice()); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /March2022/C++/BrokenCalculator.cpp: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(logn) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public: 8 | int brokenCalc(int startValue, int target) { 9 | int res = 0; 10 | while (target > startValue) { 11 | if (target&1) 12 | target += 1; 13 | else 14 | target >>= 1; 15 | res += 1; 16 | } 17 | res += (startValue - target); 18 | return res; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /October2021/JumpGame.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | if(nums.length==1){ 4 | return true; 5 | } 6 | int iMaxReach = nums[0]; 7 | int i = 1; 8 | while(iMaxReach>=i) { 9 | if(iMaxReach>=nums.length-1){ 10 | return true; 11 | } 12 | iMaxReach = Math.max(nums[i] +i , iMaxReach); 13 | i++; 14 | 15 | } 16 | return false; 17 | } 18 | } -------------------------------------------------------------------------------- /August2021/FindMinimuminRotatedSortedArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(logn) 3 | // SC : O() 4 | public int findMin(int[] nums) { 5 | int low = 0; 6 | int high = nums.length - 1; 7 | while(low < high){ 8 | int mid = low + ( high - low)/2; 9 | if(nums[high] >= nums[mid]){ 10 | high = mid; 11 | } else { 12 | low = mid+1; 13 | } 14 | } 15 | return nums[low]; 16 | } 17 | } -------------------------------------------------------------------------------- /Contest/FindtheWinneroftheCircularGame.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTheWinner(int n, int k) { 3 | Queue qu = new LinkedList<>(); 4 | for(int i=1;i<=n;i++){ 5 | qu.offer(i); 6 | } 7 | 8 | while(qu.size() !=1){ 9 | for(int j=1;j<=k-1;j++){ 10 | qu.offer(qu.poll()); 11 | } 12 | // k th element 13 | qu.poll(); 14 | } 15 | 16 | return qu.poll(); 17 | } 18 | } -------------------------------------------------------------------------------- /December2022/C++/climbing-stairs.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | 5 | 6 | vectordp(n+1,0); 7 | 8 | int prev1 = 1; 9 | int prev2 = 1; 10 | int curr = 1; 11 | 12 | for(int i=2;i<=n;i++){ 13 | curr = prev1 + prev2; 14 | 15 | prev1 = prev2; 16 | prev2 = curr; 17 | } 18 | 19 | 20 | 21 | return curr; 22 | } 23 | }; -------------------------------------------------------------------------------- /June2021/python/Remove All Adjacent Duplicates in String: -------------------------------------------------------------------------------- 1 | #author-Aditya Chauhan 2 | #Qlink-https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/ 3 | 4 | class Solution: 5 | def removeDuplicates(self, s: str) -> str: 6 | stack=[0] 7 | for i in s: 8 | if i != stack[-1]: 9 | stack+=i 10 | else: 11 | stack.pop() 12 | stack.pop(0) 13 | 14 | return "".join(stack) 15 | 16 | -------------------------------------------------------------------------------- /August2022/C++/PowerOfThree.cpp: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/power-of-three/ 2 | class Solution { 3 | public: 4 | // TC: O(log3(n)) 5 | // SC: O(1) 6 | bool isPowerOfThree(int n) { 7 | if (n < 1) { 8 | return false; 9 | } 10 | while (n > 1) { 11 | int remainder = n % 3; 12 | if (remainder != 0) { 13 | return false; 14 | } 15 | n /= 3; 16 | } 17 | return true; 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /January2021/Count Sorted Vowel Strings.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | //TC : O(n) 3 | // SC: o(1) 4 | public int countVowelStrings(int n) { 5 | int[] dp = new int[5]; 6 | Arrays.fill(dp, 1); 7 | int ans = 0; 8 | for(int i=2;i<=n;i++) { 9 | for(int j=3;j>=0;j--){ 10 | dp[j] = dp[j]+ dp[j+1]; 11 | } 12 | } 13 | 14 | for(int el:dp){ 15 | ans+=el; 16 | } 17 | return ans; 18 | } 19 | } -------------------------------------------------------------------------------- /July2022/C++/pascalsTriangle.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector> generate(int numRows) { 4 | 5 | int n = numRows; 6 | vector> ans(n); 7 | 8 | for(int i=0;i& stones) { 4 | 5 | int n = stones.size(); 6 | priority_queuepq(stones.begin(),stones.end()); 7 | 8 | while(pq.size()>1) 9 | { 10 | int a = pq.top(); pq.pop(); 11 | int b = pq.top(); pq.pop(); 12 | int c = a-b; 13 | pq.push(c); 14 | } 15 | return pq.top(); 16 | } 17 | }; 18 | -------------------------------------------------------------------------------- /Leetcode2023/April2023/C++/2390RemovingStarsFromAString.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string removeStars(string s) { 4 | 5 | int n = s.length(); 6 | stack st; 7 | for(int i=0;i>i) & 1) == 1){ 9 | binarySetCount[i]++; 10 | } 11 | } 12 | } 13 | 14 | int ans = 0; 15 | for(int el: binarySetCount){ 16 | ans = Math.max(ans, el); 17 | } 18 | return ans; 19 | } 20 | } -------------------------------------------------------------------------------- /Contest/RearrangeArrayElementsbySign.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC: O(n) 4 | public int[] rearrangeArray(int[] nums) { 5 | int len = nums.length; 6 | int[] ans = new int[len]; 7 | int positiveIndex = 0; 8 | int negativeIndex = 1; 9 | for(int i=0;i0){ 11 | ans[positiveIndex] = nums[i]; 12 | positiveIndex += 2; 13 | } else { 14 | 15 | ans[negativeIndex] = nums[i]; 16 | negativeIndex += 2; 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /December2022/Java/HouseRobber.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public int rob(int[] nums) { 5 | int l = nums.length; 6 | if (l == 1) 7 | return nums[0]; 8 | int dp2 = nums[0]; 9 | int dp1 = Math.max(nums[0], nums[1]); 10 | int dp = dp1; 11 | for (int i = 2; i < l; i++) { 12 | dp = Math.max(dp2 + nums[i], dp1); 13 | dp2 = dp1; 14 | dp1 = dp; 15 | } 16 | return dp; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /July2022/C++/kInversePairsArray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int kInversePairs(int n, int k) { 4 | 5 | int dp[1001][1001] = {1}; 6 | int mod = 1e9+7; 7 | for(int N=1;N<=n;N++) 8 | { 9 | for(int K=0;K<=k;K++) 10 | { 11 | for(int i=0;i<=min(N-1,K);i++) 12 | 13 | dp[N][K] = (dp[N][K] +dp[N-1][K-i])%mod; 14 | } 15 | } 16 | return dp[n][k]; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /March2021/check-if-a-string-contains-allbinary-codesofsize-K.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O((n-k)*k) 4 | // SC : O(2^k) 5 | public boolean hasAllCodes(String s, int k) { 6 | if(s.length() < k){ 7 | return false; 8 | } 9 | 10 | HashSet set = new HashSet<>(); 11 | 12 | for(int i=0;i<=s.length() - k ;i++){ 13 | 14 | set.add(s.substring(i, i+k)); 15 | } 16 | 17 | return set.size() == (Math.pow(2, k)); 18 | } 19 | } -------------------------------------------------------------------------------- /October2022/Java/DeleteNodeInALinkedList.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | 4 | /** 5 | * Definition for singly-linked list. 6 | * public class ListNode { 7 | * int val; 8 | * ListNode next; 9 | * ListNode(int x) { val = x; } 10 | * } 11 | */ 12 | class Solution { 13 | public void deleteNode(ListNode node) { 14 | ListNode rem = node.next; 15 | // System.out.println(rem.val + " " + rem.next.val); 16 | node.val = rem.val; 17 | node.next = rem.next; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /April2022/C++/Top_K_Frequent_Elements.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector topKFrequent(vector& nums, int k) { 4 | unordered_map dict; 5 | for(auto& i:nums) dict[i]++; 6 | priority_queue> q; 7 | for(auto& i:dict) q.push({i.second,i.first}); 8 | 9 | vector res; 10 | while(k--) 11 | { 12 | res.push_back(q.top().second); 13 | q.pop(); 14 | } 15 | return res; 16 | } 17 | }; -------------------------------------------------------------------------------- /Contest/Find TheOriginalArrayofPrefixXor.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/contest/weekly-contest-314/problems/find-the-original-array-of-prefix-xor/ 2 | // @author: anuj0503 3 | class Solution { 4 | public int[] findArray(int[] pref) { 5 | int[] result = new int[pref.length]; 6 | 7 | result[0] = pref[0]; 8 | 9 | for(int i = 1; i < pref.length; i++){ 10 | result[i] = pref[i] ^ pref[i - 1]; 11 | } 12 | 13 | return result; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Contest/RemovingMinimumNumberofMagicBeans.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(nlogn) 4 | // Sc : O(1) 5 | public long minimumRemoval(int[] beans) { 6 | Arrays.sort(beans); 7 | 8 | long total =0; 9 | long min=Long.MAX_VALUE; 10 | 11 | int n=beans.length; 12 | 13 | for(int bean:beans) { 14 | total +=bean; 15 | } 16 | 17 | for(int i=0;i0; i++) { 7 | if(flowerbed[i] == 0) { 8 | int next = (i == flowerbed.length - 1) ? 0 : flowerbed[i + 1]; 9 | int prev = (i == 0) ? 0 : flowerbed[i - 1]; 10 | if(next == 0 && prev == 0) { 11 | flowerbed[i] = 1; 12 | n--; 13 | } 14 | } 15 | } 16 | 17 | return n==0; 18 | } 19 | } -------------------------------------------------------------------------------- /January2022/Python/richest_customer_health.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(M*N) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def maximumWealth(self, accounts: List[List[int]]) -> int: 8 | maxWealth = 0 9 | for row in accounts: 10 | currWealth = 0 11 | for col in row: 12 | currWealth += col 13 | maxWealth = currWealth if currWealth > maxWealth else maxWealth 14 | return maxWealth 15 | -------------------------------------------------------------------------------- /June2022/Java/MergeSortedArray.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(m+n) 4 | // SC : O(m+n) 5 | 6 | class Solution { 7 | public void merge(int[] nums1, int m, int[] nums2, int n) { 8 | int i = m-1; 9 | int j = n-1; 10 | int k = m+n-1; 11 | while (k >= 0) { 12 | if (j < 0 || (i >= 0 && nums1[i] > nums2[j])) 13 | nums1[k--] = nums1[i--]; 14 | else 15 | nums1[k--] = nums2[j--]; 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /November2021/C++/Product_of_Array_Except_Self.cpp: -------------------------------------------------------------------------------- 1 | // author:shyam2520 2 | 3 | class Solution { 4 | public: 5 | vector productExceptSelf(vector& nums) { 6 | vector ans(nums.size(),1); 7 | for(int i=nums.size()-2;i>=0;i--) 8 | ans[i]=nums[i+1]*ans[i+1]; 9 | int prefix=1; 10 | for(int i=0;ic){ 11 | right--; 12 | 13 | } else { 14 | left++; 15 | } 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /August2023 /Sliding Windows Maximum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector maxSlidingWindow(vector& nums, int k) { 4 | deque dq; 5 | vector res; 6 | for(int i=0;i=0) res.push_back(nums[dq.front()]); 11 | } 12 | 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /Contest/TotalAppealofAString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(n) 4 | public long appealSum(String s) { 5 | 6 | int n = s.length(); 7 | 8 | long[] dp = new long[n+1]; 9 | 10 | 11 | int[] lastIndex = new int[26]; 12 | Arrays.fill(lastIndex, -1); 13 | 14 | 15 | long ans = 0l; 16 | 17 | for(int i = 0; i < n; i ++) { 18 | int c = s.charAt(i)-'a'; 19 | dp[i+1] = dp[i]+ i- lastIndex[c]; 20 | ans += dp[i+1]; 21 | 22 | lastIndex[c] = i; 23 | } 24 | return ans; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /February2021/ValidateStackSequences.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | // SC : O(n) 5 | public boolean validateStackSequences(int[] pushed, int[] popped) { 6 | Stack st= new Stack<>(); 7 | int s = 0; 8 | for(int i=0;i1 5 | public void rotate(int[] nums, int k) { 6 | k %= nums.length; 7 | reverse(nums, 0, nums.length - 1); 8 | reverse(nums, 0, k - 1); 9 | reverse(nums, k, nums.length - 1); 10 | } 11 | 12 | public void reverse(int[] nums, int start, int end) { 13 | while (start < end) { 14 | int temp = nums[start]; 15 | nums[start] = nums[end]; 16 | nums[end] = temp; 17 | start++; 18 | end--; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode2023/January2023/C++/delete-columns-to-make-sorted.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minDeletionSize(vector& strs) { 4 | 5 | int n = strs.size(); 6 | int c = 0; 7 | for(int i=0;istrs[j+1][i]) {f = 1; break;} 13 | } 14 | if(f) c++; 15 | } 16 | return c; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/AdaptorPattern/VegMenuAdapter.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.AdaptorPattern; 2 | 3 | import java.util.List; 4 | 5 | class VegMenuAdapter implements IVegetarianMenu { 6 | 7 | private final CombinedMenu adaptee; 8 | 9 | public VegMenuAdapter(CombinedMenu combinedMenu) { 10 | this.adaptee = combinedMenu; 11 | } 12 | 13 | @Override 14 | public List getVegetarianItems() { 15 | return this.adaptee.getVegetarianItems(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/InterfaceSegregationPrinciple/src/ISPCompliant/Redesign/TasteOfIndia.java: -------------------------------------------------------------------------------- 1 | package ISPCompliant.Redesign; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | 7 | public class TasteOfIndia implements IVegetarianMenu { 8 | 9 | @Override 10 | public List getVegetarianItems() { 11 | List vegFood = new ArrayList<>(); 12 | vegFood.add(FOODITEMS.DAL_MAKNI); 13 | vegFood.add(FOODITEMS.PANEER_PASANDA); 14 | return vegFood; 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/Test.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | CarType testCar = CarType.SEDAN; 6 | MarutiCarFactory marutiCarFactory = new MarutiCarFactory(); 7 | Car car = marutiCarFactory.getCar(testCar); 8 | System.out.println("Car " + car.getClass().getName() + " has " + car.getEngineCC() + " with price " + car.getPrice()); 9 | 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /February2022/Python/ContiguosArray.py: -------------------------------------------------------------------------------- 1 | # TC : O(N) 2 | # SC : O(N) 3 | 4 | class Solution: 5 | def findMaxLength(self, nums: List[int]) -> int: 6 | map={0:-1} 7 | maxLen,count=0,0 8 | for i,val in enumerate(nums): 9 | if val==0: 10 | count-=1 11 | else: 12 | count+=1 13 | if count in map: 14 | maxLen=max(maxLen,i-map[count]) 15 | else: 16 | map[count]=i 17 | return maxLen 18 | -------------------------------------------------------------------------------- /January2022/C++/Solving_Questions_With_Brainpower.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | long long mostPoints(vector>& arr) { 4 | long long n = arr.size(); 5 | vector dp(n,0); 6 | for(long i = n-1;i>=0;i--){ 7 | dp[i]=arr[i][0]; 8 | if((arr[i][1]+i+1)> merge(vector>& I) { 4 | sort(begin(I), end(I)); 5 | vector> ans {I[0]}; 6 | for(auto i : I) 7 | if(i[0] <= ans.back()[1]) 8 | ans.back()[1] = max(ans.back()[1], i[1]); 9 | else 10 | ans.push_back(i); 11 | return ans; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /December2022/Java/DominoAndTrominoTiling.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public int numTilings(int n) { 5 | if (n == 1) 6 | return 1; 7 | if (n == 2) 8 | return 2; 9 | int [] dp = new int[n+1]; 10 | int mod = 1000000007; 11 | dp[1] = 1; 12 | dp[2] = 2; 13 | dp[3] = 5; 14 | for (int i = 4; i <= n; i++) { 15 | dp[i] = (2*dp[i-1]%mod + dp[i-3]%mod)%mod; 16 | } 17 | return dp[n]; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /March2022/Kotlin/BinarySearch.kt: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | fun search(nums: IntArray, target: Int): Int { 4 | var i:Int=0 5 | var j:Int=nums.size-1 6 | while(i<=j){ 7 | var m:Int=(i+j)/2 8 | if(nums[m]==target){ 9 | return m 10 | }else if(nums[m]>target){ 11 | j=m-1 12 | }else{ 13 | i=m+1 14 | } 15 | } 16 | 17 | return -1 18 | } 19 | } -------------------------------------------------------------------------------- /October2022/C++/219. Contains Duplicate II: -------------------------------------------------------------------------------- 1 | Problem Link: https://leetcode.com/problems/contains-duplicate-ii/ 2 | Solution: 3 | class Solution { 4 | public: 5 | bool containsNearbyDuplicate(vector& nums, int k) { 6 | map m; 7 | for(int i = 0; i < nums.size(); i++) 8 | { 9 | if(m[nums[i]]) 10 | if(abs(m[nums[i]] - (i + 1)) <= k) 11 | return true; 12 | m[nums[i]] = i + 1; 13 | } 14 | return false; 15 | } 16 | }; 17 | -------------------------------------------------------------------------------- /Contest/LongestUploadedPrefix.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/longest-uploaded-prefix/ 2 | // @author: anuj0503 3 | class LUPrefix { 4 | boolean[] video; 5 | int l; 6 | public LUPrefix(int n) { 7 | video = new boolean[n + 1]; 8 | l = 0; 9 | } 10 | 11 | public void upload(int v) { 12 | video[v] = true; 13 | 14 | while(l + 1 < video.length && video[l + 1]){ 15 | l++; 16 | } 17 | } 18 | 19 | public int longest() { 20 | return l; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Contest/MinimumConsecutiveCardstoPickUp.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(1) 4 | public int minimumCardPickup(int[] cards) { 5 | 6 | int[] lastIndex = new int[1000001]; 7 | Arrays.fill(lastIndex, -1); 8 | int ans = Integer.MAX_VALUE; 9 | for(int i=0;i= move * move; move++) { 9 | if (n - move * move == 0) { 10 | // current player won 11 | aliceWins = true; 12 | break; 13 | } else { 14 | aliceWins = aliceWins || !winnerSquareGame(n - move * move); 15 | } 16 | } 17 | return dp[n] = aliceWins; 18 | } 19 | } -------------------------------------------------------------------------------- /January2022/Python/validMountain.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(N) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def validMountainArray(self, arr: List[int]) -> bool: 8 | ind = 0 9 | l = len(arr) 10 | while ind+1 < l and arr[ind] < arr[ind+1]: 11 | ind += 1 12 | if ind == 0 or ind == l-1: 13 | return False 14 | while ind+1 < l and arr[ind] > arr[ind+1]: 15 | ind += 1 16 | return ind == l-1 17 | -------------------------------------------------------------------------------- /Contest/CalculateAmountPaidinTaxes.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : o(n) 3 | // SC : O(1) 4 | public double calculateTax(int[][] brackets, int income) { 5 | int previousEarning = 0; 6 | int i=0; 7 | double tax = 0.0D; 8 | while(i= income) break; 15 | i++; 16 | } 17 | 18 | return tax; 19 | 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /December2021/C++/Maximum_Product_Subarray.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxProduct(vector& nums) { 4 | int max_so_far=nums[0],curr_max=nums[0],curr_min=nums[0]; 5 | for(int i=1;i kidsWithCandies(vector& candies, int extraCandies) { 4 | 5 | int n = candies.size(); 6 | int maxi = *max_element(candies.begin(), candies.end()); 7 | vectorans; 8 | for(int i=0;i=maxi) ans.push_back(true); 11 | else ans.push_back(false); 12 | } 13 | return ans; 14 | } 15 | }; 16 | -------------------------------------------------------------------------------- /November2021/SingleNumbersIII.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] singleNumber(int[] nums) { 3 | int xor = 0; 4 | for(int num: nums){ 5 | xor^=num; 6 | } 7 | int elWithLowestBitSet = Integer.highestOneBit(xor); 8 | 9 | int[] ans = new int[2]; 10 | 11 | for(int num: nums){ 12 | // Column where the bit is unset 13 | if((elWithLowestBitSet & num) == 0){ 14 | ans[0] ^= num; 15 | } 16 | else{ 17 | // Column where the bit is set 18 | ans[1] ^= num; 19 | } 20 | } 21 | return ans; 22 | } 23 | } -------------------------------------------------------------------------------- /November2022/C++/make-the-string-great.cpp: -------------------------------------------------------------------------------- 1 | // @Sarang661 2 | class Solution { 3 | public: 4 | string makeGood(string s) { 5 | 6 | string ans; 7 | 8 | for(int i = 0 ; i < s.size() ; i++) 9 | { 10 | ans.push_back(s[i]); 11 | 12 | while(ans.size() && (ans.back()==s[i+1]+32 || ans.back()==s[i+1]-32)) 13 | { 14 | ans.pop_back(); 15 | i++; 16 | } 17 | } 18 | 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /April2022/Kotlin/TopKFrequentElements.kt: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | fun topKFrequent(nums: IntArray, k: Int): IntArray { 3 | val map = mutableMapOf() 4 | nums.forEach { map[it] = map.getOrDefault(it, 0) + 1 } 5 | 6 | val uniques = mutableListOf() 7 | val pairs = map.toList().sortedByDescending { it.second } 8 | pairs.forEach { 9 | while (uniques.size < k) { 10 | uniques.add(it.first) 11 | break 12 | } 13 | } 14 | 15 | return uniques.toIntArray() 16 | } -------------------------------------------------------------------------------- /Contest/MinimumMovesToReachTargetScore.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | class Solution { 4 | 5 | 6 | public int minMoves(int target, int maxDoubles) { 7 | int count = 0; 8 | while(target!=1){ 9 | if(target %2 == 1){ 10 | count++; 11 | target = target-1; 12 | } else { 13 | // target is even no 14 | if(maxDoubles>0){ 15 | target = target /2; 16 | maxDoubles--; 17 | count++; 18 | 19 | } else { 20 | count += target-1; 21 | target =1; 22 | } 23 | } 24 | } 25 | return count; 26 | 27 | 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /Contest/NumberofSmoothDescentPeriodsofaStock.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC :O(n) 3 | // SC : O(1) 4 | public long getDescentPeriods(int[] prices) { 5 | int start=0; 6 | int end=0; 7 | long ans = 1; 8 | for(end=1;end): Int { 4 | Arrays.sort(arr){a:IntArray,b:IntArray->if(a[0]==b[0])b[1]-a[1] else a[0]-b[0]} 5 | var ans=0 6 | var j=-1 7 | for(i in arr.indices){ 8 | if(arr[i][1]>j){ 9 | j=arr[i][1] 10 | }else{ 11 | ans++ 12 | } 13 | } 14 | 15 | return arr.size-ans 16 | } 17 | } -------------------------------------------------------------------------------- /January2021/CheckIfAll1'sAreatLeastLengthKPlacesAway.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(1) 4 | public boolean kLengthApart(int[] nums, int k) { 5 | int prev1Index = Integer.MIN_VALUE; 6 | for(int i=0;iA[i+1]){ 24 | 25 | } else { 26 | break; 27 | } 28 | } 29 | return i==(len-1); 30 | } 31 | } -------------------------------------------------------------------------------- /January2022/Python/detect_capital.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(N) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def detectCapitalUse(self, word: str) -> bool: 8 | cap, small = 0, 0 9 | for ch in word: 10 | if ch.isupper(): 11 | cap += 1 12 | else: 13 | small += 1 14 | if (cap == 1 and word[0].isupper()) or cap == len(word) or small == len(word): 15 | return True 16 | return False 17 | -------------------------------------------------------------------------------- /November2021/RemoveLinkedListElements.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC: O(n) 3 | // SC : O(1) 4 | public ListNode removeElements(ListNode head, int target) { 5 | ListNode dummy = new ListNode(-1); 6 | dummy.next = head; 7 | ListNode prev = dummy; 8 | ListNode curr = dummy.next; 9 | while(curr!=null){ 10 | while(curr!=null && curr.val==target){ 11 | prev.next = curr.next; 12 | curr=curr.next; 13 | 14 | } 15 | prev = prev.next; 16 | if(curr!=null) 17 | curr=curr.next; 18 | } 19 | return dummy.next; 20 | } 21 | } -------------------------------------------------------------------------------- /October2021/FindMinimumInRotatedSortedArrayII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(1) 4 | public int findMin(int[] nums) { 5 | int low = 0; 6 | int high = nums.length - 1; 7 | while(low < high){ 8 | int mid = low + ( high - low)/2; 9 | if(nums[high] == nums[mid]){ 10 | high = high -1; 11 | } 12 | else if(nums[high] > nums[mid]){ 13 | high = mid; 14 | } else{ 15 | // nums[high] < nums[mid] 16 | low = mid+1; 17 | 18 | } 19 | } 20 | return nums[low]; 21 | } 22 | } 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /April2022/Java/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(n) 4 | // SC : O(1) 5 | 6 | class Solution { 7 | public int maxArea(int[] height) { 8 | int l = height.length-1; 9 | int max = Integer.MIN_VALUE; 10 | int i = 0; 11 | while (i < l) { 12 | max = Math.max(max, Math.min(height[i], height[l]) * (l-i)); 13 | if (height[i] < height[l]) 14 | i += 1; 15 | else 16 | l -= 1; 17 | } 18 | return max; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Contest/CalculateDigitSumOfAString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String digitSum(String s, int k) { 3 | while(s.length()>k){ 4 | s = roundTrip(s, k); 5 | } 6 | return s; 7 | } 8 | 9 | private String roundTrip(String s, int k){ 10 | int i=0; 11 | int j=0; 12 | String ans = ""; 13 | int sum=0; 14 | for(;i int: 5 | map = defaultdict(int) 6 | count = 0 7 | for val1 in nums1: 8 | for val2 in nums2: 9 | map[val1+val2] += 1 10 | for val1 in nums3: 11 | for val2 in nums4: 12 | sum = -(val1+val2) 13 | if sum in map: 14 | count += map[sum] 15 | return count 16 | -------------------------------------------------------------------------------- /February2022/Python/CombinationSum.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: 3 | result = [] 4 | self.dfs(candidates, target, [], result) 5 | return result 6 | 7 | def dfs(self, nums, target, path, result): 8 | if target < 0: 9 | return 10 | if target == 0: 11 | result.append(path) 12 | return 13 | for i in range(len(nums)): 14 | self.dfs(nums[i:], target-nums[i], path+[nums[i]], result) 15 | -------------------------------------------------------------------------------- /January2022/C++/Divide_a_String_Into_Groups_of_Size_k.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector divideString(string s, int k, char fill) { 4 | string temp; 5 | int n = s.size(); 6 | vector ans; 7 | for(int i=0;i& seats) { 4 | int ans=0,n=seats.size(),temp=0,lead=0,i =0; 5 | while(i a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]); 6 | // both arraylist and linkedlist works 7 | List list = new ArrayList<>(); 8 | for (int[] person : people) { 9 | 10 | list.add(person[1], person); 11 | } 12 | return list.toArray(new int[list.size()][]); 13 | } 14 | 15 | 16 | } 17 | 18 | -------------------------------------------------------------------------------- /October2021/FindAllDuplicatesinanArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDuplicates(int[] nums) { 3 | 4 | List ans = new ArrayList<>(); 5 | for(int i=0;i set = new HashSet<>(); 4 | 5 | for(int[] circle : circles){ 6 | 7 | int x = circle[0]; 8 | int y = circle[1]; 9 | int r = circle[2]; 10 | 11 | for(int i = x - r; i <= x + r; i++){ 12 | for(int j = y - r; j <= y + r; j++){ 13 | 14 | if((i - x) * (i - x) + (j - y) * (j - y) <= r * r){ 15 | set.add(new int[]{i, j}); 16 | } 17 | } 18 | } 19 | } 20 | 21 | return set.size(); 22 | } 23 | } -------------------------------------------------------------------------------- /Contest/RemoveDigitFromNumbertoMaximizeResult.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(len) 3 | // SC : O(1) 4 | public String removeDigit(String number, char digit) { 5 | 6 | int i=1; 7 | for(;i number.charAt(i-1)){ 12 | return (number.substring(0,i-1) + number.substring(i)); 13 | } 14 | } 15 | } 16 | 17 | int lastIndex = number.lastIndexOf(digit); 18 | return (number.substring(0,lastIndex) + number.substring(lastIndex+1)); 19 | } 20 | } -------------------------------------------------------------------------------- /December2021/BinaryTreeTilt.java: -------------------------------------------------------------------------------- 1 | // TC : O(n) 2 | // SC : O(logn) 3 | public class Solution { 4 | private int result = 0; 5 | 6 | public int findTilt(TreeNode root) { 7 | postOrder(root); 8 | return result; 9 | } 10 | 11 | private int postOrder(TreeNode root) { 12 | if (root == null) return 0; 13 | 14 | int leftSubstreeSum = postOrder(root.left); 15 | int rightSubstreeSum = postOrder(root.right); 16 | 17 | result += Math.abs(leftSubstreeSum - rightSubstreeSum); 18 | 19 | return leftSubstreeSum + rightSubstreeSum + root.val; 20 | } 21 | } -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/PrototypeDesignPattern/Car.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.PrototypeDesignPattern; 2 | 3 | public abstract class Car implements VehicleInterface, Clone { 4 | private int modelYear; 5 | 6 | public Car() { 7 | } 8 | 9 | public int getModelYear() { 10 | return modelYear; 11 | } 12 | 13 | public void setModelYear(int modelYear) { 14 | this.modelYear = modelYear; 15 | } 16 | 17 | public Car(Car car) { 18 | this.modelYear = car.modelYear; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /July2021/C++/KthSmallestElementInASortedMatrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author - geekySapien 3 | QLink - https://leetcode.com/problems/kth-smallest-element-in-a-sorted-matrix/ 4 | */ 5 | 6 | 7 | 8 | class Solution 9 | { 10 | public: 11 | int kthSmallest(vector> &matrix, int k) 12 | { 13 | vector vect_1D; 14 | for (vector row : matrix) 15 | for (auto element : row) 16 | vect_1D.push_back(element); 17 | sort(vect_1D.begin(), vect_1D.end()); 18 | return vect_1D[k - 1]; 19 | } 20 | }; -------------------------------------------------------------------------------- /July2021/MaximumLengthofRepeatedSubarray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLength(int[] nums1, int[] nums2) { 3 | int max =0; 4 | int[][] dp = new int[nums1.length+1][nums2.length+1]; 5 | 6 | for(int i=1;i<=nums1.length;i++){ 7 | for(int j=1;j<=nums2.length;j++){ 8 | 9 | if(nums1[i-1]==nums2[j-1]){ 10 | dp[i][j] = dp[i-1][j-1] +1; 11 | 12 | max = Math.max(dp[i][j], max); 13 | } else { 14 | dp[i][j] = 0; 15 | } 16 | 17 | 18 | } 19 | } 20 | return max; 21 | } 22 | } -------------------------------------------------------------------------------- /March2022/C++/Simplify_Path.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string simplifyPath(string path) { 4 | vector value; 5 | stringstream stream(path); 6 | string res; 7 | while(getline(stream,path,'/')) 8 | { 9 | if(path=="" || path==".") continue; 10 | else if(path=="..") { if(value.size()) value.pop_back();} 11 | else value.push_back(path); 12 | } 13 | 14 | for(auto& i:value) res+=("/"+i); 15 | return res.length()>1?res:"/"; 16 | } 17 | }; -------------------------------------------------------------------------------- /May2022/Java/BackspaceStringCompare.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | // SC : O(n) 5 | public boolean backspaceCompare(String s, String t) { 6 | return updateStr(s).equals(updateStr(t)); 7 | } 8 | 9 | private String updateStr(String str) { 10 | StringBuilder ans = new StringBuilder(); 11 | 12 | for (char c : str.toCharArray()) { 13 | 14 | if (c != '#') { 15 | ans.append(c); 16 | } else if (ans.length() > 0) { 17 | ans.deleteCharAt(ans.length() - 1);// O(1) 18 | } 19 | } 20 | return ans.toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /November2021/C++/Daily_Temperatures.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector dailyTemperatures(vector& temp) { 4 | vector stack(temp.size(),0); 5 | vector res(temp.size(),0); 6 | int top=-1; 7 | for(int i=0;i-1 && temp[i]>temp[stack[top]]) 10 | { 11 | int idx=stack[top--]; 12 | res[idx]=i-idx; 13 | } 14 | stack[++top]=i; 15 | } 16 | return res; 17 | } 18 | }; -------------------------------------------------------------------------------- /November2022/Java/Maximum69Number.java: -------------------------------------------------------------------------------- 1 | // @saorav1994 2 | class Solution { 3 | public int maximum69Number (int num) { 4 | String str = String.valueOf(num); 5 | char [] arr = str.toCharArray(); 6 | int l = str.length(); 7 | int firstSix = -1; 8 | int lastNine = l; 9 | for (int i = 0; i < l; i++) { 10 | if (arr[i] == '6') { 11 | arr[i] = '9'; 12 | break; 13 | } 14 | } 15 | return Integer.parseInt(String.valueOf(arr)); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /November2022/Java/UglyNumber.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public boolean isUgly(int n) { 5 | 6 | if (n == 0) 7 | return false; 8 | 9 | while (true) { 10 | while (n%2 == 0) 11 | n /= 2; 12 | while (n%3 == 0) 13 | n /= 3; 14 | while (n%5 == 0) 15 | n /= 5; 16 | break; 17 | } 18 | 19 | if (n != 1) 20 | return false; 21 | return true; 22 | 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /October2022/Java/ContainsDuplicateII.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | 4 | class Solution { 5 | public boolean containsNearbyDuplicate(int[] nums, int k) { 6 | 7 | Map map = new HashMap<>(); 8 | 9 | for (int i = 0; i < nums.length; i++) { 10 | Integer pos = map.get(nums[i]); 11 | if (pos != null && i-pos <= k) 12 | return true; 13 | else 14 | map.put(nums[i], i); 15 | } 16 | 17 | return false; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Contest/CheckifMatrixIsXMatrix.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n2) 3 | 4 | public boolean checkXMatrix(int[][] grid) { 5 | 6 | int n = grid.length; 7 | for(int i=0;icapacity) { 15 | return false; 16 | } 17 | } 18 | 19 | return true; 20 | } 21 | } -------------------------------------------------------------------------------- /January2022/Python/gasStation.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(N) 4 | # SC : O(1) 5 | 6 | 7 | class Solution: 8 | def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int: 9 | if sum(gas) < sum(cost): 10 | return -1 11 | total = 0 12 | start = 0 13 | for ind in range(len(gas)): 14 | total += gas[ind]-cost[ind] 15 | if total < 0: 16 | total = 0 17 | start = ind+1 18 | return start 19 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/openclosed/noncompliant/Rectangle.java: -------------------------------------------------------------------------------- 1 | package com.codingdecoded.lowlevel.solid.openclosed.noncompliant; 2 | 3 | public class Rectangle implements Shape { 4 | 5 | 6 | private int length; 7 | private int breadth; 8 | 9 | public int getLength() { 10 | return length; 11 | } 12 | 13 | public void setLength(int length) { 14 | this.length = length; 15 | } 16 | 17 | public int getBreadth() { 18 | return breadth; 19 | } 20 | 21 | public void setBreadth(int breadth) { 22 | this.breadth = breadth; 23 | } 24 | } 25 | 26 | -------------------------------------------------------------------------------- /Contest/BiWeeklyContest76/Find_Closest_Number_to_Zero.java: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | public int findClosestNumber(int[] nums) { 4 | int val=Integer.MIN_VALUE; 5 | int diff=Integer.MAX_VALUE; 6 | for(int v:nums){ 7 | int h=Math.abs(v); 8 | if((h-0)0){ 15 | ans+=(cSecurityDevicesFreq*pSecurityDevicesFreq); 16 | pSecurityDevicesFreq=cSecurityDevicesFreq; 17 | } 18 | } 19 | return ans; 20 | } 21 | } -------------------------------------------------------------------------------- /Contest/PartitionArraySuchThatMaximumDifferenceIsK.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(nlogn) 3 | // SC : O(1) 4 | public int partitionArray(int[] nums, int k) { 5 | 6 | if(nums.length <=1) { return nums.length; } 7 | 8 | Arrays.sort(nums); 9 | 10 | int ans = 1; 11 | int min =nums[0]; 12 | for(int i=1;ik){ 15 | 16 | // element out of range k 17 | 18 | min = nums[i]; 19 | 20 | ans++; 21 | } else { 22 | 23 | // element within of range k 24 | } 25 | } 26 | return ans; 27 | } 28 | } -------------------------------------------------------------------------------- /Contest/ReplaceNonCoprimeNumbersinArray.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List replaceNonCoprimes(int[] nums) { 3 | LinkedList res = new LinkedList(); 4 | for (int el : nums) { 5 | while (true) { 6 | int last = res.isEmpty() ? 1 : res.getLast(); 7 | int gcd = gcd(last, el); 8 | if (gcd == 1) break; // co-prime 9 | el *= (res.removeLast()) / gcd; // (el * lastEl)/GCD 10 | } 11 | res.add(el); 12 | } 13 | return res; 14 | } 15 | 16 | private int gcd(int a, int b) { 17 | return b > 0 ? gcd(b, a % b) : a; 18 | } 19 | } -------------------------------------------------------------------------------- /January2022/Python/rotate_array.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(N) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def rotate(self, nums: List[int], k: int) -> None: 8 | k = k % len(nums) 9 | 10 | def reverse(start, end): 11 | while start < end: 12 | nums[start], nums[end] = nums[end], nums[start] 13 | start += 1 14 | end -= 1 15 | reverse(0, len(nums)-1) 16 | reverse(0, k-1) 17 | reverse(k, len(nums)-1) 18 | -------------------------------------------------------------------------------- /August2020/SortArrayByPartiy.java: -------------------------------------------------------------------------------- 1 | class SortArrayByParity { 2 | public int[] sortArrayByParity(int[] A) { 3 | int[] sortedArray = new int[A.length]; 4 | int start =0; 5 | int end = A.length-1; 6 | for(int el: A){ 7 | if(el%2==0){ // Track for even values 8 | sortedArray[start] = el; 9 | start++; 10 | } else{ // Track for odd values 11 | sortedArray[end] = el; 12 | end--; 13 | } 14 | } 15 | return sortedArray; 16 | } 17 | } -------------------------------------------------------------------------------- /Concepts/LongestIncreasingSubsequence.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(nlogn) 4 | // SC: O(n) 5 | public int lengthOfLIS(int[] nums) { 6 | TreeSet bst = new TreeSet<>(); 7 | for(int num: nums){ 8 | Integer higherOrEqual = bst.ceiling(num); 9 | if(higherOrEqual == null){ 10 | bst.add(num); 11 | } else{ 12 | bst.remove(higherOrEqual); 13 | bst.add(num); // O(logn) 14 | } 15 | } 16 | 17 | return bst.size(); 18 | } 19 | } -------------------------------------------------------------------------------- /Contest/GreatestEnglishLetterinUpperandLowerCase.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(1) 4 | public String greatestLetter(String s) { 5 | int[] lowerCase = new int[26]; 6 | int[] upperCase = new int[26]; 7 | 8 | for(char c: s.toCharArray()){ 9 | if(Character.isLowerCase(c)){ 10 | lowerCase[c-'a']++; 11 | } else { 12 | upperCase[c-'A']++; 13 | } 14 | } 15 | 16 | for(int i=25;i>=0;i--){ 17 | if(lowerCase[i] >0 && upperCase[i]>0) { 18 | return new String( "" +(char)(i +'A')); 19 | } 20 | } 21 | return ""; 22 | } 23 | } -------------------------------------------------------------------------------- /Contest/RemovingStarsFromaString.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | // SC : O(n) 5 | public String removeStars(String s) { 6 | Stack stack = new Stack<>(); 7 | for (int i = 0; i < s.length(); i++) { 8 | if (s.charAt(i) != '*'){ 9 | stack.push(s.charAt(i)); 10 | continue; 11 | } else { 12 | stack.pop(); 13 | } 14 | } 15 | if(stack.isEmpty()) return ""; 16 | StringBuilder sb = new StringBuilder(); 17 | while (!stack.isEmpty()){ 18 | sb.append(stack.pop()); 19 | } 20 | return sb.reverse().toString(); 21 | } 22 | } -------------------------------------------------------------------------------- /December2020/SmallestRangeII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | TC : O(nlogn) SC: O(1) 3 | public int smallestRangeII(int[] A, int K) { 4 | Arrays.sort(A); // O(nlogn) 5 | int len = A.length -1; 6 | int lowest = A[0]; 7 | int highest = A[len]; 8 | int res = highest - lowest; 9 | for(int i=0;i> minimumAbsDifference(int[] arr) { 5 | int min = Integer.MAX_VALUE; 6 | int n = arr.length; 7 | 8 | List> res = new ArrayList<>(); 9 | 10 | Arrays.sort(arr); 11 | 12 | for(int i = 0; i < n - 1; i++) 13 | min = Math.min(min, arr[i+1] - arr[i]); 14 | 15 | for(int i = 0; i < n - 1; i++) 16 | if( min == arr[i + 1] - arr[i]) 17 | res.add(Arrays.asList(arr[i], arr[i + 1])); 18 | 19 | return res; 20 | } 21 | } -------------------------------------------------------------------------------- /February2022/Kotlin/ExcelSheetColumnNumber.kt: -------------------------------------------------------------------------------- 1 | //Author : Shobhit Behl (LC:shobhitbruh) 2 | class Solution { 3 | fun titleToNumber(a: String): Int { 4 | val arr=a.toCharArray(); 5 | var ans=0 6 | var p=0 7 | for(i in arr.indices.reversed()){ 8 | ans+=(pow(p)*(arr[i]-'A'+1)) 9 | p++ 10 | } 11 | 12 | return ans 13 | } 14 | 15 | fun pow(x: Int):Int{ 16 | var i=1 17 | var t=x 18 | while(t-->0){ 19 | i*=26 20 | } 21 | 22 | return i 23 | } 24 | } -------------------------------------------------------------------------------- /October2022/Java/CheckIfTwoStringArraysAreEquivalent.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | 4 | class Solution { 5 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) { 6 | StringBuffer s1 = new StringBuffer(""); 7 | StringBuffer s2 = new StringBuffer(""); 8 | for (String word : word1) { 9 | s1.append(word); 10 | } 11 | for (String word : word2) { 12 | s2.append(word); 13 | } 14 | if (s1.toString().equals(s2.toString())) return true; 15 | return false; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /August2022/C++/LIS.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int lengthOfLIS(vector& nums) { 4 | 5 | vector temp; 6 | temp.push_back(nums[0]); 7 | for(int i=1;itemp.back()) temp.push_back(nums[i]); 10 | else 11 | { 12 | int ind = lower_bound 13 | (temp.begin(),temp.end(),nums[i])-temp.begin(); 14 | temp[ind] = nums[i]; 15 | } 16 | } 17 | return temp.size(); 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /August2022/C++/myCalendar1.cpp: -------------------------------------------------------------------------------- 1 | class MyCalendar { 2 | public: 3 | map m; 4 | MyCalendar() { 5 | 6 | } 7 | 8 | bool book(int start, int end) { 9 | 10 | for(auto &it:m) 11 | { 12 | if(!(start>=it.second or end<=it.first)) return false; 13 | } 14 | m[start] = end; 15 | return true; 16 | } 17 | }; 18 | 19 | /** 20 | * Your MyCalendar object will be instantiated and called as such: 21 | * MyCalendar* obj = new MyCalendar(); 22 | * bool param_1 = obj->book(start,end); 23 | */ 24 | -------------------------------------------------------------------------------- /Contest/BiWeeklyContest76/Number_of_Ways_to_Buy_Pens_and_Pencils.java: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | class Solution { 3 | public long waysToBuyPensPencils(int total, int cost1, int cost2) { 4 | int max=Math.max(cost1,cost2); 5 | int min=Math.min(cost1,cost2); 6 | long ans=0; 7 | long j=(long)Math.floor(total/max); 8 | for(int i=0;i<=j;i++){ 9 | int k=total; 10 | k-=(i*max); 11 | ans+=(long)Math.floor(k/min)+1; 12 | } 13 | 14 | return ans; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /January2021/smallest-string-with-a-given-numeric-value.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC: O(n) 3 | //SC: O(n) 4 | public String getSmallestString(int n, int k) { 5 | char[] ans = new char[n]; 6 | for(int i=0;i=26){ 12 | ans[index] = 'z'; 13 | k = k+1-26; 14 | index--; 15 | } 16 | 17 | ans[index] = (char)(k +'a'); 18 | return new String(ans); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /June2021/MinCostClimbingStairs.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(n) => 2 4 | public int minCostClimbingStairs(int[] cost) { 5 | 6 | // dp signifies cost to use each stair starting the zeroth index 7 | int[] dp = new int[cost.length]; 8 | 9 | dp[0] = cost[0]; 10 | dp[1] = cost[1]; 11 | for(int i=2;i& nums, int target) { 4 | int start = 0; 5 | int end = nums.size() - 1; 6 | while (start <= end) { 7 | int mid = start + (end - start) / 2; 8 | if (nums[mid] == target) { 9 | return mid; 10 | } else if (nums[mid] < target) { 11 | start = mid + 1; 12 | } else { 13 | end = mid - 1; 14 | } 15 | } 16 | return -1; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /Leetcode2023/January2023/Java/DetectCapital.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public boolean detectCapitalUse(String word) { 5 | String upCase = word.toUpperCase(); 6 | String downCase = word.toLowerCase(); 7 | char firstChar = word.charAt(0); 8 | String firstCharCase = String.valueOf(firstChar).toUpperCase() + word.toLowerCase().substring(1, word.length()); 9 | if (word.equals(upCase) || word.equals(downCase) || word.equals(firstCharCase)) 10 | return true; 11 | return false; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /March2021/DistributeCandies.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(n) 3 | // SC : O(n) // 1 2 3 4 6 ...n 4 | public int distributeCandies(int[] candyType) { 5 | 6 | HashSet hashSet = new HashSet<>(); 7 | for(int type : candyType){ 8 | hashSet.add(type); 9 | } 10 | 11 | return Math.min(hashSet.size(), candyType.length/2); 12 | } 13 | 14 | public int distributeCandies(int[] candyType) { 15 | return Math.min(candyType.length/2, ((int)Arrays.stream(candyType).distinct().count())); 16 | } 17 | } -------------------------------------------------------------------------------- /March2022/C++/partition_labels.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector partitionLabels(string s) { 4 | vector idx(26,-1); 5 | for(int i=0;i res; 8 | for(int i=0;ia[j]){ 12 | j-- 13 | }else{ 14 | i++ 15 | j-- 16 | } 17 | } 18 | 19 | return ans 20 | } 21 | } -------------------------------------------------------------------------------- /Leetcode2023/April2023/C++/1768.MergeStringsAlternately.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string mergeAlternately(string word1, string word2) { 4 | 5 | int n = word1.length(), m = word2.length(); 6 | int i = 0, j = 0; 7 | string ans; 8 | 9 | while(i getVegetarianItems() { 10 | List vegFood = new ArrayList<>(); 11 | vegFood.add(FOODITEMS.DAL_MAKNI); 12 | vegFood.add(FOODITEMS.PANEER_PASANDA); 13 | return vegFood; 14 | } 15 | 16 | @Override 17 | public List getNonVegetarianItems() { 18 | return null; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /April2022/C++/conatiner_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | //https://leetcode.com/problems/container-with-most-water/ 2 | class Solution { 3 | public: 4 | int maxArea(vector& height) { 5 | int n = height.size(); 6 | int l = 0; 7 | int r = n - 1; 8 | int ans = 0; 9 | while(l=height[r]){ 12 | r--; 13 | } 14 | else{ 15 | l++; 16 | } 17 | } 18 | return ans; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /Contest/MaximumConsecutiveFloorsWithoutSpecialFloors.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(nlogn) 3 | // SC : O(1) 4 | public int maxConsecutive(int bottom, int top, int[] special) { 5 | 6 | Arrays.sort(special); 7 | int start = bottom; 8 | int end = top; 9 | int ans = 0; 10 | for(int i=0;i x){ 14 | high = mid; 15 | } else { 16 | low = mid + 1; 17 | } 18 | } 19 | 20 | return (int)(low -1); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /February2022/Python/FindtheDifference.py: -------------------------------------------------------------------------------- 1 | # TC : O(Max(len(s), len(t))) 2 | # SC : O(1) 3 | 4 | class Solution: 5 | def findTheDifference(self, s: str, t: str) -> str: 6 | if len(s) == 0: 7 | return t 8 | if len(t) == 0: 9 | return s 10 | ind = 0 11 | tSum, sSum = 0, 0 12 | while ind < len(s) or ind < len(t): 13 | if ind < len(t): 14 | tSum += ord(t[ind]) 15 | if ind < len(s): 16 | sSum += ord(s[ind]) 17 | ind += 1 18 | return chr(abs(tSum-sSum)) 19 | -------------------------------------------------------------------------------- /July2021/python/Reshape the matrix: -------------------------------------------------------------------------------- 1 | #author-Aditya Chauhan 2 | Qlink-https://leetcode.com/problems/reshape-the-matrix/ 3 | 4 | class Solution: 5 | def matrixReshape(self, mat: List[List[int]], r: int, c: int) -> List[List[int]]: 6 | lis,ret=[],[] 7 | d=0 8 | row,col=len(mat),len(mat[0]) 9 | if row*col != r*c: 10 | return mat 11 | for i in mat: 12 | for j in i: 13 | lis.append(j) 14 | for i in range(r): 15 | ret+=[lis[d:d+c]] 16 | d=d+c 17 | 18 | return ret 19 | -------------------------------------------------------------------------------- /Leetcode2023/April2023/C++/946.ValidateStackSequences.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool validateStackSequences(vector& pushed, vector& popped) { 4 | 5 | int n = pushed.size(); 6 | stack st; 7 | 8 | int i = 0, j = 0; 9 | for(int i=0;i ts; 7 | public SmallestInfiniteSet() { 8 | ts = new TreeSet<>(); 9 | for(int i=1;i<=1000;i++){ 10 | ts.add(i); 11 | } 12 | } 13 | public int popSmallest() { 14 | return ts.pollFirst(); 15 | } 16 | 17 | public void addBack(int num) { 18 | ts.add(num); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /August2020/HIndex.java: -------------------------------------------------------------------------------- 1 | class HIndex { 2 | public int hIndex(int[] citations) { 3 | int len = citations.length; 4 | int[] bucket = new int[len+1]; 5 | 6 | for(int el: citations){ 7 | if(el>= len){ 8 | bucket[len]++; 9 | } else{ 10 | bucket[el]++; 11 | } 12 | } 13 | int total =0; 14 | for(int i=len;i>=0;i--){ 15 | total += bucket[i]; 16 | if(total>=i){ 17 | return i; 18 | } 19 | } 20 | return 0; 21 | } 22 | } -------------------------------------------------------------------------------- /December2022/Java/ClimbingStairs.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | 3 | class Solution { 4 | public int climbStairs(int n) { 5 | // fibbonacci 6 | if (n == 1) 7 | return 1; 8 | else if (n == 2) 9 | return 2; 10 | else { 11 | int res = 0; 12 | int a = 1, b = 2; 13 | n -= 2; 14 | while (n > 0) { 15 | res = a + b; 16 | a = b; 17 | b = res; 18 | n -= 1; 19 | } 20 | return res; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /June2021/python/Pascal's Triangle-Day 21.py: -------------------------------------------------------------------------------- 1 | #Name-Aditya Chauhan 2 | 3 | class Solution: 4 | def generate(self, numRows: int) -> List[List[int]]: 5 | if numRows==1: 6 | return [[1]] 7 | new_lis=[[1]] 8 | lis=[] 9 | for j in range(2,numRows+1): 10 | lis=[0 for _ in range(j)] 11 | lis[0],lis[-1]=1,1 12 | for i in range(1,len(lis)-1): 13 | 14 | lis[i]=new_lis[-1][i-1]+new_lis[-1][i] 15 | 16 | new_lis+=[lis] 17 | 18 | return new_lis 19 | -------------------------------------------------------------------------------- /March2022/C++/ScoreOfParentheses.cpp: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(n) 4 | // SC : O(1) 5 | 6 | 7 | class Solution { 8 | public: 9 | int scoreOfParentheses(string s) { 10 | int l = s.length(); 11 | int count = 0; 12 | int res = 0; 13 | for (int i = 0; i < l; i++) { 14 | if (s[i] == '(') 15 | count += 1; 16 | else { 17 | count--; 18 | if (s[i-1] == '(') 19 | res += (int)pow(2, count); 20 | } 21 | } 22 | return res; 23 | } 24 | }; 25 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPViolated/ContractEmployee.java: -------------------------------------------------------------------------------- 1 | package LSPViolated; 2 | 3 | import java.util.List; 4 | 5 | public class ContractEmployee extends Employee { 6 | 7 | public ContractEmployee(Integer id, String name) { 8 | super(id, name); 9 | } 10 | 11 | @Override 12 | public Double calculateBonus() { 13 | throw new UnsupportedOperationException(); 14 | } 15 | 16 | @Override 17 | public Double getSalary() { 18 | return 25000d; 19 | } 20 | 21 | @Override 22 | public List getPerks() { 23 | return null; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /August2021/VerifyPreorderSerializationofaBinaryTree.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(len(preorder)) 3 | public boolean isValidSerialization(String preorder) { 4 | String[] nodes = preorder.split(","); 5 | int vacancy = 1; 6 | for (String node: nodes) { 7 | // visiting a node 8 | vacancy--; 9 | // failure check 10 | if (vacancy < 0) return false; 11 | 12 | if (!node.equals("#")) { 13 | vacancy += 2; 14 | } 15 | 16 | } 17 | return vacancy == 0; 18 | } 19 | } -------------------------------------------------------------------------------- /February2021/NumberofStepstoReduceNumbertoZero.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSteps (int num) { 3 | int count = 0; 4 | while(num!=0){ 5 | if(num%2 ==0 ){ 6 | num= num/2; 7 | } else{ 8 | num = num-1; 9 | } 10 | count++; 11 | } 12 | return count; 13 | } 14 | 15 | public int numberOfSteps (int num) { 16 | if(num ==0 ){ 17 | return 0; 18 | } 19 | return 1 + (num%2 ==0 ? numberOfSteps(num/2) : numberOfSteps(num-1)); 20 | } 21 | } -------------------------------------------------------------------------------- /February2022/C++/Compare_Version_Numbers.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int compareVersion(string v1, string v2) { 4 | int res=0,temp0=0,temp1=0,i=0,j=0; 5 | for(;(i int: 6 | sumOccurenceMap = {0: 1} 7 | prefixSum, count = 0, 0 8 | for ind in range(len(nums)): 9 | prefixSum += nums[ind] 10 | if prefixSum-k in sumOccurenceMap: 11 | count += sumOccurenceMap[prefixSum-k] 12 | if prefixSum not in sumOccurenceMap: 13 | sumOccurenceMap[prefixSum] = 0 14 | sumOccurenceMap[prefixSum] += 1 15 | return count 16 | -------------------------------------------------------------------------------- /January2021/CheckIfTwoStringArraysareEquivalent.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | // TC : O(n) n stands for no of elements in wordList1 + wordList2 4 | // SC : O(m) , m stands for no of char in wordList1 + wordsList2 5 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) { 6 | return concatStringList(word1).equals(concatStringList(word2)); 7 | } 8 | 9 | private String concatStringList(String[] wordList){ 10 | String ans = ""; 11 | for(String word: wordList){ 12 | ans += word; 13 | } 14 | return ans; 15 | } 16 | } -------------------------------------------------------------------------------- /January2022/Python/RobotBoundedInCircle.py: -------------------------------------------------------------------------------- 1 | #Author : Karthik K ( https://www.linkedin.com/in/karthikmunirathinam/ ) 2 | 3 | # TC : O(n) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def isRobotBounded(self, instructions: str) -> bool: 8 | x,y=0,0 9 | dirX,dirY=0,1 10 | for direction in instructions: 11 | if direction=='G': 12 | x,y=x+dirX,y+dirY 13 | elif direction=='L': 14 | dirX,dirY=-1*dirY,dirX 15 | else: 16 | dirX,dirY=dirY,-1*dirX 17 | return (x,y)==(0,0) or (dirX,dirY)!=(0,1) -------------------------------------------------------------------------------- /July2021/C++/ReshapeTheMatrix.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Author - geekySapien 3 | QLink - https://leetcode.com/problems/reshape-the-matrix/ 4 | */ 5 | 6 | 7 | 8 | class Solution 9 | { 10 | public: 11 | vector> matrixReshape(vector> &mat, int r, int c) 12 | { 13 | int m = size(mat), n = size(mat[0]), total = m * n; 14 | if (r * c != total) 15 | return mat; 16 | vector> ans(r, vector(c)); 17 | for (int i = 0; i < total; i++) 18 | ans[i / c][i % c] = mat[i / n][i % n]; 19 | return ans; 20 | } 21 | }; -------------------------------------------------------------------------------- /Leetcode2023/May2023/Java/MatrixDiagonalSum.java: -------------------------------------------------------------------------------- 1 | // Author: Mahesh Reddy B N 2 | // Problem Link: https://leetcode.com/problems/matrix-diagonal-sum/description/ 3 | 4 | class Solution { 5 | public int diagonalSum(int[][] mat) { 6 | int ans = 0; 7 | int j =0; 8 | int k = mat.length-1; 9 | for(int i=0;ihigh){ 10 | if(l!=null){ 11 | return l; 12 | } 13 | return r; 14 | } 15 | root.left=l; 16 | root.right=r; 17 | return root; 18 | } 19 | } -------------------------------------------------------------------------------- /Contest/MinimumAmountofTimetoFillCups.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | 4 | public int fillCups(int[] amount) { 5 | // Max Heap 6 | PriorityQueue pq = new PriorityQueue((a,b) ->(b-a)); 7 | 8 | for(int el: amount){ 9 | pq.offer(el); 10 | } 11 | 12 | int ans = 0; 13 | 14 | while(!pq.isEmpty()) { 15 | 16 | int first = pq.poll(); 17 | int second = pq.poll(); 18 | 19 | if(first<=0 && second <=0) { 20 | break; 21 | } 22 | first--; 23 | second--; 24 | pq.offer(first); 25 | pq.offer(second); 26 | ans++; 27 | } 28 | return ans; 29 | } 30 | } -------------------------------------------------------------------------------- /Contest/MinimumNumberofStepstoMakeTwoStringsAnagramII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // TC : O(len(s+t)) 3 | // SC : O(1) 4 | public int minSteps(String s, String t) { 5 | int[] freqS =freqMap(s); 6 | int[] freqT =freqMap(t); 7 | int ans = 0; 8 | for(int i=0;i<26;i++){ 9 | int maxChar = Math.max(freqS[i] , freqT[i]); 10 | ans += (maxChar - freqS[i] ) + (maxChar - freqT[i]); 11 | } 12 | return ans; 13 | } 14 | 15 | private int[] freqMap(String s){ 16 | int[] freq = new int[26]; 17 | for(char c: s.toCharArray()){ 18 | freq[c-'a']++; 19 | } 20 | return freq; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /February2022/Python/MajorityElement.py: -------------------------------------------------------------------------------- 1 | # TC : O(NlogN) 2 | # Sc : O(1) 3 | 4 | class Solution: 5 | def majorityElement(self, nums: List[int]) -> int: 6 | nums.sort() 7 | prev = nums[0] 8 | count = 1 9 | length = len(nums) 10 | if length <= 2: 11 | return nums[0] 12 | for ind in range(1, length): 13 | if nums[ind] == prev: 14 | count += 1 15 | if count > length//2: 16 | return nums[ind] 17 | else: 18 | count = 1 19 | prev = nums[ind] 20 | -------------------------------------------------------------------------------- /July2022/C++/validAnagram.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isAnagram(string s, string t) { 4 | 5 | vectorhash(256); 6 | if(s.length()!=t.length()) return false; 7 | 8 | for(int i=0;i& costs, int coins) { 4 | 5 | int n = costs.size(); 6 | sort(costs.begin(),costs.end()); 7 | int ans = 0; 8 | 9 | for(int i=0;i=0) 12 | { 13 | ans++; 14 | coins -= costs[i]; 15 | if(coins<0) return ans; 16 | } 17 | else break; 18 | } 19 | return ans; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /November2021/KthNumberinMultiplicationTable.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | public int findKthNumber(int m, int n, int k) { 4 | int low = 1 , high = m * n ; 5 | 6 | while (low < high) { 7 | int mid = low + (high - low) / 2; 8 | 9 | int capacity = count(mid, m, n); 10 | if (capacity >= k) high = mid; 11 | else low = mid + 1; 12 | } 13 | 14 | return high; 15 | } 16 | 17 | private int count(int mid, int m, int n) { 18 | int count = 0; 19 | for (int i = 1; i <= m; i++) { 20 | int temp = Math.min(mid / i , n); 21 | count += temp; 22 | } 23 | return count; 24 | } 25 | } -------------------------------------------------------------------------------- /August2022/C++/FirstUniqueCharacterInAString.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | // TC: O(n) 4 | // SC: O(26) ~ O(1) 5 | int firstUniqChar(string s) { 6 | int letters[26] = {0}; 7 | char c; 8 | for (int i = 0; i < s.size(); i++) { 9 | c = s[i]; 10 | letters[c - 'a']++; 11 | } 12 | 13 | for (int i = 0; i < s.size(); i++) { 14 | c = s[i]; 15 | if (letters[c - 'a'] == 1) { 16 | return i; 17 | } 18 | } 19 | 20 | return -1; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /August2022/Java/RansomNote.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(max(magazine,ransomNote)) 4 | // SC : O(26) = O(1) 5 | 6 | class Solution { 7 | public boolean canConstruct(String ransomNote, String magazine) { 8 | int [] hash = new int[26]; 9 | for (char ch : magazine.toCharArray()) { 10 | hash[(int)ch-97] += 1; 11 | } 12 | for (char ch : ransomNote.toCharArray()) { 13 | hash[(int)ch-97] -= 1; 14 | if (hash[(int)ch-97] < 0) 15 | return false; 16 | } 17 | return true; 18 | 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Contest/CountCollisionsonaRoad.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | // Tc : O(n) 3 | // SC : O(1) 4 | public int countCollisions(String directions) { 5 | int left = 0; 6 | int right = directions.length() - 1; 7 | 8 | while (left < directions.length() && directions.charAt(left) == 'L') { 9 | left++; 10 | } 11 | 12 | while (right >= 0 && directions.charAt(right) == 'R') { 13 | right--; 14 | } 15 | 16 | int count = 0; 17 | for (int i = left; i <= right; i++) { 18 | if (directions.charAt(i) != 'S') { 19 | // R L 20 | count++; 21 | } 22 | } 23 | 24 | return count; 25 | } 26 | } -------------------------------------------------------------------------------- /Contest/FirstBadVersion.java: -------------------------------------------------------------------------------- 1 | // https://leetcode.com/problems/first-bad-version/ 2 | // @author: anuj0503 3 | public class Solution extends VersionControl { 4 | public int firstBadVersion(int n) { 5 | long low = 0, high = n; 6 | 7 | while(low < high){ 8 | long mid = low + (high - low)/2; 9 | boolean v = isBadVersion((int)mid); 10 | 11 | if(!v){ 12 | low = mid + 1; 13 | } else{ 14 | high = mid; 15 | } 16 | } 17 | 18 | return (int)low; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /DesignPatterns/src/CreationalDesignPattern/FactoryDesignPattern/MarutiCarFactory.java: -------------------------------------------------------------------------------- 1 | package CreationalDesignPattern.FactoryDesignPattern; 2 | 3 | public class MarutiCarFactory { 4 | 5 | public MarutiCarFactory() { 6 | } 7 | 8 | public Car getCar(CarType carType) { 9 | switch (carType) { 10 | 11 | case SEDAN: 12 | return new Ciaz(); 13 | 14 | case SUV: 15 | return new Breeza(); 16 | 17 | case HATCHBACK: 18 | return new WagonR(); 19 | 20 | 21 | } 22 | return null; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /February2021/NoOf1Bits.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | // TC : O(1) 4 | public int hammingWeight(int n) { 5 | int count = 0; 6 | 7 | for(int i=0;i<32;i++){ 8 | if(((n>>i)&1) ==1){ 9 | count++; 10 | } 11 | } 12 | return count; 13 | } 14 | 15 | // TC : O(log32) 16 | public int hammingWeight(int n) { 17 | int count =0; 18 | while(n!=0){ 19 | n = (n& (n-1)); 20 | count++; 21 | } 22 | return count; 23 | } 24 | } -------------------------------------------------------------------------------- /July2022/C++/jumpGame6.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxResult(vector& nums, int k) { 4 | 5 | int n = nums.size(); 6 | deque q{0}; 7 | 8 | for(int i=1;i& costs, int coins) { 7 | sort(costs.begin(), costs.end()); 8 | 9 | int count = 0; 10 | for (int cost : costs) { 11 | if (coins < cost) { 12 | break; 13 | } 14 | count++; 15 | coins -= cost; 16 | } 17 | 18 | return count; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /March2022/Java/BrokenCalculator.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC : O(logn) 4 | // SC : O(1) 5 | 6 | // Work backwards from target -> startValue. For multiplication -> division, subtraction -> addition 7 | 8 | class Solution { 9 | public int brokenCalc(int startValue, int target) { 10 | int res = 0; 11 | while (target > startValue) { 12 | if (target%2 == 1) 13 | target += 1; 14 | else 15 | target /= 2; 16 | res += 1; 17 | } 18 | res += (startValue - target); 19 | return res; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /May2022/Java/CoinChange.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | // TC : O(amount * Coin count) 4 | // SC : O(amount) 5 | public int coinChange(int[] coins, int amount) { 6 | int[] dp = new int[amount+1]; 7 | 8 | Arrays.fill(dp, Integer.MAX_VALUE); 9 | dp[0] = 0; 10 | 11 | for(int i=1;i<=amount;i++){ 12 | 13 | for(int coin: coins){ 14 | if( i - coin >=0 && dp[i-coin] != Integer.MAX_VALUE){ 15 | dp[i] = Math.min(dp[i], dp[i-coin] +1); 16 | } 17 | } 18 | } 19 | 20 | if(dp[amount] == Integer.MAX_VALUE){ 21 | return -1; 22 | } else { 23 | return dp[amount]; 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /November2021/C++/Arranging_Coins.cpp: -------------------------------------------------------------------------------- 1 | // author : shyam2520 2 | //https://leetcode.com/problems/arranging-coins/ 3 | class Solution { 4 | public: 5 | int arrangeCoins(int n) { 6 | int i=1; 7 | while(n>=i) 8 | { 9 | n-=i; 10 | i++; 11 | } 12 | return i-1; 13 | } 14 | }; 15 | 16 | /* 17 | Alternate Solution : https://leetcode.com/problems/arranging-coins/discuss/92365/C%2B%2B-1-line-code 18 | class Solution { 19 | public: 20 | int arrangeCoins(int n) { 21 | return floor((sqrt((double)2*n + 0.25)-0.5)); 22 | } 23 | }; 24 | */ -------------------------------------------------------------------------------- /August2022/C++/ValidateBinarySearchTree.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | private: 3 | bool helper(TreeNode* root, long min, long max) { 4 | if (root == NULL) { 5 | return true; 6 | } 7 | 8 | return root->val > min && root->val < max && helper(root->left, min, root->val) && helper(root->right, root->val, max); 9 | } 10 | 11 | public: 12 | // TC: O(n) 13 | // SC: O(logn) 14 | bool isValidBST(TreeNode* root) { 15 | long min = LONG_MIN; 16 | long max = LONG_MAX; 17 | return helper(root, min, max); 18 | } 19 | }; -------------------------------------------------------------------------------- /Contest/Weekly Contest 315/LargestPositiveIntegerThatExistsWithItsNegative.py: -------------------------------------------------------------------------------- 1 | # https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/ 2 | # Author : @Jiganesh 3 | 4 | from typing import List 5 | 6 | class Solution: 7 | 8 | # TC : O(N) 9 | # SC : O(N) 10 | def findMaxK(self, nums: List[int]) -> int: 11 | 12 | lookup = set(nums) 13 | 14 | result = float("-inf") 15 | for num in nums: 16 | if -num in lookup: 17 | result = max(result, num) 18 | 19 | return result if result != float("-inf") else -1 20 | -------------------------------------------------------------------------------- /July2021/ValidTriangleNumber.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n2) 4 | // SC : O(1) 5 | public int triangleNumber(int[] nums) { 6 | int ans = 0; 7 | if (nums.length < 3) return ans; 8 | 9 | Arrays.sort(nums); 10 | 11 | for (int i = 2; i < nums.length; i++) { 12 | int left = 0, right = i - 1; 13 | while (left < right) { 14 | if (nums[left] + nums[right] > nums[i]) { 15 | ans += (right - left); 16 | right--; 17 | } 18 | else { 19 | left++; 20 | } 21 | } 22 | } 23 | 24 | return ans; 25 | } 26 | } -------------------------------------------------------------------------------- /May2022/C++/Implementing_stack_using_queues.cpp: -------------------------------------------------------------------------------- 1 | class MyStack { 2 | queue q; 3 | public: 4 | MyStack() { 5 | 6 | } 7 | 8 | void push(int x) { 9 | q.push(x); 10 | for(int i=0;i mid) { // right element 10 | return true; 11 | } else if(elleft){ 12 | mid = el; 13 | } else if(el < left){ 14 | left = el; 15 | } 16 | 17 | } 18 | return false; 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /July2022/Java/PascalsTriangle.java: -------------------------------------------------------------------------------- 1 | 2 | // @saorav21994 3 | // TC: O(n*(n-1)) = O(n^2) 4 | // SC: O(n^2) - output 5 | 6 | class Solution { 7 | public List> generate(int numRows) { 8 | List> res = new ArrayList<>(); 9 | for (int i = 1; i <= numRows; i++) { 10 | List tmp = new ArrayList(); 11 | int n = 1; 12 | for (int j = 1; j <= i; j++) { 13 | tmp.add(n); 14 | n = n*(i-j)/j; 15 | } 16 | res.add(tmp); 17 | } 18 | return res; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /April2022/Kotlin/SwappingNodesInALinkedList.kt: -------------------------------------------------------------------------------- 1 | // Author: Shobhit Behl (LC: shobhitbruh) 2 | var len = 1 3 | var j = head 4 | while (j != null) { 5 | len++ 6 | j = j.next 7 | } 8 | var i = head 9 | var p = 1 10 | while (p < k) { 11 | i = i!!.next 12 | p++ 13 | } 14 | p = 1 15 | var d = head 16 | while (p < len - k) { 17 | d = d!!.next 18 | p++ 19 | } 20 | val temp = d!!.`val` 21 | d.`val` = i!!.`val` 22 | i.`val` = temp 23 | return head -------------------------------------------------------------------------------- /December2021/PowerOfTwo.java: -------------------------------------------------------------------------------- 1 | // TC : O(1) 2 | // SC : O(1) 3 | class Solution { 4 | public boolean isPowerOfTwo(int n) { 5 | if(n == 0 || n ==Integer.MIN_VALUE){ 6 | return false; 7 | } 8 | int ans = n&(n-1); 9 | return (ans==0); 10 | } 11 | 12 | public boolean isPowerOfTwo(int n) { 13 | 14 | if(n == 0 || n ==Integer.MIN_VALUE){ 15 | return false; 16 | } 17 | boolean foundOne = false; 18 | for(int i=0;i<32;i++){ 19 | if(((n>>i)&1)==1){ 20 | if(foundOne){ 21 | return false; 22 | } 23 | foundOne = true; 24 | } 25 | } 26 | return true; 27 | } 28 | } -------------------------------------------------------------------------------- /February2021/ContainerWithMostWater.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | // SC : O(1) 5 | public int maxArea(int[] height) { 6 | int left = 0; 7 | int right = height.length-1; 8 | int maxArea = 0; 9 | while(leftnext&¬Found){ 9 | p = p->next; 10 | q = q->next->next; 11 | if(p == q) 12 | notFound = false; 13 | } 14 | if(notFound) return NULL; 15 | p = head; 16 | while(p != q){ 17 | p = p->next; 18 | q = q->next; 19 | } 20 | return p; 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /Leetcode2023/January2023/Java/GasStation.java: -------------------------------------------------------------------------------- 1 | // @saorav21994 2 | class Solution { 3 | public int canCompleteCircuit(int[] gas, int[] cost) { 4 | int n = gas.length; 5 | int total_surplus = 0; 6 | int surplus = 0; 7 | int start = 0; 8 | 9 | for(int i = 0; i < n; i++){ 10 | total_surplus += gas[i] - cost[i]; 11 | surplus += gas[i] - cost[i]; 12 | if(surplus < 0){ 13 | surplus = 0; 14 | start = i + 1; 15 | } 16 | } 17 | return (total_surplus < 0) ? -1 : start; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPViolated/Employee.java: -------------------------------------------------------------------------------- 1 | package LSPViolated; 2 | 3 | public abstract class Employee implements IEmployee { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Employee(Integer id, String name) { 9 | this.id = id; 10 | this.name = name; 11 | } 12 | 13 | public Integer getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Integer id) { 18 | this.id = id; 19 | } 20 | 21 | public String getName() { 22 | return name; 23 | } 24 | 25 | public void setName(String name) { 26 | this.name = name; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /April2021/BeautifulArrangementII.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | 3 | // TC : O(n) 4 | // SC : O(1) 5 | public int[] constructArray(int n, int k) { 6 | int[] ans = new int[n]; 7 | int low =1; 8 | int high = k+1; 9 | for(int i=0;i=0;i--){ 8 | int furtureReachIndex = i + questions[i][1] +1; 9 | if(furtureReachIndex& flowerbed, int n) { 5 | int temp=0,i=0,len=flowerbed.size(); 6 | while(i=n; 10 | n-=i/2; 11 | for(;i0;i++){ 12 | if(flowerbed[i]){ 13 | n-=temp>2?(temp-1)/2:0; 14 | temp=0; 15 | }else 16 | temp++; 17 | } 18 | n-=temp/2; 19 | return n<=0; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /January2022/C++/can_place_flowers.cpp: -------------------------------------------------------------------------------- 1 | created by @pranav108 2 | class Solution { 3 | public: 4 | bool canPlaceFlowers(vector& flowerbed, int n) { 5 | int temp=0,i=0,len=flowerbed.size(); 6 | while(i=n; 10 | n-=i/2; 11 | for(;i0;i++){ 12 | if(flowerbed[i]){ 13 | n-=temp>2?(temp-1)/2:0; 14 | temp=0; 15 | }else 16 | temp++; 17 | } 18 | n-=temp/2; 19 | return n<=0; 20 | } 21 | }; 22 | -------------------------------------------------------------------------------- /January2022/Java/AddBinary.java: -------------------------------------------------------------------------------- 1 | 2 | class Solution { 3 | // TC : O(Max(a.length, b.length) 4 | // SC : O(a.length +b.length) 5 | public String addBinary(String a, String b) { 6 | String ans = ""; 7 | int n = a.length(); 8 | int m = b.length(); 9 | int i=n-1; 10 | int j=m-1; 11 | int carry=0; 12 | while(i>=0 || j>=0) { 13 | 14 | int x = (i>=0)? a.charAt(i)-'0':0; 15 | int y= (j>=0)?b.charAt(j)-'0':0; 16 | int sum = x+y+carry; 17 | carry = sum/2; 18 | ans = sum%2 + ans; 19 | i--; 20 | j--; 21 | } 22 | if(carry>0) { 23 | ans = carry + ans; 24 | } 25 | return ans; 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /January2022/Python/minimum_number_of_arrows_to_burst_balloons.py: -------------------------------------------------------------------------------- 1 | # Author : Karthik K (https://www.linkedin.com/in/karthikmunirathinam/) 2 | 3 | # TC : O(nlogn) 4 | # SC : O(1) 5 | 6 | class Solution: 7 | def findMinArrowShots(self, points: List[List[int]]) -> int: 8 | points.sort(key=lambda x: x[0]) 9 | arrowCount = 0 10 | x = points[0][1] 11 | for i in range(1, len(points)): 12 | if points[i][0] <= x: 13 | x = min(points[i][1], x) 14 | else: 15 | arrowCount += 1 16 | x = points[i][1] 17 | return arrowCount+1 18 | -------------------------------------------------------------------------------- /SystemDesign/Lowlevel/Solid/LiskovSubstitutionPrinciple/src/LSPCompliant/Employee.java: -------------------------------------------------------------------------------- 1 | package LSPCompliant; 2 | 3 | public abstract class Employee implements IEmployee { 4 | 5 | private Integer id; 6 | private String name; 7 | 8 | public Employee(Integer id, String name) { 9 | this.id = id; 10 | this.name = name; 11 | } 12 | 13 | public Integer getId() { 14 | return id; 15 | } 16 | 17 | public void setId(Integer id) { 18 | this.id = id; 19 | } 20 | 21 | 22 | public String getName() { 23 | return name; 24 | } 25 | 26 | public void setName(String name) { 27 | this.name = name; 28 | } 29 | 30 | } 31 | --------------------------------------------------------------------------------