├── .gitignore ├── LICENSE ├── README.md ├── battleship ├── .gitignore ├── LICENSE └── README.md ├── copy_list_with_random_pointer ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── count_substrings_with_exactly_k_distinct_chars ├── .gitignore ├── LICENSE └── README.md ├── critical_connections ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── critical_routers ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── favorite_genres ├── .gitignore ├── LICENSE └── README.md ├── find_pair_with_given_sum ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── generate_parentheses ├── .gitignore ├── LICENSE └── README.md ├── k_closest_points_to_origin ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── load_balancer ├── .gitignore ├── LICENSE └── README.md ├── longest_palindromic_substring ├── .gitignore ├── LICENSE └── README.md ├── longest_string_made_up_of_only_vowels ├── .gitignore ├── LICENSE └── README.md ├── longest_string_without_3_consecutive_characters ├── .gitignore ├── LICENSE └── README.md ├── max_of_min_altitudes ├── .gitignore ├── LICENSE └── README.md ├── merge_intervals ├── .gitignore ├── LICENSE └── README.md ├── merge_two_sorted_lists ├── .gitignore ├── LICENSE └── README.md ├── min_cost_to_connect_all_nodes ├── .gitignore ├── LICENSE └── README.md ├── min_cost_to_connect_ropes ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── min_cost_to_repair_edges ├── .gitignore ├── LICENSE └── README.md ├── most_common_word ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── nth_gp ├── .gitignore ├── LICENSE └── README.md ├── number-of-dice-rolls-with-target-sum ├── .gitignore ├── LICENSE └── README.md ├── number_of_islands ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── optimal_utilization ├── .gitignore ├── LICENSE └── README.md ├── partition_labels ├── .gitignore ├── LICENSE └── README.md ├── point_of_lattice ├── .gitignore ├── LICENSE └── README.md ├── prison_cells_after_N_days ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── product_suggestions_system ├── .gitignore ├── LICENSE └── README.md ├── reorder_data_in_log_files ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── roll_dice ├── .gitignore ├── LICENSE └── README.md ├── search_a_2d_matrix_2 ├── .gitignore ├── LICENSE └── README.md ├── spiral_matrix ├── .gitignore ├── LICENSE └── README.md ├── substrings_of_size_k_with_k_distinct_chars ├── .gitignore ├── LICENSE └── README.md ├── subtree_of_another_tree ├── .gitignore ├── LICENSE └── README.md ├── subtree_with_maximum_average ├── .gitignore ├── LICENSE └── README.md ├── top_n_buzzwords ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── treasure_island ├── .gitignore ├── LICENSE └── README.md ├── treasure_island_2 ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp ├── two_sum_-_unique_pairs ├── .gitignore ├── LICENSE └── README.md └── zombie_in_matrix ├── .gitignore ├── CMakeLists.txt ├── LICENSE ├── README.md └── main.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # amazon 2 | Solutions for known Amazon online assessment tasks 3 | -------------------------------------------------------------------------------- /battleship/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /battleship/LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /battleship/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /copy_list_with_random_pointer/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /copy_list_with_random_pointer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(copy_list_with_random_pointer) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(copy_list_with_random_pointer main.cpp) -------------------------------------------------------------------------------- /copy_list_with_random_pointer/README.md: -------------------------------------------------------------------------------- 1 | # Copy List with Random Pointer 2 | 3 | https://leetcode.com/problems/copy-list-with-random-pointer/ 4 | 5 | A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. 6 | 7 | Return a deep copy of the list. 8 | 9 | The Linked List is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where: 10 | 11 | val: an integer representing Node.val 12 | 13 | random_index: the index of the node (range from 0 to n-1) where random pointer points to, or null if it does not point to any node. 14 | 15 | 16 | 17 | Example 1: 18 | 19 | Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]] 20 | Output: [[7,null],[13,0],[11,4],[10,2],[1,0]] 21 | 22 | Example 2: 23 | 24 | Input: head = [[1,1],[2,1]] 25 | Output: [[1,1],[2,1]] 26 | 27 | Example 3: 28 | 29 | Input: head = [[3,null],[3,0],[3,null]] 30 | Output: [[3,null],[3,0],[3,null]] 31 | 32 | Example 4: 33 | 34 | Input: head = [] 35 | Output: [] 36 | Explanation: Given linked list is empty (null pointer), so return null. 37 | 38 | 39 | 40 | Constraints: 41 | 42 | -10000 <= Node.val <= 10000 43 | Node.random is null or pointing to a node in the linked list. 44 | Number of Nodes will not exceed 1000. 45 | 46 | 47 | Detailed description of the best solution. 48 | Picture in the comments helps to understand the algorithm very much. 49 | https://leetcode.com/problems/copy-list-with-random-pointer/discuss/43491/A-solution-with-constant-space-complexity-O(1)-and-linear-time-complexity-O(N) 50 | -------------------------------------------------------------------------------- /copy_list_with_random_pointer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Definition for a Node. 8 | class Node { 9 | public: 10 | int val; 11 | Node* next; 12 | Node* random; 13 | 14 | Node(int _val) { 15 | val = _val; 16 | next = nullptr; 17 | random = nullptr; 18 | } 19 | }; 20 | 21 | 22 | 23 | class Solution { 24 | public: 25 | Node* copyRandomList(Node* head) { 26 | if (!head) return nullptr; 27 | Node *current = head; 28 | // First round: make copy of each node, 29 | // and link them together side-by-side in a single list. 30 | while (current) { 31 | Node *node = new Node(current->val); 32 | node->next = current->next; 33 | current->next = node; 34 | current = node->next; 35 | } 36 | // Second round: assign random pointers for the copy nodes. 37 | current = head; 38 | while (current) { 39 | if (current->random) { 40 | current->next->random = current->random->next; 41 | } 42 | else 43 | current->next->random = nullptr; 44 | current = current->next->next; 45 | } 46 | // Third round: restore the original list, and extract the copy list. 47 | current = head; 48 | Node *res = head->next; 49 | Node *tmp = res; 50 | while(current){ 51 | current->next = current->next->next; 52 | current = current->next; 53 | if(tmp->next) 54 | tmp->next = tmp->next->next; 55 | tmp = tmp->next; 56 | } 57 | return res; 58 | } 59 | }; 60 | 61 | Node* make_list(vector> & v) { 62 | vector tmp; 63 | for(auto i:v) { 64 | Node * n = new Node(i[0]); 65 | tmp.push_back(n); 66 | } 67 | 68 | for(int i = 0; i < tmp.size(); ++i) { 69 | if(i+1 < tmp.size()) { 70 | tmp[i]->next = tmp[i + 1]; 71 | } 72 | else { 73 | tmp[i]->next = nullptr; 74 | } 75 | 76 | if(v[i][1] != -1) { 77 | tmp[i]->random = tmp[v[i][1]]; 78 | } 79 | } 80 | return tmp[0]; 81 | } 82 | 83 | void print_list(Node* n) { 84 | while(n != nullptr) { 85 | std::cout << "----------------------" << std::endl; 86 | std::cout << "Address: " << (void*)n << std::endl; 87 | std::cout << "Data: " << n->val << std::endl; 88 | std::cout << "Random: " << (void*)(n->random) << std::endl; 89 | n = n->next; 90 | } 91 | } 92 | 93 | 94 | int main() { 95 | vector> head = {{7,-1},{13,0},{11,4},{10,2},{1,0}}; 96 | std::cout << "Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]" << std::endl; 97 | Node* list = make_list(head); 98 | print_list(list); 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /count_substrings_with_exactly_k_distinct_chars/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /count_substrings_with_exactly_k_distinct_chars/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /critical_connections/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /critical_connections/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(critical_connections) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(critical_connections main.cpp) -------------------------------------------------------------------------------- /critical_connections/README.md: -------------------------------------------------------------------------------- 1 | # Critical Connections 2 | 3 | Given an undirected connected graph with n nodes labeled 1..n. A bridge (cut edge) is defined as an edge which, when removed, makes the graph disconnected (or more precisely, increases the number of connected components in the graph). Equivalently, an edge is a bridge if and only if it is not contained in any cycle. The task is to find all bridges in the given graph. Output an empty list if there are no bridges. 4 | 5 | Input: 6 | 7 | n, an int representing the total number of nodes. 8 | edges, a list of pairs of integers representing the nodes connected by an edge. 9 | 10 | Example 1: 11 | 12 | Input: n = 5, edges = [[1, 2], [1, 3], [3, 4], [1, 4], [4, 5]] 13 | 14 | Output: [[1, 2], [4, 5]] 15 | 16 | Explanation: 17 | There are 2 bridges: 18 | 1. Between node 1 and 2 19 | 2. Between node 4 and 5 20 | If we remove these edges, then the graph will be disconnected. 21 | If we remove any of the remaining edges, the graph will remain connected. 22 | 23 | Example 2: 24 | 25 | Input: n = 6, edges = [[1, 2], [1, 3], [2, 3], [2, 4], [2, 5], [4, 6], [5, 6]] 26 | 27 | Output: [] 28 | Explanation: 29 | We can remove any edge, the graph will remain connected. 30 | 31 | Example 3: 32 | 33 | Input: n = 9, edges = [[1, 2], [1, 3], [2, 3], [3, 4], [3, 6], [4, 5], [6, 7], [6, 9], [7, 8], [8, 9]] 34 | 35 | Output: [[3, 4], [3, 6], [4, 5]] 36 | 37 | Easy-to-understand explanation video 38 | 39 | https://www.youtube.com/watch?v=aZXi1unBdJA 40 | 41 | Source code for the video: https://github.com/williamfiset/Algorithms/tree/master/src/main/java/com/williamfiset/algorithms 42 | 43 | URL to Amazon question: 44 | https://leetcode.com/discuss/interview-question/372581 45 | URL to Leetcode task: 46 | https://leetcode.com/problems/critical-connections-in-a-network/ 47 | -------------------------------------------------------------------------------- /critical_connections/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int MAXN = 5; 7 | vector g[MAXN]; 8 | bool used[MAXN]; 9 | int timer, tin[MAXN], fup[MAXN]; 10 | 11 | 12 | void IS_BRIDGE(int v, int to) { 13 | cout << v << ", " << to << endl; 14 | } 15 | 16 | 17 | void dfs (int v, int p = -1) { 18 | used[v] = true; 19 | tin[v] = fup[v] = timer++; 20 | for (size_t i=0; i tin[v]) 29 | IS_BRIDGE(v+1,to+1); 30 | } 31 | } 32 | } 33 | 34 | int main() { 35 | 36 | /* 37 | Input: n = 5, edges = [[1, 2], [1, 3], [3, 4], [1, 4], [4, 5]] 38 | Output: [[1, 2], [4, 5]] 39 | */ 40 | 41 | vector> edges = {{1, 2}, {1, 3}, {3, 4}, {1, 4}, {4, 5}}; 42 | 43 | int n = MAXN; 44 | timer = 0; 45 | 46 | // fill g 47 | 48 | for(auto e: edges) { 49 | g[e[0]-1].push_back(e[1]-1); 50 | g[e[1]-1].push_back(e[0]-1); 51 | } 52 | 53 | /* 54 | cout << "Graph: " << endl; 55 | for(int i = 0; i < MAXN; ++i) { 56 | cout << "Node: " << i << ": "; 57 | for(auto n: g[i]) { 58 | cout << n << ", "; 59 | } 60 | cout << endl; 61 | } 62 | cout << endl; 63 | */ 64 | 65 | cout << "Bridges: " << endl; 66 | 67 | for (int i=0; i 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /critical_routers/README.md: -------------------------------------------------------------------------------- 1 | # Critical Routers 2 | 3 | You are given an undirected connected graph. An articulation point (or cut vertex) is defined as a vertex which, when removed along with associated edges, makes the graph disconnected (or more precisely, increases the number of connected components in the graph). The task is to find all articulation points in the given graph. 4 | 5 | Input: 6 | The input to the function/method consists of three arguments: 7 | 8 | numNodes, an integer representing the number of nodes in the graph. 9 | numEdges, an integer representing the number of edges in the graph. 10 | edges, the list of pair of integers - A, B representing an edge between the nodes A and B. 11 | 12 | Output: 13 | Return a list of integers representing the critical nodes. 14 | 15 | Example: 16 | 17 | Input: numNodes = 7, numEdges = 7, edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]] 18 | 19 | Output: [2, 3, 5] 20 | 21 | Easy-to-understand explanation video 22 | 23 | https://www.youtube.com/watch?v=aZXi1unBdJA 24 | 25 | Source code for the video: https://github.com/williamfiset/Algorithms/tree/master/src/main/java/com/williamfiset/algorithms 26 | 27 | Related problems: 28 | 29 | https://leetcode.com/discuss/interview-question/372581 30 | https://leetcode.com/problems/critical-connections-in-a-network/ 31 | https://www.geeksforgeeks.org/articulation-points-or-cut-vertices-in-a-graph/ 32 | https://cp-algorithms.com/graph/cutpoints.html 33 | -------------------------------------------------------------------------------- /critical_routers/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | const int MAXN = 7; 7 | vector g[MAXN]; 8 | bool used[MAXN]; 9 | int timer, tin[MAXN], fup[MAXN]; 10 | 11 | 12 | void IS_CUTPOINT(int v) { 13 | cout << v << ", "; 14 | } 15 | 16 | void dfs (int v, int p = -1) { 17 | used[v] = true; 18 | tin[v] = fup[v] = timer++; 19 | int children = 0; 20 | for (size_t i=0; i= tin[v] && p != -1) 29 | IS_CUTPOINT(v); 30 | ++children; 31 | } 32 | } 33 | if (p == -1 && children > 1) 34 | IS_CUTPOINT(v); 35 | } 36 | 37 | int main() { 38 | 39 | // Input: numNodes = 7, numEdges = 7, edges = [[0, 1], [0, 2], [1, 3], [2, 3], [2, 5], [5, 6], [3, 4]] 40 | // Output: [2, 3, 5] 41 | 42 | vector> edges = {{0, 1}, {0, 2}, {1, 3}, {2, 3}, {2, 5}, {5, 6}, {3, 4}}; 43 | 44 | int n = MAXN; 45 | timer = 0; 46 | 47 | // fill g 48 | 49 | for(auto e: edges) { 50 | g[e[0]].push_back(e[1]); 51 | g[e[1]].push_back(e[0]); 52 | } 53 | 54 | /* 55 | cout << "Graph: " << endl; 56 | for(int i = 0; i < MAXN; ++i) { 57 | cout << "Node: " << i << ": "; 58 | for(auto n: g[i]) { 59 | cout << n << ", "; 60 | } 61 | cout << endl; 62 | } 63 | cout << endl; 64 | */ 65 | 66 | cout << "Articulation points: " << endl; 67 | 68 | for (int i=0; i 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /favorite_genres/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /find_pair_with_given_sum/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /find_pair_with_given_sum/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(find_pair_with_given_sum) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(find_pair_with_given_sum main.cpp) -------------------------------------------------------------------------------- /find_pair_with_given_sum/README.md: -------------------------------------------------------------------------------- 1 | # Find Pair With Given Sum 2 | 3 | Given a list of positive integers nums and an int target, return indices of the two numbers such that they add up to a target - 30. 4 | 5 | Conditions: 6 | 7 | You will pick exactly 2 numbers. 8 | You cannot pick the same element twice. 9 | If you have muliple pairs, select the pair with the largest number. 10 | 11 | Example 1: 12 | 13 | Input: nums = [1, 10, 25, 35, 60], target = 90 14 | Output: [2, 3] 15 | 16 | Explanation: 17 | 18 | nums[2] + nums[3] = 25 + 35 = 60 = 90 - 30 19 | 20 | Example 2: 21 | 22 | Input: nums = [20, 50, 40, 25, 30, 10], target = 90 23 | Output: [1, 5] 24 | Explanation: 25 | 26 | nums[0] + nums[2] = 20 + 40 = 60 = 90 - 30 27 | nums[1] + nums[5] = 50 + 10 = 60 = 90 - 30 28 | You should return the pair with the largest number. 29 | 30 | Solution 31 | 32 | Related problems: 33 | 34 | https://leetcode.com/problems/two-sum 35 | 36 | -------------------------------------------------------------------------------- /find_pair_with_given_sum/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | vector twoSum(vector& nums, int target) { 8 | unordered_map m; 9 | int max_num = 0; 10 | vector res; 11 | 12 | target -= 30; 13 | 14 | for (int i = 0; i < nums.size(); ++i) { 15 | auto it = m.find(target - nums[i]); 16 | 17 | if (it != m.end()) { 18 | if(it->second > max_num) { 19 | max_num = it->second; 20 | res = {it->second, i}; 21 | } 22 | if(i > max_num) { 23 | max_num = i; 24 | res = {it->second, i}; 25 | } 26 | } 27 | m[nums[i]] = i; 28 | } 29 | return res; 30 | } 31 | 32 | int main() { 33 | 34 | vector nums {1, 10, 25, 35, 60}; 35 | int target = 90; 36 | std::cout << "Input {1, 10, 25, 35, 60} Target 90" << std::endl; 37 | std::cout << "Expected output: 2, 3" << std::endl; 38 | vector res = twoSum(nums, target); 39 | std::cout << "Actual output: " << res[0] << ", " << res[1] << std::endl; 40 | 41 | nums = {20, 50, 40, 25, 30, 10}; 42 | std::cout << "Input {20, 50, 40, 25, 30, 10} Target 90" << std::endl; 43 | std::cout << "Expected output: 1, 5" << std::endl; 44 | res = twoSum(nums, target); 45 | std::cout << "Actual output: " << res[0] << ", " << res[1] << std::endl; 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /generate_parentheses/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /generate_parentheses/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /k_closest_points_to_origin/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /k_closest_points_to_origin/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(k_closest_points_to_origin) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(k_closest_points_to_origin main.cpp) -------------------------------------------------------------------------------- /k_closest_points_to_origin/README.md: -------------------------------------------------------------------------------- 1 | K Closest Points to Origin 2 | 3 | We have a list of points on the plane. Find the K closest points to the origin (0, 0). 4 | 5 | (Here, the distance between two points on a plane is the Euclidean distance.) 6 | 7 | You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in.) 8 | 9 | 10 | 11 | Example 1: 12 | 13 | Input: points = [[1,3],[-2,2]], K = 1 14 | Output: [[-2,2]] 15 | 16 | Explanation: 17 | 18 | The distance between (1, 3) and the origin is sqrt(10). 19 | The distance between (-2, 2) and the origin is sqrt(8). 20 | Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. 21 | We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]]. 22 | 23 | Example 2: 24 | 25 | Input: points = [[3,3],[5,-1],[-2,4]], K = 2 26 | Output: [[3,3],[-2,4]] 27 | (The answer [[-2,4],[3,3]] would also be accepted.) 28 | 29 | 30 | 31 | Note: 32 | 33 | 1 <= K <= points.length <= 10000 34 | -10000 < points[i][0] < 10000 35 | -10000 < points[i][1] < 10000 36 | 37 | -------------------------------------------------------------------------------- /k_closest_points_to_origin/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Here, I am showing four solutions: 3 | 4 | Naïve sorting solution, O(n log n) 5 | Short and efficient O(n) solution using nth_element <-- recommended 6 | Simple implementation of quickselect, with O(n) average case complexity 7 | Improved quickselect with O(n) worst case complexity 8 | 9 | Solution 1: Sorting 10 | 11 | We can just sort our points by the distance using a multimap (value is the index in the input array). Then we can take K first elements from the multimap and build the result vector. 12 | 13 | vector> kClosest(vector>& ps, int K) { 14 | multimap m; 15 | for (int i = 0; i < ps.size(); ++i) 16 | m.insert({ ps[i][0] * ps[i][0] + ps[i][1] * ps[i][1], i }); 17 | vector> res; 18 | for (auto it = m.begin(); K > 0; ++it, --K) res.push_back(ps[it->second]); 19 | return res; 20 | } 21 | 22 | Solution 2: nth_element 23 | 24 | However, multimap sorts all elements, and what we need is just re-arrange elements so that none of the elements preceding Kth are greater than it, and none of the elements following it are less. To accomplish this, we could use partial_sort or nth_element functions, and the latter is better suited for this problem as we do not care about the order of the returning elements. 25 | 26 | The nth_element function is typically implemented using Introselect, which brings the average complexity down to O(n). 27 | 28 | vector> kClosest(vector>& ps, int K) { 29 | nth_element(begin(ps), begin(ps) + K, end(ps), [](vector &a, vector &b) { 30 | return a[0]*a[0]+a[1]*a[1] < b[0]*b[0]+b[1]*b[1]; 31 | }); 32 | ps.resize(K); 33 | return ps; 34 | } 35 | 36 | Solution 3: Quickselect 37 | 38 | If you don't want to use the library function, below is a sample implementation using quickselect. 39 | I implemented it in a generic way to keep the same signature as for nth_element. 40 | You can have a simpler implementation without using generics, like Approach 2: Divide and Conquer. 41 | 42 | For this implementation, we use the last element as a pivot. This gives us average O(n) complexity. 43 | 44 | vector> kClosest(vector>& ps, int K) { 45 | quickselect(begin(ps), end(ps), K, [](vector &a, vector &b) { 46 | return a[0]*a[0]+a[1]*a[1] < b[0]*b[0]+b[1]*b[1]; 47 | }); 48 | ps.resize(K); 49 | return ps; 50 | } 51 | template void quickselect(RndIt first, RndIt end, int K, Compare cmp) { 52 | auto p = partition(first, end, cmp); 53 | if (p - first + 1 == K) return; 54 | if (p - first + 1 < K) quickselect(p + 1, end, K - (p - first + 1), cmp); 55 | else quickselect(first, p, K, cmp); 56 | } 57 | template RndIt partition(RndIt first, RndIt end, Compare cmp) { 58 | auto last = next(end, -1); 59 | auto pivot = *last; 60 | while (first < last) { 61 | while (cmp(*first, pivot)) ++first; 62 | while (cmp(pivot, *last)) --last; 63 | if (first < last) swap(*first, *last); 64 | } 65 | return first; 66 | } 67 | 68 | Solution 4: Improved quickselect 69 | 70 | In the previous solution, we used the last element as a pivot. It's a simple approach, but in the worst case the 71 | runtime can be O(n * n). To combat that, we coluld use the median-of-median method to pick our pivot. 72 | 73 | */ 74 | 75 | #include 76 | #include 77 | #include 78 | 79 | using namespace std; 80 | 81 | vector> kClosest(vector>& ps, int K) { 82 | multimap m; 83 | for (int i = 0; i < ps.size(); ++i) 84 | m.insert({ ps[i][0] * ps[i][0] + ps[i][1] * ps[i][1], i }); 85 | vector> res; 86 | for (auto it = m.begin(); K > 0; ++it, --K) res.push_back(ps[it->second]); 87 | return res; 88 | } 89 | 90 | 91 | 92 | int main() { 93 | vector> points {{1,3}, {-2,2}}; 94 | int K = 1; 95 | std::cout << "Input: points = [[1,3],[-2,2]], K = 1" << std::endl; 96 | std::cout << "Expected Output: [[-2,2]]" << std::endl; 97 | points = kClosest(points, K); 98 | std::cout << "Actual Output: ["; 99 | for(auto a: points) { 100 | std::cout << "[" << a[0] << "," << a[1] << "] "; 101 | } 102 | std::cout << "]"<< std::endl; 103 | 104 | points = {{3,3}, {5,-1}, {-2,4}}; 105 | K = 2; 106 | std::cout << "Input: points = [[3,3],[5,-1],[-2,4]], K = 2" << std::endl; 107 | std::cout << "Expected Output: [[3,3],[-2,4]] or [[-2,4],[3,3]]" << std::endl; 108 | points = kClosest(points, K); 109 | std::cout << "Actual Output: ["; 110 | for(auto a: points) { 111 | std::cout << "[" << a[0] << "," << a[1] << "] "; 112 | } 113 | std::cout << "]"<< std::endl; 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /load_balancer/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /load_balancer/LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /load_balancer/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /longest_palindromic_substring/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /longest_palindromic_substring/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /longest_string_made_up_of_only_vowels/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /longest_string_made_up_of_only_vowels/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /longest_string_without_3_consecutive_characters/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /longest_string_without_3_consecutive_characters/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /max_of_min_altitudes/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /max_of_min_altitudes/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /merge_intervals/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /merge_intervals/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /merge_two_sorted_lists/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /merge_two_sorted_lists/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /min_cost_to_connect_all_nodes/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /min_cost_to_connect_all_nodes/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /min_cost_to_connect_ropes/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /min_cost_to_connect_ropes/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(min_cost_to_connect_ropes) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(min_cost_to_connect_ropes main.cpp) -------------------------------------------------------------------------------- /min_cost_to_connect_ropes/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /min_cost_to_connect_ropes/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "Hello, World!" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /min_cost_to_repair_edges/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /min_cost_to_repair_edges/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /most_common_word/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /most_common_word/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(most_common_word) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(most_common_word main.cpp) -------------------------------------------------------------------------------- /most_common_word/README.md: -------------------------------------------------------------------------------- 1 | # Most Common Word 2 | 3 | Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn't banned, and that the answer is unique. 4 | 5 | Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase. 6 | 7 | 8 | 9 | Example: 10 | 11 | Input: 12 | 13 | paragraph = "Bob hit a ball, the hit BALL flew far after it was hit." 14 | banned = ["hit"] 15 | 16 | Output: "ball" 17 | 18 | Explanation: 19 | "hit" occurs 3 times, but it is a banned word. 20 | 21 | "ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph. 22 | Note that words in the paragraph are not case sensitive, 23 | that punctuation is ignored (even if adjacent to words, such as "ball,"), 24 | and that "hit" isn't the answer even though it occurs more because it is banned. 25 | 26 | 27 | 28 | Note: 29 | 30 | 1 <= paragraph.length <= 1000. 31 | 0 <= banned.length <= 100. 32 | 1 <= banned[i].length <= 10. 33 | The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.) 34 | paragraph only consists of letters, spaces, or the punctuation symbols !?',;. 35 | There are no hyphens or hyphenated words. 36 | Words only consist of letters, never apostrophes or other punctuation symbols. 37 | 38 | 39 | -------------------------------------------------------------------------------- /most_common_word/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | string mostCommonWord(string paragraph, vector& banned) { 9 | 10 | unordered_map numbers_of_words; 11 | auto paragraph_len = paragraph.size(); 12 | 13 | // find all words in the input line, convert then to lower case and same in a map 14 | // complexity O(N + S) where N is a number of characters in the input line and S is complexity 15 | // of the string.push_back which is amortized constant. 16 | auto i = 0; 17 | string s; 18 | while(i < paragraph_len) { 19 | 20 | while(i < paragraph_len && isalpha(paragraph[i])) { 21 | s.push_back(tolower(paragraph[i++])); 22 | } 23 | 24 | while(i < paragraph_len && !isalpha(paragraph[i])) { i++; } 25 | 26 | // complexity of adding a value to the unordered_map is constant in average case and 27 | // linear in number of container elements in worst case because of rebuilding of the hashes 28 | // generally it is an amortized constant 29 | if( ! s.empty()) { 30 | numbers_of_words[s]++; 31 | s.clear(); 32 | } 33 | } 34 | 35 | // mark all banned words by 0 36 | // complexity O(1) 37 | for(auto & word: banned) { numbers_of_words[word] = 0; } 38 | 39 | auto count = 0; 40 | 41 | // find max number of unbanned word 42 | // complexity O(N) where N is a number of allowed words 43 | // it may be improved but it doesn't make sense because of it is the 44 | // only pass and the number of words is not big 45 | for(auto & pair: numbers_of_words) { 46 | if (pair.second > count) { s = pair.first, count = pair.second; } 47 | } 48 | return s; 49 | 50 | // General complexity is about O(N) where N is a number of characters in the input line 51 | } 52 | 53 | 54 | int main() { 55 | 56 | string paragraph("Bob hit a ball, the hit BALL flew far after it was hit."); 57 | vector banned({"hit"}); 58 | std::cout << "Expected output is: ball" << endl << "Actual output is: "; 59 | 60 | std::cout << mostCommonWord(paragraph, banned) << std::endl; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /nth_gp/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /nth_gp/LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /nth_gp/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /number-of-dice-rolls-with-target-sum/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /number-of-dice-rolls-with-target-sum/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /number_of_islands/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /number_of_islands/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(number_of_islands) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(number_of_islands main.cpp) -------------------------------------------------------------------------------- /number_of_islands/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /number_of_islands/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "Hello, World!" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /optimal_utilization/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /optimal_utilization/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /partition_labels/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /partition_labels/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /point_of_lattice/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /point_of_lattice/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /prison_cells_after_N_days/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /prison_cells_after_N_days/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(prison_cells_after_N_days) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(prison_cells_after_N_days main.cpp) -------------------------------------------------------------------------------- /prison_cells_after_N_days/README.md: -------------------------------------------------------------------------------- 1 | # Prison Cells After N Days 2 | 3 | Classic test for work with bits in a byte. 4 | 5 | Following description is for a task on leetcode.com. 6 | Description on an Amazon online assessment website is different. 7 | The difference described below. 8 | 9 | There are 8 prison cells in a row, and each cell is either occupied or vacant. 10 | 11 | Each day, whether the cell is occupied or vacant changes according to the following rules: 12 | 13 | If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied. 14 | Otherwise, it becomes vacant. 15 | 16 | (Note that because the prison is a row, the first and the last cells in the row can't have two adjacent neighbors.) 17 | 18 | We describe the current state of the prison in the following way: cells[i] == 1 if the i-th cell is occupied, else cells[i] == 0. 19 | 20 | Given the initial state of the prison, return the state of the prison after N days (and N such changes described above.) 21 | 22 | ``` 23 | 24 | Example 1: 25 | 26 | Input: cells = [0,1,0,1,1,0,0,1], N = 7 27 | Output: [0,0,1,1,0,0,0,0] 28 | Explanation: 29 | The following table summarizes the state of the prison on each day: 30 | Day 0: [0, 1, 0, 1, 1, 0, 0, 1] 31 | Day 1: [0, 1, 1, 0, 0, 0, 0, 0] 32 | Day 2: [0, 0, 0, 0, 1, 1, 1, 0] 33 | Day 3: [0, 1, 1, 0, 0, 1, 0, 0] 34 | Day 4: [0, 0, 0, 0, 0, 1, 0, 0] 35 | Day 5: [0, 1, 1, 1, 0, 1, 0, 0] 36 | Day 6: [0, 0, 1, 0, 1, 1, 0, 0] 37 | Day 7: [0, 0, 1, 1, 0, 0, 0, 0] 38 | 39 | Example 2: 40 | 41 | Input: cells = [1,0,0,1,0,0,1,0], N = 1000000000 42 | Output: [0,0,1,1,1,1,1,0] 43 | 44 | 45 | Note: 46 | 47 | cells.length == 8 48 | cells[i] is in {0, 1} 49 | 1 <= N <= 10^9 50 | 51 | ``` 52 | 53 | The idea of the solution is based of the fact that there are only 2^6 possible combinations of cells because from the second step first 54 | and last cells are always = 0. It means that after 2^6 steps all combinations will repeat again. 55 | 56 | So we will keep all combinations of the cells in a hash map until we meet the loop. After it happens we will not calculate the next 57 | combinations but will take precalculated ones from the hash map. 58 | 59 | Discussion and test environment are here: 60 | https://leetcode.com/problems/prison-cells-after-n-days/ 61 | 62 | The difference on an Amazon online assessment website. 63 | 64 | 1. The first and the last cells in the row can't have two adjacent neighbors so we should consider 65 | these cells as the cells that always have vacant neighbors. In other words an imaginary 66 | cells before the first cell and after the last cells are always empty. 67 | 68 | 2. If a cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes vacant. 69 | So the first and the last cells are always occupied. -------------------------------------------------------------------------------- /prison_cells_after_N_days/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | using cells_t = bitset<8>; 10 | 11 | // --------------- Leetcode --------------------- 12 | 13 | // these convertion functions need only because 14 | // of input type is vector which I can't change to pass the auto test on leetcode.com 15 | // But I'd prefer bitset<8> type everywhere because it is better describes the task 16 | // and because unordered_map doesn't work with vector as a key directly 17 | 18 | cells_t vector_to_bitset(const std::vector & cells) { 19 | cells_t bits; 20 | for (auto i = 0; i < 8; ++i) { 21 | bits[i] = cells[i]; 22 | } 23 | return bits; 24 | } 25 | 26 | std::vector bitset_to_vector(const cells_t & bits) { 27 | vector cells(8); 28 | for (auto i = 0; i < 8; ++i) { 29 | cells[i] = bits[i]; 30 | } 31 | return cells; 32 | } 33 | 34 | vector prisonAfterNDays_Leetcode(vector& cells_vector, int N) { 35 | // because unordered_map has constant access complexity. O(1) 36 | unordered_map seen; 37 | 38 | cells_t cells = vector_to_bitset(cells_vector); 39 | 40 | while (N > 0) { 41 | cells_t cells2; 42 | 43 | seen[cells] = N--; 44 | 45 | for (int i = 1; i < 7; ++i) { 46 | cells2[i] = cells[i - 1] == cells[i + 1] ? 1 : 0; 47 | } 48 | 49 | cells = cells2; 50 | 51 | cout << "N = " << N << endl; 52 | cout << cells << endl; 53 | auto pair = seen.find(cells); 54 | 55 | if ( pair != seen.end()) { 56 | auto diff = pair->second - N; 57 | if(diff > 0) { 58 | N %= diff; 59 | } 60 | } 61 | 62 | } 63 | return bitset_to_vector(cells); 64 | } 65 | 66 | 67 | // ------ Amazon --------- 68 | 69 | cells_t array_to_bitset(int* states) { 70 | cells_t bits; 71 | for (auto i = 0; i < 8; ++i) { 72 | bits[i] = states[7-i]; 73 | } 74 | return bits; 75 | } 76 | 77 | 78 | std::vector bitset_to_vector_Amazon(const cells_t & bits) { 79 | vector cells(8); 80 | for (auto i = 0; i < 8; ++i) { 81 | cells[i] = bits[7-i]; 82 | } 83 | return cells; 84 | } 85 | 86 | vector prisonAfterNDays_Amazon(int* states, int N) { 87 | // because unordered_map has constant access complexity. O(1) 88 | unordered_map seen; 89 | 90 | cells_t cells = array_to_bitset(states); 91 | 92 | while (N > 0) { 93 | cells_t cells2; 94 | 95 | seen[cells] = N--; 96 | 97 | // first bit 98 | cells2[0] = 0 != cells[1]; 99 | 100 | for (int i = 1; i < 7; ++i) { 101 | cells2[i] = cells[i - 1] != cells[i + 1]; 102 | } 103 | 104 | // last bit 105 | cells2[7] = 0 != cells[6]; 106 | 107 | cells = cells2; 108 | 109 | auto pair = seen.find(cells); 110 | 111 | if ( pair != seen.end()) { 112 | auto diff = pair->second - N; 113 | if(diff > 0) { 114 | N %= diff; 115 | } 116 | } 117 | } 118 | return bitset_to_vector_Amazon(cells); 119 | } 120 | 121 | 122 | vector cellCompete(int* states, int days) { 123 | return prisonAfterNDays_Amazon(states, days); 124 | } 125 | 126 | // ------ 127 | 128 | // -------- Bonus solution for the second test task ------- 129 | 130 | static int gcd(int x, int y) { 131 | int r; 132 | 133 | if (x <= 0 || y <= 0) { return 0; } 134 | 135 | while ((r = x % y) != 0) { 136 | x = y; 137 | y = r; 138 | } 139 | 140 | return y; 141 | } 142 | 143 | int generalizedGCD(int num, int* arr) { 144 | int g = arr[0]; 145 | 146 | for (int i = 1; i < num; i++) { 147 | g = gcd(g, arr[i]); 148 | } 149 | 150 | return g; 151 | 152 | } 153 | 154 | // ------------ 155 | 156 | 157 | int main() { 158 | 159 | // vector cells = {1,0,0,0,0,1,0,0}; 1 160 | // "Expected: [0,1,0,0,1,0,1,0]" 161 | 162 | // vector cells = {1,1,1,0,1,1,1,1}; 2 163 | // "Expected: [0,0,0,0,0,1,1,0]" 164 | 165 | vector cells = {1,1,1,0,1,1,1,1}; 166 | vector res = prisonAfterNDays_Amazon(cells.data(), 2); 167 | 168 | cout << "Amazon Input: cells = [1,1,1,0,1,1,1,1], N = 2" << endl << 169 | "Expected: [0,0,0,0,0,1,1,0]" << endl << 170 | "Actual output: "; 171 | 172 | for (auto i : res) { 173 | cout << i << " "; 174 | } 175 | 176 | cout << endl << endl; 177 | 178 | vector cells2 = {0,1,0,1,1,0,0,1}; 179 | 180 | res = prisonAfterNDays_Leetcode(cells2, 7); 181 | 182 | cout << "Leetcode Input: cells2 = [0,1,0,1,1,0,0,1], N = 7" << endl << 183 | "Expected: [0,0,1,1,0,0,0,0]" << endl << 184 | "Actual output: "; 185 | 186 | for (auto i : res) { 187 | cout << i << " "; 188 | } 189 | 190 | return 0; 191 | } -------------------------------------------------------------------------------- /product_suggestions_system/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /product_suggestions_system/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /reorder_data_in_log_files/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /reorder_data_in_log_files/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(reorder_data_in_log_files) 3 | 4 | set(CMAKE_CXX_STANDARD 14) 5 | 6 | add_executable(reorder_data_in_log_files main.cpp) -------------------------------------------------------------------------------- /reorder_data_in_log_files/README.md: -------------------------------------------------------------------------------- 1 | You have an array of logs. Each log is a space delimited string of words. 2 | 3 | For each log, the first word in each log is an alphanumeric identifier. Then, either: 4 | 5 | Each word after the identifier will consist only of lowercase letters, or; 6 | Each word after the identifier will consist only of digits. 7 | 8 | We will call these two varieties of logs letter-logs and digit-logs. It is guaranteed that each log has at least one word after its identifier. 9 | 10 | Reorder the logs so that all of the letter-logs come before any digit-log. The letter-logs are ordered lexicographically ignoring identifier, with the identifier used in case of ties. The digit-logs should be put in their original order. 11 | 12 | Return the final order of the logs. 13 | 14 | 15 | 16 | Example 1: 17 | 18 | Input: logs = ["dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"] 19 | 20 | Output: ["let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"] 21 | 22 | 23 | 24 | Constraints: 25 | 26 | 0 <= logs.length <= 100 27 | 3 <= logs[i].length <= 100 28 | logs[i] is guaranteed to have an identifier, and a word after the identifier. 29 | 30 | 31 | -------------------------------------------------------------------------------- /reorder_data_in_log_files/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /* 7 | Position of this solution on leetcode.com 8 | Runtime: 8 ms, faster than 98.31% of C++ online submissions for Reorder Data in Log Files. 9 | Memory Usage: 11.7 MB, less than 100.00% of C++ online submissions for Reorder Data in Log Files. 10 | */ 11 | 12 | using namespace std; 13 | 14 | static bool compare(const string & a, const string & b) { 15 | auto second_word1 = a.find(' '); 16 | auto second_word2 = b.find(' '); 17 | bool is_digit_log1 = isdigit(a[second_word1 + 1]); 18 | bool is_digit_log2 = isdigit(b[second_word2 + 1]); 19 | if(is_digit_log1) { 20 | // a is a digit log, b is a letter log, a > b 21 | // or a and b are both digit logs so keep their original order 22 | // in both cases return false is correct. 23 | /* 24 | From stl_algo.h: 25 | The relative ordering of equivalent elements is preserved, so any two 26 | elements @p x and @p y in the range @p [__first,__last) such that 27 | @p __comp(x,y) is false and @p __comp(y,x) is false will have the same 28 | relative ordering after calling @p stable_sort(). 29 | So it doesn't matter what kind of the second argument. 30 | To preserve relative ordering of equivalent elements it is enough to return false if the first argument is a digit log. 31 | */ 32 | return false; 33 | } 34 | else { 35 | if (is_digit_log2) { 36 | return true; // a is a letter log, b is a digit log, a < b 37 | } 38 | else { 39 | auto res = a.compare(second_word1, a.size(), b, second_word2, b.size()); 40 | 41 | if (0 > res) { 42 | // a and b are both letter logs and a > b 43 | return true; 44 | } 45 | else { 46 | if (0 == res) { 47 | //If string parts are the same then compare key 48 | auto key_res = a.compare(0, second_word1, b, 0, second_word2); 49 | return 0 > key_res; 50 | } 51 | // a and b are both letter logs and a < b 52 | return false; 53 | } 54 | } 55 | } 56 | } 57 | 58 | 59 | vector reorderLogFiles(vector& logs) { 60 | //The order of equal elements is guaranteed to be preserved in stable_sort. 61 | stable_sort(logs.begin(), logs.end(), compare); 62 | return logs; 63 | } 64 | 65 | 66 | int main() { 67 | 68 | // Input: 69 | vector logs = {"dig1 8 1 5 1","let1 art can","dig2 3 6","let2 own kit dig","let3 art zero"}; 70 | 71 | // Output: 72 | vector output = {"let1 art can","let3 art zero","let2 own kit dig","dig1 8 1 5 1","dig2 3 6"}; 73 | 74 | std::cout << "Input: " << std::endl; 75 | for (auto s: logs) { 76 | std::cout << '"' << s << "\" "; 77 | } 78 | 79 | std::cout << std::endl; 80 | 81 | std::cout << "Expected output: " << std::endl; 82 | for (auto s: output) { 83 | std::cout << '"' << s << "\" "; 84 | } 85 | 86 | std::cout << std::endl; 87 | 88 | vector res = reorderLogFiles(logs); 89 | 90 | std::cout << "Actual output: " << std::endl; 91 | for (auto s: res) { 92 | std::cout << '"' << s << "\" "; 93 | } 94 | 95 | std::cout << std::endl; 96 | 97 | 98 | return 0; 99 | } 100 | -------------------------------------------------------------------------------- /roll_dice/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /roll_dice/LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /roll_dice/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /search_a_2d_matrix_2/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /search_a_2d_matrix_2/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /spiral_matrix/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /spiral_matrix/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /substrings_of_size_k_with_k_distinct_chars/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /substrings_of_size_k_with_k_distinct_chars/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /subtree_of_another_tree/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /subtree_of_another_tree/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /subtree_with_maximum_average/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /subtree_with_maximum_average/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /top_n_buzzwords/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /top_n_buzzwords/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(top_n_buzzwords) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(top_n_buzzwords main.cpp) -------------------------------------------------------------------------------- /top_n_buzzwords/README.md: -------------------------------------------------------------------------------- 1 | # Top K Frequently Mentioned Keywords 2 | 3 | Given a list of reviews, a list of keywords and an integer k. Find the most popular k keywords in order of most to least frequently mentioned. 4 | 5 | The comparison of strings is case-insensitive. If keywords are mentioned an equal number of times in reviews, sort alphabetically. 6 | 7 | Example 1: 8 | ``` 9 | Input: 10 | k = 2 11 | keywords = ["anacell", "cetracular", "betacellular"] 12 | reviews = [ 13 | "Anacell provides the best services in the city", 14 | "betacellular has awesome services", 15 | "Best services provided by anacell, everyone should use anacell", 16 | ] 17 | ``` 18 | Output: 19 | ["anacell", "betacellular"] 20 | 21 | Explanation: 22 | "anacell" is occuring in 2 different reviews and "betacellular" is only occuring in 1 review. 23 | 24 | Example 2: 25 | ``` 26 | Input: 27 | k = 2 28 | keywords = ["anacell", "betacellular", "cetracular", "deltacellular", "eurocell"] 29 | reviews = [ 30 | "I love anacell Best services; Best services provided by anacell", 31 | "betacellular has great services", 32 | "deltacellular provides much better services than betacellular", 33 | "cetracular is worse than anacell", 34 | "Betacellular is better than deltacellular.", 35 | ] 36 | ``` 37 | Output: 38 | ["betacellular", "anacell"] 39 | 40 | Explanation: 41 | "betacellular" is occuring in 3 different reviews. "anacell" and "deltacellular" are occuring in 2 reviews, but "anacell" is lexicographically smaller. 42 | 43 | Related problems: 44 | 45 | https://leetcode.com/problems/top-k-frequent-words/ 46 | https://leetcode.com/problems/top-k-frequent-elements/ 47 | -------------------------------------------------------------------------------- /top_n_buzzwords/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | 12 | vector get_top_keywords(const vector & keywords, vector & reviews, int k ) { 13 | // This map will contains pairs of the keywords and their frequency of their appearance in the reviews 14 | unordered_map map_keys; 15 | // Reserve buckets for all possible keywords to prevent rehash in process of work 16 | // It allows to add all keywords in constant time. 17 | map_keys.reserve(keywords.size()); 18 | 19 | // Fill the map with all keywords. Then we will use them to recognize in the text of the reviews 20 | for(auto & key : keywords) { 21 | map_keys.emplace(key, 0); 22 | } 23 | 24 | using key_counter = pair; 25 | 26 | // Write a custom comparator which allows to sort the keywords corresponding to the requirements of the task 27 | auto comp = [](const key_counter & lhs, const key_counter & rhs) -> bool { 28 | return lhs.second > rhs.second || (lhs.first < rhs.first && lhs.second == rhs.second); 29 | }; 30 | 31 | // We will keep here properly sorted keywords. 32 | set counter_set (comp); 33 | 34 | // Process the reviews one by one 35 | for(string review : reviews) { 36 | // transform each review to lower before processing 37 | std::transform(review.begin(), review.end(), review.begin(), 38 | [](unsigned char c){ return std::tolower(c); }); 39 | // This set needs to keep all keywords from the review which were added to 40 | // count each keyword only one time per review as the task requires 41 | unordered_set added; 42 | // This is the most short and obvious way to break a string with many delimiters to tokens. C++ is weak here. 43 | for(char *token = strtok((char*)review.c_str(), " .,;"); token != NULL; token = strtok(NULL, " .,;")) { 44 | // If the token is a given keyword and this token was not counted for this review count it. 45 | if(map_keys.count(token) && !added.count(token)) { 46 | map_keys[token]++; 47 | added.emplace(token); 48 | } 49 | } 50 | } 51 | // Easy and fast way to sort the keywords corresponding to the requirements of the task 52 | for(auto& e: map_keys) { counter_set.emplace(e); } 53 | 54 | // Take k sorted keywords and add return them as the result 55 | vector res; 56 | for(auto& e: counter_set) { 57 | if(k == 0) { break; } 58 | res.push_back(e.first); 59 | --k; 60 | } 61 | return res; 62 | } 63 | 64 | 65 | int main() { 66 | int k1 = 2; 67 | vector keywords1 = { "anacell", "cetracular", "betacellular" }; 68 | vector reviews1 = { "Anacell provides the best services in the city", 69 | "betacellular has awesome services", 70 | "Best services provided by anacell, everyone should use anacell"}; 71 | int k2 = 2; 72 | vector keywords2 = { "anacell", "betacellular", "cetracular", "deltacellular", "eurocell" }; 73 | vector reviews2 = { "I love anacell Best services; Best services provided by anacell", 74 | "betacellular has great services", 75 | "deltacellular provides much better services than betacellular", 76 | "cetracular is worse than anacell", 77 | "Betacellular is better than deltacellular."}; 78 | 79 | std::cout << "test 1: " << std::endl; 80 | 81 | auto res = get_top_keywords(keywords1, reviews1, k1); 82 | 83 | for(auto e: res) { 84 | cout << e << endl; 85 | } 86 | 87 | std::cout << std::endl << "test 2: " << std::endl; 88 | 89 | res = get_top_keywords(keywords2, reviews2, k2); 90 | 91 | for(auto e: res) { 92 | cout << e << endl; 93 | } 94 | 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /treasure_island/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /treasure_island/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /treasure_island_2/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /treasure_island_2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(treasure_island_2) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(treasure_island_2 main.cpp) -------------------------------------------------------------------------------- /treasure_island_2/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /treasure_island_2/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "Hello, World!" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /two_sum_-_unique_pairs/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /two_sum_-_unique_pairs/README.md: -------------------------------------------------------------------------------- 1 | # trainins 2 | My training, contest and test solutions 3 | -------------------------------------------------------------------------------- /zombie_in_matrix/.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Compiled Object files 5 | *.slo 6 | *.lo 7 | *.o 8 | *.obj 9 | 10 | # Precompiled Headers 11 | *.gch 12 | *.pch 13 | 14 | # Compiled Dynamic libraries 15 | *.so 16 | *.dylib 17 | *.dll 18 | 19 | # Fortran module files 20 | *.mod 21 | *.smod 22 | 23 | # Compiled Static libraries 24 | *.lai 25 | *.la 26 | *.a 27 | *.lib 28 | 29 | # Executables 30 | *.exe 31 | *.out 32 | *.app 33 | 34 | #Clion settings 35 | /.idea 36 | /cmake-build-debug 37 | /cmake-build-default 38 | /cmake-build-release 39 | /cmake-build-relwithdebinfo -------------------------------------------------------------------------------- /zombie_in_matrix/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.15) 2 | project(zombie_in_matrix) 3 | 4 | set(CMAKE_CXX_STANDARD 17) 5 | 6 | add_executable(zombie_in_matrix main.cpp) -------------------------------------------------------------------------------- /zombie_in_matrix/README.md: -------------------------------------------------------------------------------- 1 | #Zombie in Matrix 2 | 3 | Given a 2D grid, each cell is either a zombie 1 or a human 0. Zombies can turn adjacent (up/down/left/right) human beings into zombies every hour. Find out how many hours does it take to infect all humans? 4 | 5 | Example: 6 | 7 | Input: 8 | [[0, 1, 1, 0, 1], 9 | [0, 1, 0, 1, 0], 10 | [0, 0, 0, 0, 1], 11 | [0, 1, 0, 0, 0]] 12 | 13 | Output: 2 14 | 15 | Explanation: 16 | 17 | At the end of the 1st hour, the status of the grid: 18 | [[1, 1, 1, 1, 1], 19 | [1, 1, 1, 1, 1], 20 | [0, 1, 0, 1, 1], 21 | [1, 1, 1, 0, 1]] 22 | 23 | At the end of the 2nd hour, the status of the grid: 24 | [[1, 1, 1, 1, 1], 25 | [1, 1, 1, 1, 1], 26 | [1, 1, 1, 1, 1], 27 | [1, 1, 1, 1, 1]] 28 | 29 | Related problems: 30 | 31 | https://leetcode.com/problems/rotting-oranges/ 32 | https://leetcode.com/problems/walls-and-gates/ (premium) 33 | 34 | -------------------------------------------------------------------------------- /zombie_in_matrix/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // question source: 8 | // https://leetcode.com/discuss/interview-question/411357/ 9 | 10 | 11 | struct node_t { int row = 0, col = 0; }; 12 | 13 | int min_hours(vector> &matrix) { 14 | int hours = -1; // return -1 in case of error 15 | 16 | int directions[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; 17 | 18 | size_t rows = matrix.size(); 19 | size_t cols = rows ? matrix[0].size() : 0; // to prevent access error when rows == 0 20 | 21 | // collect all positions of the zombies into a queue 22 | queue q; 23 | for (int i = 0; i < rows; i++) { 24 | for (int j = 0; j < cols; j++) { 25 | if (matrix[i][j] == 1) { q.push({i, j}); } 26 | } 27 | } 28 | // Do BFS until the end of the humans 29 | while (!q.empty()) { 30 | int q_size = q.size(); 31 | // Process all positions which are already added into the queue. 32 | for (int i = 0; i < q_size; i++) { 33 | auto node = q.front(); 34 | q.pop(); 35 | // Find all humans in adjacent cells and add them into the queue 36 | for (auto dir: directions) { 37 | int nr = node.row + dir[0]; 38 | int nc = node.col + dir[1]; 39 | if (nr >= 0 && nr < rows && nc >= 0 && nc < cols && matrix[nr][nc] == 0) { 40 | q.push({nr, nc}); 41 | matrix[nr][nc] = 1; 42 | } 43 | } 44 | } 45 | hours++; 46 | } 47 | return hours; 48 | } 49 | 50 | 51 | int main() { 52 | 53 | vector> empty = {}; 54 | vector> one = {{0,0}}; 55 | vector> two = {{0,0},{0,1}}; 56 | 57 | vector> m0 = { 58 | {0, 0, 0, 0, 0}, 59 | {0, 0, 0, 0, 0}, 60 | {0, 0, 0, 0, 0}, 61 | {0, 0, 0, 0, 0}, 62 | }; 63 | 64 | vector> m1 = { 65 | {0, 0, 0, 0, 1}, 66 | {0, 0, 0, 0, 0}, 67 | {0, 0, 0, 0, 0}, 68 | {0, 0, 0, 0, 0}, 69 | }; 70 | 71 | vector> m = { 72 | {0, 1, 1, 0, 1}, 73 | {0, 1, 0, 1, 0}, 74 | {0, 0, 0, 0, 1}, 75 | {0, 1, 0, 0, 0}, 76 | }; 77 | 78 | 79 | cout << min_hours(m) << endl; 80 | 81 | return 0; 82 | } --------------------------------------------------------------------------------