Repository: ashishps1/awesome-leetcode-resources Branch: main Commit: 75fb5edb6db7 Files: 58 Total size: 128.1 KB Directory structure: gitextract_ohk49sj0/ ├── LICENSE ├── README.md └── patterns/ ├── c#/ │ ├── FastAndSlowPointers.cs │ ├── KadaneAlgorithm.cs │ ├── LevelOrderTraversal.cs │ ├── MonotonicStack.cs │ ├── ReverseList.cs │ ├── SlidingWindow.cs │ ├── TopKElements.cs │ └── TwoPointers.cs ├── c++/ │ ├── FastAndSlowPointers.cpp │ ├── KadaneAlgorithm.cpp │ ├── LevelOrderTraversal.cpp │ ├── MonotonicStack.cpp │ ├── ReverseList.cpp │ ├── SlidingWindow.cpp │ ├── TopKElements.cpp │ └── TwoPointers.cpp ├── go/ │ ├── fast_and_slow_pointers.go │ ├── kadane_algorithm.go │ ├── level_order_traversal.go │ ├── monotonic_stack.go │ ├── reverse_list.go │ ├── sliding_window.go │ ├── top_k_elements.go │ └── two_pointers.go ├── java/ │ ├── FastAndSlowPointers.java │ ├── KadaneAlgorithm.java │ ├── LevelOrderTraversal.java │ ├── MonotonicStack.java │ ├── ReverseLinkedList.java │ ├── SlidingWindow.java │ ├── TopKElements.java │ └── TwoPointers.java ├── javascript/ │ ├── fastAndSlowPointers.js │ ├── kadaneAlgorithm.js │ ├── levelOrderTraversal.js │ ├── monotonicStack.js │ ├── reverseList.js │ ├── slidingWindow.js │ ├── topKElements.js │ └── twoPointers.js ├── python/ │ ├── fast_and_slow_pointers.py │ ├── kadane_algorithm.py │ ├── level_order_traversal.py │ ├── monotonic_stack.py │ ├── reverse_list.py │ ├── sliding_window.py │ ├── top_k_elements.py │ └── two_pointers.py └── typescript/ ├── fastAndSlowPointers.ts ├── kadaneAlgorithm.ts ├── levelOrderTraversal.ts ├── monotonicStack.ts ├── reverseList.ts ├── slidingWindow.ts ├── topKElements.ts └── twoPointers.ts ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The GNU General Public License is a free, copyleft license for software and other kinds of works. The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users. We, the Free Software Foundation, use the GNU General Public License for most of our software; it applies also to any other work released this way by its authors. You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things. To protect your rights, we need to prevent others from denying you these rights or asking you to surrender the rights. Therefore, you have certain responsibilities if you distribute copies of the software, or if you modify it: responsibilities to respect the freedom of others. For example, if you distribute copies of such a program, whether gratis or for a fee, you must pass on to the recipients the same freedoms that you received. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. Developers that use the GNU GPL protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License giving you legal permission to copy, distribute and/or modify it. For the developers' and authors' protection, the GPL clearly explains that there is no warranty for this free software. For both users' and authors' sake, the GPL requires that modified versions be marked as changed, so that their problems will not be attributed erroneously to authors of previous versions. Some devices are designed to deny users access to install or run modified versions of the software inside them, although the manufacturer can do so. This is fundamentally incompatible with the aim of protecting users' freedom to change the software. The systematic pattern of such abuse occurs in the area of products for individuals to use, which is precisely where it is most unacceptable. Therefore, we have designed this version of the GPL to prohibit the practice for those products. If such problems arise substantially in other domains, we stand ready to extend this provision to those domains in future versions of the GPL, as needed to protect the freedom of users. Finally, every program is threatened constantly by software patents. States should not allow patents to restrict development and use of software on general-purpose computers, but in those that do, we wish to avoid the special danger that patents applied to a free program could make it effectively proprietary. To prevent this, the GPL assures that patents cannot be used to render the program non-free. The precise terms and conditions for copying, distribution and modification follow. TERMS AND CONDITIONS 0. Definitions. "This License" refers to version 3 of the GNU General Public License. "Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks. "The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations. To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work. A "covered work" means either the unmodified Program or a work based on the Program. To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well. To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying. An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion. 1. Source Code. The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work. A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language. The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it. The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work. The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source. The Corresponding Source for a work in source code form is that same work. 2. Basic Permissions. All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law. You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you. Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 3. Protecting Users' Legal Rights From Anti-Circumvention Law. No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures. When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures. 4. Conveying Verbatim Copies. You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program. You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee. 5. Conveying Modified Source Versions. You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions: a) The work must carry prominent notices stating that you modified it, and giving a relevant date. b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices". c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it. d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so. A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate. 6. Conveying Non-Source Forms. You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways: a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange. b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge. c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b. d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements. e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d. A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work. A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product. "Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made. If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM). The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network. Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying. 7. Additional Terms. "Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions. When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission. Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms: a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or d) Limiting the use for publicity purposes of names of licensors or authors of the material; or e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors. All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying. If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms. Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way. 8. Termination. You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11). However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation. Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice. Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10. 9. Acceptance Not Required for Having Copies. You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so. 10. Automatic Licensing of Downstream Recipients. Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License. An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts. You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it. 11. Patents. A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version". A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License. Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version. In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party. If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid. If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it. A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007. Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law. 12. No Surrender of Others' Freedom. If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program. 13. Use with the GNU Affero General Public License. Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU Affero General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the special requirements of the GNU Affero General Public License, section 13, concerning interaction through a network will apply to the combination as such. 14. Revised Versions of this License. The Free Software Foundation may publish revised and/or new versions of the GNU General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU General Public License, you may choose any version ever published by the Free Software Foundation. If the Program specifies that a proxy can decide which future versions of the GNU General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program. Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version. 15. Disclaimer of Warranty. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 16. Limitation of Liability. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 17. Interpretation of Sections 15 and 16. If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Also add information on how to contact you by electronic and paper mail. If the program does terminal interaction, make it output a short notice like this when it starts in an interactive mode: Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, your program's commands might be different; for a GUI interface, you would use an "about box". You should also get your employer (if you work as a programmer) or school, if any, to sign a "copyright disclaimer" for the program, if necessary. For more information on this, and how to apply and follow the GNU GPL, see . The GNU General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . ================================================ FILE: README.md ================================================

Join Free Newsletter

This repository contains awesome LeetCode resources to learn Data Structures and Algorithms (DSA) and prepare for Coding interviews. 👉 If you want to master DSA patterns, checkout [AlgoMaster.io](https://algomaster.io) ## 💡 Tips - [How I Mastered DSA](https://blog.algomaster.io/p/how-i-mastered-data-structures-and-algorithms) - [How to Start LeetCode](https://blog.algomaster.io/p/how-to-start-leetcode-in-2025) - [15 Leetcode Patterns](https://blog.algomaster.io/p/15-leetcode-patterns) ## 📌 Fundamental Concepts - [Algorithmic Complexity](https://blog.algomaster.io/p/57bd4963-462f-4294-a972-4012691fc729) - [Big-O Cheat Sheet](https://www.bigocheatsheet.com/) - [Arrays](https://www.youtube.com/watch?v=SlNq09scdWE&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Bit Manipulation Techniques](https://blog.algomaster.io/p/c650df76-f978-46ee-a572-eb13c354905d) - [Sorting Algorithms](https://medium.com/jl-codes/understanding-sorting-algorithms-af6222995c8) - [Linked List](https://www.youtube.com/watch?v=FbHf0ii0WDg&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Queues](https://medium.com/basecs/to-queue-or-not-to-queue-2653bcde5b04) - [Stacks](https://medium.com/basecs/stacks-and-overflows-dbcf7854dc67) - [Hash Tables](https://medium.com/basecs/taking-hash-tables-off-the-shelf-139cbf4752f0) - [Heaps](https://medium.com/basecs/learning-to-love-heaps-cef2b273a238) - [Recursion](https://leetcode.com/discuss/study-guide/1733447/become-master-in-recursion) - [Backtracking](https://medium.com/algorithms-and-leetcode/backtracking-e001561b9f28) - [Trees](https://leetcode.com/discuss/study-guide/1820334/Become-Master-in-Tree) - [Tries](https://medium.com/basecs/trying-to-understand-tries-3ec6bede0014) - [Binary Search](https://leetcode.com/discuss/study-guide/786126/Python-Powerful-Ultimate-Binary-Search-Template.-Solved-many-problems) - [Greedy Algorithm](https://www.freecodecamp.org/news/greedy-algorithms/) - [Dynamic Programming](https://medium.com/basecs/less-repetition-more-dynamic-programming-43d29830a630) - [Graph Theory](https://www.youtube.com/watch?v=xN5VGzK9_FQ&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Master Graph Algorithms](https://blog.algomaster.io/p/master-graph-algorithms-for-coding) - [DFS Traversal](https://medium.com/basecs/deep-dive-through-a-graph-dfs-traversal-8177df5d0f13) - [BFS Traversal](https://medium.com/basecs/going-broad-in-a-graph-bfs-traversal-959bd1a09255) - [Union-Find](https://leetcode.com/discuss/general-discussion/1072418/Disjoint-Set-Union-(DSU)Union-Find-A-Complete-Guide) - [Dijkstra Algorithm](https://leetcode.com/discuss/study-guide/1059477/A-guide-to-Dijkstra's-Algorithm) - [Minimum Spanning Tree](https://www.hackerearth.com/practice/algorithms/graphs/minimum-spanning-tree/tutorial/) ## 🚀 Patterns - [15 Leetcode Patterns](https://blog.algomaster.io/p/15-leetcode-patterns) - [20 DP Patterns](https://blog.algomaster.io/p/20-patterns-to-master-dynamic-programming) - [Two Pointers Pattern](https://www.youtube.com/watch?v=QzZ7nmouLTI&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Sliding Window Pattern](https://www.youtube.com/watch?v=y2d0VHdvfdc&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Prefix Sum Pattern](https://www.youtube.com/watch?v=yuws7YK0Yng&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Fast and Slow Pointers Pattern](https://www.youtube.com/watch?v=b139yf7Ik-E&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Top 'K' Elements Pattern](https://www.youtube.com/watch?v=6_v6OoxvMOE&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Kadane's Algorithm](https://www.youtube.com/watch?v=NUWAXbSlsws&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Linked List In-place Reversal Pattern](https://www.youtube.com/watch?v=auoTGovuo9A&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Monotonic Stack Pattern](https://www.youtube.com/watch?v=DtJVwbbicjQ&list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2) - [Overlapping Intervals Pattern](https://blog.algomaster.io/p/812e72f7-eced-4256-a4c1-00606ae50679) - [Backtracking Pattern](https://blog.algomaster.io/p/81d42ca2-600c-4252-aa33-a56462090048) - [Modified Binary Search Pattern](https://blog.algomaster.io/p/d0d81b04-4c2a-4b45-a101-5137c3146686) - [Tree Patterns](https://leetcode.com/discuss/study-guide/937307/Iterative-or-Recursive-or-DFS-and-BFS-Tree-Traversal-or-In-Pre-Post-and-LevelOrder-or-Views) - [Tree Iterative Traversal](https://medium.com/leetcode-patterns/leetcode-pattern-0-iterative-traversals-on-trees-d373568eb0ec) - [Tree Question Pattern](https://leetcode.com/discuss/study-guide/2879240/TREE-QUESTION-PATTERN-2023-oror-TREE-STUDY-GUIDE) - [Graph Patterns](https://leetcode.com/discuss/study-guide/655708/Graph-For-Beginners-Problems-or-Pattern-or-Sample-Solutions) - [DFS + BFS Patterns (1)](https://medium.com/leetcode-patterns/leetcode-pattern-1-bfs-dfs-25-of-the-problems-part-1-519450a84353) - [DFS + BFS Patterns (2)](https://medium.com/leetcode-patterns/leetcode-pattern-2-dfs-bfs-25-of-the-problems-part-2-a5b269597f52) ## 📝 Must-Read Leetcode Articles - [Sliding Window Template](https://leetcode.com/problems/frequency-of-the-most-frequent-element/solutions/1175088/C++-Maximum-Sliding-Window-Cheatsheet-Template/) - [Two Pointers Patterns](https://leetcode.com/discuss/study-guide/1688903/Solved-all-two-pointers-problems-in-100-days) - [Collections of Important String Questions](https://leetcode.com/discuss/study-guide/2001789/Collections-of-Important-String-questions-Pattern) - [Substring Problem Template](https://leetcode.com/problems/minimum-window-substring/solutions/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems/) - [Binary Search Template](https://leetcode.com/discuss/study-guide/786126/Python-Powerful-Ultimate-Binary-Search-Template.-Solved-many-problems) - [A General Approach to Backtracking Questions](https://leetcode.com/problems/permutations/solutions/18239/A-general-approach-to-backtracking-questions-in-Java-(Subsets-Permutations-Combination-Sum-Palindrome-Partioning)/) - [Monotonic Stack Template](https://leetcode.com/discuss/study-guide/2347639/A-comprehensive-guide-and-template-for-monotonic-stack-based-problems) - [Heap Patterns](https://leetcode.com/discuss/general-discussion/1127238/master-heap-by-solving-23-questions-in-4-patterns-category) - [Bit Manipulation Patterns](https://leetcode.com/discuss/study-guide/4282051/all-types-of-patterns-for-bits-manipulations-and-how-to-use-it) - [Dynamic Programming Patterns](https://leetcode.com/discuss/study-guide/458695/Dynamic-Programming-Patterns) - [Stock Series Patterns](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/solutions/108870/most-consistent-ways-of-dealing-with-the-series-of-stock-problems/) ## ✅ Curated Problems - [AlgoMaster 300](https://algomaster.io/practice/dsa-patterns) - [Blind 75](https://leetcode.com/discuss/general-discussion/460599/blind-75-leetcode-questions) - [Leetcode Top 100 Liked](https://leetcode.com/studyplan/top-100-liked/) - [Leetcode Top Interview 150](https://leetcode.com/studyplan/top-interview-150/) ## 📺 YouTube Playlist - [AlgoMaster DSA Playlist](https://www.youtube.com/playlist?list=PLK63NuByH5o9odyBT7nfYkHZyvGQ5oVp2&pp=gAQB) - [AlgoMaster LeetCode Pattern Playlist](https://www.youtube.com/playlist?list=PLK63NuByH5o-tqaMUHRA4r8ObRW7PWz45) - [Abdul Bari's Algorithms Playlist](https://www.youtube.com/playlist?list=PLDN4rrl48XKpZkf03iYFl-O29szjTrs_O) - [William Fiset's Data Structure Playlist](https://www.youtube.com/playlist?list=PLDV1Zeh2NRsB6SWUrDFW2RmDotAfPbeHu) - [William Fiset's Graphs Playlist](https://www.youtube.com/playlist?list=PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P) - [Tushar Roy's Dynamic Programming Playlist](https://www.youtube.com/playlist?list=PLrmLmBdmIlpsHaNTPP_jHHDx_os9ItYXr) ## 📇 Courses - [Coursera - Algorithms, Part I](https://www.coursera.org/learn/algorithms-part1) - [Coursera - Algorithms, Part 2](https://www.coursera.org/learn/algorithms-part2) ## 📚 Books - [Data Structures And Algorithms Made Easy](https://www.amazon.in/dp/B08CMLS7LZ) - [Cracking the Coding Interview](https://www.amazon.in/dp/0984782850) ## 📩 Newsletter - [AlgoMaster Newsletter](https://blog.algomaster.io/) ## 🔎 Visualization - [AlgoMaster DSA Animations](https://algomaster.io/animations/dsa) - [VisuAlgo](https://visualgo.net/en) ## 📎 LeetCode Extensions - [LeetCode Timer](https://chromewebstore.google.com/detail/leetcode-timer/gfkgelnlcnomnahkfmhemgpahgmibofd): Easily time your leetcode practise sessions with automatic time setting based on difficulty. - [LeetCode Video Solutions](https://chromewebstore.google.com/detail/leetcode-video-solutions/ilnmgkahgjdpkoliooildngldmilhelm): Watch free LeetCode video ▶ solutions on the problem page itself. - [LeetCode Format](https://chromewebstore.google.com/detail/leetcode-format/imogghebhifnnlgogigikjecilkicfpp): Adds Format code button on leetcode to format the code using Prettier code formatter. - [LeetHub v2](https://chromewebstore.google.com/detail/leethub-v2/mhanfgfagplhgemhjfeolkkdidbakocm?hl=en): Automatically integrate your Leetcode & GeeksforGeeks submissions to GitHub. - [LeetCode VS Code Extension](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode): Solve LeetCode problems in VS Code. Your contributions are most welcome! ---

If you find this resource helpful, please give it a star ⭐️ and share it with others!

================================================ FILE: patterns/c#/FastAndSlowPointers.cs ================================================ using System; using System.Collections.Generic; public class ListNode { public int val; public ListNode next; public ListNode(int x) { val = x; next = null; } } public class FastAndSlowPointers { // LeetCode 141 - Linked List Cycle (HashSet Approach) public bool HasCycleHashSetApproach(ListNode head) { HashSet visited = new HashSet(); ListNode current = head; while (current != null) { if (visited.Contains(current)) { return true; // Cycle detected } visited.Add(current); current = current.next; } return false; // No cycle } // LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) public bool HasCycleFastAndSlowPointersApproach(ListNode head) { if (head == null || head.next == null) return false; ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } return false; } // LeetCode 876 - Middle of the Linked List (Counting Approach) public ListNode MiddleNodeCountingApproach(ListNode head) { int count = 0; ListNode current = head; while (current != null) { count++; current = current.next; } current = head; for (int i = 0; i < count / 2; i++) { current = current.next; } return current; } // LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) public ListNode MiddleNodeFastAndSlowPointerApproach(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; } // LeetCode 202 - Happy Number (HashSet Approach) private int GetSumOfSquares(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit * digit; n /= 10; } return sum; } public bool IsHappyHashSetApproach(int n) { HashSet seen = new HashSet(); while (n != 1 && !seen.Contains(n)) { seen.Add(n); n = GetSumOfSquares(n); } return n == 1; } // LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) public bool IsHappyFastAndSlowPointersApproach(int n) { int slow = n; int fast = GetSumOfSquares(n); while (fast != 1 && slow != fast) { slow = GetSumOfSquares(slow); fast = GetSumOfSquares(GetSumOfSquares(fast)); } return fast == 1; } } ================================================ FILE: patterns/c#/KadaneAlgorithm.cs ================================================ using System; public class KadaneAlgorithm { public int MaxSubArray(int[] nums) { int currentSum = nums[0]; int maxSum = nums[0]; for (int i = 1; i < nums.Length; i++) { currentSum = Math.Max(nums[i], currentSum + nums[i]); maxSum = Math.Max(maxSum, currentSum); } return maxSum; } } ================================================ FILE: patterns/c#/LevelOrderTraversal.cs ================================================ using System; using System.Collections.Generic; public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode(int x) { val = x; } } public class LevelOrderTraversal { public void LevelOrder(TreeNode root) { if (root == null) return; Queue queue = new Queue(); queue.Enqueue(root); while (queue.Count > 0) { TreeNode node = queue.Dequeue(); Console.Write(node.val + " "); // Process the node by printing its value // Add the left and right children to the queue, if they exist if (node.left != null) queue.Enqueue(node.left); if (node.right != null) queue.Enqueue(node.right); } } } ================================================ FILE: patterns/c#/MonotonicStack.cs ================================================ using System; using System.Collections.Generic; public class MonotonicStack { public int[] NextGreaterElement(int[] nums) { int n = nums.Length; int[] result = new int[n]; Array.Fill(result, -1); // Default to -1 if no greater element exists Stack stack = new Stack(); // Stack stores indices for (int i = 0; i < n; i++) { while (stack.Count > 0 && nums[i] > nums[stack.Peek()]) { int index = stack.Pop(); result[index] = nums[i]; } stack.Push(i); } return result; } public int[] DailyTemperatures(int[] temperatures) { int n = temperatures.Length; int[] result = new int[n]; // Result array initialized with 0s Stack stack = new Stack(); // Monotonic decreasing stack for (int i = 0; i < n; i++) { while (stack.Count > 0 && temperatures[i] > temperatures[stack.Peek()]) { int prevIndex = stack.Pop(); result[prevIndex] = i - prevIndex; } stack.Push(i); } return result; } } ================================================ FILE: patterns/c#/ReverseList.cs ================================================ public class ListNode { public int val; public ListNode next; public ListNode(int val = 0, ListNode next = null) { this.val = val; this.next = next; } } public class Solution { public ListNode ReverseList(ListNode head) { ListNode prev = null; ListNode curr = head; while (curr != null) { ListNode next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } } ================================================ FILE: patterns/c#/SlidingWindow.cs ================================================ using System; using System.Collections.Generic; public class SlidingWindow { public double FindMaxAverageBruteForce(int[] nums, int k) { int n = nums.Length; double maxAvg = double.MinValue; for (int i = 0; i <= n - k; i++) { int sum = 0; for (int j = i; j < i + k; j++) { sum += nums[j]; } maxAvg = Math.Max(maxAvg, (double)sum / k); } return maxAvg; } public double FindMaxAverageSlidingWindow(int[] nums, int k) { int n = nums.Length; int sum = 0; for (int i = 0; i < k; i++) { sum += nums[i]; } int maxSum = sum; for (int i = k; i < n; i++) { sum += nums[i]; sum -= nums[i - k]; maxSum = Math.Max(maxSum, sum); } return (double)maxSum / k; } public int LengthOfLongestSubstringSlidingWindow(string s) { HashSet seen = new HashSet(); int maxLength = 0, left = 0; for (int right = 0; right < s.Length; right++) { while (seen.Contains(s[right])) { seen.Remove(s[left]); left++; } seen.Add(s[right]); maxLength = Math.Max(maxLength, right - left + 1); } return maxLength; } public int LengthOfLongestSubstringSlidingWindowFrequencyArray(string s) { int[] freq = new int[128]; int maxLength = 0, left = 0; for (int right = 0; right < s.Length; right++) { freq[s[right]]++; while (freq[s[right]] > 1) { freq[s[left]]--; left++; } maxLength = Math.Max(maxLength, right - left + 1); } return maxLength; } } ================================================ FILE: patterns/c#/TopKElements.cs ================================================ using System; using System.Collections.Generic; using System.Linq; public class TopKElements { // K Largest Elements using Sorting public int[] KLargestElementsSortingApproach(int[] nums, int k) { Array.Sort(nums, (a, b) => b.CompareTo(a)); return nums.Take(k).ToArray(); } // K Largest Elements using Max Heap public int[] KLargestElementsMaxHeapApproach(int[] nums, int k) { PriorityQueue maxHeap = new PriorityQueue(Comparer.Create((a, b) => b - a)); foreach (var num in nums) { maxHeap.Enqueue(num, num); } var result = new int[k]; for (int i = 0; i < k; i++) { result[i] = maxHeap.Dequeue(); } return result; } // K Largest Elements using Min Heap public int[] KLargestElementsMinHeapApproach(int[] nums, int k) { PriorityQueue minHeap = new PriorityQueue(); for (int i = 0; i < k; i++) { minHeap.Enqueue(nums[i], nums[i]); } for (int i = k; i < nums.Length; i++) { minHeap.Enqueue(nums[i], nums[i]); if (minHeap.Count > k) { minHeap.Dequeue(); } } var result = new int[k]; for (int i = 0; i < k; i++) { result[i] = minHeap.Dequeue(); } return result; } // Top K Frequent Elements using Sorting public int[] TopKFrequentElementsSortingApproach(int[] nums, int k) { var frequencyMap = new Dictionary(); foreach (var num in nums) { if (!frequencyMap.ContainsKey(num)) { frequencyMap[num] = 0; } frequencyMap[num]++; } var sortedEntries = frequencyMap.OrderByDescending(e => e.Value).Take(k).Select(e => e.Key).ToArray(); return sortedEntries; } // Top K Frequent Elements using Min Heap public int[] TopKFrequentElementsMinHeapApproach(int[] nums, int k) { var frequencyMap = new Dictionary(); foreach (var num in nums) { if (!frequencyMap.ContainsKey(num)) { frequencyMap[num] = 0; } frequencyMap[num]++; } var minHeap = new PriorityQueue(Comparer.Create((a, b) => a.CompareTo(b))); foreach (var entry in frequencyMap) { minHeap.Enqueue(entry.Key, entry.Value); if (minHeap.Count > k) { minHeap.Dequeue(); } } var result = new int[k]; for (int i = 0; i < k; i++) { result[i] = minHeap.Dequeue(); } return result; } // K Closest Points to Origin using Max Heap private int GetDistance(int[] point) { return point[0] * point[0] + point[1] * point[1]; } public int[][] KClosestPointsToOriginMaxHeapApproach(int[][] points, int k) { PriorityQueue maxHeap = new PriorityQueue(Comparer.Create((a, b) => b - a)); foreach (var point in points) { maxHeap.Enqueue(point, GetDistance(point)); if (maxHeap.Count > k) { maxHeap.Dequeue(); } } var result = new int[k][]; for (int i = 0; i < k; i++) { result[i] = maxHeap.Dequeue(); } return result; } } ================================================ FILE: patterns/c#/TwoPointers.cs ================================================ using System; public class TwoPointers { // Move Zeroes using Two Pointers public void MoveZeroesTwoPointers(int[] nums) { int left = 0; // Pointer for placing non-zero elements // Iterate with right pointer for (int right = 0; right < nums.Length; right++) { if (nums[right] != 0) { // Swap elements if right pointer finds a non-zero (nums[left], nums[right]) = (nums[right], nums[left]); left++; // Move left pointer forward } } } // Brute Force approach for Container with Most Water public int MaxAreaBruteForce(int[] height) { int n = height.Length; int maxArea = 0; // Check all pairs (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Compute the minimum height and width int minHeight = Math.Min(height[i], height[j]); int width = j - i; int area = minHeight * width; // Compute water contained maxArea = Math.Max(maxArea, area); // Update max water } } return maxArea; } // Two Pointers approach for Container with Most Water public int MaxAreaTwoPointers(int[] height) { int left = 0, right = height.Length - 1; int maxArea = 0; // Move pointers toward each other while (left < right) { int width = right - left; // Distance between lines int minHeight = Math.Min(height[left], height[right]); // Compute height int area = minHeight * width; // Compute water contained maxArea = Math.Max(maxArea, area); // Update max water // Move the pointer pointing to the shorter height if (height[left] < height[right]) { left++; // Move left pointer forward } else { right--; // Move right pointer backward } } return maxArea; } } ================================================ FILE: patterns/c++/FastAndSlowPointers.cpp ================================================ #include using namespace std; class ListNode { public: int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; class FastAndSlowPointers { public: // LeetCode 141 - Linked List Cycle (HashSet Approach) bool hasCycleHashSetApproach(ListNode* head) { unordered_set visited; ListNode* current = head; while (current != nullptr) { if (visited.find(current) != visited.end()) { return true; // Cycle detected } visited.insert(current); current = current->next; } return false; // No cycle } // LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) bool hasCycleFastAndSlowPointersApproach(ListNode* head) { if (head == nullptr || head->next == nullptr) { return false; } ListNode* slow = head; ListNode* fast = head; while (fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; if (slow == fast) { return true; // Cycle detected } } return false; // No cycle } // LeetCode 876 - Middle of the Linked List (Counting Approach) ListNode* middleNodeCountingApproach(ListNode* head) { int count = 0; ListNode* current = head; while (current != nullptr) { count++; current = current->next; } current = head; for (int i = 0; i < count / 2; i++) { current = current->next; } return current; } // LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) ListNode* middleNodeFastAndSlowPointerApproach(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while (fast != nullptr && fast->next != nullptr) { slow = slow->next; fast = fast->next->next; } return slow; } // LeetCode 202 - Happy Number (HashSet Approach) int getSumOfSquares(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit * digit; n /= 10; } return sum; } bool isHappyHashSetApproach(int n) { unordered_set seen; while (n != 1 && seen.find(n) == seen.end()) { seen.insert(n); n = getSumOfSquares(n); } return n == 1; } // LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) bool isHappyFastAndSlowPointersApproach(int n) { int slow = n; int fast = getSumOfSquares(n); while (fast != 1 && slow != fast) { slow = getSumOfSquares(slow); fast = getSumOfSquares(getSumOfSquares(fast)); } return fast == 1; } }; ================================================ FILE: patterns/c++/KadaneAlgorithm.cpp ================================================ #include #include // For std::max class KadaneAlgorithm { public: int maxSubArray(std::vector& nums) { int currentSum = nums[0]; // Start with the first element int maxSum = nums[0]; // Initialize maxSum with the first element // Traverse the array from the second element for (size_t i = 1; i < nums.size(); i++) { // If currentSum is negative, reset to current element currentSum = std::max(nums[i], currentSum + nums[i]); // Update maxSum if currentSum is greater maxSum = std::max(maxSum, currentSum); } return maxSum; } }; ================================================ FILE: patterns/c++/LevelOrderTraversal.cpp ================================================ #include #include using namespace std; // Definition for a binary tree node. struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; class LevelOrderTraversal { public: void levelOrder(TreeNode* root) { if (root == nullptr) return; queue q; q.push(root); while (!q.empty()) { TreeNode* node = q.front(); q.pop(); cout << node->val << " "; // Process the node by printing its value // Add the left and right children to the queue, if they exist if (node->left != nullptr) q.push(node->left); if (node->right != nullptr) q.push(node->right); } } }; ================================================ FILE: patterns/c++/MonotonicStack.cpp ================================================ #include #include using namespace std; class MonotonicStack { public: vector nextGreaterElement(vector& nums) { int n = nums.size(); vector result(n, -1); // Default to -1 if no greater element exists stack stack; // Stack stores indices for (int i = 0; i < n; i++) { while (!stack.empty() && nums[i] > nums[stack.top()]) { int index = stack.top(); stack.pop(); result[index] = nums[i]; } stack.push(i); } return result; } vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); vector result(n, 0); stack stack; // Monotonic decreasing stack for (int i = 0; i < n; i++) { while (!stack.empty() && temperatures[i] > temperatures[stack.top()]) { int prevIndex = stack.top(); stack.pop(); result[prevIndex] = i - prevIndex; } stack.push(i); } return result; } }; ================================================ FILE: patterns/c++/ReverseList.cpp ================================================ struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; ListNode* reverseList(ListNode* head) { ListNode* prev = nullptr; ListNode* curr = head; while (curr != nullptr) { ListNode* next = curr->next; curr->next = prev; prev = curr; curr = next; } return prev; } ================================================ FILE: patterns/c++/SlidingWindow.cpp ================================================ #include #include #include #include using namespace std; class SlidingWindow { public: double findMaxAverageBruteForce(vector& nums, int k) { int n = nums.size(); double maxAvg = INT_MIN; for (int i = 0; i <= n - k; i++) { int sum = 0; for (int j = i; j < i + k; j++) { sum += nums[j]; } maxAvg = max(maxAvg, (double)sum / k); } return maxAvg; } double findMaxAverageSlidingWindow(vector& nums, int k) { int n = nums.size(); int sum = 0; for (int i = 0; i < k; i++) { sum += nums[i]; } int maxSum = sum; for (int i = k; i < n; i++) { sum += nums[i]; sum -= nums[i - k]; maxSum = max(maxSum, sum); } return (double)maxSum / k; } int lengthOfLongestSubstringSlidingWindow(string s) { unordered_set seen; int maxLength = 0, left = 0; for (int right = 0; right < s.size(); right++) { while (seen.count(s[right])) { seen.erase(s[left]); left++; } seen.insert(s[right]); maxLength = max(maxLength, right - left + 1); } return maxLength; } int lengthOfLongestSubstringSlidingWindowFrequencyArray(string s) { vector freq(128, 0); int maxLength = 0, left = 0; for (int right = 0; right < s.size(); right++) { freq[s[right]]++; while (freq[s[right]] > 1) { freq[s[left]]--; left++; } maxLength = max(maxLength, right - left + 1); } return maxLength; } }; ================================================ FILE: patterns/c++/TopKElements.cpp ================================================ #include #include #include #include #include using namespace std; class TopKElements { public: // K Largest Elements using Sorting vector kLargestElementsSortingAppraoch(vector& nums, int k) { sort(nums.begin(), nums.end(), greater()); return vector(nums.begin(), nums.begin() + k); } // K Largest Elements using Max Heap vector kLargestElementsMaxHeapAppraoch(vector& nums, int k) { priority_queue maxHeap(nums.begin(), nums.end()); vector result; for (int i = 0; i < k; i++) { result.push_back(maxHeap.top()); maxHeap.pop(); } return result; } // K Largest Elements using Min Heap vector kLargestElementsMinHeapAppraoch(vector& nums, int k) { priority_queue, greater> minHeap; for (int i = 0; i < k; i++) { minHeap.push(nums[i]); } for (int i = k; i < nums.size(); i++) { minHeap.push(nums[i]); if (minHeap.size() > k) { minHeap.pop(); } } vector result; while (!minHeap.empty()) { result.push_back(minHeap.top()); minHeap.pop(); } return result; } // Top K Frequent Elements using Sorting vector topKFrequentElementsSortingApproach(vector& nums, int k) { unordered_map frequencyMap; for (int num : nums) { frequencyMap[num]++; } vector> freqVec(frequencyMap.begin(), frequencyMap.end()); sort(freqVec.begin(), freqVec.end(), [](pair& a, pair& b) { return b.second < a.second; }); vector result; for (int i = 0; i < k; i++) { result.push_back(freqVec[i].first); } return result; } // Top K Frequent Elements using Min Heap vector topKFrequentElementsMinHeapApproach(vector& nums, int k) { unordered_map frequencyMap; for (int num : nums) { frequencyMap[num]++; } priority_queue, vector>, greater>> minHeap; for (auto& entry : frequencyMap) { minHeap.push({entry.second, entry.first}); if (minHeap.size() > k) { minHeap.pop(); } } vector result; while (!minHeap.empty()) { result.push_back(minHeap.top().second); minHeap.pop(); } return result; } // K Closest Points to Origin using Max Heap int getDistance(vector& point) { return point[0] * point[0] + point[1] * point[1]; } vector> kClosestPointsToOriginMaxHeapApproach(vector>& points, int k) { priority_queue>> maxHeap; for (vector& point : points) { maxHeap.push({getDistance(point), point}); if (maxHeap.size() > k) { maxHeap.pop(); } } vector> result; while (!maxHeap.empty()) { result.push_back(maxHeap.top().second); maxHeap.pop(); } return result; } }; ================================================ FILE: patterns/c++/TwoPointers.cpp ================================================ #include #include #include using namespace std; class TwoPointers { public: // Move Zeroes using Two Pointers void moveZeroesTwoPointers(vector& nums) { int left = 0; // Pointer for placing non-zero elements // Iterate with right pointer for (int right = 0; right < nums.size(); right++) { if (nums[right] != 0) { // Swap elements if right pointer finds a non-zero swap(nums[left], nums[right]); left++; // Move left pointer forward } } } // Brute Force approach for Container with Most Water int maxAreaBruteForce(vector& height) { int n = height.size(); int maxArea = 0; // Check all pairs (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Compute the minimum height and width int minHeight = min(height[i], height[j]); int width = j - i; int area = minHeight * width; // Compute water contained maxArea = max(maxArea, area); // Update max water } } return maxArea; } // Two Pointers approach for Container with Most Water int maxAreaTwoPointers(vector& height) { int left = 0, right = height.size() - 1; int maxArea = 0; // Move pointers toward each other while (left < right) { int width = right - left; // Distance between lines int minHeight = min(height[left], height[right]); // Compute height int area = minHeight * width; // Compute water contained maxArea = max(maxArea, area); // Update max water // Move the pointer pointing to the shorter height if (height[left] < height[right]) { left++; // Move left pointer forward } else { right--; // Move right pointer backward } } return maxArea; } }; ================================================ FILE: patterns/go/fast_and_slow_pointers.go ================================================ package main import "fmt" type ListNode struct { Val int Next *ListNode } // LeetCode 141 - Linked List Cycle (HashSet Approach) func hasCycleHashSetApproach(head *ListNode) bool { visited := map[*ListNode]bool{} current := head for current != nil { if visited[current] { return true } visited[current] = true current = current.Next } return false } // LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) func hasCycleFastAndSlowPointersApproach(head *ListNode) bool { if head == nil || head.Next == nil { return false } slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next if slow == fast { return true } } return false } // LeetCode 876 - Middle of the Linked List (Counting Approach) func middleNodeCountingApproach(head *ListNode) *ListNode { count := 0 current := head for current != nil { count++ current = current.Next } current = head for i := 0; i < count/2; i++ { current = current.Next } return current } // LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) func middleNodeFastAndSlowPointerApproach(head *ListNode) *ListNode { slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next } return slow } // LeetCode 202 - Happy Number (HashSet Approach) func getSumOfSquares(n int) int { sum := 0 for n > 0 { digit := n % 10 sum += digit * digit n /= 10 } return sum } func isHappyHashSetApproach(n int) bool { seen := map[int]bool{} for n != 1 && !seen[n] { seen[n] = true n = getSumOfSquares(n) } return n == 1 } // LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) func isHappyFastAndSlowPointersApproach(n int) bool { slow := n fast := getSumOfSquares(n) for fast != 1 && slow != fast { slow = getSumOfSquares(slow) fast = getSumOfSquares(getSumOfSquares(fast)) } return fast == 1 } func main() { // You can test the implementations here } ================================================ FILE: patterns/go/kadane_algorithm.go ================================================ package main import "math" func maxSubArray(nums []int) int { currentSum := nums[0] maxSum := nums[0] for i := 1; i < len(nums); i++ { currentSum = int(math.Max(float64(nums[i]), float64(currentSum+nums[i]))) maxSum = int(math.Max(float64(maxSum), float64(currentSum))) } return maxSum } ================================================ FILE: patterns/go/level_order_traversal.go ================================================ package main import "fmt" // Definition for a binary tree node. type TreeNode struct { Val int Left *TreeNode Right *TreeNode } func levelOrder(root *TreeNode) { if root == nil { return } queue := []*TreeNode{root} for len(queue) > 0 { node := queue[0] queue = queue[1:] fmt.Print(node.Val, " ") // Process the node by printing its value // Add the left and right children to the queue, if they exist if node.Left != nil { queue = append(queue, node.Left) } if node.Right != nil { queue = append(queue, node.Right) } } func main() { // Example usage root := &TreeNode{Val: 1} root.Left = &TreeNode{Val: 2} root.Right = &TreeNode{Val: 3} levelOrder(root) // Output: 1 2 3 } ================================================ FILE: patterns/go/monotonic_stack.go ================================================ package main import "fmt" func nextGreaterElement(nums []int) []int { n := len(nums) result := make([]int, n) for i := range result { result[i] = -1 // Default to -1 if no greater element exists } stack := []int{} // Stack stores indices for i := 0; i < n; i++ { for len(stack) > 0 && nums[i] > nums[stack[len(stack)-1]] { index := stack[len(stack)-1] stack = stack[:len(stack)-1] result[index] = nums[i] } stack = append(stack, i) } return result } func dailyTemperatures(temperatures []int) []int { n := len(temperatures) result := make([]int, n) // Result array initialized with 0s stack := []int{} // Monotonic decreasing stack for i := 0; i < n; i++ { for len(stack) > 0 && temperatures[i] > temperatures[stack[len(stack)-1]] { prevIndex := stack[len(stack)-1] stack = stack[:len(stack)-1] result[prevIndex] = i - prevIndex } stack = append(stack, i) } return result } func main() { nums := []int{2, 1, 5, 6, 2, 3} fmt.Println(nextGreaterElement(nums)) temperatures := []int{73, 74, 75, 71, 69, 72, 76, 73} fmt.Println(dailyTemperatures(temperatures)) } ================================================ FILE: patterns/go/reverse_list.go ================================================ package main type ListNode struct { Val int Next *ListNode } func reverseList(head *ListNode) *ListNode { var prev *ListNode = nil curr := head for curr != nil { next := curr.Next curr.Next = prev prev = curr curr = next } return prev } ================================================ FILE: patterns/go/sliding_window.go ================================================ package main import ( "math" ) // Brute Force Approach - O(n * k) func findMaxAverageBruteForce(nums []int, k int) float64 { n := len(nums) maxAvg := math.Inf(-1) for i := 0; i <= n-k; i++ { sum := 0 for j := i; j < i+k; j++ { sum += nums[j] } maxAvg = math.Max(maxAvg, float64(sum)/float64(k)) } return maxAvg } // Sliding Window Approach - O(n) func findMaxAverageSlidingWindow(nums []int, k int) float64 { sum := 0 for i := 0; i < k; i++ { sum += nums[i] } maxSum := sum for i := k; i < len(nums); i++ { sum += nums[i] - nums[i-k] if sum > maxSum { maxSum = sum } } return float64(maxSum) / float64(k) } // Sliding Window for Longest Substring Without Repeating Characters func lengthOfLongestSubstringSlidingWindow(s string) int { seen := make(map[byte]bool) maxLength, left := 0, 0 for right := 0; right < len(s); right++ { for seen[s[right]] { delete(seen, s[left]) left++ } seen[s[right]] = true maxLength = max(maxLength, right-left+1) } return maxLength } // Sliding Window using Frequency Array func lengthOfLongestSubstringSlidingWindowFrequencyArray(s string) int { freq := make([]int, 128) maxLength, left := 0, 0 for right := 0; right < len(s); right++ { freq[s[right]]++ for freq[s[right]] > 1 { freq[s[left]]-- left++ } maxLength = max(maxLength, right-left+1) } return maxLength } // Helper function to get max of two numbers func max(a, b int) int { if a > b { return a } return b } ================================================ FILE: patterns/go/top_k_elements.go ================================================ package main import ( "container/heap" "sort" ) // ********** K Largest Elements ********** // K Largest Elements using Sorting func kLargestElementsSortingApproach(nums []int, k int) []int { sort.Sort(sort.Reverse(sort.IntSlice(nums))) return nums[:k] } // K Largest Elements using Max Heap func kLargestElementsMaxHeapApproach(nums []int, k int) []int { h := &MaxHeap{} heap.Init(h) for _, num := range nums { heap.Push(h, num) } result := make([]int, k) for i := 0; i < k; i++ { result[i] = heap.Pop(h).(int) } return result } // K Largest Elements using Min Heap func kLargestElementsMinHeapApproach(nums []int, k int) []int { h := &MinHeap{} heap.Init(h) for i := 0; i < k; i++ { heap.Push(h, nums[i]) } for i := k; i < len(nums); i++ { heap.Push(h, nums[i]) if h.Len() > k { heap.Pop(h) } } result := make([]int, k) for i := 0; i < k; i++ { result[i] = heap.Pop(h).(int) } return result } // ********** Helper Structures ********** type MaxHeap []int type MinHeap []int func (h MaxHeap) Len() int { return len(h) } func (h MaxHeap) Less(i, j int) bool { return h[i] > h[j] } // Max heap func (h MaxHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *MaxHeap) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *MaxHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } func (h MinHeap) Len() int { return len(h) } func (h MinHeap) Less(i, j int) bool { return h[i] < h[j] } // Min heap func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *MinHeap) Push(x interface{}) { *h = append(*h, x.(int)) } func (h *MinHeap) Pop() interface{} { old := *h n := len(old) x := old[n-1] *h = old[0 : n-1] return x } // ********** Main ********** func main() { // Example test cases nums := []int{3, 2, 1, 5, 6, 4} k := 2 // Sorting Approach result := kLargestElementsSortingApproach(nums, k) fmt.Println("K Largest Elements (Sorting Approach):", result) // Max Heap Approach result = kLargestElementsMaxHeapApproach(nums, k) fmt.Println("K Largest Elements (Max Heap Approach):", result) // Min Heap Approach result = kLargestElementsMinHeapApproach(nums, k) fmt.Println("K Largest Elements (Min Heap Approach):", result) } ================================================ FILE: patterns/go/two_pointers.go ================================================ package main // Move Zeroes using Two Pointers func moveZeroesTwoPointers(nums []int) { left := 0 // Pointer for placing non-zero elements // Iterate with right pointer for right := 0; right < len(nums); right++ { if nums[right] != 0 { // Swap elements if right pointer finds a non-zero nums[left], nums[right] = nums[right], nums[left] left++ // Move left pointer forward } } } // Brute Force approach for Container with Most Water func maxAreaBruteForce(height []int) int { n := len(height) maxArea := 0 // Check all pairs (i, j) for i := 0; i < n; i++ { for j := i + 1; j < n; j++ { // Compute the minimum height and width minHeight := min(height[i], height[j]) width := j - i area := minHeight * width // Compute water contained if area > maxArea { maxArea = area // Update max water } } } return maxArea } // Two Pointers approach for Container with Most Water func maxAreaTwoPointers(height []int) int { left, right := 0, len(height)-1 maxArea := 0 // Move pointers toward each other for left < right { width := right - left // Distance between lines minHeight := min(height[left], height[right]) // Compute height area := minHeight * width // Compute water contained if area > maxArea { maxArea = area // Update max water } // Move the pointer pointing to the shorter height if height[left] < height[right] { left++ // Move left pointer forward } else { right-- // Move right pointer backward } } return maxArea } // Helper function to return the minimum of two integers func min(a, b int) int { if a < b { return a } return b } ================================================ FILE: patterns/java/FastAndSlowPointers.java ================================================ package patterns.java; import java.util.HashSet; public class FastAndSlowPointers { class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } /* * ********** LeetCode 141 - Linked List Cycle (https://leetcode.com/problems/linked-list-cycle/) ********** */ public boolean hasCycleHashSetAppraoch(ListNode head) { HashSet visited = new HashSet<>(); ListNode current = head; while (current != null) { if (visited.contains(current)) { return true; // Cycle detected } visited.add(current); current = current.next; } return false; // No cycle } public boolean hasCycleFastAndSlowPointersAppraoch(ListNode head) { if (head == null || head.next == null) { return false; // No cycle if the list is empty or has only one node } ListNode slow = head; ListNode fast = head; while (fast != null && fast.next != null) { slow = slow.next; // Move slow pointer by 1 step fast = fast.next.next; // Move fast pointer by 2 steps if (slow == fast) { return true; // Cycle detected } } return false; // No cycle } /* * ********** LeetCode 876 - Middle of the Linked List (https://leetcode.com/problems/middle-of-the-linked-list/description/) ********** */ public ListNode middleNodeCountingApproach(ListNode head) { int count = 0; ListNode current = head; // First pass to count the number of nodes while (current != null) { count++; current = current.next; } // Second pass to find the middle node current = head; for (int i = 0; i < count / 2; i++) { current = current.next; } return current; // This will be the middle node } public ListNode middleNodeFastAndSlowPointerApproach(ListNode head) { ListNode slow = head; ListNode fast = head; // Move slow by 1 and fast by 2 steps while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow; // Slow will be at the middle node } /* * ********** LeetCode 202 - Happy Number (https://leetcode.com/problems/happy-number/description/) ********** */ private int getSumOfSquares(int n) { int sum = 0; while (n > 0) { int digit = n % 10; sum += digit * digit; n /= 10; } return sum; } public boolean isHappyHashSetApproach(int n) { HashSet seen = new HashSet<>(); while (n != 1 && !seen.contains(n)) { seen.add(n); n = getSumOfSquares(n); } return n == 1; } public boolean isHappyFastAndSlowPointersApproach(int n) { int slow = n; int fast = getSumOfSquares(n); while (fast != 1 && slow != fast) { slow = getSumOfSquares(slow); // Move slow by 1 step fast = getSumOfSquares(getSumOfSquares(fast)); // Move fast by 2 steps } return fast == 1; } } ================================================ FILE: patterns/java/KadaneAlgorithm.java ================================================ package patterns.java; public class KadaneAlgorithm { public int maxSubArray(int[] nums) { int currentSum = nums[0]; // Start with the first element int maxSum = nums[0]; // Initialize maxSum with the first element // Traverse the array from the second element for (int i = 1; i < nums.length; i++) { // If currentSum is negative, reset to current element currentSum = Math.max(nums[i], currentSum + nums[i]); // Update maxSum if currentSum is greater maxSum = Math.max(maxSum, currentSum); } return maxSum; } } ================================================ FILE: patterns/java/LevelOrderTraversal.java ================================================ package patterns.java; import java.util.LinkedList; import java.util.Queue; // Definition for a binary tree node. class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public class LevelOrderTraversal { public void levelOrder(TreeNode root) { if (root == null) return; Queue queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode node = queue.poll(); System.out.print(node.val + " "); // Process the node by printing its value // Add the left and right children to the queue, if they exist if (node.left != null) queue.add(node.left); if (node.right != null) queue.add(node.right); } } } ================================================ FILE: patterns/java/MonotonicStack.java ================================================ package patterns.java; import java.util.Arrays; import java.util.Stack; public class MonotonicStack { public int[] nextGreaterElement(int[] nums) { int n = nums.length; int[] result = new int[n]; // Output array Arrays.fill(result, -1); // Default to -1 if no greater element exists Stack stack = new Stack<>(); // Stack stores indices // Iterate through the array for (int i = 0; i < n; i++) { // While stack is not empty and current element is greater than stack top while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) { int index = stack.pop(); // Pop the top element result[index] = nums[i]; // The current element is the Next Greater Element } stack.push(i); // Push the current index onto the stack } return result; } public int[] dailyTemperatures(int[] temperatures) { int n = temperatures.length; int[] result = new int[n]; // Result array initialized with 0s Stack stack = new Stack<>(); // Monotonic decreasing stack (stores indices) // Iterate through the temperature array for (int i = 0; i < n; i++) { // While stack is not empty AND the current temperature is warmer than the temperature at stack top while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) { int prevIndex = stack.pop(); // Pop the previous day's index result[prevIndex] = i - prevIndex; // Calculate the wait time } stack.push(i); // Push current index onto the stack } return result; // Return the computed results } } ================================================ FILE: patterns/java/ReverseLinkedList.java ================================================ package patterns.java; class ListNode { int val; ListNode next; } public class ReverseLinkedList { public ListNode reverseList(ListNode head) { ListNode prev = null; // Previous node, initially null ListNode curr = head; // Current node starts from the head while (curr != null) { ListNode next = curr.next; // Store next node curr.next = prev; // Reverse the current node's pointer prev = curr; // Move prev to current curr = next; // Move curr to next } return prev; // New head of the reversed list } } ================================================ FILE: patterns/java/SlidingWindow.java ================================================ package patterns.java; import java.util.HashSet; public class SlidingWindow { public double findMaxAverageBruteForce(int[] nums, int k) { int n = nums.length; double maxAvg = Integer.MIN_VALUE; // Iterate through all possible subarrays of length k for (int i = 0; i <= n - k; i++) { int sum = 0; // Calculate sum of subarray starting at index i for (int j = i; j < i + k; j++) { sum += nums[j]; } // Compute average and update maxAvg maxAvg = Math.max(maxAvg, (double) sum / k); } return maxAvg; } public double findMaxAverageSlidingWindow(int[] nums, int k) { int n = nums.length; // Compute the sum of the first 'k' elements int sum = 0; for (int i = 0; i < k; i++) { sum += nums[i]; } // Initialize maxSum as the sum of the first window int maxSum = sum; // Slide the window across the array for (int i = k; i < n; i++) { sum += nums[i]; // Add new element entering window sum -= nums[i - k]; // Remove element leaving window maxSum = Math.max(maxSum, sum); // Update maxSum } // Return maximum average return (double) maxSum / k; } public int lengthOfLongestSubstringSlidingWindow(String s) { int n = s.length(); HashSet seen = new HashSet<>(); // Store characters in the current window int maxLength = 0; int left = 0; // Expand window by moving 'right' for (int right = 0; right < n; right++) { // If a duplicate is found, shrink the window from the left while (seen.contains(s.charAt(right))) { seen.remove(s.charAt(left)); left++; } // Add current character to window and update max length seen.add(s.charAt(right)); maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } public int lengthOfLongestSubstringSlidingWindowFrequencyArray(String s) { int n = s.length(); int[] freq = new int[128]; // ASCII character frequency array int maxLength = 0; int left = 0; // Expand window by moving 'right' for (int right = 0; right < n; right++) { char currentChar = s.charAt(right); freq[currentChar]++; // Increase frequency of the current character // If there is a duplicate, shrink the window from the left while (freq[currentChar] > 1) { freq[s.charAt(left)]--; // Remove character at left pointer left++; // Shrink window } // Update maximum window size maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } } ================================================ FILE: patterns/java/TopKElements.java ================================================ package patterns.java; import patterns.java.FastAndSlowPointers.ListNode; import java.util.*; public class TopKElements { /* * ********** K Largest Elements ********** */ public int[] kLargestElementsSortingAppraoch(int[] nums, int k) { // Step 1: Sort the array in descending order Integer[] numsArray = Arrays.stream(nums).boxed().toArray(Integer[]::new); Arrays.sort(numsArray, Collections.reverseOrder()); // Step 2: Extract the first K elements int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = numsArray[i]; } return result; } public int[] kLargestElementsMaxHeapAppraoch(int[] nums, int k) { // Max heap PriorityQueue maxHeap = new PriorityQueue<>(Collections.reverseOrder()); // Add all numbers to the max heap for (int num : nums) { maxHeap.add(num); } // Extract the top K largest elements int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = maxHeap.poll(); // Extracts the largest element } return result; } public int[] kLargestElementsMinHeapAppraoch(int[] nums, int k) { // Min heap PriorityQueue minHeap = new PriorityQueue<>(); // Add first K elements into the min heap for(int i = 0; i < k; i++) { minHeap.add(nums[i]); } // Process the remaining elements for (int i = k; i < nums.length; i++) { minHeap.add(nums[i]); if (minHeap.size() > k) { minHeap.poll(); } } // Extract the top K largest elements from the min heap int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = minHeap.poll(); } return result; } /* * ********** LeetCode 347 - Top K Frequent Elements (https://leetcode.com/problems/top-k-frequent-elements/description/) ********** */ public int[] topKFrequentElementsSortingApproach(int[] nums, int k) { // Step 1: Build the frequency map Map frequencyMap = new HashMap<>(); for (int num : nums) { frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); } // Step 2: Sort the entries by frequency in descending order List> entryList = new ArrayList<>(frequencyMap.entrySet()); entryList.sort((a, b) -> b.getValue() - a.getValue()); // Step 3: Extract the top K elements int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = entryList.get(i).getKey(); } return result; } public int[] topKFrequentElementsMinHeapApproach(int[] nums, int k) { // Step 1: Build the frequency map Map frequencyMap = new HashMap<>(); for (int num : nums) { frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1); } // Step 2: Use a min heap to keep track of the top K elements PriorityQueue> minHeap = new PriorityQueue<>( (a, b) -> a.getValue() - b.getValue() // Compare by frequency ); // Step 3: Add each entry to the heap, and maintain size K for (Map.Entry entry : frequencyMap.entrySet()) { minHeap.add(entry); if (minHeap.size() > k) { minHeap.poll(); // Remove the element with the lowest frequency } } // Step 4: Extract the elements from the heap int[] result = new int[k]; for (int i = 0; i < k; i++) { result[i] = minHeap.poll().getKey(); // Get the element (key) from the heap } return result; } /* * ********** LeetCode 973 - K Closest Points to Origin (https://leetcode.com/problems/k-closest-points-to-origin/description/) ********** */ private int getDistance(int[] point) { return point[0] * point[0] + point[1] * point[1]; // Squared distance to avoid floating-point operations } public int[][] kClosestPointsToOriginMaxHeapApproach(int[][] points, int k) { // Max heap with custom comparator to compare by distance PriorityQueue maxHeap = new PriorityQueue<>( (a, b) -> Integer.compare(getDistance(b), getDistance(a)) ); // Iterate through all points for (int[] point : points) { maxHeap.add(point); // Add the current point to the heap // If the heap exceeds size K, remove the farthest point if (maxHeap.size() > k) { maxHeap.poll(); // Remove the point with the largest distance (root of max heap) } } // Convert the remaining points in the heap to the result array int[][] result = new int[k][2]; for (int i = 0; i < k; i++) { result[i] = maxHeap.poll(); } return result; } } ================================================ FILE: patterns/java/TwoPointers.java ================================================ package patterns.java; public class TwoPointers { public void moveZeroesTwoPointers(int[] nums) { int left = 0; // Pointer for placing non-zero elements // Iterate with right pointer for (int right = 0; right < nums.length; right++) { if (nums[right] != 0) { // Swap elements if right pointer finds a non-zero int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; // Move left pointer forward } } } public int maxAreaBruteForce(int[] height) { int n = height.length; int maxArea = 0; // Check all pairs (i, j) for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { // Height of the container int minHeight = Math.min(height[i], height[j]); int width = j - i; // Distance between lines int area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water } } return maxArea; } public int maxAreaTwoPointers(int[] height) { int left = 0, right = height.length - 1; int maxArea = 0; // Move pointers toward each other while (left <= right) { int width = right - left; // Distance between lines int minHeight = Math.min(height[left], height[right]); int area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water // Move the pointer pointing to the shorter height if (height[left] < height[right]) { left++; // Move left pointer forward } else { right--; // Move right pointer backward } } return maxArea; } } ================================================ FILE: patterns/javascript/fastAndSlowPointers.js ================================================ class ListNode { constructor(x) { this.val = x; this.next = null; } } class FastAndSlowPointers { // LeetCode 141 - Linked List Cycle (HashSet Approach) hasCycleHashSetApproach(head) { const visited = new Set(); let current = head; while (current) { if (visited.has(current)) { return true; } visited.add(current); current = current.next; } return false; } // LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) hasCycleFastAndSlowPointersApproach(head) { if (!head || !head.next) return false; let slow = head, fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; if (slow === fast) return true; } return false; } // LeetCode 876 - Middle of the Linked List (Counting Approach) middleNodeCountingApproach(head) { let count = 0; let current = head; while (current) { count++; current = current.next; } current = head; for (let i = 0; i < Math.floor(count / 2); i++) { current = current.next; } return current; } // LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) middleNodeFastAndSlowPointerApproach(head) { let slow = head, fast = head; while (fast && fast.next) { slow = slow.next; fast = fast.next.next; } return slow; } // LeetCode 202 - Happy Number (HashSet Approach) getSumOfSquares(n) { return String(n).split('').reduce((sum, digit) => sum + digit * digit, 0); } isHappyHashSetApproach(n) { const seen = new Set(); while (n !== 1 && !seen.has(n)) { seen.add(n); n = this.getSumOfSquares(n); } return n === 1; } // LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) isHappyFastAndSlowPointersApproach(n) { let slow = n; let fast = this.getSumOfSquares(n); while (fast !== 1 && slow !== fast) { slow = this.getSumOfSquares(slow); fast = this.getSumOfSquares(this.getSumOfSquares(fast)); } return fast === 1; } } ================================================ FILE: patterns/javascript/kadaneAlgorithm.js ================================================ class KadaneAlgorithm { maxSubArray(nums) { let currentSum = nums[0]; let maxSum = nums[0]; for (let i = 1; i < nums.length; i++) { currentSum = Math.max(nums[i], currentSum + nums[i]); maxSum = Math.max(maxSum, currentSum); } return maxSum; } } ================================================ FILE: patterns/javascript/levelOrderTraversal.js ================================================ // Definition for a binary tree node. class TreeNode { constructor(val) { this.val = val; this.left = this.right = null; } } class LevelOrderTraversal { levelOrder(root) { if (root === null) return; const queue = [root]; while (queue.length > 0) { const node = queue.shift(); console.log(node.val); // Process the node by printing its value // Add the left and right children to the queue, if they exist if (node.left !== null) queue.push(node.left); if (node.right !== null) queue.push(node.right); } } } ================================================ FILE: patterns/javascript/monotonicStack.js ================================================ class MonotonicStack { nextGreaterElement(nums) { let n = nums.length; let result = new Array(n).fill(-1); // Default to -1 if no greater element exists let stack = []; // Stack stores indices for (let i = 0; i < n; i++) { while (stack.length > 0 && nums[i] > nums[stack[stack.length - 1]]) { let index = stack.pop(); result[index] = nums[i]; } stack.push(i); } return result; } dailyTemperatures(temperatures) { let n = temperatures.length; let result = new Array(n).fill(0); // Result array initialized with 0s let stack = []; // Monotonic decreasing stack for (let i = 0; i < n; i++) { while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) { let prevIndex = stack.pop(); result[prevIndex] = i - prevIndex; } stack.push(i); } return result; } } ================================================ FILE: patterns/javascript/reverseList.js ================================================ class ListNode { constructor(val = 0, next = null) { this.val = val; this.next = next; } } function reverseList(head) { let prev = null; let curr = head; while (curr !== null) { let next = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } ================================================ FILE: patterns/javascript/slidingWindow.js ================================================ class SlidingWindow { // Brute Force Approach - O(n * k) findMaxAverageBruteForce(nums, k) { let maxAvg = -Infinity; for (let i = 0; i <= nums.length - k; i++) { let sum = 0; for (let j = i; j < i + k; j++) { sum += nums[j]; } maxAvg = Math.max(maxAvg, sum / k); } return maxAvg; } // Sliding Window Approach - O(n) findMaxAverageSlidingWindow(nums, k) { let sum = nums.slice(0, k).reduce((a, b) => a + b, 0); let maxSum = sum; for (let i = k; i < nums.length; i++) { sum += nums[i] - nums[i - k]; maxSum = Math.max(maxSum, sum); } return maxSum / k; } // Sliding Window for Longest Substring Without Repeating Characters lengthOfLongestSubstringSlidingWindow(s) { let seen = new Set(); let maxLength = 0, left = 0; for (let right = 0; right < s.length; right++) { while (seen.has(s[right])) { seen.delete(s[left]); left++; } seen.add(s[right]); maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } // Sliding Window using Frequency Array lengthOfLongestSubstringSlidingWindowFrequencyArray(s) { let freq = new Array(128).fill(0); let maxLength = 0, left = 0; for (let right = 0; right < s.length; right++) { freq[s.charCodeAt(right)]++; while (freq[s.charCodeAt(right)] > 1) { freq[s.charCodeAt(left)]--; left++; } maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } } ================================================ FILE: patterns/javascript/topKElements.js ================================================ class TopKElements { // K Largest Elements using Sorting kLargestElementsSortingApproach(nums, k) { nums.sort((a, b) => b - a); return nums.slice(0, k); } // K Largest Elements using Max Heap kLargestElementsMaxHeapApproach(nums, k) { const maxHeap = new MaxPriorityQueue({ priority: x => x }); for (const num of nums) { maxHeap.enqueue(num); } const result = []; for (let i = 0; i < k; i++) { result.push(maxHeap.dequeue().element); } return result; } // K Largest Elements using Min Heap kLargestElementsMinHeapApproach(nums, k) { const minHeap = new MinPriorityQueue({ priority: x => x }); for (let i = 0; i < k; i++) { minHeap.enqueue(nums[i]); } for (let i = k; i < nums.length; i++) { minHeap.enqueue(nums[i]); if (minHeap.size() > k) { minHeap.dequeue(); } } const result = []; for (let i = 0; i < k; i++) { result.push(minHeap.dequeue().element); } return result; } // Top K Frequent Elements using Sorting topKFrequentElementsSortingApproach(nums, k) { const frequencyMap = new Map(); nums.forEach(num => frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)); return Array.from(frequencyMap) .sort((a, b) => b[1] - a[1]) .slice(0, k) .map(entry => entry[0]); } // Top K Frequent Elements using Min Heap topKFrequentElementsMinHeapApproach(nums, k) { const frequencyMap = new Map(); nums.forEach(num => frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)); const minHeap = new MinPriorityQueue({ priority: x => x[1] }); frequencyMap.forEach((value, key) => { minHeap.enqueue([key, value]); if (minHeap.size() > k) { minHeap.dequeue(); } }); const result = []; for (let i = 0; i < k; i++) { result.push(minHeap.dequeue().element[0]); } return result; } // K Closest Points to Origin using Max Heap getDistance(point) { return point[0] ** 2 + point[1] ** 2; } kClosestPointsToOriginMaxHeapApproach(points, k) { const maxHeap = new MaxPriorityQueue({ priority: point => -this.getDistance(point) }); points.forEach(point => { maxHeap.enqueue(point); if (maxHeap.size() > k) { maxHeap.dequeue(); } }); const result = []; for (let i = 0; i < k; i++) { result.push(maxHeap.dequeue().element); } return result; } } ================================================ FILE: patterns/javascript/twoPointers.js ================================================ class TwoPointers { /** * Move Zeroes using Two Pointers * @param {number[]} nums - Input array */ moveZeroesTwoPointers(nums) { let left = 0; // Pointer for placing non-zero elements // Iterate with right pointer for (let right = 0; right < nums.length; right++) { if (nums[right] !== 0) { // Swap elements if right pointer finds a non-zero [nums[left], nums[right]] = [nums[right], nums[left]]; left++; // Move left pointer forward } } } /** * Brute Force approach for Container with Most Water * @param {number[]} height - Array of heights * @return {number} - Maximum water that can be contained */ maxAreaBruteForce(height) { let maxArea = 0; let n = height.length; // Check all pairs (i, j) for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { // Compute the minimum height and width let minHeight = Math.min(height[i], height[j]); let width = j - i; let area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water } } return maxArea; } /** * Two Pointers approach for Container with Most Water * @param {number[]} height - Array of heights * @return {number} - Maximum water that can be contained */ maxAreaTwoPointers(height) { let left = 0, right = height.length - 1; let maxArea = 0; // Move pointers toward each other while (left < right) { let width = right - left; // Distance between lines let minHeight = Math.min(height[left], height[right]); // Compute height let area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water // Move the pointer pointing to the shorter height if (height[left] < height[right]) { left++; // Move left pointer forward } else { right--; // Move right pointer backward } } return maxArea; } } ================================================ FILE: patterns/python/fast_and_slow_pointers.py ================================================ class ListNode: def __init__(self, x): self.val = x self.next = None class FastAndSlowPointers: # LeetCode 141 - Linked List Cycle (HashSet Approach) def hasCycleHashSetApproach(self, head): visited = set() current = head while current: if current in visited: return True visited.add(current) current = current.next return False # LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) def hasCycleFastAndSlowPointersApproach(self, head): if not head or not head.next: return False slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next if slow == fast: return True return False # LeetCode 876 - Middle of the Linked List (Counting Approach) def middleNodeCountingApproach(self, head): count = 0 current = head while current: count += 1 current = current.next current = head for _ in range(count // 2): current = current.next return current # LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) def middleNodeFastAndSlowPointerApproach(self, head): slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next return slow # LeetCode 202 - Happy Number (HashSet Approach) def getSumOfSquares(self, n): return sum(int(digit)**2 for digit in str(n)) def isHappyHashSetApproach(self, n): seen = set() while n != 1 and n not in seen: seen.add(n) n = self.getSumOfSquares(n) return n == 1 # LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) def isHappyFastAndSlowPointersApproach(self, n): slow = n fast = self.getSumOfSquares(n) while fast != 1 and slow != fast: slow = self.getSumOfSquares(slow) fast = self.getSumOfSquares(self.getSumOfSquares(fast)) return fast == 1 ================================================ FILE: patterns/python/kadane_algorithm.py ================================================ class KadaneAlgorithm: def max_sub_array(self, nums): current_sum = nums[0] max_sum = nums[0] for i in range(1, len(nums)): current_sum = max(nums[i], current_sum + nums[i]) max_sum = max(max_sum, current_sum) return max_sum ================================================ FILE: patterns/python/level_order_traversal.py ================================================ from collections import deque # Definition for a binary tree node. class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class LevelOrderTraversal: def level_order(self, root): if root is None: return queue = deque([root]) while queue: node = queue.popleft() print(node.val, end=" ") # Process the node by printing its value # Add the left and right children to the queue, if they exist if node.left: queue.append(node.left) if node.right: queue.append(node.right) ================================================ FILE: patterns/python/monotonic_stack.py ================================================ class MonotonicStack: def next_greater_element(self, nums): n = len(nums) result = [-1] * n # Default to -1 if no greater element exists stack = [] # Stack stores indices for i in range(n): while stack and nums[i] > nums[stack[-1]]: index = stack.pop() result[index] = nums[i] stack.append(i) return result def daily_temperatures(self, temperatures): n = len(temperatures) result = [0] * n # Result array initialized with 0s stack = [] # Monotonic decreasing stack for i in range(n): while stack and temperatures[i] > temperatures[stack[-1]]: prev_index = stack.pop() result[prev_index] = i - prev_index stack.append(i) return result ================================================ FILE: patterns/python/reverse_list.py ================================================ class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_list(head): prev = None curr = head while curr: next_node = curr.next curr.next = prev prev = curr curr = next_node return prev ================================================ FILE: patterns/python/sliding_window.py ================================================ class SlidingWindow: def find_max_average_brute_force(self, nums, k): max_avg = float('-inf') for i in range(len(nums) - k + 1): max_avg = max(max_avg, sum(nums[i:i + k]) / k) return max_avg def find_max_average_sliding_window(self, nums, k): sum_window = sum(nums[:k]) max_sum = sum_window for i in range(k, len(nums)): sum_window += nums[i] - nums[i - k] max_sum = max(max_sum, sum_window) return max_sum / k def length_of_longest_substring_sliding_window(self, s): seen = set() max_length = left = 0 for right in range(len(s)): while s[right] in seen: seen.remove(s[left]) left += 1 seen.add(s[right]) max_length = max(max_length, right - left + 1) return max_length def length_of_longest_substring_sliding_window_frequency_array(self, s): freq = [0] * 128 max_length = left = 0 for right in range(len(s)): freq[ord(s[right])] += 1 while freq[ord(s[right])] > 1: freq[ord(s[left])] -= 1 left += 1 max_length = max(max_length, right - left + 1) return max_length ================================================ FILE: patterns/python/top_k_elements.py ================================================ import heapq from collections import Counter class TopKElements: # K Largest Elements using Sorting def k_largest_elements_sorting_approach(self, nums, k): return sorted(nums, reverse=True)[:k] # K Largest Elements using Max Heap def k_largest_elements_max_heap_approach(self, nums, k): return heapq.nlargest(k, nums) # K Largest Elements using Min Heap def k_largest_elements_min_heap_approach(self, nums, k): min_heap = nums[:k] heapq.heapify(min_heap) for num in nums[k:]: heapq.heappush(min_heap, num) if len(min_heap) > k: heapq.heappop(min_heap) return [heapq.heappop(min_heap) for _ in range(k)][::-1] # Top K Frequent Elements using Sorting def top_k_frequent_elements_sorting_approach(self, nums, k): count = Counter(nums) return [num for num, freq in count.most_common(k)] # Top K Frequent Elements using Min Heap def top_k_frequent_elements_min_heap_approach(self, nums, k): count = Counter(nums) min_heap = [] for num, freq in count.items(): heapq.heappush(min_heap, (freq, num)) if len(min_heap) > k: heapq.heappop(min_heap) return [heapq.heappop(min_heap)[1] for _ in range(k)][::-1] # K Closest Points to Origin using Max Heap def get_distance(self, point): return point[0] ** 2 + point[1] ** 2 def k_closest_points_to_origin_max_heap_approach(self, points, k): max_heap = [] for point in points: heapq.heappush(max_heap, (-self.get_distance(point), point)) if len(max_heap) > k: heapq.heappop(max_heap) return [heapq.heappop(max_heap)[1] for _ in range(k)][::-1] ================================================ FILE: patterns/python/two_pointers.py ================================================ class TwoPointers: # Move Zeroes using Two Pointers def move_zeroes_two_pointers(self, nums): left = 0 # Pointer for placing non-zero elements # Iterate with right pointer for right in range(len(nums)): if nums[right] != 0: # Swap elements if right pointer finds a non-zero nums[left], nums[right] = nums[right], nums[left] left += 1 # Move left pointer forward # Brute Force approach for Container with Most Water def max_area_brute_force(self, height): n = len(height) max_area = 0 # Check all pairs (i, j) for i in range(n): for j in range(i + 1, n): # Compute the minimum height and width min_height = min(height[i], height[j]) width = j - i area = min_height * width # Compute water contained max_area = max(max_area, area) # Update max water return max_area # Two Pointers approach for Container with Most Water def max_area_two_pointers(self, height): left, right = 0, len(height) - 1 max_area = 0 # Move pointers toward each other while left < right: width = right - left # Distance between lines min_height = min(height[left], height[right]) # Compute height area = min_height * width # Compute water contained max_area = max(max_area, area) # Update max water # Move the pointer pointing to the shorter height if height[left] < height[right]: left += 1 # Move left pointer forward else: right -= 1 # Move right pointer backward return max_area ================================================ FILE: patterns/typescript/fastAndSlowPointers.ts ================================================ class ListNode { val: number; next: ListNode | null = null; constructor(x: number) { this.val = x; } } class FastAndSlowPointers { // LeetCode 141 - Linked List Cycle (HashSet Approach) hasCycleHashSetApproach(head: ListNode | null): boolean { const visited = new Set(); let current = head; while (current) { if (visited.has(current)) { return true; } visited.add(current); current = current.next; } return false; } // LeetCode 141 - Linked List Cycle (Fast and Slow Pointer Approach) hasCycleFastAndSlowPointersApproach(head: ListNode | null): boolean { if (!head || !head.next) return false; let slow: ListNode | null = head; let fast: ListNode | null = head; while (fast && fast.next) { slow = slow!.next; fast = fast.next.next; if (slow === fast) return true; } return false; } // LeetCode 876 - Middle of the Linked List (Counting Approach) middleNodeCountingApproach(head: ListNode | null): ListNode | null { let count = 0; let current = head; while (current) { count++; current = current.next; } current = head; for (let i = 0; i < Math.floor(count / 2); i++) { current = current!.next; } return current; } // LeetCode 876 - Middle of the Linked List (Fast and Slow Pointer Approach) middleNodeFastAndSlowPointerApproach(head: ListNode | null): ListNode | null { let slow = head, fast = head; while (fast && fast.next) { slow = slow!.next; fast = fast.next.next; } return slow; } // LeetCode 202 - Happy Number (HashSet Approach) getSumOfSquares(n: number): number { return String(n).split('').reduce((sum, digit) => sum + Number(digit) ** 2, 0); } isHappyHashSetApproach(n: number): boolean { const seen = new Set(); while (n !== 1 && !seen.has(n)) { seen.add(n); n = this.getSumOfSquares(n); } return n === 1; } // LeetCode 202 - Happy Number (Fast and Slow Pointer Approach) isHappyFastAndSlowPointersApproach(n: number): boolean { let slow = n; let fast = this.getSumOfSquares(n); while (fast !== 1 && slow !== fast) { slow = this.getSumOfSquares(slow); fast = this.getSumOfSquares(this.getSumOfSquares(fast)); } return fast === 1; } } ================================================ FILE: patterns/typescript/kadaneAlgorithm.ts ================================================ class KadaneAlgorithm { maxSubArray(nums: number[]): number { let currentSum: number = nums[0]; let maxSum: number = nums[0]; for (let i = 1; i < nums.length; i++) { currentSum = Math.max(nums[i], currentSum + nums[i]); maxSum = Math.max(maxSum, currentSum); } return maxSum; } } ================================================ FILE: patterns/typescript/levelOrderTraversal.ts ================================================ // Definition for a binary tree node. class BinaryTreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = this.right = null; } } class BinaryTreeLevelOrderTraversal { levelOrder(root: BinaryTreeNode | null): void { if (root === null) return; const queue: BinaryTreeNode[] = [root]; while (queue.length > 0) { const node = queue.shift()!; console.log(node.val); // Process the node by printing its value // Add the left and right children to the queue, if they exist if (node.left !== null) queue.push(node.left); if (node.right !== null) queue.push(node.right); } } } ================================================ FILE: patterns/typescript/monotonicStack.ts ================================================ class MonotonicStack { nextGreaterElement(nums: number[]): number[] { let n = nums.length; let result: number[] = new Array(n).fill(-1); // Default to -1 if no greater element exists let stack: number[] = []; // Stack stores indices for (let i = 0; i < n; i++) { while (stack.length > 0 && nums[i] > nums[stack[stack.length - 1]]) { let index = stack.pop()!; result[index] = nums[i]; } stack.push(i); } return result; } dailyTemperatures(temperatures: number[]): number[] { let n = temperatures.length; let result: number[] = new Array(n).fill(0); // Result array initialized with 0s let stack: number[] = []; // Monotonic decreasing stack for (let i = 0; i < n; i++) { while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) { let prevIndex = stack.pop()!; result[prevIndex] = i - prevIndex; } stack.push(i); } return result; } } ================================================ FILE: patterns/typescript/reverseList.ts ================================================ class ListNode { val: number; next: ListNode | null; constructor(val: number = 0, next: ListNode | null = null) { this.val = val; this.next = next; } } function reverseList(head: ListNode | null): ListNode | null { let prev: ListNode | null = null; let curr: ListNode | null = head; while (curr !== null) { let next: ListNode | null = curr.next; curr.next = prev; prev = curr; curr = next; } return prev; } ================================================ FILE: patterns/typescript/slidingWindow.ts ================================================ class SlidingWindow { // Brute Force Approach - O(n * k) findMaxAverageBruteForce(nums: number[], k: number): number { let maxAvg = -Infinity; for (let i = 0; i <= nums.length - k; i++) { let sum = 0; for (let j = i; j < i + k; j++) { sum += nums[j]; } maxAvg = Math.max(maxAvg, sum / k); } return maxAvg; } // Sliding Window Approach - O(n) findMaxAverageSlidingWindow(nums: number[], k: number): number { let sum = nums.slice(0, k).reduce((a, b) => a + b, 0); let maxSum = sum; for (let i = k; i < nums.length; i++) { sum += nums[i] - nums[i - k]; maxSum = Math.max(maxSum, sum); } return maxSum / k; } // Sliding Window for Longest Substring Without Repeating Characters lengthOfLongestSubstringSlidingWindow(s: string): number { let seen = new Set(); let maxLength = 0, left = 0; for (let right = 0; right < s.length; right++) { while (seen.has(s[right])) { seen.delete(s[left]); left++; } seen.add(s[right]); maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } // Sliding Window using Frequency Array lengthOfLongestSubstringSlidingWindowFrequencyArray(s: string): number { let freq = new Array(128).fill(0); let maxLength = 0, left = 0; for (let right = 0; right < s.length; right++) { freq[s.charCodeAt(right)]++; while (freq[s.charCodeAt(right)] > 1) { freq[s.charCodeAt(left)]--; left++; } maxLength = Math.max(maxLength, right - left + 1); } return maxLength; } } ================================================ FILE: patterns/typescript/topKElements.ts ================================================ class TopKElements { // K Largest Elements using Sorting kLargestElementsSortingApproach(nums: number[], k: number): number[] { nums.sort((a, b) => b - a); return nums.slice(0, k); } // K Largest Elements using Max Heap kLargestElementsMaxHeapApproach(nums: number[], k: number): number[] { const maxHeap = new MaxPriorityQueue({ priority: (x: number) => x }); for (const num of nums) { maxHeap.enqueue(num); } const result: number[] = []; for (let i = 0; i < k; i++) { result.push(maxHeap.dequeue().element); } return result; } // K Largest Elements using Min Heap kLargestElementsMinHeapApproach(nums: number[], k: number): number[] { const minHeap = new MinPriorityQueue({ priority: (x: number) => x }); for (let i = 0; i < k; i++) { minHeap.enqueue(nums[i]); } for (let i = k; i < nums.length; i++) { minHeap.enqueue(nums[i]); if (minHeap.size() > k) { minHeap.dequeue(); } } const result: number[] = []; for (let i = 0; i < k; i++) { result.push(minHeap.dequeue().element); } return result; } // Top K Frequent Elements using Sorting topKFrequentElementsSortingApproach(nums: number[], k: number): number[] { const frequencyMap = new Map(); nums.forEach(num => frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)); return Array.from(frequencyMap) .sort((a, b) => b[1] - a[1]) .slice(0, k) .map(entry => entry[0]); } // Top K Frequent Elements using Min Heap topKFrequentElementsMinHeapApproach(nums: number[], k: number): number[] { const frequencyMap = new Map(); nums.forEach(num => frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)); const minHeap = new MinPriorityQueue({ priority: (x: [number, number]) => x[1] }); frequencyMap.forEach((value, key) => { minHeap.enqueue([key, value]); if (minHeap.size() > k) { minHeap.dequeue(); } }); const result: number[] = []; for (let i = 0; i < k; i++) { result.push(minHeap.dequeue().element[0]); } return result; } // K Closest Points to Origin using Max Heap getDistance(point: number[]): number { return point[0] ** 2 + point[1] ** 2; } kClosestPointsToOriginMaxHeapApproach(points: number[][], k: number): number[][] { const maxHeap = new MaxPriorityQueue({ priority: (point: number[]) => -this.getDistance(point) }); points.forEach(point => { maxHeap.enqueue(point); if (maxHeap.size() > k) { maxHeap.dequeue(); } }); const result: number[][] = []; for (let i = 0; i < k; i++) { result.push(maxHeap.dequeue().element); } return result; } } ================================================ FILE: patterns/typescript/twoPointers.ts ================================================ class TwoPointers { // Move Zeroes using Two Pointers moveZeroesTwoPointers(nums: number[]): void { let left = 0; // Pointer for placing non-zero elements // Iterate with right pointer for (let right = 0; right < nums.length; right++) { if (nums[right] !== 0) { // Swap elements if right pointer finds a non-zero [nums[left], nums[right]] = [nums[right], nums[left]]; left++; // Move left pointer forward } } } // Brute Force approach for Container with Most Water maxAreaBruteForce(height: number[]): number { let maxArea = 0; let n = height.length; // Check all pairs (i, j) for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { // Compute the minimum height and width let minHeight = Math.min(height[i], height[j]); let width = j - i; let area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water } } return maxArea; } // Two Pointers approach for Container with Most Water maxAreaTwoPointers(height: number[]): number { let left = 0, right = height.length - 1; let maxArea = 0; // Move pointers toward each other while (left < right) { let width = right - left; // Distance between lines let minHeight = Math.min(height[left], height[right]); // Compute height let area = minHeight * width; // Compute water contained maxArea = Math.max(maxArea, area); // Update max water // Move the pointer pointing to the shorter height if (height[left] < height[right]) { left++; // Move left pointer forward } else { right--; // Move right pointer backward } } return maxArea; } }