{"config":{"lang":["ru"],"separator":"[\\s\\-]+","pipeline":["stopWordFilter"],"fields":{"title":{"boost":1000.0},"text":{"boost":1.0},"tags":{"boost":1000000.0}}},"docs":[{"location":"chapter_appendix/","title":"\u0413\u043b\u0430\u0432\u0430 16. \u00a0 \u041f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435","text":""},{"location":"chapter_appendix/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_appendix/contribution/","title":"16.2 \u00a0 \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u0439\u0442\u0435\u0441\u044c \u043a \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044e \u043a\u043d\u0438\u0433\u0438","text":"

\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0430\u0432\u0442\u043e\u0440\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043a\u043d\u0438\u0433\u0435 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0442\u044c\u0441\u044f \u0443\u043f\u0443\u0449\u0435\u043d\u0438\u044f \u0438 \u043e\u0448\u0438\u0431\u043a\u0438. \u041f\u0440\u043e\u0441\u0438\u043c \u043e\u0442\u043d\u0435\u0441\u0442\u0438\u0441\u044c \u043a \u044d\u0442\u043e\u043c\u0443 \u0441 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c. \u0415\u0441\u043b\u0438 \u0432\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u0435 \u043e\u043f\u0435\u0447\u0430\u0442\u043a\u0438, \u043d\u0435\u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0449\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438, \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0438 \u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0438, \u0434\u0432\u0443\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0438, \u043d\u0435\u044f\u0441\u043d\u044b\u0435 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0438\u043b\u0438 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043f\u043e\u043c\u043e\u0433\u0438\u0442\u0435 \u043d\u0430\u043c \u044d\u0442\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0443\u0447\u0435\u0431\u043d\u044b\u0439 \u0440\u0435\u0441\u0443\u0440\u0441.

GitHub ID \u0432\u0441\u0435\u0445 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u0431\u0443\u0434\u0443\u0442 \u0443\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043a\u043d\u0438\u0433\u0438, \u0432\u0435\u0431-\u0432\u0435\u0440\u0441\u0438\u0438 \u0438 PDF-\u0432\u0435\u0440\u0441\u0438\u0438 \u0432 \u0437\u043d\u0430\u043a \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u043d\u043e\u0441\u0442\u0438 \u0437\u0430 \u0438\u0445 \u0431\u0435\u0441\u043a\u043e\u0440\u044b\u0441\u0442\u043d\u044b\u0439 \u0432\u043a\u043b\u0430\u0434 \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430.

\u0421\u0438\u043b\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430

\u0418\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0442\u0438\u0440\u0430\u0436\u0430\u043c\u0438 \u0431\u0443\u043c\u0430\u0436\u043d\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u0435\u043b\u0438\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u0443\u0434\u043e\u0431\u043d\u043e.

\u0412 \u044d\u0442\u043e\u0439 \u0436\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u0446\u0438\u043a\u043b \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0434\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0434\u043d\u0435\u0439, \u0430 \u0438\u043d\u043e\u0433\u0434\u0430 \u0434\u0430\u0436\u0435 \u0434\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0447\u0430\u0441\u043e\u0432.

"},{"location":"chapter_appendix/contribution/#1","title":"1. \u00a0 \u041d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043f\u0440\u0430\u0432\u043a\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 16-3, \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0443\u0433\u043b\u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0435\u0441\u0442\u044c \"\u0437\u043d\u0430\u0447\u043e\u043a \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\". \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u0438\u043b\u0438 \u043a\u043e\u0434 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.

  1. \u041d\u0430\u0436\u043c\u0438\u0442\u0435 \u043d\u0430 \"\u0437\u043d\u0430\u0447\u043e\u043a \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\". \u0415\u0441\u043b\u0438 \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \"You need to fork this repository\", \u0441\u043e\u0433\u043b\u0430\u0441\u0438\u0442\u0435\u0441\u044c \u0441 \u044d\u0442\u0438\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435\u043c.
  2. \u0418\u0437\u043c\u0435\u043d\u0438\u0442\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e Markdown-\u0444\u0430\u0439\u043b\u0430, \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u043a \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0439\u0442\u0435\u0441\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0435\u0434\u0438\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c \u043e\u0444\u043e\u0440\u043c\u043b\u0435\u043d\u0438\u044f.
  3. \u0412\u043d\u0438\u0437\u0443 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u0437\u0430\u0442\u0435\u043c \u043d\u0430\u0436\u043c\u0438\u0442\u0435 \u043a\u043d\u043e\u043f\u043a\u0443 \"Propose file change\". \u041f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u043d\u0430\u0436\u043c\u0438\u0442\u0435 \u043a\u043d\u043e\u043f\u043a\u0443 \"Create pull request\", \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0437\u0434\u0430\u0442\u044c pull request.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 16-3 \u00a0 \u041a\u043d\u043e\u043f\u043a\u0430 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b

\u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 \u043d\u0438\u043c\u0438 \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043d\u043e\u0432\u044b\u0439 Issue \u0438\u043b\u0438 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439. \u041c\u044b \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043f\u0435\u0440\u0435\u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435.

"},{"location":"chapter_appendix/contribution/#2","title":"2. \u00a0 \u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f","text":"

\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u0443\u0447\u0430\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u043b\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u0442\u0435\u0439, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u0438\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 Pull Request.

  1. \u0412\u043e\u0439\u0434\u0438\u0442\u0435 \u0432 GitHub \u0438 \u0441\u0434\u0435\u043b\u0430\u0439\u0442\u0435 Fork \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043a\u043d\u0438\u0433\u0438 \u0432 \u0441\u0432\u043e\u0439 \u043b\u0438\u0447\u043d\u044b\u0439 \u0430\u043a\u043a\u0430\u0443\u043d\u0442.
  2. \u041f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0441\u0432\u043e\u0435\u0433\u043e Fork-\u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u044b git clone \u043a\u043b\u043e\u043d\u0438\u0440\u0443\u0439\u0442\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e.
  3. \u0421\u043e\u0437\u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u0443\u0439\u0442\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0434\u0438\u0442\u0435 \u043f\u043e\u043b\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044c\u0442\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u0434\u0430.
  4. \u0421\u0434\u0435\u043b\u0430\u0439\u0442\u0435 Commit \u0434\u043b\u044f \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 Push \u0432 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439.
  5. \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0438 \u043d\u0430\u0436\u043c\u0438\u0442\u0435 \u043a\u043d\u043e\u043f\u043a\u0443 \"Create pull request\", \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043f\u0440\u0430\u0432\u0438\u0442\u044c pull request.
"},{"location":"chapter_appendix/contribution/#3-docker","title":"3. \u00a0 \u0420\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u0435 Docker","text":"

\u0412 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u043c \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u0435 hello-algo \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 Docker-\u0441\u043a\u0440\u0438\u043f\u0442, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 http://localhost:8000:

docker-compose up -d\n

\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0430\u0437\u0432\u0435\u0440\u0442\u044b\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439:

docker-compose down\n
"},{"location":"chapter_appendix/installation/","title":"16.1 \u00a0 \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0441\u0440\u0435\u0434\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":""},{"location":"chapter_appendix/installation/#1611-ide","title":"16.1.1 \u00a0 \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 IDE","text":"

\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u044b \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 (IDE) \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u0443\u044e \u0438 \u043b\u0435\u0433\u043a\u043e\u0432\u0435\u0441\u043d\u0443\u044e VS Code. \u041f\u0435\u0440\u0435\u0439\u0434\u0438\u0442\u0435 \u043d\u0430 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0430\u0439\u0442 VS Code, \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u0432\u0435\u0440\u0441\u0438\u044e \u0434\u043b\u044f \u0441\u0432\u043e\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0435\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 16-1 \u00a0 \u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 VS Code \u0441 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0430\u0439\u0442\u0430

VS Code \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u043c\u043e\u0449\u043d\u043e\u0439 \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u043e\u0439 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \"Python Extension Pack\" \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 Python. \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 16-2.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 16-2 \u00a0 \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code

"},{"location":"chapter_appendix/installation/#1612","title":"16.1.2 \u00a0 \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b","text":""},{"location":"chapter_appendix/installation/#1-python","title":"1. \u00a0 \u0421\u0440\u0435\u0434\u0430 Python","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Miniconda3, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f Python 3.10 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043d\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 python \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Python Extension Pack.
  3. (\u041d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e) \u0412\u0432\u0435\u0434\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 pip install black, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.
"},{"location":"chapter_appendix/installation/#2-cc","title":"2. \u00a0 \u0421\u0440\u0435\u0434\u0430 C/C++","text":"
  1. \u0412 Windows \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c MinGW (\u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435); \u0432 macOS Clang \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 c++ \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 C/C++ Extension Pack.
  3. (\u041d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e) \u041e\u0442\u043a\u0440\u043e\u0439\u0442\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 Settings, \u043d\u0430\u0439\u0434\u0438\u0442\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Clang_format_fallback Style \u0438 \u0437\u0430\u0434\u0430\u0439\u0442\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 { BasedOnStyle: Microsoft, BreakBeforeBraces: Attach }.
"},{"location":"chapter_appendix/installation/#3-java","title":"3. \u00a0 \u0421\u0440\u0435\u0434\u0430 Java","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 OpenJDK (\u0442\u0440\u0435\u0431\u0443\u0435\u043c\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f: > JDK 9).
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 java \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Extension Pack for Java.
"},{"location":"chapter_appendix/installation/#4-c","title":"4. \u00a0 \u0421\u0440\u0435\u0434\u0430 C","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 .Net 8.0.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 C# Dev Kit \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 C# Dev Kit (\u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0435).
  3. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Visual Studio (\u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e \u043f\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435).
"},{"location":"chapter_appendix/installation/#5-go","title":"5. \u00a0 \u0421\u0440\u0435\u0434\u0430 Go","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 go.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 go \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Go.
  3. \u041d\u0430\u0436\u043c\u0438\u0442\u0435 Ctrl + Shift + P, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u0443\u044e \u043f\u0430\u043b\u0438\u0442\u0440\u0443, \u0432\u0432\u0435\u0434\u0438\u0442\u0435 go, \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 Go: Install/Update Tools, \u043e\u0442\u043c\u0435\u0442\u044c\u0442\u0435 \u0432\u0441\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0438\u0445.
"},{"location":"chapter_appendix/installation/#6-swift","title":"6. \u00a0 \u0421\u0440\u0435\u0434\u0430 Swift","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Swift.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 swift \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Swift for Visual Studio Code.
"},{"location":"chapter_appendix/installation/#7-javascript","title":"7. \u00a0 \u0421\u0440\u0435\u0434\u0430 JavaScript","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Node.js.
  2. (\u041d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e) \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 Prettier \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.
"},{"location":"chapter_appendix/installation/#8-typescript","title":"8. \u00a0 \u0421\u0440\u0435\u0434\u0430 TypeScript","text":"
  1. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0442\u0435 \u0436\u0435 \u0448\u0430\u0433\u0438, \u0447\u0442\u043e \u0438 \u0434\u043b\u044f \u0441\u0440\u0435\u0434\u044b JavaScript.
  2. \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 TypeScript Execute (tsx).
  3. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 typescript \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Pretty TypeScript Errors.
"},{"location":"chapter_appendix/installation/#9-dart","title":"9. \u00a0 \u0421\u0440\u0435\u0434\u0430 Dart","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Dart.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 dart \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Dart.
"},{"location":"chapter_appendix/installation/#10-rust","title":"10. \u00a0 \u0421\u0440\u0435\u0434\u0430 Rust","text":"
  1. \u0417\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u0435 \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 Rust.
  2. \u0412 \u043c\u0430\u0433\u0430\u0437\u0438\u043d\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439 VS Code \u043d\u0430\u0439\u0434\u0438\u0442\u0435 rust \u0438 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0435 rust-analyzer.
"},{"location":"chapter_appendix/terminology/","title":"16.3 \u00a0 \u0413\u043b\u043e\u0441\u0441\u0430\u0440\u0438\u0439","text":"

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 16-1 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u044b \u0432\u0430\u0436\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b, \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u0432 \u043a\u043d\u0438\u0433\u0435. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 16-1 \u00a0 \u0412\u0430\u0436\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c

English \u0420\u0443\u0441\u0441\u043a\u0438\u0439 algorithm \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c data structure \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 code \u043a\u043e\u0434 file \u0444\u0430\u0439\u043b function \u0444\u0443\u043d\u043a\u0446\u0438\u044f method \u043c\u0435\u0442\u043e\u0434 variable \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f asymptotic complexity analysis \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 time complexity \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c space complexity \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c loop \u0446\u0438\u043a\u043b iteration \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f recursion \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f tail recursion \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f recursion tree \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 big-\\(O\\) notation \u043d\u043e\u0442\u0430\u0446\u0438\u044f big-\\(O\\) asymptotic upper bound \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 sign-magnitude \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 1\u2019s complement \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434 2\u2019s complement \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 array \u043c\u0430\u0441\u0441\u0438\u0432 index \u0438\u043d\u0434\u0435\u043a\u0441 linked list \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a linked list node, list node \u0443\u0437\u0435\u043b \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 head node \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b tail node \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b list \u0441\u043f\u0438\u0441\u043e\u043a dynamic array \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 hard disk \u0436\u0435\u0441\u0442\u043a\u0438\u0439 \u0434\u0438\u0441\u043a random-access memory (RAM) \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c cache memory \u043a\u0435\u0448-\u043f\u0430\u043c\u044f\u0442\u044c cache miss \u043f\u0440\u043e\u043c\u0430\u0445 \u043a\u0435\u0448\u0430 cache hit rate \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0432 \u043a\u0435\u0448 stack \u0441\u0442\u0435\u043a top of the stack \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430 bottom of the stack \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0435\u043a\u0430 queue \u043e\u0447\u0435\u0440\u0435\u0434\u044c double-ended queue \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c front of the queue \u0433\u043e\u043b\u043e\u0432\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 rear of the queue \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 hash table \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 hash set \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 bucket \u043a\u043e\u0440\u0437\u0438\u043d\u0430 hash function \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f hash collision \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u044f load factor \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f separate chaining \u0446\u0435\u043f\u043d\u0430\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f open addressing \u043e\u0442\u043a\u0440\u044b\u0442\u0430\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f linear probing \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u0437\u043e\u043d\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 lazy deletion \u043b\u0435\u043d\u0438\u0432\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 binary tree \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e tree node \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430 left-child node \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b right-child node \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b parent node \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0443\u0437\u0435\u043b left subtree \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e right subtree \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e root node \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b leaf node \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b edge \u0440\u0435\u0431\u0440\u043e level \u0443\u0440\u043e\u0432\u0435\u043d\u044c degree \u0441\u0442\u0435\u043f\u0435\u043d\u044c height \u0432\u044b\u0441\u043e\u0442\u0430 depth \u0433\u043b\u0443\u0431\u0438\u043d\u0430 perfect binary tree \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e complete binary tree \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e full binary tree \u043f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e balanced binary tree \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e binary search tree \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 AVL tree \u0410\u0412\u041b-\u0434\u0435\u0440\u0435\u0432\u043e red-black tree \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e level-order traversal \u043e\u0431\u0445\u043e\u0434 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c breadth-first traversal \u043e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 depth-first traversal \u043e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 binary search tree \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 balanced binary search tree \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 balance factor \u0444\u0430\u043a\u0442\u043e\u0440 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 heap \u043a\u0443\u0447\u0430 max heap \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 min heap \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 priority queue \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c heapify \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 top-\\(k\\) problem \u043f\u043e\u0438\u0441\u043a \\(k\\) \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 graph \u0433\u0440\u0430\u0444 vertex \u0432\u0435\u0440\u0448\u0438\u043d\u0430 undirected graph \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 directed graph \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 connected graph \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 disconnected graph \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 weighted graph \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 adjacency \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u044c path \u043f\u0443\u0442\u044c in-degree \u0432\u0445\u043e\u0434\u044f\u0449\u0430\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c out-degree \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0430\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c adjacency matrix \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 adjacency list \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 breadth-first search \u043f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 depth-first search \u043f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 binary search \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a searching algorithm \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 sorting algorithm \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 selection sort \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c bubble sort \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c insertion sort \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u043e\u0439 quick sort \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 merge sort \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c heap sort \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 bucket sort \u0431\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 counting sort \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c radix sort \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 divide and conquer \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 hanota problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435 backtracking algorithm \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c constraint \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 solution \u0440\u0435\u0448\u0435\u043d\u0438\u0435 state \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 pruning \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 permutations problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445 subset-sum problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0441\u0443\u043c\u043c\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 \\(n\\)-queens problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \\(n\\) \u0444\u0435\u0440\u0437\u044f\u0445 dynamic programming \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 initial state \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 state-transition equation \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f knapsack problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 edit distance problem \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f greedy algorithm \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c"},{"location":"chapter_array_and_linkedlist/","title":"\u0413\u043b\u0430\u0432\u0430 4. \u00a0 \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u043f\u0438\u0441\u043a\u0438","text":"

Abstract

\u041c\u0438\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u043f\u0440\u043e\u0447\u043d\u0443\u044e \u043a\u0438\u0440\u043f\u0438\u0447\u043d\u0443\u044e \u0441\u0442\u0435\u043d\u0443.

\u041a\u0438\u0440\u043f\u0438\u0447\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043b\u043e\u0436\u0435\u043d\u044b \u0440\u043e\u0432\u043d\u043e \u0438 \u043f\u043b\u043e\u0442\u043d\u043e \u043f\u0440\u0438\u043b\u0435\u0433\u0430\u044e\u0442 \u0434\u0440\u0443\u0433 \u043a \u0434\u0440\u0443\u0433\u0443. \u041a\u0438\u0440\u043f\u0438\u0447\u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0440\u0430\u0437\u0431\u0440\u043e\u0441\u0430\u043d\u044b \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445, \u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u0435 \u0438\u0445 \u043b\u043e\u0437\u044b \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0442\u044f\u043d\u0443\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0449\u0435\u043b\u044f\u043c\u0438.

"},{"location":"chapter_array_and_linkedlist/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_array_and_linkedlist/array/","title":"4.1 \u00a0 \u041c\u0430\u0441\u0441\u0438\u0432","text":"

\u041c\u0430\u0441\u0441\u0438\u0432 (array) - \u044d\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c (index). \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-1 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0435\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-1 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f

"},{"location":"chapter_array_and_linkedlist/array/#411","title":"4.1.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c","text":""},{"location":"chapter_array_and_linkedlist/array/#1","title":"1. \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u0434\u0432\u0443\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430: \u0431\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438\u043b\u0438 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438. \u0415\u0441\u043b\u0438 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435 \u0443\u043a\u0430\u0437\u0430\u043d\u044b, \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(0\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\narr: list[int] = [0] * 5  # [ 0, 0, 0, 0, 0 ]\nnums: list[int] = [1, 3, 2, 5, 4]\n
array.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\n// \u0425\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0441\u0442\u0435\u043a\u0435\nint arr[5];\nint nums[5] = { 1, 3, 2, 5, 4 };\n// \u0425\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0435 (\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0443\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438)\nint* arr1 = new int[5];\nint* nums1 = new int[5] { 1, 3, 2, 5, 4 };\n
array.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint[] arr = new int[5]; // { 0, 0, 0, 0, 0 }\nint[] nums = { 1, 3, 2, 5, 4 };\n
array.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint[] arr = new int[5]; // [ 0, 0, 0, 0, 0 ]\nint[] nums = [1, 3, 2, 5, 4];\n
array.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvar arr [5]int\n// \u0412 Go \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b ([5]int) \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u043b\u0438\u043d\u044b ([]int) - \u0441\u0440\u0435\u0437\n// \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 Go \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b\n// \u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430 extend(), \u043d\u0438\u0436\u0435 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0441\u0440\u0435\u0437\u044b (Slice) \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432\u044b (Array)\nnums := []int{1, 3, 2, 5, 4}\n
array.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nlet arr = Array(repeating: 0, count: 5) // [0, 0, 0, 0, 0]\nlet nums = [1, 3, 2, 5, 4]\n
array.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvar arr = new Array(5).fill(0);\nvar nums = [1, 3, 2, 5, 4];\n
array.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nlet arr: number[] = new Array(5).fill(0);\nlet nums: number[] = [1, 3, 2, 5, 4];\n
array.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nList<int> arr = List.filled(5, 0); // [0, 0, 0, 0, 0]\nList<int> nums = [1, 3, 2, 5, 4];\n
array.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nlet arr: [i32; 5] = [0; 5]; // [0, 0, 0, 0, 0]\nlet slice: &[i32] = &[0; 5];\n// \u0412 Rust \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b ([i32; 5]) \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0430 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0434\u043b\u0438\u043d\u044b (&[i32]) - \u0441\u0440\u0435\u0437\n// \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 Rust \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b\n// Vector \u0432 Rust \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n// \u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430 extend(), \u043d\u0438\u0436\u0435 \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c vector \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 (array)\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
array.c
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }\nint nums[5] = { 1, 3, 2, 5, 4 };\n
array.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvar arr = IntArray(5) // { 0, 0, 0, 0, 0 }\nvar nums = intArrayOf(1, 3, 2, 5, 4)\n
array.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\narr = Array.new(5, 0)\nnums = [1, 3, 2, 5, 4]\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BC%D0%B0%D1%81%D1%81%D0%B8%D0%B2%0Aarr%20%3D%20%5B0%5D%20%2A%205%20%20%23%20%5B%200%2C%200%2C%200%2C%200%2C%200%20%5D%0Anums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/array/#2","title":"2. \u00a0 \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c","text":"

\u042d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u043b\u044e\u0431\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0417\u043d\u0430\u044f \u0430\u0434\u0440\u0435\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 (\u0442\u043e \u0435\u0441\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430) \u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435 \u0441 \u0440\u0438\u0441\u0443\u043d\u043a\u0430 \u043d\u0438\u0436\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u044d\u0442\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u043c\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-2 \u00a0 \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 4-2, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u0435\u043d \\(0\\) , \u0438 \u044d\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u043c, \u0432\u0435\u0434\u044c \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0435\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441\u0447\u0435\u0442 \u0441 \\(1\\) . \u041e\u0434\u043d\u0430\u043a\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u044b \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u0421\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0440\u0430\u0432\u043d\u043e \\(0\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441 \\(0\\) \u0432\u043f\u043e\u043b\u043d\u0435 \u043b\u043e\u0433\u0438\u0447\u0435\u043d.

\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d: \u043b\u044e\u0431\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def random_access(nums: list[int]) -> int:\n    \"\"\"\u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443\"\"\"\n    # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, len(nums)-1]\n    random_index = random.randint(0, len(nums) - 1)\n    # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    random_num = nums[random_index]\n    return random_num\n
array.cpp
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint randomAccess(int *nums, int size) {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, size)\n    int randomIndex = rand() % size;\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
array.java
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint randomAccess(int[] nums) {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n    int randomIndex = ThreadLocalRandom.current().nextInt(0, nums.length);\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
array.cs
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint RandomAccess(int[] nums) {\n    Random random = new();\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.Length)\n    int randomIndex = random.Next(nums.Length);\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
array.go
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfunc randomAccess(nums []int) (randomNum int) {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n    randomIndex := rand.Intn(len(nums))\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    randomNum = nums[randomIndex]\n    return\n}\n
array.swift
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfunc randomAccess(nums: [Int]) -> Int {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.count)\n    let randomIndex = nums.indices.randomElement()!\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    let randomNum = nums[randomIndex]\n    return randomNum\n}\n
array.js
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfunction randomAccess(nums) {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    const random_num = nums[random_index];\n    return random_num;\n}\n
array.ts
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfunction randomAccess(nums: number[]): number {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n    const random_index = Math.floor(Math.random() * nums.length);\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    const random_num = nums[random_index];\n    return random_num;\n}\n
array.dart
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint randomAccess(List<int> nums) {\n  // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  int randomIndex = Random().nextInt(nums.length);\n  // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  int randomNum = nums[randomIndex];\n  return randomNum;\n}\n
array.rs
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfn random_access(nums: &[i32]) -> i32 {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.len())\n    let random_index = rand::thread_rng().gen_range(0..nums.len());\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    let random_num = nums[random_index];\n    random_num\n}\n
array.c
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint randomAccess(int *nums, int size) {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, size)\n    int randomIndex = rand() % size;\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    int randomNum = nums[randomIndex];\n    return randomNum;\n}\n
array.kt
/* \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfun randomAccess(nums: IntArray): Int {\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.size)\n    val randomIndex = ThreadLocalRandom.current().nextInt(0, nums.size)\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    val randomNum = nums[randomIndex]\n    return randomNum\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/array/#3","title":"3. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430","text":"

\u042d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \"\u0432\u043f\u043b\u043e\u0442\u043d\u0443\u044e\" \u0434\u0440\u0443\u0433 \u043a \u0434\u0440\u0443\u0433\u0443, \u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043d\u0435\u0442 \u043c\u0435\u0441\u0442\u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-3, \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u043e \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e, \u0430 \u0437\u0430\u0442\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u043d\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u0442 \u043a \"\u043f\u043e\u0442\u0435\u0440\u0435\" \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u044b \u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0434\u043b\u044f \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043e \"\u0441\u043f\u0438\u0441\u043a\u0430\u0445\".

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def insert(nums: list[int], num: int, index: int):\n    \"\"\"\u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\"\"\"\n    # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for i in range(len(nums) - 1, index, -1):\n        nums[i] = nums[i - 1]\n    # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num\n
array.cpp
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.java
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nvoid insert(int[] nums, int num, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (int i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.cs
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nvoid Insert(int[] nums, int num, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (int i = nums.Length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.go
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfunc insert(nums []int, num int, index int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for i := len(nums) - 1; i > index; i-- {\n        nums[i] = nums[i-1]\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num\n}\n
array.swift
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfunc insert(nums: inout [Int], num: Int, index: Int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for i in nums.indices.dropFirst(index).reversed() {\n        nums[i] = nums[i - 1]\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num\n}\n
array.js
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfunction insert(nums, num, index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.ts
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfunction insert(nums: number[], num: number, index: number): void {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (let i = nums.length - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.dart
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 _num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nvoid insert(List<int> nums, int _num, int index) {\n  // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n  for (var i = nums.length - 1; i > index; i--) {\n    nums[i] = nums[i - 1];\n  }\n  // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c _num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n  nums[index] = _num;\n}\n
array.rs
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfn insert(nums: &mut [i32], num: i32, index: usize) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for i in (index + 1..nums.len()).rev() {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.c
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nvoid insert(int *nums, int size, int num, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (int i = size - 1; i > index; i--) {\n        nums[i] = nums[i - 1];\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num;\n}\n
array.kt
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\nfun insert(nums: IntArray, num: Int, index: Int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (i in nums.size - 1 downTo index + 1) {\n        nums[i] = nums[i - 1]\n    }\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n    nums[index] = num\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n\n\n# ## \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\n# \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 Ruby \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n# \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 Array \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\ndef extend(nums, enlarge)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  res\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 ###\ndef insert(nums, num, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n  nums[index] = num\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/array/#4","title":"4. \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430","text":"

\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-4, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 \\(i\\) , \u0442\u043e \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \\(i\\) \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-4 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \"\u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c\", \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def remove(nums: list[int], index: int):\n    \"\"\"\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\"\"\"\n    # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for i in range(index, len(nums) - 1):\n        nums[i] = nums[i + 1]\n
array.cpp
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nvoid remove(int *nums, int size, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.java
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nvoid remove(int[] nums, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (int i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.cs
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nvoid Remove(int[] nums, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (int i = index; i < nums.Length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.go
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunc remove(nums []int, index int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for i := index; i < len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n}\n
array.swift
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunc remove(nums: inout [Int], index: Int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for i in nums.indices.dropFirst(index).dropLast() {\n        nums[i] = nums[i + 1]\n    }\n}\n
array.js
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunction remove(nums, index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.ts
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunction remove(nums: number[], index: number): void {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (let i = index; i < nums.length - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.dart
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nvoid remove(List<int> nums, int index) {\n  // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n  for (var i = index; i < nums.length - 1; i++) {\n    nums[i] = nums[i + 1];\n  }\n}\n
array.rs
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfn remove(nums: &mut [i32], index: usize) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for i in index..nums.len() - 1 {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.c
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\n// \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435: stdio.h \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e remove\nvoid removeItem(int *nums, int size, int index) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (int i = index; i < size - 1; i++) {\n        nums[i] = nums[i + 1];\n    }\n}\n
array.kt
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfun remove(nums: IntArray, index: Int) {\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (i in index..<nums.size - 1) {\n        nums[i] = nums[i + 1]\n    }\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n\n\n# ## \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\n# \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 Ruby \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n# \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 Array \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\ndef extend(nums, enlarge)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  res\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 ###\ndef insert(nums, num, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n  nums[index] = num\nend\n\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index ###\ndef remove(nums, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412 \u0446\u0435\u043b\u043e\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438\u043c\u0435\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438.

"},{"location":"chapter_array_and_linkedlist/array/#5","title":"5. \u00a0 \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043a\u0430\u043a \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443, \u0442\u0430\u043a \u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u044f \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def traverse(nums: list[int]):\n    \"\"\"\u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n    count = 0\n    # \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for i in range(len(nums)):\n        count += nums[i]\n    # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for num in nums:\n        count += num\n    # \u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0430\u043d\u043d\u044b\u0445\n    for i, num in enumerate(nums):\n        count += nums[i]\n        count += num\n
array.cpp
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
array.java
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvoid traverse(int[] nums) {\n    int count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (int i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int num : nums) {\n        count += num;\n    }\n}\n
array.cs
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvoid Traverse(int[] nums) {\n    int count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (int i = 0; i < nums.Length; i++) {\n        count += nums[i];\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    foreach (int num in nums) {\n        count += num;\n    }\n}\n
array.go
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunc traverse(nums []int) {\n    count := 0\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for i := 0; i < len(nums); i++ {\n        count += nums[i]\n    }\n    count = 0\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for _, num := range nums {\n        count += num\n    }\n    // \u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0430\u043d\u043d\u044b\u0445\n    for i, num := range nums {\n        count += nums[i]\n        count += num\n    }\n}\n
array.swift
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunc traverse(nums: [Int]) {\n    var count = 0\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for i in nums.indices {\n        count += nums[i]\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for num in nums {\n        count += num\n    }\n    // \u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0430\u043d\u043d\u044b\u0445\n    for (i, num) in nums.enumerated() {\n        count += nums[i]\n        count += num\n    }\n}\n
array.js
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunction traverse(nums) {\n    let count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (const num of nums) {\n        count += num;\n    }\n}\n
array.ts
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunction traverse(nums: number[]): void {\n    let count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (let i = 0; i < nums.length; i++) {\n        count += nums[i];\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (const num of nums) {\n        count += num;\n    }\n}\n
array.dart
/* \u041f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvoid traverse(List<int> nums) {\n  int count = 0;\n  // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n  for (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n  }\n  // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for (int _num in nums) {\n    count += _num;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u0435\u0442\u043e\u0434\u043e\u043c forEach\n  nums.forEach((_num) {\n    count += _num;\n  });\n}\n
array.rs
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfn traverse(nums: &[i32]) {\n    let mut _count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for i in 0..nums.len() {\n        _count += nums[i];\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    _count = 0;\n    for &num in nums {\n        _count += num;\n    }\n}\n
array.c
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nvoid traverse(int *nums, int size) {\n    int count = 0;\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (int i = 0; i < size; i++) {\n        count += nums[i];\n    }\n}\n
array.kt
/* \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfun traverse(nums: IntArray) {\n    var count = 0\n    // \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    for (i in nums.indices) {\n        count += nums[i]\n    }\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (j in nums) {\n        count += j\n    }\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n\n\n# ## \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\n# \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 Ruby \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n# \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 Array \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\ndef extend(nums, enlarge)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  res\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 ###\ndef insert(nums, num, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n  nums[index] = num\nend\n\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index ###\ndef remove(nums, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n\n# ## \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\ndef traverse(nums)\n  count = 0\n\n  # \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += num\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/array/#6","title":"6. \u00a0 \u041f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435; \u0435\u0441\u043b\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0430\u0441\u0441\u0438\u0432 - \u044d\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u043f\u043e\u0438\u0441\u043a\u043e\u043c\".

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def find(nums: list[int], target: int) -> int:\n    \"\"\"\u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\"\"\"\n    for i in range(len(nums)):\n        if nums[i] == target:\n            return i\n    return -1\n
array.cpp
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
array.java
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint find(int[] nums, int target) {\n    for (int i = 0; i < nums.length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
array.cs
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint Find(int[] nums, int target) {\n    for (int i = 0; i < nums.Length; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
array.go
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfunc find(nums []int, target int) (index int) {\n    index = -1\n    for i := 0; i < len(nums); i++ {\n        if nums[i] == target {\n            index = i\n            break\n        }\n    }\n    return\n}\n
array.swift
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfunc find(nums: [Int], target: Int) -> Int {\n    for i in nums.indices {\n        if nums[i] == target {\n            return i\n        }\n    }\n    return -1\n}\n
array.js
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfunction find(nums, target) {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) return i;\n    }\n    return -1;\n}\n
array.ts
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfunction find(nums: number[], target: number): number {\n    for (let i = 0; i < nums.length; i++) {\n        if (nums[i] === target) {\n            return i;\n        }\n    }\n    return -1;\n}\n
array.dart
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint find(List<int> nums, int target) {\n  for (var i = 0; i < nums.length; i++) {\n    if (nums[i] == target) return i;\n  }\n  return -1;\n}\n
array.rs
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfn find(nums: &[i32], target: i32) -> Option<usize> {\n    for i in 0..nums.len() {\n        if nums[i] == target {\n            return Some(i);\n        }\n    }\n    None\n}\n
array.c
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint find(int *nums, int size, int target) {\n    for (int i = 0; i < size; i++) {\n        if (nums[i] == target)\n            return i;\n    }\n    return -1;\n}\n
array.kt
/* \u041d\u0430\u0439\u0442\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfun find(nums: IntArray, target: Int): Int {\n    for (i in nums.indices) {\n        if (nums[i] == target)\n            return i\n    }\n    return -1\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n\n\n# ## \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\n# \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 Ruby \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n# \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 Array \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\ndef extend(nums, enlarge)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  res\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 ###\ndef insert(nums, num, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n  for i in (nums.length - 1).downto(index + 1)\n    nums[i] = nums[i - 1]\n  end\n\n  # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c num \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\n  nums[index] = num\nend\n\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index ###\ndef remove(nums, index)\n  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n  for i in index...(nums.length - 1)\n    nums[i] = nums[i + 1]\n  end\nend\n\n# ## \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\ndef traverse(nums)\n  count = 0\n\n  # \u041e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n  for i in 0...nums.length\n    count += nums[i]\n  end\n\n  # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += num\n  end\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 ###\ndef find(nums, target)\n  for i in 0...nums.length\n    return i if nums[i] == target\n  end\n\n  -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/array/#7","title":"7. \u00a0 \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u0412 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430.

\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u042d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \\(O(n)\\) , \u0438 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u043e\u043d\u0430 \u043e\u0447\u0435\u043d\u044c \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u0430. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array.py
def extend(nums: list[int], enlarge: int) -> list[int]:\n    \"\"\"\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    res = [0] * (len(nums) + enlarge)\n    # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for i in range(len(nums)):\n        res[i] = nums[i]\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res\n
array.cpp
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    int *res = new int[size + enlarge];\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    delete[] nums;\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.java
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint[] extend(int[] nums, int enlarge) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    int[] res = new int[nums.length + enlarge];\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (int i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.cs
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint[] Extend(int[] nums, int enlarge) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    int[] res = new int[nums.Length + enlarge];\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (int i = 0; i < nums.Length; i++) {\n        res[i] = nums[i];\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.go
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunc extend(nums []int, enlarge int) []int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    res := make([]int, len(nums)+enlarge)\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for i, num := range nums {\n        res[i] = num\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res\n}\n
array.swift
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfunc extend(nums: [Int], enlarge: Int) -> [Int] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    var res = Array(repeating: 0, count: nums.count + enlarge)\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for i in nums.indices {\n        res[i] = nums[i]\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res\n}\n
array.js
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\n// \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 JavaScript \u2014 \u044d\u0442\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n// \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Array \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\nfunction extend(nums, enlarge) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.ts
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\n// \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 TypeScript \u2014 \u044d\u0442\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n// \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 Array \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\nfunction extend(nums: number[], enlarge: number): number[] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    const res = new Array(nums.length + enlarge).fill(0);\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (let i = 0; i < nums.length; i++) {\n        res[i] = nums[i];\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.dart
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nList<int> extend(List<int> nums, int enlarge) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  List<int> res = List.filled(nums.length + enlarge, 0);\n  // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for (var i = 0; i < nums.length; i++) {\n    res[i] = nums[i];\n  }\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  return res;\n}\n
array.rs
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfn extend(nums: &[i32], enlarge: usize) -> Vec<i32> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    let mut res: Vec<i32> = vec![0; nums.len() + enlarge];\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439\n    res[0..nums.len()].copy_from_slice(nums);\n\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    res\n}\n
array.c
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nint *extend(int *nums, int size, int enlarge) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    int *res = (int *)malloc(sizeof(int) * (size + enlarge));\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (int i = 0; i < size; i++) {\n        res[i] = nums[i];\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\n    for (int i = size; i < size + enlarge; i++) {\n        res[i] = 0;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res;\n}\n
array.kt
/* \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nfun extend(nums: IntArray, enlarge: Int): IntArray {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    val res = IntArray(nums.size + enlarge)\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for (i in nums.indices) {\n        res[i] = nums[i]\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    return res\n}\n
array.rb
=begin\nFile: array.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\ndef random_access(nums)\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 [0, nums.length)\n  random_index = Random.rand(0...nums.length)\n\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  nums[random_index]\nend\n\n\n# ## \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\n# \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: Array \u0432 Ruby \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n# \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 Array \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\ndef extend(nums, enlarge)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n  res = Array.new(nums.length + enlarge, 0)\n\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  for i in 0...nums.length\n    res[i] = nums[i]\n  end\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/array/#412","title":"4.1.2 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u041c\u0430\u0441\u0441\u0438\u0432 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 \u0432\u0441\u0435 \u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0442\u0438\u043f. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043c\u043d\u043e\u0433\u043e \u0430\u043f\u0440\u0438\u043e\u0440\u043d\u043e\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.

\u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 - \u043f\u0430\u043b\u043a\u0430 \u043e \u0434\u0432\u0443\u0445 \u043a\u043e\u043d\u0446\u0430\u0445, \u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.

"},{"location":"chapter_array_and_linkedlist/array/#413","title":"4.1.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u041c\u0430\u0441\u0441\u0438\u0432 - \u044d\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u0438 \u043e\u0447\u0435\u043d\u044c \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u043d \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0432 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445, \u0442\u0430\u043a \u0438 \u043f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.

"},{"location":"chapter_array_and_linkedlist/linked_list/","title":"4.2 \u00a0 \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a","text":"

\u041f\u0430\u043c\u044f\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u043c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u0438 \u0432 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0435 \u0443\u0447\u0430\u0441\u0442\u043a\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0437\u0431\u0440\u043e\u0441\u0430\u043d\u044b \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u0430\u0434\u0440\u0435\u0441\u0443 \u043f\u0430\u043c\u044f\u0442\u0438. \u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439, \u0430 \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0447\u0435\u043d\u044c \u0432\u0435\u043b\u0438\u043a, \u043f\u0430\u043c\u044f\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0441\u0443\u043c\u0435\u0442\u044c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u0431\u043b\u043e\u043a. \u0418\u043c\u0435\u043d\u043d\u043e \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430.

\u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a (linked list) - \u044d\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442-\u0443\u0437\u0435\u043b, \u0430 \u0441\u0430\u043c\u0438 \u0443\u0437\u043b\u044b \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u044b \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0447\u0435\u0440\u0435\u0437 \"\u0441\u0441\u044b\u043b\u043a\u0438\". \u0421\u0441\u044b\u043b\u043a\u0430 \u0445\u0440\u0430\u043d\u0438\u0442 \u0430\u0434\u0440\u0435\u0441 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443.

\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0443\u0437\u043b\u044b \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 \u0438\u0445 \u0430\u0434\u0440\u0435\u0441\u0430 \u0432\u043e\u0432\u0441\u0435 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u0431\u044b\u0442\u044c \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u043c\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-5 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 4-5, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0435\u0434\u0438\u043d\u0438\u0446\u0435\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 \u0443\u0437\u0435\u043b (node). \u041a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0432\u0435 \u0447\u0430\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445: \"\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\" \u0443\u0437\u043b\u0430 \u0438 \"\u0441\u0441\u044b\u043b\u043a\u0443\" \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043a\u043e\u0434\u0435 \u043d\u0438\u0436\u0435, \u0443\u0437\u0435\u043b \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 ListNode \u0445\u0440\u0430\u043d\u0438\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u043e \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c). \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u043c \u043e\u0431\u044a\u0435\u043c\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
class ListNode:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val               # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        self.next: ListNode | None = None # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nstruct ListNode {\n    int val;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode *next;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode(int x) : val(x), next(nullptr) {}  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n};\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    int val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode(int x) { val = x; }  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode(int x) {  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n    int val = x;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode? next;      // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype ListNode struct {\n    Val  int       // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    Next *ListNode // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n\n// NewListNode \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a\nfunc NewListNode(val int) *ListNode {\n    return &ListNode{\n        Val:  val,\n        Next: nil,\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    var val: Int // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var next: ListNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\n    init(x: Int) { // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n        val = x\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    constructor(val, next) {\n        this.val = (val === undefined ? 0 : val);       // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = (next === undefined ? null : next); // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    constructor(val?: number, next?: ListNode | null) {\n        this.val = val === undefined ? 0 : val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = next === undefined ? null : next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n  int val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  ListNode? next; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n  ListNode(this.val, [this.next]); // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    next: Option<Rc<RefCell<ListNode>>>, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct ListNode {\n    int val;               // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    struct ListNode *next; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n} ListNode;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    return node;\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\nclass ListNode(x: Int) {\n    val _val: Int = x          // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val next: ListNode? = null // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
# \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\nclass ListNode\n  attr_accessor :val  # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  attr_accessor :next # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\n  def initialize(val=0, next_node=nil)\n    @val = val\n    @next = next_node\n  end\nend\n
"},{"location":"chapter_array_and_linkedlist/linked_list/#421","title":"4.2.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u043e\u043c","text":""},{"location":"chapter_array_and_linkedlist/linked_list/#1","title":"1. \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0448\u0430\u0433\u043e\u0432: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u0437\u0430\u0442\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u0438-\u0441\u0441\u044b\u043b\u043a\u0438 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438. \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u043f\u0438\u0441\u043a\u0430, \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 next.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nn0 = ListNode(1)\nn1 = ListNode(3)\nn2 = ListNode(2)\nn3 = ListNode(5)\nn4 = ListNode(4)\n# \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nListNode* n0 = new ListNode(1);\nListNode* n1 = new ListNode(3);\nListNode* n2 = new ListNode(2);\nListNode* n3 = new ListNode(5);\nListNode* n4 = new ListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nListNode n0 = new ListNode(1);\nListNode n1 = new ListNode(3);\nListNode n2 = new ListNode(2);\nListNode n3 = new ListNode(5);\nListNode n4 = new ListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nListNode n0 = new(1);\nListNode n1 = new(3);\nListNode n2 = new(2);\nListNode n3 = new(5);\nListNode n4 = new(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nn0 := NewListNode(1)\nn1 := NewListNode(3)\nn2 := NewListNode(2)\nn3 := NewListNode(5)\nn4 := NewListNode(4)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.Next = n1\nn1.Next = n2\nn2.Next = n3\nn3.Next = n4\n
linked_list.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nlet n0 = ListNode(x: 1)\nlet n1 = ListNode(x: 3)\nlet n2 = ListNode(x: 2)\nlet n3 = ListNode(x: 5)\nlet n4 = ListNode(x: 4)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
linked_list.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nconst n0 = new ListNode(1);\nconst n1 = new ListNode(3);\nconst n2 = new ListNode(2);\nconst n3 = new ListNode(5);\nconst n4 = new ListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\\\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nListNode n0 = ListNode(1);\nListNode n1 = ListNode(3);\nListNode n2 = ListNode(2);\nListNode n3 = ListNode(5);\nListNode n4 = ListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nlet n0 = Rc::new(RefCell::new(ListNode { val: 1, next: None }));\nlet n1 = Rc::new(RefCell::new(ListNode { val: 3, next: None }));\nlet n2 = Rc::new(RefCell::new(ListNode { val: 2, next: None }));\nlet n3 = Rc::new(RefCell::new(ListNode { val: 5, next: None }));\nlet n4 = Rc::new(RefCell::new(ListNode { val: 4, next: None }));\n\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.borrow_mut().next = Some(n1.clone());\nn1.borrow_mut().next = Some(n2.clone());\nn2.borrow_mut().next = Some(n3.clone());\nn3.borrow_mut().next = Some(n4.clone());\n
linked_list.c
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nListNode* n0 = newListNode(1);\nListNode* n1 = newListNode(3);\nListNode* n2 = newListNode(2);\nListNode* n3 = newListNode(5);\nListNode* n4 = newListNode(4);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0->next = n1;\nn1->next = n2;\nn2->next = n3;\nn3->next = n4;\n
linked_list.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nval n0 = ListNode(1)\nval n1 = ListNode(3)\nval n2 = ListNode(2)\nval n3 = ListNode(5)\nval n4 = ListNode(4)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1;\nn1.next = n2;\nn2.next = n3;\nn3.next = n4;\n
linked_list.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 1 -> 3 -> 2 -> 5 -> 4\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\nn0 = ListNode.new(1)\nn1 = ListNode.new(3)\nn2 = ListNode.new(2)\nn3 = ListNode.new(5)\nn4 = ListNode.new(4)\n# \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn0.next = n1\nn1.next = n2\nn2.next = n3\nn3.next = n4\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%D1%81%D0%B2%D1%8F%D0%B7%D0%BD%D1%8B%D0%B9%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%D1%83%D0%B7%D0%B5%D0%BB%D0%BA%D0%BB%D0%B0%D1%81%D1%81%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%D0%97%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%B0%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D1%81%D0%BB%D0%B5%D0%B4%D1%83%D1%8E%D1%89%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%B2%D1%8F%D0%B7%D0%BD%D1%8B%D0%B9%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%201%20-%3E%203%20-%3E%202%20-%3E%205%20-%3E%204%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BA%D0%B0%D0%B6%D0%B4%D1%8B%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%20%20%20%20n0%20%3D%20ListNode%281%29%0A%20%20%20%20n1%20%3D%20ListNode%283%29%0A%20%20%20%20n2%20%3D%20ListNode%282%29%0A%20%20%20%20n3%20%3D%20ListNode%285%29%0A%20%20%20%20n4%20%3D%20ListNode%284%29%0A%20%20%20%20%23%20%D0%9F%D0%BE%D1%81%D1%82%D1%80%D0%BE%D0%B8%D1%82%D1%8C%20%D1%81%D1%81%D1%8B%D0%BB%D0%BA%D0%B8%20%D0%BC%D0%B5%D0%B6%D0%B4%D1%83%20%D1%83%D0%B7%D0%BB%D0%B0%D0%BC%D0%B8%0A%20%20%20%20n0.next%20%3D%20n1%0A%20%20%20%20n1.next%20%3D%20n2%0A%20%20%20%20n2.next%20%3D%20n3%0A%20%20%20%20n3.next%20%3D%20n4&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

\u041c\u0430\u0441\u0441\u0438\u0432 \u0432 \u0446\u0435\u043b\u043e\u043c - \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b nums[0] , nums[1] \u0438 \u0442.\u0434. \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432-\u0443\u0437\u043b\u043e\u0432. \u041e\u0431\u044b\u0447\u043d\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0433\u043e \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b; \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u0430\u043a \u0441\u043f\u0438\u0441\u043e\u043a n0 .

"},{"location":"chapter_array_and_linkedlist/linked_list/#2","title":"2. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430","text":"

\u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-6, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438 n0 \u0438 n1 ; \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0441\u0435\u0433\u043e \u0434\u0432\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f), \u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) .

\u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f: \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(n)\\) , \u0438 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0435\u043c\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u043e \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-6 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0443\u0437\u043b\u0430 \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
def insert(n0: ListNode, P: ListNode):\n    \"\"\"\u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435\"\"\"\n    n1 = n0.next\n    P.next = n1\n    n0.next = P\n
linked_list.cpp
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
linked_list.java
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid insert(ListNode n0, ListNode P) {\n    ListNode n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
linked_list.cs
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid Insert(ListNode n0, ListNode P) {\n    ListNode? n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
linked_list.go
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunc insertNode(n0 *ListNode, P *ListNode) {\n    n1 := n0.Next\n    P.Next = n1\n    n0.Next = P\n}\n
linked_list.swift
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunc insert(n0: ListNode, P: ListNode) {\n    let n1 = n0.next\n    P.next = n1\n    n0.next = P\n}\n
linked_list.js
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunction insert(n0, P) {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
linked_list.ts
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunction insert(n0: ListNode, P: ListNode): void {\n    const n1 = n0.next;\n    P.next = n1;\n    n0.next = P;\n}\n
linked_list.dart
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid insert(ListNode n0, ListNode P) {\n  ListNode? n1 = n0.next;\n  P.next = n1;\n  n0.next = P;\n}\n
linked_list.rs
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\n#[allow(non_snake_case)]\npub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) {\n    let n1 = n0.borrow_mut().next.take();\n    P.borrow_mut().next = n1;\n    n0.borrow_mut().next = Some(P);\n}\n
linked_list.c
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid insert(ListNode *n0, ListNode *P) {\n    ListNode *n1 = n0->next;\n    P->next = n1;\n    n0->next = P;\n}\n
linked_list.kt
/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfun insert(n0: ListNode?, p: ListNode?) {\n    val n1 = n0?.next\n    p?.next = n1\n    n0?.next = p\n}\n
linked_list.rb
=begin\nFile: linked_list.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 _p \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\n# \u0412 Ruby `p` \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0430 `P` \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `_p`\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/linked_list/#3","title":"3. \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-7, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0438\u0437 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0442\u043e\u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e: \u043d\u0443\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u0443 \u0441\u0441\u044b\u043b\u043a\u0443 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c).

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0445\u043e\u0442\u044f \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u0435\u043b P \u0432\u0441\u0435 \u0435\u0449\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 n1 , \u043f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0434\u043e P \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 P \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0438\u0442 \u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-7 \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0438\u0437 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
def remove(n0: ListNode):\n    \"\"\"\u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435\"\"\"\n    if not n0.next:\n        return\n    # n0 -> P -> n1\n    P = n0.next\n    n1 = P.next\n    n0.next = n1\n
linked_list.cpp
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid remove(ListNode *n0) {\n    if (n0->next == nullptr)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    delete P;\n}\n
linked_list.java
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode n1 = P.next;\n    n0.next = n1;\n}\n
linked_list.cs
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid Remove(ListNode n0) {\n    if (n0.next == null)\n        return;\n    // n0 -> P -> n1\n    ListNode P = n0.next;\n    ListNode? n1 = P.next;\n    n0.next = n1;\n}\n
linked_list.go
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunc removeItem(n0 *ListNode) {\n    if n0.Next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    P := n0.Next\n    n1 := P.Next\n    n0.Next = n1\n}\n
linked_list.swift
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunc remove(n0: ListNode) {\n    if n0.next == nil {\n        return\n    }\n    // n0 -> P -> n1\n    let P = n0.next\n    let n1 = P?.next\n    n0.next = n1\n}\n
linked_list.js
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunction remove(n0) {\n    if (!n0.next) return;\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
linked_list.ts
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfunction remove(n0: ListNode): void {\n    if (!n0.next) {\n        return;\n    }\n    // n0 -> P -> n1\n    const P = n0.next;\n    const n1 = P.next;\n    n0.next = n1;\n}\n
linked_list.dart
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nvoid remove(ListNode n0) {\n  if (n0.next == null) return;\n  // n0 -> P -> n1\n  ListNode P = n0.next!;\n  ListNode? n1 = P.next;\n  n0.next = n1;\n}\n
linked_list.rs
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\n#[allow(non_snake_case)]\npub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) {\n    // n0 -> P -> n1\n    let P = n0.borrow_mut().next.take();\n    if let Some(node) = P {\n        let n1 = node.borrow_mut().next.take();\n        n0.borrow_mut().next = n1;\n    }\n}\n
linked_list.c
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\n// \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435: stdio.h \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e remove\nvoid removeItem(ListNode *n0) {\n    if (!n0->next)\n        return;\n    // n0 -> P -> n1\n    ListNode *P = n0->next;\n    ListNode *n1 = P->next;\n    n0->next = n1;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(P);\n}\n
linked_list.kt
/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 */\nfun remove(n0: ListNode?) {\n    if (n0?.next == null)\n        return\n    // n0 -> P -> n1\n    val p = n0.next\n    val n1 = p?.next\n    n0.next = n1\n}\n
linked_list.rb
=begin\nFile: linked_list.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 _p \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\n# \u0412 Ruby `p` \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0430 `P` \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `_p`\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/linked_list/#4","title":"4. \u00a0 \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443","text":"

\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0430\u043c \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043c\u0435\u043d\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d. \u041a\u0430\u043a \u0443\u0436\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u043e\u0441\u044c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435, \u043a \u043b\u044e\u0431\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u0437\u0430 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0421\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a: \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u0442\u044c \u043e\u0442 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0434\u0435\u043d \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b. \u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \\(i\\) -\u043c\u0443 \u0443\u0437\u043b\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \\(i - 1\\) \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439, \u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(n)\\) .

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
def access(head: ListNode, index: int) -> ListNode | None:\n    \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index\"\"\"\n    for _ in range(index):\n        if not head:\n            return None\n        head = head.next\n    return head\n
linked_list.cpp
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == nullptr)\n            return nullptr;\n        head = head->next;\n    }\n    return head;\n}\n
linked_list.java
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nListNode access(ListNode head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
linked_list.cs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nListNode? Access(ListNode? head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == null)\n            return null;\n        head = head.next;\n    }\n    return head;\n}\n
linked_list.go
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunc access(head *ListNode, index int) *ListNode {\n    for i := 0; i < index; i++ {\n        if head == nil {\n            return nil\n        }\n        head = head.Next\n    }\n    return head\n}\n
linked_list.swift
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunc access(head: ListNode, index: Int) -> ListNode? {\n    var head: ListNode? = head\n    for _ in 0 ..< index {\n        if head == nil {\n            return nil\n        }\n        head = head?.next\n    }\n    return head\n}\n
linked_list.js
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunction access(head, index) {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
linked_list.ts
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfunction access(head: ListNode | null, index: number): ListNode | null {\n    for (let i = 0; i < index; i++) {\n        if (!head) {\n            return null;\n        }\n        head = head.next;\n    }\n    return head;\n}\n
linked_list.dart
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nListNode? access(ListNode? head, int index) {\n  for (var i = 0; i < index; i++) {\n    if (head == null) return null;\n    head = head.next;\n  }\n  return head;\n}\n
linked_list.rs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\npub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> {\n    fn dfs<T>(\n        head: Option<&Rc<RefCell<ListNode<T>>>>,\n        index: i32,\n    ) -> Option<Rc<RefCell<ListNode<T>>>> {\n        if index <= 0 {\n            return head.cloned();\n        }\n\n        if let Some(node) = head {\n            dfs(node.borrow().next.as_ref(), index - 1)\n        } else {\n            None\n        }\n    }\n\n    dfs(Some(head).as_ref(), index)\n}\n
linked_list.c
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nListNode *access(ListNode *head, int index) {\n    for (int i = 0; i < index; i++) {\n        if (head == NULL)\n            return NULL;\n        head = head->next;\n    }\n    return head;\n}\n
linked_list.kt
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index */\nfun access(head: ListNode?, index: Int): ListNode? {\n    var h = head\n    for (i in 0..<index) {\n        if (h == null)\n            return null\n        h = h.next\n    }\n    return h\n}\n
linked_list.rb
=begin\nFile: linked_list.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 _p \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\n# \u0412 Ruby `p` \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0430 `P` \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `_p`\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/linked_list/#5","title":"5. \u00a0 \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430","text":"

\u0412\u044b\u043f\u043e\u043b\u043d\u0438 \u043e\u0431\u0445\u043e\u0434 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u043d\u0430\u0439\u0434\u0438 \u0432 \u043d\u0435\u043c \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target \u0438 \u0432\u0435\u0440\u043d\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0442\u043e\u0436\u0435 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u0443. \u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linked_list.py
def find(head: ListNode, target: int) -> int:\n    \"\"\"\u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target\"\"\"\n    index = 0\n    while head:\n        if head.val == target:\n            return index\n        head = head.next\n        index += 1\n    return -1\n
linked_list.cpp
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head != nullptr) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
linked_list.java
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nint find(ListNode head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
linked_list.cs
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nint Find(ListNode? head, int target) {\n    int index = 0;\n    while (head != null) {\n        if (head.val == target)\n            return index;\n        head = head.next;\n        index++;\n    }\n    return -1;\n}\n
linked_list.go
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nfunc findNode(head *ListNode, target int) int {\n    index := 0\n    for head != nil {\n        if head.Val == target {\n            return index\n        }\n        head = head.Next\n        index++\n    }\n    return -1\n}\n
linked_list.swift
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nfunc find(head: ListNode, target: Int) -> Int {\n    var head: ListNode? = head\n    var index = 0\n    while head != nil {\n        if head?.val == target {\n            return index\n        }\n        head = head?.next\n        index += 1\n    }\n    return -1\n}\n
linked_list.js
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nfunction find(head, target) {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
linked_list.ts
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nfunction find(head: ListNode | null, target: number): number {\n    let index = 0;\n    while (head !== null) {\n        if (head.val === target) {\n            return index;\n        }\n        head = head.next;\n        index += 1;\n    }\n    return -1;\n}\n
linked_list.dart
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nint find(ListNode? head, int target) {\n  int index = 0;\n  while (head != null) {\n    if (head.val == target) {\n      return index;\n    }\n    head = head.next;\n    index++;\n  }\n  return -1;\n}\n
linked_list.rs
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\npub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 {\n    fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 {\n        if let Some(node) = head {\n            if node.borrow().val == target {\n                return idx;\n            }\n            return find(node.borrow().next.as_ref(), target, idx + 1);\n        } else {\n            -1\n        }\n    }\n\n    find(Some(head).as_ref(), target, 0)\n}\n
linked_list.c
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nint find(ListNode *head, int target) {\n    int index = 0;\n    while (head) {\n        if (head->val == target)\n            return index;\n        head = head->next;\n        index++;\n    }\n    return -1;\n}\n
linked_list.kt
/* \u041d\u0430\u0439\u0442\u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target */\nfun find(head: ListNode?, target: Int): Int {\n    var index = 0\n    var h = head\n    while (h != null) {\n        if (h._val == target)\n            return index\n        h = h.next\n        index++\n    }\n    return -1\n}\n
linked_list.rb
=begin\nFile: linked_list.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 _p \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\n# \u0412 Ruby `p` \u2014 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0430 `P` \u2014 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c `_p`\ndef insert(n0, _p)\n  n1 = n0.next\n  _p.next = n1\n  n0.next = _p\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043f\u043e\u0441\u043b\u0435 \u0443\u0437\u043b\u0430 n0 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\ndef remove(n0)\n  return if n0.next.nil?\n\n  # n0 -> remove_node -> n1\n  remove_node = n0.next\n  n1 = remove_node.next\n  n0.next = n1\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 index ###\ndef access(head, index)\n  for i in 0...index\n    return nil if head.nil?\n    head = head.next\n  end\n\n  head\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c target \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 ###\ndef find(head, target)\n  index = 0\n  while head\n    return index if head.val == target\n    head = head.next\n    index += 1\n  end\n\n  -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/linked_list/#422","title":"4.2.2 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 4-1 \u043e\u0431\u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f, \u0438\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0442\u043e\u0436\u0435 \u0432\u043e \u043c\u043d\u043e\u0433\u043e\u043c \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 4-1 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430

\u041c\u0430\u0441\u0441\u0438\u0432 \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0421\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u041d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u0420\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u0438 \u0414\u043b\u0438\u043d\u0430 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430 \u0413\u0438\u0431\u043a\u043e\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u0438 \u042d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043f\u043e\u0442\u0435\u0440\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u042d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \\(O(1)\\) \\(O(n)\\) \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(1)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(1)\\)"},{"location":"chapter_array_and_linkedlist/linked_list/#423","title":"4.2.3 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-8, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0440\u0438 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0430 \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
class ListNode:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        self.next: ListNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n        self.prev: ListNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nstruct ListNode {\n    int val;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode *next;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode *prev;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode(int x) : val(x), next(nullptr), prev(nullptr) {}  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n};\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    int val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode prev;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode(int x) { val = x; }  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode(int x) {  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n    int val = x;    // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode prev;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype DoublyListNode struct {\n    Val  int             // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    Next *DoublyListNode // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    Prev *DoublyListNode // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n\n// NewDoublyListNode \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f\nfunc NewDoublyListNode(val int) *DoublyListNode {\n    return &DoublyListNode{\n        Val:  val,\n        Next: nil,\n        Prev: nil,\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    var val: Int // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var next: ListNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    var prev: ListNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\n    init(x: Int) { // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n        val = x\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    constructor(val, next, prev) {\n        this.val = val  ===  undefined ? 0 : val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = next  ===  undefined ? null : next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n        this.prev = prev  ===  undefined ? null : prev;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    val: number;\n    next: ListNode | null;\n    prev: ListNode | null;\n    constructor(val?: number, next?: ListNode | null, prev?: ListNode | null) {\n        this.val = val  ===  undefined ? 0 : val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = next  ===  undefined ? null : next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n        this.prev = prev  ===  undefined ? null : prev;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    int val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode? next;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode? prev;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    ListNode(this.val, [this.next, this.prev]);  // \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u0422\u0438\u043f \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[derive(Debug)]\nstruct ListNode {\n    val: i32, // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    next: Option<Rc<RefCell<ListNode>>>, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    prev: Option<Rc<RefCell<ListNode>>>, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nimpl ListNode {\n    fn new(val: i32) -> Self {\n        ListNode {\n            val,\n            next: None,\n            prev: None,\n        }\n    }\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct ListNode {\n    int val;               // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    struct ListNode *next; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    struct ListNode *prev; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n} ListNode;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nListNode *newListNode(int val) {\n    ListNode *node;\n    node = (ListNode *) malloc(sizeof(ListNode));\n    node->val = val;\n    node->next = NULL;\n    node->prev = NULL;\n    return node;\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\nclass ListNode(x: Int) {\n    val _val: Int = x           // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val next: ListNode? = null  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    val prev: ListNode? = null  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
# \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\nclass ListNode\n  attr_accessor :val    # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  attr_accessor :next   # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n  attr_accessor :prev   # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\n  def initialize(val=0, next_node=nil, prev_node=nil)\n    @val = val\n    @next = next_node\n    @prev = prev_node\n  end\nend\n

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-8 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432

"},{"location":"chapter_array_and_linkedlist/linked_list/#424","title":"4.2.4 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432","text":"

\u041e\u0434\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u043e\u0432, \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439, \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446 \u0438 \u0433\u0440\u0430\u0444\u043e\u0432.

\u0414\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0442\u0430\u043c, \u0433\u0434\u0435 \u043d\u0443\u0436\u0435\u043d \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a\u0430\u043a \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443, \u0442\u0430\u043a \u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443.

\u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445, \u0442\u0440\u0435\u0431\u0443\u044e\u0449\u0438\u0445 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438 \u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435.

"},{"location":"chapter_array_and_linkedlist/list/","title":"4.3 \u00a0 \u0421\u043f\u0438\u0441\u043e\u043a","text":"

\u0421\u043f\u0438\u0441\u043e\u043a (list) - \u044d\u0442\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0435\u0435 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c, \u0438\u0445 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0438 \u043e\u0431\u0445\u043e\u0434, \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044f \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0435\u043c\u043a\u043e\u0441\u0442\u0438. \u0421\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u0430\u043a \u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.

\u041a\u043e\u0433\u0434\u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u044b \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0442\u043e\u0447\u043d\u043e \u0437\u043d\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0443\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0442\u0440\u0443\u0434\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430. \u0415\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u0430, \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u043e\u043a\u0440\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438; \u0435\u0441\u043b\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0432\u0435\u043b\u0438\u043a\u0430, \u0431\u0443\u0434\u0435\u0442 \u0437\u0440\u044f \u0440\u0430\u0441\u0445\u043e\u0434\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c.

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (dynamic array) \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u0430. \u041e\u043d \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0432\u0441\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 list \u0432 Python, ArrayList \u0432 Java, vector \u0432 C++ \u0438 List \u0432 C#. \u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0438\u044f \"\u0441\u043f\u0438\u0441\u043e\u043a\" \u0438 \"\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\" \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u043c\u0438.

"},{"location":"chapter_array_and_linkedlist/list/#431","title":"4.3.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c","text":""},{"location":"chapter_array_and_linkedlist/list/#1","title":"1. \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438: \"\u0431\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\" \u0438 \"\u0441 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\":

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430\n# \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nnums1: list[int] = []\n# \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nnums: list[int] = [1, 3, 2, 5, 4]\n
list.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0432 C++ vector \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c nums\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nvector<int> nums1;\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nvector<int> nums = { 1, 3, 2, 5, 4 };\n
list.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nList<Integer> nums1 = new ArrayList<>();\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (\u043e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0435\u0440\u0442\u043a\u0443 Integer[] \u0432\u043c\u0435\u0441\u0442\u043e int[])\nInteger[] numbers = new Integer[] { 1, 3, 2, 5, 4 };\nList<Integer> nums = new ArrayList<>(Arrays.asList(numbers));\n
list.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nList<int> nums1 = [];\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nint[] numbers = [1, 3, 2, 5, 4];\nList<int> nums = [.. numbers];\n
list_test.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nnums1 := []int{}\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nnums := []int{1, 3, 2, 5, 4}\n
list.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nlet nums1: [Int] = []\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nvar nums = [1, 3, 2, 5, 4]\n
list.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nconst nums1 = [];\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nconst nums = [1, 3, 2, 5, 4];\n
list.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nconst nums1: number[] = [];\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nconst nums: number[] = [1, 3, 2, 5, 4];\n
list.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nList<int> nums1 = [];\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nList<int> nums = [1, 3, 2, 5, 4];\n
list.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nlet nums1: Vec<i32> = Vec::new();\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nlet nums: Vec<i32> = vec![1, 3, 2, 5, 4];\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 */\n// \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nvar nums1 = listOf<Int>()\n// \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nvar numbers = arrayOf(1, 3, 2, 5, 4)\nvar nums = numbers.toMutableList()\n
list.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430\n# \u0411\u0435\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439\nnums1 = []\n# \u0421 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438\nnums = [1, 3, 2, 5, 4]\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20%23%20%D0%91%D0%B5%D0%B7%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D1%85%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B9%0A%20%20%20%20nums1%20%3D%20%5B%5D%0A%20%20%20%20%23%20%D0%A1%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%BC%D0%B8%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%D0%BC%D0%B8%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D&cumulative=false&curInstr=4&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#2","title":"2. \u00a0 \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c","text":"

\u0421\u043f\u0438\u0441\u043e\u043a \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0438 \u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443\nnum: int = nums[1]  # \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n# \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\nnums[1] = 0    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.cpp
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint num = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.java
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint num = nums.get(1);  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums.set(1, 0);  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.cs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint num = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list_test.go
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nnum := nums[1]  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0     // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.swift
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nlet num = nums[1] // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0 // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.js
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nconst num = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.ts
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nconst num: number = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.dart
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint num = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.rs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nlet num: i32 = nums[1];  // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0;             // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nval num = nums[1]       // \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nnums[1] = 0             // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
list.rb
# \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443\nnum = nums[1] # \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1\n# \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\nnums[1] = 0 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c 0\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%20%D0%BA%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D1%83%0A%20%20%20%20num%20%3D%20nums%5B1%5D%20%20%23%20%D0%BE%D0%B1%D1%80%D0%B0%D1%82%D0%B8%D1%82%D1%8C%D1%81%D1%8F%20%D0%BA%D0%B8%D0%BD%D0%B4%D0%B5%D0%BA%D1%81%201%20%D0%BF%D0%BE%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%0A%0A%20%20%20%20%23%20%D0%9E%D0%B1%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%0A%20%20%20%20nums%5B1%5D%20%3D%200%20%20%20%20%23%20%D0%9E%D0%B1%D0%BD%D0%BE%D0%B2%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%BF%D0%BE%20%D0%B8%D0%BD%D0%B4%D0%B5%D0%BA%D1%81%D1%83%201%20%D0%B4%D0%BE%200&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#3","title":"3. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\) , \u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0438\u043c\u0435\u044e\u0442 \u0442\u0443 \u0436\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c, \u0447\u0442\u043e \u0438 \u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \\(O(n)\\) .

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a\nnums.clear()\n\n# \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n# \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\nnums.insert(3, 6)  # \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\nnums.pop(3)        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.cpp
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.push_back(1);\nnums.push_back(3);\nnums.push_back(2);\nnums.push_back(5);\nnums.push_back(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.insert(nums.begin() + 3, 6);  // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.erase(nums.begin() + 3);      // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.java
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.add(3, 6);  // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.remove(3);  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.cs
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.Clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.Add(1);\nnums.Add(3);\nnums.Add(2);\nnums.Add(5);\nnums.Add(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.Insert(3, 6);  // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.RemoveAt(3);  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list_test.go
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums = nil\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums = append(nums, 1)\nnums = append(nums, 3)\nnums = append(nums, 2)\nnums = append(nums, 5)\nnums = append(nums, 4)\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums = append(nums[:3], append([]int{6}, nums[3:]...)...) // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums = append(nums[:3], nums[4:]...) // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.swift
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.removeAll()\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.append(1)\nnums.append(3)\nnums.append(2)\nnums.append(5)\nnums.append(4)\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.insert(6, at: 3) // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.remove(at: 3) // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.js
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.length = 0;\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.splice(3, 0, 6); // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.splice(3, 1);  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.ts
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.length = 0;\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.splice(3, 0, 6); // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.splice(3, 1);  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.dart
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.insert(3, 6); // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.removeAt(3); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.rs
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.push(1);\nnums.push(3);\nnums.push(2);\nnums.push(5);\nnums.push(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.insert(3, 6);  // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.remove(3);    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.clear();\n\n/* \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 */\nnums.add(1);\nnums.add(3);\nnums.add(2);\nnums.add(5);\nnums.add(4);\n\n/* \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nnums.add(3, 6);  // \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n/* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nnums.remove(3);  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
list.rb
# \u041e\u0447\u0438\u0441\u0442\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a\nnums.clear\n\n# \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043d\u0435\u0446\nnums << 1\nnums << 3\nnums << 2\nnums << 5\nnums << 4\n\n# \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\nnums.insert(3, 6) # \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e 6 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\nnums.delete_at(3) # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 3\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%A1%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%BC%D0%B8%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%D0%BC%D0%B8%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D1%87%D0%B8%D1%81%D1%82%D0%B8%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums.clear%28%29%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BA%D0%BE%D0%BD%D0%B5%D1%86%0A%20%20%20%20nums.append%281%29%0A%20%20%20%20nums.append%283%29%0A%20%20%20%20nums.append%282%29%0A%20%20%20%20nums.append%285%29%0A%20%20%20%20nums.append%284%29%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%92%D1%81%D1%82%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D1%81%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%D0%BD%D1%83%0A%20%20%20%20nums.insert%283%2C%206%29%20%20%23%20%D0%92%D1%81%D1%82%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D1%87%D0%B8%D1%81%D0%BB%D0%BE%206%20%D0%BF%D0%BE%20%D0%B8%D0%BD%D0%B4%D0%B5%D0%BA%D1%81%D1%83%203%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%A3%D0%B4%D0%B0%D0%BB%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%0A%20%20%20%20nums.pop%283%29%20%20%20%20%20%20%20%20%23%20%D0%A3%D0%B4%D0%B0%D0%BB%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%BF%D0%BE%20%D0%B8%D0%BD%D0%B4%D0%B5%D0%BA%D1%81%D1%83%203&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#4","title":"4. \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u041a\u0430\u043a \u0438 \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043a\u0430\u043a \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c, \u0442\u0430\u043a \u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\ncount = 0\nfor i in range(len(nums)):\n    count += nums[i]\n\n# \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430\nfor num in nums:\n    count += num\n
list.cpp
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums[i];\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0;\nfor (int num : nums) {\n    count += num;\n}\n
list.java
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nint count = 0;\nfor (int i = 0; i < nums.size(); i++) {\n    count += nums.get(i);\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\nfor (int num : nums) {\n    count += num;\n}\n
list.cs
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nint count = 0;\nfor (int i = 0; i < nums.Count; i++) {\n    count += nums[i];\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0;\nforeach (int num in nums) {\n    count += num;\n}\n
list_test.go
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\ncount := 0\nfor i := 0; i < len(nums); i++ {\n    count += nums[i]\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0\nfor _, num := range nums {\n    count += num\n}\n
list.swift
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nvar count = 0\nfor i in nums.indices {\n    count += nums[i]\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0\nfor num in nums {\n    count += num\n}\n
list.js
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
list.ts
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nlet count = 0;\nfor (let i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0;\nfor (const num of nums) {\n    count += num;\n}\n
list.dart
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nint count = 0;\nfor (var i = 0; i < nums.length; i++) {\n    count += nums[i];\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\ncount = 0;\nfor (var num in nums) {\n    count += num;\n}\n
list.rs
// \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\nlet mut _count = 0;\nfor i in 0..nums.len() {\n    _count += nums[i];\n}\n\n// \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430\n_count = 0;\nfor num in &nums {\n    _count += num;\n}\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c */\nvar count = 0\nfor (i in nums.indices) {\n    count += nums[i]\n}\n\n/* \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 */\nfor (num in nums) {\n    count += num\n}\n
list.rb
# \u041e\u0431\u0445\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\ncount = 0\nfor i in 0...nums.length\n    count += nums[i]\nend\n\n# \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430\ncount = 0\nfor num in nums\n    count += num\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%B1%D1%85%D0%BE%D0%B4%D0%B8%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%20%D0%BF%D0%BE%20%D0%B8%D0%BD%D0%B4%D0%B5%D0%BA%D1%81%D0%B0%D0%BC%0A%20%20%20%20count%20%3D%200%0A%20%20%20%20for%20i%20in%20range%28len%28nums%29%29%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20nums%5Bi%5D%0A%0A%20%20%20%20%23%20%D0%9D%D0%B5%D0%BF%D0%BE%D1%81%D1%80%D0%B5%D0%B4%D1%81%D1%82%D0%B2%D0%B5%D0%BD%D0%BD%D0%BE%20%D0%BE%D0%B1%D1%85%D0%BE%D0%B4%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D1%8B%20%D1%81%D0%BF%D0%B8%D1%81%D0%BA%D0%B0%0A%20%20%20%20for%20num%20in%20nums%3A%0A%20%20%20%20%20%20%20%20count%20%2B%3D%20num&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#5","title":"5. \u00a0 \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u043e\u0432","text":"

\u0415\u0441\u043b\u0438 \u0434\u0430\u043d \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a nums1 , \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u043a \u0445\u0432\u043e\u0441\u0442\u0443 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432\nnums1: list[int] = [6, 8, 7, 10, 9]\nnums += nums1  # \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.cpp
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nvector<int> nums1 = { 6, 8, 7, 10, 9 };\n// \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\nnums.insert(nums.end(), nums1.begin(), nums1.end());\n
list.java
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nList<Integer> nums1 = new ArrayList<>(Arrays.asList(new Integer[] { 6, 8, 7, 10, 9 }));\nnums.addAll(nums1);  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.cs
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.AddRange(nums1);  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list_test.go
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nnums1 := []int{6, 8, 7, 10, 9}\nnums = append(nums, nums1...)  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.swift
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nlet nums1 = [6, 8, 7, 10, 9]\nnums.append(contentsOf: nums1) // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.js
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nconst nums1 = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.ts
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nconst nums1: number[] = [6, 8, 7, 10, 9];\nnums.push(...nums1);  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.dart
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nList<int> nums1 = [6, 8, 7, 10, 9];\nnums.addAll(nums1);  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.rs
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nlet nums1: Vec<i32> = vec![6, 8, 7, 10, 9];\nnums.extend(nums1);\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 */\nval nums1 = intArrayOf(6, 8, 7, 10, 9).toMutableList()\nnums.addAll(nums1)  // \u041f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a nums1 \u043a \u043a\u043e\u043d\u0446\u0443 nums\n
list.rb
# \u041a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432\nnums1 = [6, 8, 7, 10, 9]\nnums += nums1\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%B1%D1%8A%D0%B5%D0%B4%D0%B8%D0%BD%D0%B8%D1%82%D1%8C%20%D0%B4%D0%B2%D0%B0%20%D1%81%D0%BF%D0%B8%D1%81%D0%BA%D0%B0%0A%20%20%20%20nums1%20%3D%20%5B6%2C%208%2C%207%2C%2010%2C%209%5D%0A%20%20%20%20nums%20%2B%3D%20nums1%20%20%23%20%D0%9F%D1%80%D0%B8%D1%81%D0%BE%D0%B5%D0%B4%D0%B8%D0%BD%D0%B8%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%20nums1%20%D0%BA%20nums&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#6","title":"6. \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u044b \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \"\u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\" \u0438 \"\u0434\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\", \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby list.py
# \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a\nnums.sort()  # \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.cpp
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nsort(nums.begin(), nums.end());  // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.java
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nCollections.sort(nums);  // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.cs
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.Sort(); // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list_test.go
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nsort.Ints(nums)  // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.swift
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort() // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.js
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort((a, b) => a - b);  // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.ts
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort((a, b) => a - b);  // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.dart
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort(); // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.rs
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort(); // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n
list.kt
/* \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a */\nnums.sort() // \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
list.rb
# \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a\nnums = nums.sort { |a, b| a <=> b } # \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0434\u0443\u0442 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D1%82%D1%81%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%0A%20%20%20%20nums.sort%28%29%20%20%23%20%D0%A1%D0%BE%D1%80%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%BA%D0%B0%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%2C%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D1%80%D0%B0%D1%81%D0%BF%D0%BE%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D1%8B%20%D0%BF%D0%BE%20%D0%B2%D0%BE%D0%B7%D1%80%D0%B0%D1%81%D1%82%D0%B0%D0%BD%D0%B8%D1%8E&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_array_and_linkedlist/list/#432","title":"4.3.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u0432 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 Java, C++ \u0438 Python. \u0418\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0430, \u0430 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0442\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u043d\u044b: \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0435\u043c\u043a\u043e\u0441\u0442\u044c, \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435. \u0415\u0441\u043b\u0438 \u0442\u0435\u0431\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0441\u0442\u043e\u0438\u0442 \u0437\u0430\u0433\u043b\u044f\u043d\u0443\u0442\u044c \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434.

\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_list.py
class MyList:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._capacity: int = 10  # \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        self._arr: list[int] = [0] * self._capacity  # \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n        self._size: int = 0  # \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n        self._extend_ratio: int = 2  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\"\"\"\n        return self._size\n\n    def capacity(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n        return self._capacity\n\n    def get(self, index: int) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443\"\"\"\n        # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        return self._arr[index]\n\n    def set(self, num: int, index: int):\n        \"\"\"\u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        self._arr[index] = num\n\n    def add(self, num: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446\"\"\"\n        # \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.size() == self.capacity():\n            self.extend_capacity()\n        self._arr[self._size] = num\n        self._size += 1\n\n    def insert(self, num: int, index: int):\n        \"\"\"\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        # \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self._size == self.capacity():\n            self.extend_capacity()\n        # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for j in range(self._size - 1, index - 1, -1):\n            self._arr[j + 1] = self._arr[j]\n        self._arr[index] = num\n        # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        self._size += 1\n\n    def remove(self, index: int) -> int:\n        \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\"\"\"\n        if index < 0 or index >= self._size:\n            raise IndexError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        num = self._arr[index]\n        # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for j in range(index, self._size - 1):\n            self._arr[j] = self._arr[j + 1]\n        # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        self._size -= 1\n        # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num\n\n    def extend_capacity(self):\n        \"\"\"\u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n        # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 _extend_ratio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        self._arr = self._arr + [0] * self.capacity() * (self._extend_ratio - 1)\n        # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        self._capacity = len(self._arr)\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\"\"\"\n        return self._arr[: self._size]\n
my_list.cpp
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n  private:\n    int *arr;             // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    int arrCapacity = 10; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    int arrSize = 0;      // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    int extendRatio = 2;   // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u041c\u0435\u0442\u043e\u0434-\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ~MyList() {\n        delete[] arr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    int size() {\n        return arrSize;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    int capacity() {\n        return arrCapacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    int get(int index) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        return arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    void set(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    void add(int num) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size() == capacity())\n            extendCapacity();\n        arr[size()] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize++;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    void insert(int index, int num) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size() == capacity())\n            extendCapacity();\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (int j = size() - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize++;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    int remove(int index) {\n        if (index < 0 || index >= size())\n            throw out_of_range(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        int num = arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (int j = index; j < size() - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize--;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    void extendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        int newCapacity = capacity() * extendRatio;\n        int *tmp = arr;\n        arr = new int[newCapacity];\n        // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        for (int i = 0; i < size(); i++) {\n            arr[i] = tmp[i];\n        }\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        delete[] tmp;\n        arrCapacity = newCapacity;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Vector \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    vector<int> toVector() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        vector<int> vec(size());\n        for (int i = 0; i < size(); i++) {\n            vec[i] = arr[i];\n        }\n        return vec;\n    }\n};\n
my_list.java
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    private int[] arr; // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    private int capacity = 10; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    private int size = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    private int extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public MyList() {\n        arr = new int[capacity];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    public int size() {\n        return size;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public int capacity() {\n        return capacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    public int get(int index) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        return arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public void set(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    public void add(int num) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size == capacity())\n            extendCapacity();\n        arr[size] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size++;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    public void insert(int index, int num) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size == capacity())\n            extendCapacity();\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (int j = size - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size++;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public int remove(int index) {\n        if (index < 0 || index >= size)\n            throw new IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        int num = arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (int j = index; j < size - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size--;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public void extendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        arr = Arrays.copyOf(arr, capacity() * extendRatio);\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        capacity = arr.length;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    public int[] toArray() {\n        int size = size();\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] arr = new int[size];\n        for (int i = 0; i < size; i++) {\n            arr[i] = get(i);\n        }\n        return arr;\n    }\n}\n
my_list.cs
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    private int[] arr;           // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    private int arrCapacity = 10;    // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    private int arrSize = 0;         // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    private readonly int extendRatio = 2;  // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public MyList() {\n        arr = new int[arrCapacity];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    public int Size() {\n        return arrSize;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public int Capacity() {\n        return arrCapacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    public int Get(int index) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        return arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public void Set(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    public void Add(int num) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        arr[arrSize] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize++;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    public void Insert(int index, int num) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (arrSize == arrCapacity)\n            ExtendCapacity();\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (int j = arrSize - 1; j >= index; j--) {\n            arr[j + 1] = arr[j];\n        }\n        arr[index] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize++;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public int Remove(int index) {\n        if (index < 0 || index >= arrSize)\n            throw new IndexOutOfRangeException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\");\n        int num = arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (int j = index; j < arrSize - 1; j++) {\n            arr[j] = arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        arrSize--;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public void ExtendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 arrCapacity * extendRatio \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        Array.Resize(ref arr, arrCapacity * extendRatio);\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        arrCapacity = arr.Length;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    public int[] ToArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] arr = new int[arrSize];\n        for (int i = 0; i < arrSize; i++) {\n            arr[i] = Get(i);\n        }\n        return arr;\n    }\n}\n
my_list.go
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype myList struct {\n    arrCapacity int\n    arr         []int\n    arrSize     int\n    extendRatio int\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newMyList() *myList {\n    return &myList{\n        arrCapacity: 10,              // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        arr:         make([]int, 10), // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n        arrSize:     0,               // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n        extendRatio: 2,               // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n    }\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfunc (l *myList) size() int {\n    return l.arrSize\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\nfunc (l *myList) capacity() int {\n    return l.arrCapacity\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nfunc (l *myList) get(index int) int {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n    }\n    return l.arr[index]\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nfunc (l *myList) set(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n    }\n    l.arr[index] = num\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\nfunc (l *myList) add(num int) {\n    // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    l.arr[l.arrSize] = num\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    l.arrSize++\n}\n\n/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nfunc (l *myList) insert(num, index int) {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n    }\n    // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if l.arrSize == l.arrCapacity {\n        l.extendCapacity()\n    }\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for j := l.arrSize - 1; j >= index; j-- {\n        l.arr[j+1] = l.arr[j]\n    }\n    l.arr[index] = num\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    l.arrSize++\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nfunc (l *myList) remove(index int) int {\n    if index < 0 || index >= l.arrSize {\n        panic(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n    }\n    num := l.arr[index]\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for j := index; j < l.arrSize-1; j++ {\n        l.arr[j] = l.arr[j+1]\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    l.arrSize--\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    return num\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\nfunc (l *myList) extendCapacity() {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    l.arr = append(l.arr, make([]int, l.arrCapacity*(l.extendRatio-1))...)\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    l.arrCapacity = len(l.arr)\n}\n\n/* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b */\nfunc (l *myList) toArray() []int {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    return l.arr[:l.arrSize]\n}\n
my_list.swift
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    private var arr: [Int] // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    private var _capacity: Int // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    private var _size: Int // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    private let extendRatio: Int // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init() {\n        _capacity = 10\n        _size = 0\n        extendRatio = 2\n        arr = Array(repeating: 0, count: _capacity)\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    func capacity() -> Int {\n        _capacity\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    func get(index: Int) -> Int {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if index < 0 || index >= size() {\n            fatalError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        }\n        return arr[index]\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    func set(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        }\n        arr[index] = num\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    func add(num: Int) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if size() == capacity() {\n            extendCapacity()\n        }\n        arr[size()] = num\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        _size += 1\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    func insert(index: Int, num: Int) {\n        if index < 0 || index >= size() {\n            fatalError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        }\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if size() == capacity() {\n            extendCapacity()\n        }\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for j in (index ..< size()).reversed() {\n            arr[j + 1] = arr[j]\n        }\n        arr[index] = num\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        _size += 1\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    @discardableResult\n    func remove(index: Int) -> Int {\n        if index < 0 || index >= size() {\n            fatalError(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        }\n        let num = arr[index]\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for j in index ..< (size() - 1) {\n            arr[j] = arr[j + 1]\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        _size -= 1\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    func extendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        arr = arr + Array(repeating: 0, count: capacity() * (extendRatio - 1))\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        _capacity = arr.count\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    func toArray() -> [Int] {\n        Array(arr.prefix(size()))\n    }\n}\n
my_list.js
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    #arr = new Array(); // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    #capacity = 10; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    #size = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    #extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.#arr = new Array(this.#capacity);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    size() {\n        return this.#size;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    capacity() {\n        return this.#capacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    get(index) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= this.#size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        return this.#arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    set(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        this.#arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    add(num) {\n        // \u0415\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430\n        this.#arr[this.#size] = num;\n        this.#size++;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    insert(index, num) {\n        if (index < 0 || index >= this.#size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.#size === this.#capacity) {\n            this.extendCapacity();\n        }\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (let j = this.#size - 1; j >= index; j--) {\n            this.#arr[j + 1] = this.#arr[j];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this.#arr[index] = num;\n        this.#size++;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    remove(index) {\n        if (index < 0 || index >= this.#size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        let num = this.#arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (let j = index; j < this.#size - 1; j++) {\n            this.#arr[j] = this.#arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this.#size--;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    extendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        this.#arr = this.#arr.concat(\n            new Array(this.capacity() * (this.#extendRatio - 1))\n        );\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        this.#capacity = this.#arr.length;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    toArray() {\n        let size = this.size();\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
my_list.ts
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    private arr: Array<number>; // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    private _capacity: number = 10; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    private _size: number = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    private extendRatio: number = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.arr = new Array(this._capacity);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    public size(): number {\n        return this._size;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public capacity(): number {\n        return this._capacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    public get(index: number): number {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= this._size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        return this.arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public set(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        this.arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    public add(num: number): void {\n        // \u0415\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this._size === this._capacity) this.extendCapacity();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430\n        this.arr[this._size] = num;\n        this._size++;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    public insert(index: number, num: number): void {\n        if (index < 0 || index >= this._size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this._size === this._capacity) {\n            this.extendCapacity();\n        }\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (let j = this._size - 1; j >= index; j--) {\n            this.arr[j + 1] = this.arr[j];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this.arr[index] = num;\n        this._size++;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    public remove(index: number): number {\n        if (index < 0 || index >= this._size) throw new Error('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n        let num = this.arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (let j = index; j < this._size - 1; j++) {\n            this.arr[j] = this.arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this._size--;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public extendCapacity(): void {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 size \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        this.arr = this.arr.concat(\n            new Array(this.capacity() * (this.extendRatio - 1))\n        );\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        this._capacity = this.arr.length;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    public toArray(): number[] {\n        let size = this.size();\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const arr = new Array(size);\n        for (let i = 0; i < size; i++) {\n            arr[i] = this.get(i);\n        }\n        return arr;\n    }\n}\n
my_list.dart
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n  late List<int> _arr; // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n  int _capacity = 10; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n  int _size = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n  int _extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  MyList() {\n    _arr = List.filled(_capacity, 0);\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n  int size() => _size;\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n  int capacity() => _capacity;\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n  int get(int index) {\n    if (index >= _size) throw RangeError('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n    return _arr[index];\n  }\n\n  /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n  void set(int index, int _num) {\n    if (index >= _size) throw RangeError('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n    _arr[index] = _num;\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n  void add(int _num) {\n    // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (_size == _capacity) extendCapacity();\n    _arr[_size] = _num;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    _size++;\n  }\n\n  /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n  void insert(int index, int _num) {\n    if (index >= _size) throw RangeError('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n    // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (_size == _capacity) extendCapacity();\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for (var j = _size - 1; j >= index; j--) {\n      _arr[j + 1] = _arr[j];\n    }\n    _arr[index] = _num;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    _size++;\n  }\n\n  /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n  int remove(int index) {\n    if (index >= _size) throw RangeError('\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b');\n    int _num = _arr[index];\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for (var j = index; j < _size - 1; j++) {\n      _arr[j] = _arr[j + 1];\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    _size--;\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    return _num;\n  }\n\n  /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n  void extendCapacity() {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 _extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    final _newNums = List.filled(_capacity * _extendRatio, 0);\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432 \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    List.copyRange(_newNums, 0, _arr);\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 _arr\n    _arr = _newNums;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    _capacity = _arr.length;\n  }\n\n  /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n  List<int> toArray() {\n    List<int> arr = [];\n    for (var i = 0; i < _size; i++) {\n      arr.add(get(i));\n    }\n    return arr;\n  }\n}\n
my_list.rs
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[allow(dead_code)]\nstruct MyList {\n    arr: Vec<i32>,       // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    capacity: usize,     // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    size: usize,         // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    extend_ratio: usize, // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n}\n\n#[allow(unused, unused_comparisons)]\nimpl MyList {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    pub fn new(capacity: usize) -> Self {\n        let mut vec = vec![0; capacity];\n        Self {\n            arr: vec,\n            capacity,\n            size: 0,\n            extend_ratio: 2,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    pub fn size(&self) -> usize {\n        return self.size;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    pub fn capacity(&self) -> usize {\n        return self.capacity;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    pub fn get(&self, index: usize) -> i32 {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if index >= self.size {\n            panic!(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        };\n        return self.arr[index];\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    pub fn set(&mut self, index: usize, num: i32) {\n        if index >= self.size {\n            panic!(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        };\n        self.arr[index] = num;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    pub fn add(&mut self, num: i32) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        self.arr[self.size] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        self.size += 1;\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    pub fn insert(&mut self, index: usize, num: i32) {\n        if index >= self.size() {\n            panic!(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        };\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.size == self.capacity() {\n            self.extend_capacity();\n        }\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for j in (index..self.size).rev() {\n            self.arr[j + 1] = self.arr[j];\n        }\n        self.arr[index] = num;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        self.size += 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    pub fn remove(&mut self, index: usize) -> i32 {\n        if index >= self.size() {\n            panic!(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        };\n        let num = self.arr[index];\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for j in index..self.size - 1 {\n            self.arr[j] = self.arr[j + 1];\n        }\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        self.size -= 1;\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num;\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    pub fn extend_capacity(&mut self) {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extend_ratio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        let new_capacity = self.capacity * self.extend_ratio;\n        self.arr.resize(new_capacity, 0);\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        self.capacity = new_capacity;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    pub fn to_array(&self) -> Vec<i32> {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        let mut arr = Vec::new();\n        for i in 0..self.size {\n            arr.push(self.get(i));\n        }\n        arr\n    }\n}\n
my_list.c
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct {\n    int *arr;        // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    int capacity;    // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    int size;        // \u0420\u0430\u0437\u043c\u0435\u0440 \u0441\u043f\u0438\u0441\u043a\u0430\n    int extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u0438\n} MyList;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nMyList *newMyList() {\n    MyList *nums = malloc(sizeof(MyList));\n    nums->capacity = 10;\n    nums->arr = malloc(sizeof(int) * nums->capacity);\n    nums->size = 0;\n    nums->extendRatio = 2;\n    return nums;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delMyList(MyList *nums) {\n    free(nums->arr);\n    free(nums);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 */\nint size(MyList *nums) {\n    return nums->size;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\nint capacity(MyList *nums) {\n    return nums->capacity;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\nint get(MyList *nums, int index) {\n    assert(index >= 0 && index < nums->size);\n    return nums->arr[index];\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\nvoid set(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < nums->size);\n    nums->arr[index] = num;\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\nvoid add(MyList *nums, int num) {\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u0438\n    }\n    nums->arr[size(nums)] = num;\n    nums->size++;\n}\n\n/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\nvoid insert(MyList *nums, int index, int num) {\n    assert(index >= 0 && index < size(nums));\n    // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (size(nums) == capacity(nums)) {\n        extendCapacity(nums); // \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u0438\n    }\n    for (int i = size(nums); i > index; --i) {\n        nums->arr[i] = nums->arr[i - 1];\n    }\n    nums->arr[index] = num;\n    nums->size++;\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n// \u0412\u043d\u0438\u043c\u0430\u043d\u0438\u0435: stdio.h \u0443\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e remove\nint removeItem(MyList *nums, int index) {\n    assert(index >= 0 && index < size(nums));\n    int num = nums->arr[index];\n    for (int i = index; i < size(nums) - 1; i++) {\n        nums->arr[i] = nums->arr[i + 1];\n    }\n    nums->size--;\n    return num;\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\nvoid extendCapacity(MyList *nums) {\n    // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    int newCapacity = capacity(nums) * nums->extendRatio;\n    int *extend = (int *)malloc(sizeof(int) * newCapacity);\n    int *temp = nums->arr;\n\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043d\u043e\u0432\u044b\u0435\n    for (int i = 0; i < size(nums); i++)\n        extend[i] = nums->arr[i];\n\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    free(temp);\n\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    nums->arr = extend;\n    nums->capacity = newCapacity;\n}\n\n/* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nint *toArray(MyList *nums) {\n    return nums->arr;\n}\n
my_list.kt
/* \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass MyList {\n    private var arr: IntArray = intArrayOf() // \u041c\u0430\u0441\u0441\u0438\u0432 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430)\n    private var capacity: Int = 10 // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    private var size: Int = 0 // \u0414\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n    private var extendRatio: Int = 2 // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init {\n        arr = IntArray(capacity)\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\n    fun size(): Int {\n        return size\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    fun capacity(): Int {\n        return capacity\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 */\n    fun get(index: Int): Int {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        return arr[index]\n    }\n\n    /* \u041e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    fun set(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        arr[index] = num\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 */\n    fun add(num: Int) {\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size == capacity())\n            extendCapacity()\n        arr[size] = num\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size++\n    }\n\n    /* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 */\n    fun insert(index: Int, num: Int) {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        // \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (size == capacity())\n            extendCapacity()\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        for (j in size - 1 downTo index)\n            arr[j + 1] = arr[j]\n        arr[index] = num\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size++\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 */\n    fun remove(index: Int): Int {\n        if (index < 0 || index >= size)\n            throw IndexOutOfBoundsException(\"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\")\n        val num = arr[index]\n        // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n        for (j in index..<size - 1)\n            arr[j] = arr[j + 1]\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        size--\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n        return num\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 */\n    fun extendCapacity() {\n        // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extendRatio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n        arr = arr.copyOf(capacity() * extendRatio)\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n        capacity = arr.size\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    fun toArray(): IntArray {\n        val size = size()\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        val arr = IntArray(size)\n        for (i in 0..<size) {\n            arr[i] = get(i)\n        }\n        return arr\n    }\n}\n
my_list.rb
=begin\nFile: my_list.rb\nCreated Time: 2024-03-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041a\u043b\u0430\u0441\u0441 \u0441\u043f\u0438\u0441\u043a\u0430 ###\nclass MyList\n  attr_reader :size       # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u043f\u0438\u0441\u043a\u0430 (\u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n  attr_reader :capacity   # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\n  def get(index)\n    # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u044b\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435; \u0434\u0430\u043b\u0435\u0435 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\n    raise IndexError, \"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 ###\n  def set(index, num)\n    raise IndexError, \"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 ###\n  def add(num)\n    # \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    @size += 1\n  end\n\n  # ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 ###\n  def insert(index, num)\n    raise IndexError, \"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\" if index < 0 || index >= size\n\n    # \u041f\u0440\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0438\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    extend_capacity if size == capacity\n\n    # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438 \u0432\u0441\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    for j in (size - 1).downto(index)\n      @arr[j + 1] = @arr[j]\n    end\n    @arr[index] = num\n\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    @size += 1\n  end\n\n  # ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 ###\n  def remove(index)\n    raise IndexError, \"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\" if index < 0 || index >= size\n    num = @arr[index]\n\n    # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0435\u0440\u0435\u0434\n    for j in index...size\n      @arr[j] = @arr[j + 1]\n    end\n\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    @size -= 1\n\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    num\n  end\n\n  # ## \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 ###\n  def extend_capacity\n    # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0432 extend_ratio \u0440\u0430\u0437 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    arr = @arr.dup + Array.new(capacity * (@extend_ratio - 1))\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\n    @capacity = arr.length\n  end\n\n  # ## \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 ###\n  def to_array\n    sz = size\n    # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    arr = Array.new(sz)\n    for i in 0...sz\n      arr[i] = get(i)\n    end\n    arr\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_array_and_linkedlist/ram_and_cache/","title":"4.4 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043a\u044d\u0448 *","text":"

\u0412 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445 \u044d\u0442\u043e\u0439 \u0433\u043b\u0430\u0432\u044b \u043c\u044b \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 - \u0434\u0432\u0435 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0438 \u0432\u0430\u0436\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0434\u0432\u0435 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f: \"\u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\" \u0438 \"\u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435\".

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0432\u043e \u043c\u043d\u043e\u0433\u043e\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043a\u044d\u0448, \u0430 \u044d\u0442\u043e, \u0432 \u0441\u0432\u043e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043e\u0431\u0449\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.

"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#441","title":"4.4.1 \u00a0 \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435","text":"

\u0412 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445: \u0436\u0435\u0441\u0442\u043a\u0438\u0439 \u0434\u0438\u0441\u043a (hard disk) , \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c (random-access memory, RAM) \u0438 \u043a\u044d\u0448-\u043f\u0430\u043c\u044f\u0442\u044c (cache memory) . \u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 4-2 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0438\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0440\u043e\u043b\u0438 \u0438 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 4-2 \u00a0 \u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435

\u0416\u0435\u0441\u0442\u043a\u0438\u0439 \u0434\u0438\u0441\u043a \u041e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u041a\u044d\u0448 \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0414\u043e\u043b\u0433\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u041e\u0421, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u0444\u0430\u0439\u043b\u044b \u0438 \u0442.\u0434. \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u044e\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 CPU \u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u042d\u043d\u0435\u0440\u0433\u043e\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0414\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0442\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u0438\u0442\u0430\u043d\u0438\u044f \u0414\u0430\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u0438\u0442\u0430\u043d\u0438\u044f \u0414\u0430\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u044f\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043f\u0438\u0442\u0430\u043d\u0438\u044f \u0415\u043c\u043a\u043e\u0441\u0442\u044c \u0411\u043e\u043b\u044c\u0448\u0430\u044f, \u0443\u0440\u043e\u0432\u0435\u043d\u044c TB \u041c\u0435\u043d\u044c\u0448\u0430\u044f, \u0443\u0440\u043e\u0432\u0435\u043d\u044c GB \u041e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u0430\u044f, \u0443\u0440\u043e\u0432\u0435\u043d\u044c MB \u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u041d\u0438\u0437\u043a\u0430\u044f, \u043e\u0442 \u0441\u043e\u0442\u0435\u043d \u0434\u043e \u0442\u044b\u0441\u044f\u0447 MB/s \u0412\u044b\u0441\u043e\u043a\u0430\u044f, \u0434\u0435\u0441\u044f\u0442\u043a\u0438 GB/s \u041e\u0447\u0435\u043d\u044c \u0432\u044b\u0441\u043e\u043a\u0430\u044f, \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 GB/s \u0426\u0435\u043d\u0430 (\u044e\u0430\u043d\u0438) \u0414\u0435\u0448\u0435\u0432\u044b\u0439, \u043e\u0442 \u0434\u043e\u043b\u0435\u0439 \u044e\u0430\u043d\u044f \u0434\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u044e\u0430\u043d\u0435\u0439 \u0437\u0430 GB \u0414\u043e\u0440\u043e\u0433\u0430\u044f, \u0434\u0435\u0441\u044f\u0442\u043a\u0438 \u0438 \u0441\u043e\u0442\u043d\u0438 \u044e\u0430\u043d\u0435\u0439 \u0437\u0430 GB \u041e\u0447\u0435\u043d\u044c \u0434\u043e\u0440\u043e\u0433\u043e\u0439, \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0443\u043f\u0430\u043a\u043e\u0432\u043a\u0438 CPU

\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u044b, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-9. \u0427\u0435\u043c \u0431\u043b\u0438\u0436\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u044b, \u0442\u0435\u043c \u043e\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0442\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u0435\u0433\u043e \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0438 \u0442\u0435\u043c \u0432\u044b\u0448\u0435 \u0435\u0433\u043e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c. \u0422\u0430\u043a\u0430\u044f \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0430\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u043d\u0435 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e, \u0430 \u0441\u0442\u0430\u043b\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0442\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441\u043e\u0432.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-9 \u00a0 \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430

Tip

\u0418\u0435\u0440\u0430\u0440\u0445\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0442\u043e\u043d\u043a\u0438\u0439 \u0431\u0430\u043b\u0430\u043d\u0441 \u043c\u0435\u0436\u0434\u0443 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c\u044e, \u0435\u043c\u043a\u043e\u0441\u0442\u044c\u044e \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0432\u043e \u0432\u0441\u0435\u0445 \u043e\u0442\u0440\u0430\u0441\u043b\u044f\u0445 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u0438\u0438: \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043a\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0431\u0430\u043b\u0430\u043d\u0441 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430\u043c\u0438 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438.

\u0412 \u0438\u0442\u043e\u0433\u0435 \u0436\u0435\u0441\u0442\u043a\u0438\u0439 \u0434\u0438\u0441\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0434\u043e\u043b\u0433\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c - \u0434\u043b\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441, \u0430 \u043a\u044d\u0448 - \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u044f\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c. \u0412\u0441\u0435 \u0442\u0440\u0438 \u0443\u0440\u043e\u0432\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044e\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 4-10, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f \u0441 \u0436\u0435\u0441\u0442\u043a\u043e\u0433\u043e \u0434\u0438\u0441\u043a\u0430 \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f CPU \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445. \u041a\u044d\u0448 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u044c CPU: \u043e\u043d \u0438\u043d\u0442\u0435\u043b\u043b\u0435\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044f CPU \u0432\u044b\u0441\u043e\u043a\u043e\u0441\u043a\u043e\u0440\u043e\u0441\u0442\u043d\u043e\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u044f\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0439 RAM.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 4-10 \u00a0 \u041f\u043e\u0442\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u0436\u0434\u0443 \u0436\u0435\u0441\u0442\u043a\u0438\u043c \u0434\u0438\u0441\u043a\u043e\u043c, RAM \u0438 \u043a\u044d\u0448\u0435\u043c

"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#442","title":"4.4.2 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0438\u043c\u0435\u044e\u0442 \u0441\u0432\u043e\u0438 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.

\u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430, \u0438 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435. \u042d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u043f\u043b\u043e\u0442\u043d\u043e \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438 \u0441\u043f\u0438\u0441\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043f\u043e\u0442\u0435\u0440\u044f\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u0430 \u0435\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0442\u0440\u0430\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \"\u043f\u043e \u0443\u0437\u043b\u0430\u043c\", \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c.

\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043f\u0440\u0438 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u0441\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e\u0439, \u0447\u0442\u043e \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u041c\u0430\u0441\u0441\u0438\u0432\u044b \u0438\u0437-\u0437\u0430 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u0435\u0435 \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d\u044b \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438, \u0438 \u0447\u0430\u0441\u0442\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043b\u0435\u0433\u0447\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.

"},{"location":"chapter_array_and_linkedlist/ram_and_cache/#443","title":"4.4.3 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u044d\u0448\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u0425\u043e\u0442\u044f \u043f\u043e \u043e\u0431\u044a\u0435\u043c\u0443 \u043a\u044d\u0448 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043e\u043d \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438 \u0438\u0433\u0440\u0430\u0435\u0442 \u043a\u0440\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0430\u0436\u043d\u0443\u044e \u0440\u043e\u043b\u044c \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u044a\u0435\u043c \u043a\u044d\u0448\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d \u0438 \u0432 \u043d\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0434\u043e\u043b\u044e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0433\u0434\u0430 CPU \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432 \u043a\u044d\u0448\u0435 \u043d\u0435\u0442, \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0440\u043e\u043c\u0430\u0445 \u043a\u044d\u0448\u0430 (cache miss) , \u0438 CPU \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u043d\u0443\u0436\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.

\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0447\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \"\u043f\u0440\u043e\u043c\u0430\u0445\u043e\u0432 \u043a\u044d\u0448\u0430\", \u0442\u0435\u043c \u0432\u044b\u0448\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0447\u0442\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 CPU, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0442\u0435\u043c \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0414\u043e\u043b\u044e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 CPU \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043a\u044d\u0448\u0430, \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u043c \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0432 \u043a\u044d\u0448 (cache hit rate) ; \u044d\u0442\u043e\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043a\u044d\u0448\u0430.

\u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u043a\u044d\u0448 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u044d\u0448, \u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u0445.

\u0412 \u0446\u0435\u043b\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438\u043c\u0435\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u0432 \u043a\u044d\u0448, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u043d\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u044f\u0442 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0447\u0430\u0441\u0442\u043e \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0438\u0442\u0435\u043b\u044c\u043d\u0435\u0435.

\u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043a\u044d\u0448\u0430 \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432\u043e \u0432\u0441\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043b\u0443\u0447\u0448\u0435 \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432. \u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0432\u044b\u0431\u043e\u0440 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u043c\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \"\u0441\u0442\u0435\u043a\u0430\" (\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u044d\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0433\u043b\u0430\u0432\u0435), \u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u043e\u043d\u0438 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432.

"},{"location":"chapter_array_and_linkedlist/summary/","title":"4.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_array_and_linkedlist/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_array_and_linkedlist/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u0412\u043b\u0438\u044f\u0435\u0442 \u043b\u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0441\u0442\u0435\u043a\u0435 \u0438\u043b\u0438 \u0432 \u043a\u0443\u0447\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c?

\u041c\u0430\u0441\u0441\u0438\u0432\u044b, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0438 \u0432 \u0441\u0442\u0435\u043a\u0435, \u0438 \u0432 \u043a\u0443\u0447\u0435, \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0443 \u043d\u0438\u0445 \u0432 \u0446\u0435\u043b\u043e\u043c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u0443 \u0441\u0442\u0435\u043a\u0430 \u0438 \u043a\u0443\u0447\u0438 \u0435\u0441\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u0438\u0437-\u0437\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f.

  1. \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f: \u0441\u0442\u0435\u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432 \u043d\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c; \u043a\u0443\u0447\u0430 \u0436\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0437 \u043a\u043e\u0434\u0430 \u0438 \u043b\u0435\u0433\u0447\u0435 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0432 \u0441\u0442\u0435\u043a\u0435.
  2. \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430: \u043e\u0431\u044a\u0435\u043c \u0441\u0442\u0435\u043a\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u0432\u0435\u043b\u0438\u043a, \u0430 \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u0443\u0447\u0430 \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.
  3. \u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c: \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0441\u0442\u0435\u043a\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0446\u0438\u0438, \u0430 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u044b\u043b\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0430 \u0434\u043b\u044f \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u044d\u0442\u043e \u043d\u0435 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u0435\u0442\u0441\u044f?

\u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0443\u0437\u043b\u043e\u0432, \u0430 \u0443\u0437\u043b\u044b \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u0447\u0435\u0440\u0435\u0437 \u0441\u0441\u044b\u043b\u043a\u0438 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u0437\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 int , double , string , object \u0438 \u0442.\u0434.

\u041d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430, \u0438\u043d\u0430\u0447\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0447\u0435\u0440\u0435\u0437 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 int \u0438 long , \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 4 \u0431\u0430\u0439\u0442\u0430, \u0430 \u0434\u0440\u0443\u0433\u043e\u0439 - 8 \u0431\u0430\u0439\u0442 ; \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043d\u0438\u0436\u0435 \u0443\u0436\u0435 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0437\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.

# \u0410\u0434\u0440\u0435\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 = \u0430\u0434\u0440\u0435\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 (\u0430\u0434\u0440\u0435\u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430) + \u0434\u043b\u0438\u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 * \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n

Q: \u041f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 P \u043d\u0443\u0436\u043d\u043e \u043b\u0438 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0442\u044c P.next = None ?

\u041c\u043e\u0436\u043d\u043e \u0438 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c P.next . \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u043f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u043e\u0442 \u0433\u043e\u043b\u043e\u0432\u044b \u043a \u0445\u0432\u043e\u0441\u0442\u0443 \u0443\u0437\u0435\u043b P \u0443\u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u0441\u044f. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0443\u0437\u0435\u043b P \u0443\u0436\u0435 \u0443\u0434\u0430\u043b\u0435\u043d \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430, \u0438 \u0442\u043e, \u043a\u0443\u0434\u0430 \u043e\u043d \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e, \u043d\u0430 \u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0432\u043b\u0438\u044f\u0435\u0442.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c, \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0430\u0437\u0440\u044b\u0432\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u043e, \u0435\u0441\u043b\u0438 \u043b\u043e\u0433\u0438\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439. \u041d\u043e \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0440\u0430\u0437\u043e\u0440\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0435\u0435 \u0438 \u043b\u043e\u0433\u0438\u0447\u043d\u0435\u0435. \u0415\u0441\u043b\u0438 \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u043d, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u043c\u0435\u0448\u0430\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044e \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0443\u0437\u043b\u043e\u0432.

Q: \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) . \u041d\u043e \u0434\u043e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0435\u0449\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0442\u0440\u0430\u0442\u0438\u0442\u044c \\(O(n)\\) \u043d\u0430 \u043f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u043e\u0433\u0434\u0430 \u043e\u0431\u0449\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0435 \\(O(n)\\) ?

\u0415\u0441\u043b\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0441\u043a\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0430 \u043f\u043e\u0442\u043e\u043c \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0435\u0433\u043e, \u0442\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \\(O(n)\\) . \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0441 \\(O(1)\\) \u0432\u0441\u0442\u0430\u0432\u043a\u043e\u0439 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435: \u043c\u044b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u0433\u043e\u043b\u043e\u0432\u0443 \u0438 \u0445\u0432\u043e\u0441\u0442, \u0438 \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u0436\u0434\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \\(O(1)\\) .

Q: \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \"\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f\" \u0441\u0432\u0435\u0442\u043b\u043e-\u0433\u043e\u043b\u0443\u0431\u043e\u0439 \u0431\u043b\u043e\u043a \u0441 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u043c \u0443\u0437\u043b\u0430 - \u044d\u0442\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441 \u043f\u0430\u043c\u044f\u0442\u0438? \u0418\u043b\u0438 \u043e\u043d \u0434\u0435\u043b\u0438\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u043f\u043e\u043b\u0430\u043c \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0443\u0437\u043b\u0430?

\u042d\u0442\u043e\u0442 \u0440\u0438\u0441\u0443\u043d\u043e\u043a \u0434\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435; \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u0441\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439.

Q: \u0412\u0441\u0435\u0433\u0434\u0430 \u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\) ?

\u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442\u0441\u044f, \u0442\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a, \u0430 \u0443\u0436\u0435 \u0437\u0430\u0442\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442 \u0442\u0443\u0434\u0430 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0438 \u0432 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \\(O(n)\\) .

Q: \u0412 \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0438 \"\u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u044b\u0441\u0438\u043b\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043f\u043e\u0442\u0435\u0440\u0435 \u0447\u0430\u0441\u0442\u0438 \u043f\u0430\u043c\u044f\u0442\u0438\" \u043f\u043e\u0434 \u043f\u043e\u0442\u0435\u0440\u044f\u043c\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0434 \u0442\u0430\u043a\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u043a\u0430\u043a \u0435\u043c\u043a\u043e\u0441\u0442\u044c, \u0434\u043b\u0438\u043d\u0430 \u0438 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f?

\u041f\u043e\u0442\u0435\u0440\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0437\u0434\u0435\u0441\u044c \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0438\u043c\u0435\u044e\u0442 \u0434\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f: \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0435\u043c\u043a\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0446\u0435\u043b\u0438\u043a\u043e\u043c; \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0447\u0430\u0441\u0442\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439, \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u043c\u043d\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \\(\\times 1.5\\) . \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u043f\u0443\u0441\u0442\u044b\u0445 \u0441\u043b\u043e\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c.

Q: \u0412 Python \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 n = [1, 2, 3] \u0430\u0434\u0440\u0435\u0441\u0430 \u044d\u0442\u0438\u0445 \u0442\u0440\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u043c\u0438, \u043d\u043e \u043f\u043e\u0441\u043b\u0435 m = [2, 1, 3] \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e id \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u0438\u0434\u0443\u0442 \u043f\u043e\u0434\u0440\u044f\u0434, \u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0438\u0437 n . \u0415\u0441\u043b\u0438 \u0430\u0434\u0440\u0435\u0441\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043b\u0438 m \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c?

\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0443\u0437\u043b\u044b n = [n1, n2, n3, n4, n5] . \u041e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u0438 5 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432-\u0443\u0437\u043b\u043e\u0432 \u0442\u043e\u0436\u0435 \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u0430\u043c\u044f\u0442\u0438. \u041e\u0434\u043d\u0430\u043a\u043e, \u0438\u043c\u0435\u044f \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043f\u0438\u0441\u043a\u0430, \u043c\u044b \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u0435\u043c \u0437\u0430 \\(O(1)\\) \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0430\u0434\u0440\u0435\u0441 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u043c\u0443. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0443\u0437\u043b\u044b, \u0430 \u043d\u0435 \u0441\u0430\u043c\u0438 \u0443\u0437\u043b\u044b.

\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u0440\u0443\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u043e\u0432, \u0432 Python \u0434\u0430\u0436\u0435 \u0447\u0438\u0441\u043b\u0430 \u043e\u0431\u0435\u0440\u043d\u0443\u0442\u044b \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u0438 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u0438 \u0447\u0438\u0441\u043b\u0430, \u0430 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043d\u0438\u0445. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0435\u043c, \u0447\u0442\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u0434\u0432\u0443\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 id , \u0430 \u0430\u0434\u0440\u0435\u0441\u0430 \u044d\u0442\u0438\u0445 \u0447\u0438\u0441\u0435\u043b \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u043d\u044b \u0431\u044b\u0442\u044c \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u043c\u0438.

Q: \u0412 C++ STL \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a std::list , \u043d\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0430\u0445 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c \u0438\u043c \u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0435 \u0442\u0430\u043a \u0447\u0430\u0441\u0442\u043e. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u043a\u0430\u043a\u0438\u043c\u0438-\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438?

\u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043f\u0440\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u043a \u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u0430\u043c \u043f\u0440\u0438\u0431\u0435\u0433\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438, \u043f\u043e \u0434\u0432\u0443\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c.

\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0441\u043b\u0443\u0447\u0430\u0438, \u043a\u043e\u0433\u0434\u0430 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0432 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\u0445 \u0438 \u0433\u0440\u0430\u0444\u0430\u0445. \u0414\u043b\u044f \u0441\u0442\u0435\u043a\u043e\u0432 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0447\u0430\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u044f\u0437\u044b\u043a\u043e\u043c stack \u0438 queue , \u0430 \u043d\u0435 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e.

Q: \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f res = [[0]] * n \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u041a\u0430\u0436\u0434\u044b\u0439 [0] \u0432 \u043d\u0435\u043c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c?

\u041d\u0435\u0442, \u043e\u043d\u0438 \u043d\u0435 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b. \u0412 \u0442\u0430\u043a\u043e\u043c \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0432\u0441\u0435 [0] \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442. \u0415\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445, \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0438 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.

\u0415\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 [0] \u0431\u044b\u043b \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res = [[0] for _ in range(n)] . \u0412 \u044d\u0442\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \\(n\\) \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432-\u0441\u043f\u0438\u0441\u043a\u043e\u0432 [0] .

Q: \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f res = [0] * n \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a. \u041a\u0430\u0436\u0434\u044b\u0439 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 0 \u0432 \u043d\u0435\u043c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c?

\u0412 \u044d\u0442\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0432\u0441\u0435 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 0 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430\u043c\u0438 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e Python \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043a\u044d\u0448-\u043f\u0443\u043b\u0430 \u0434\u043b\u044f \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b (\u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0442 -5 \u0434\u043e 256), \u0447\u0442\u043e\u0431\u044b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c.

\u0425\u043e\u0442\u044f \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442, \u043c\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u043c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 Python - \u044d\u0442\u043e \"\u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b\". \u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0434\u0440\u0443\u0433\u043e\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u0430 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430.

\u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \"\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b\" (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043f\u0438\u0441\u043a\u0438, \u0441\u043b\u043e\u0432\u0430\u0440\u0438 \u0438\u043b\u0438 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432), \u0442\u043e \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u0440\u044f\u043c\u043e \u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442, \u0438 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0441\u044b\u043b\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043d\u0430 \u043d\u0435\u0433\u043e, \u0443\u0432\u0438\u0434\u044f\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435.

"},{"location":"chapter_backtracking/","title":"\u0413\u043b\u0430\u0432\u0430 13. \u00a0 \u041f\u043e\u0438\u0441\u043a \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c","text":"

Abstract

\u041c\u044b \u0441\u043b\u043e\u0432\u043d\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0432 \u043b\u0430\u0431\u0438\u0440\u0438\u043d\u0442\u0435: \u043d\u0430 \u043f\u0443\u0442\u0438 \u0432\u043f\u0435\u0440\u0435\u0434 \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0442\u044c\u0441\u044f \u0442\u0443\u043f\u0438\u043a\u0438 \u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u0438.

\u0421\u0438\u043b\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043d\u0430\u0447\u0430\u0442\u044c \u0437\u0430\u043d\u043e\u0432\u043e, \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u043d\u043e\u0432\u0430 \u0438 \u0441\u043d\u043e\u0432\u0430 \u0438 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432 \u043d\u0430\u0439\u0442\u0438 \u0432\u044b\u0445\u043e\u0434 \u043a \u0441\u0432\u0435\u0442\u0443.

"},{"location":"chapter_backtracking/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_backtracking/backtracking_algorithm/","title":"13.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c (backtracking algorithm) - \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043f\u0443\u0442\u0435\u043c \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430. \u0415\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0433\u0440\u0443\u0431\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u0438\u043b\u0438 \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0438\u0441\u0447\u0435\u0440\u043f\u0430\u043d\u044b \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b.

\u041e\u0431\u044b\u0447\u043d\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \"\u043f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443\" \u0434\u043b\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439. \u0412 \u0433\u043b\u0430\u0432\u0435 \"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\" \u043c\u044b \u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u044b \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043f\u043e\u0438\u0441\u043a\u0443 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 backtracking \u0438 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u044d\u0442\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

\u041f\u0440\u0438\u043c\u0435\u0440 1

\u0414\u0430\u043d\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0438 \u0437\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(7\\) ; \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u0442\u0438\u0445 \u0443\u0437\u043b\u043e\u0432.

\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0440\u0430\u0432\u043d\u043e \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \\(7\\) ; \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 res . \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-1 \u0438 \u0432 \u043a\u043e\u0434\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_i_compact.py
def pre_order(root: TreeNode):\n    \"\"\"\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1\"\"\"\n    if root is None:\n        return\n    if root.val == 7:\n        # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(root)\n    pre_order(root.left)\n    pre_order(root.right)\n
preorder_traversal_i_compact.cpp
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push_back(root);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
preorder_traversal_i_compact.java
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.add(root);\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n}\n
preorder_traversal_i_compact.cs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.Add(root);\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n
preorder_traversal_i_compact.go
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfunc preOrderI(root *TreeNode, res *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    if (root.Val).(int) == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        *res = append(*res, root)\n    }\n    preOrderI(root.Left, res)\n    preOrderI(root.Right, res)\n}\n
preorder_traversal_i_compact.swift
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    if root.val == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(root)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n
preorder_traversal_i_compact.js
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfunction preOrder(root, res) {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.ts
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfunction preOrder(root: TreeNode | null, res: TreeNode[]): void {\n    if (root === null) {\n        return;\n    }\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push(root);\n    }\n    preOrder(root.left, res);\n    preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.dart
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nvoid preOrder(TreeNode? root, List<TreeNode> res) {\n  if (root == null) {\n    return;\n  }\n  if (root.val == 7) {\n    // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    res.add(root);\n  }\n  preOrder(root.left, res);\n  preOrder(root.right, res);\n}\n
preorder_traversal_i_compact.rs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        if node.borrow().val == 7 {\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n            res.push(node.clone());\n        }\n        pre_order(res, node.borrow().left.as_ref());\n        pre_order(res, node.borrow().right.as_ref());\n    }\n}\n
preorder_traversal_i_compact.c
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res[resSize++] = root;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n}\n
preorder_traversal_i_compact.kt
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    if (root._val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res!!.add(root)\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n}\n
preorder_traversal_i_compact.rb
=begin\nFile: preorder_traversal_i_compact.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 1 ###\ndef pre_order(root)\n  return unless root\n\n  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  $res << root if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-1 \u00a0 \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u043e\u0432 \u043f\u0440\u0438 \u043f\u0440\u044f\u043c\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435

"},{"location":"chapter_backtracking/backtracking_algorithm/#1311","title":"13.1.1 \u00a0 \u041f\u043e\u043f\u044b\u0442\u043a\u0430 \u0438 \u043e\u0442\u043a\u0430\u0442","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f backtracking, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u0438\u0441\u043a\u0435 \u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \"\u043f\u043e\u043f\u044b\u0442\u043a\u0430\" \u0438 \"\u043e\u0442\u043a\u0430\u0442\". \u041a\u043e\u0433\u0434\u0430 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u0438\u043b\u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043e\u043d \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0432\u044b\u0431\u043e\u0440, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u043d\u043d\u0435\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u0438 \u043f\u0440\u043e\u0431\u0443\u0435\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b.

\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 1 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \"\u043f\u043e\u043f\u044b\u0442\u043a\u0443\", \u0430 \u043f\u0440\u043e\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0438\u043b\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044e \u0447\u0435\u0440\u0435\u0437 return \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \"\u043e\u0442\u043a\u0430\u0442\".

\u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043e\u0442\u043a\u0430\u0442 \u043d\u0435 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043a \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0443 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u044d\u0442\u043e, \u0441\u043b\u0435\u0433\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 1.

\u041f\u0440\u0438\u043c\u0435\u0440 2

\u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(7\\) \u0438 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043f\u0443\u0442\u0438 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u044d\u0442\u0438\u0445 \u0443\u0437\u043b\u043e\u0432.

\u0412\u0437\u044f\u0432 \u0437\u0430 \u043e\u0441\u043d\u043e\u0432\u0443 \u043a\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 1, \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a path \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u0443\u0442\u0438 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(7\\) , \u043c\u044b \u043a\u043e\u043f\u0438\u0440\u0443\u0435\u043c path \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0435\u0433\u043e \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 res . \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0438\u043c\u0435\u043d\u043d\u043e res \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_ii_compact.py
def pre_order(root: TreeNode):\n    \"\"\"\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2\"\"\"\n    if root is None:\n        return\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.append(root)\n    if root.val == 7:\n        # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u041e\u0442\u043a\u0430\u0442\n    path.pop()\n
preorder_traversal_ii_compact.cpp
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop_back();\n}\n
preorder_traversal_ii_compact.java
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nvoid preOrder(TreeNode root) {\n    if (root == null) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.add(root);\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.remove(path.size() - 1);\n}\n
preorder_traversal_ii_compact.cs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.Add(root);\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_ii_compact.go
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfunc preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    if root == nil {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderII(root.Left, res, path)\n    preOrderII(root.Right, res, path)\n    // \u041e\u0442\u043a\u0430\u0442\n    *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_ii_compact.swift
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.append(root)\n    if root.val == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u041e\u0442\u043a\u0430\u0442\n    path.removeLast()\n}\n
preorder_traversal_ii_compact.js
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfunction preOrder(root, path, res) {\n    if (root === null) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push(root);\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop();\n}\n
preorder_traversal_ii_compact.ts
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    if (root === null) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push(root);\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop();\n}\n
preorder_traversal_ii_compact.dart
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null) {\n    return;\n  }\n\n  // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n  path.add(root);\n  if (root.val == 7) {\n    // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u041e\u0442\u043a\u0430\u0442\n  path.removeLast();\n}\n
preorder_traversal_ii_compact.rs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    if root.is_none() {\n        return;\n    }\n    if let Some(node) = root {\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u041e\u0442\u043a\u0430\u0442\n        path.pop();\n    }\n}\n
preorder_traversal_ii_compact.c
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nvoid preOrder(TreeNode *root) {\n    if (root == NULL) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        for (int i = 0; i < pathSize; ++i) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u041e\u0442\u043a\u0430\u0442\n    pathSize--;\n}\n
preorder_traversal_ii_compact.kt
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u041e\u0442\u043a\u0430\u0442\n    path!!.removeAt(path!!.size - 1)\n}\n
preorder_traversal_ii_compact.rb
=begin\nFile: preorder_traversal_ii_compact.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 2 ###\ndef pre_order(root)\n  return unless root\n\n  # \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n  $path << root\n\n  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u041e\u0442\u043a\u0430\u0442\n  $path.pop\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412 \u043a\u0430\u0436\u0434\u043e\u0439 \"\u043f\u043e\u043f\u044b\u0442\u043a\u0435\" \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 path , \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0443\u0442\u044c; \u0430 \u043f\u0435\u0440\u0435\u0434 \"\u043e\u0442\u043a\u0430\u0442\u043e\u043c\" \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u0438\u0437 path , \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0432\u0448\u0435\u0435 \u0434\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u0438.

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u043d\u044b\u0439 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-2, \u0442\u043e \u043f\u043e\u043f\u044b\u0442\u043a\u0443 \u0438 \u043e\u0442\u043a\u0430\u0442 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u0430\u043a \"\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u0432\u043f\u0435\u0440\u0435\u0434\" \u0438 \"\u043e\u0442\u043c\u0435\u043d\u0443\": \u044d\u0442\u043e \u0434\u0432\u0430 \u0432\u0437\u0430\u0438\u043c\u043d\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.

<1><2><3><4><5><6><7><8><9><10><11>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-2 \u00a0 \u041f\u043e\u043f\u044b\u0442\u043a\u0430 \u0438 \u043e\u0442\u043a\u0430\u0442

"},{"location":"chapter_backtracking/backtracking_algorithm/#1312","title":"13.1.2 \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430","text":"

\u0421\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 backtracking \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043e\u0434\u043d\u043e \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \"\u043e\u0431\u0440\u0435\u0437\u043a\u0438\".

\u041f\u0440\u0438\u043c\u0435\u0440 3

\u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(7\\) , \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u043f\u0443\u0442\u0438 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u044d\u0442\u0438\u0445 \u0443\u0437\u043b\u043e\u0432, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u0443\u0442\u044c \u043d\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0443\u0437\u043b\u044b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(3\\).

\u0427\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u044d\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u0440\u0435\u0437\u043a\u0438: \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430, \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443\u0437\u0435\u043b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(3\\) , \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0438 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0438\u0439 \u043f\u043e\u0438\u0441\u043a. \u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_compact.py
def pre_order(root: TreeNode):\n    \"\"\"\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3\"\"\"\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if root is None or root.val == 3:\n        return\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.append(root)\n    if root.val == 7:\n        # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(list(path))\n    pre_order(root.left)\n    pre_order(root.right)\n    # \u041e\u0442\u043a\u0430\u0442\n    path.pop()\n
preorder_traversal_iii_compact.cpp
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid preOrder(TreeNode *root) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root == nullptr || root->val == 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push_back(root);\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push_back(path);\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop_back();\n}\n
preorder_traversal_iii_compact.java
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid preOrder(TreeNode root) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.add(root);\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.add(new ArrayList<>(path));\n    }\n    preOrder(root.left);\n    preOrder(root.right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.remove(path.size() - 1);\n}\n
preorder_traversal_iii_compact.cs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid PreOrder(TreeNode? root) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root == null || root.val == 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.Add(root);\n    if (root.val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.Add(new List<TreeNode>(path));\n    }\n    PreOrder(root.left);\n    PreOrder(root.right);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.RemoveAt(path.Count - 1);\n}\n
preorder_traversal_iii_compact.go
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunc preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if root == nil || root.Val == 3 {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    *path = append(*path, root)\n    if root.Val.(int) == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        *res = append(*res, append([]*TreeNode{}, *path...))\n    }\n    preOrderIII(root.Left, res, path)\n    preOrderIII(root.Right, res, path)\n    // \u041e\u0442\u043a\u0430\u0442\n    *path = (*path)[:len(*path)-1]\n}\n
preorder_traversal_iii_compact.swift
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunc preOrder(root: TreeNode?) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    guard let root = root, root.val != 3 else {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.append(root)\n    if root.val == 7 {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.append(path)\n    }\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n    // \u041e\u0442\u043a\u0430\u0442\n    path.removeLast()\n}\n
preorder_traversal_iii_compact.js
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunction preOrder(root, path, res) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push(root);\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop();\n}\n
preorder_traversal_iii_compact.ts
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunction preOrder(\n    root: TreeNode | null,\n    path: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root === null || root.val === 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path.push(root);\n    if (root.val === 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res.push([...path]);\n    }\n    preOrder(root.left, path, res);\n    preOrder(root.right, path, res);\n    // \u041e\u0442\u043a\u0430\u0442\n    path.pop();\n}\n
preorder_traversal_iii_compact.dart
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid preOrder(\n  TreeNode? root,\n  List<TreeNode> path,\n  List<List<TreeNode>> res,\n) {\n  if (root == null || root.val == 3) {\n    return;\n  }\n\n  // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n  path.add(root);\n  if (root.val == 7) {\n    // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    res.add(List.from(path));\n  }\n  preOrder(root.left, path, res);\n  preOrder(root.right, path, res);\n  // \u041e\u0442\u043a\u0430\u0442\n  path.removeLast();\n}\n
preorder_traversal_iii_compact.rs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfn pre_order(\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n    path: &mut Vec<Rc<RefCell<TreeNode>>>,\n    root: Option<&Rc<RefCell<TreeNode>>>,\n) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {\n        return;\n    }\n    if let Some(node) = root {\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n        path.push(node.clone());\n        if node.borrow().val == 7 {\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n            res.push(path.clone());\n        }\n        pre_order(res, path, node.borrow().left.as_ref());\n        pre_order(res, path, node.borrow().right.as_ref());\n        // \u041e\u0442\u043a\u0430\u0442\n        path.pop();\n    }\n}\n
preorder_traversal_iii_compact.c
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid preOrder(TreeNode *root) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root == NULL || root->val == 3) {\n        return;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path[pathSize++] = root;\n    if (root->val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        for (int i = 0; i < pathSize; i++) {\n            res[resSize][i] = path[i];\n        }\n        resSize++;\n    }\n    preOrder(root->left);\n    preOrder(root->right);\n    // \u041e\u0442\u043a\u0430\u0442\n    pathSize--;\n}\n
preorder_traversal_iii_compact.kt
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfun preOrder(root: TreeNode?) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n    if (root == null || root._val == 3) {\n        return\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n    path!!.add(root)\n    if (root._val == 7) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        res!!.add(path!!.toMutableList())\n    }\n    preOrder(root.left)\n    preOrder(root.right)\n    // \u041e\u0442\u043a\u0430\u0442\n    path!!.removeAt(path!!.size - 1)\n}\n
preorder_traversal_iii_compact.rb
=begin\nFile: preorder_traversal_iii_compact.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434: \u043f\u0440\u0438\u043c\u0435\u0440 3 ###\ndef pre_order(root)\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435\n  return if !root || root.val == 3\n\n  # \u041f\u043e\u043f\u044b\u0442\u043a\u0430\n  $path.append(root)\n\n  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  $res << $path.dup if root.val == 7\n\n  pre_order(root.left)\n  pre_order(root.right)\n\n  # \u041e\u0442\u043a\u0430\u0442\n  $path.pop\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0422\u0435\u0440\u043c\u0438\u043d \"\u043e\u0431\u0440\u0435\u0437\u043a\u0430\" \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0433\u043b\u044f\u0434\u0435\u043d. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-3, \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u044b \"\u0441\u0440\u0435\u0437\u0430\u0435\u043c\" \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c , \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0435\u0433\u0430\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u0438 \u043f\u043e\u0432\u044b\u0448\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-3 \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c \u0437\u0430\u0434\u0430\u0447\u0438

"},{"location":"chapter_backtracking/backtracking_algorithm/#1313","title":"13.1.3 \u00a0 \u041a\u0430\u0440\u043a\u0430\u0441 \u043a\u043e\u0434\u0430","text":"

\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u043e\u0431\u0449\u0438\u0439 \u043a\u0430\u0440\u043a\u0430\u0441 \u0438\u0437 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \"\u043f\u043e\u043f\u044b\u0442\u043a\u0430\", \"\u043e\u0442\u043a\u0430\u0442\" \u0438 \"\u043e\u0431\u0440\u0435\u0437\u043a\u0430\", \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u0434 \u0431\u043e\u043b\u0435\u0435 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u043c.

\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u0430\u0440\u043a\u0430\u0441\u0435 \u043a\u043e\u0434\u0430 state \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u0430 choices - \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u044b\u0431\u043e\u0440\u043e\u0432, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
def backtrack(state: State, choices: list[choice], res: list[state]):\n    \"\"\"\u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c\"\"\"\n    # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if is_solution(state):\n        # \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        record_solution(state, res)\n        # \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for choice in choices:\n        # \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if is_valid(state, choice):\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undo_choice(state, choice)\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nvoid backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (Choice choice : choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nvoid backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (Choice choice : choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nvoid Backtrack(State state, List<Choice> choices, List<State> res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (IsSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        RecordSolution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    foreach (Choice choice in choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (IsValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            MakeChoice(state, choice);\n            Backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            UndoChoice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfunc backtrack(state *State, choices []Choice, res *[]State) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if isSolution(state) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res)\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for _, choice := range choices {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if isValid(state, choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice)\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfunc backtrack(state: inout State, choices: [Choice], res: inout [State]) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if isSolution(state: state) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state: state, res: &res)\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for choice in choices {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if isValid(state: state, choice: choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state: &state, choice: choice)\n            backtrack(state: &state, choices: choices, res: &res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfunction backtrack(state, choices, res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (let choice of choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfunction backtrack(state: State, choices: Choice[], res: State[]): void {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (let choice of choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nvoid backtrack(State state, List<Choice>, List<State> res) {\n  // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n  if (isSolution(state)) {\n    // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n    recordSolution(state, res);\n    // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n    return;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n  for (Choice choice in choices) {\n    // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n    if (isValid(state, choice)) {\n      // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      makeChoice(state, choice);\n      backtrack(state, choices, res);\n      // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      undoChoice(state, choice);\n    }\n  }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if is_solution(state) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        record_solution(state, res);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for choice in choices {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if is_valid(state, choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            make_choice(state, choice);\n            backtrack(state, choices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undo_choice(state, choice);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nvoid backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res, numRes);\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (int i = 0; i < numChoices; i++) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, &choices[i])) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, &choices[i]);\n            backtrack(state, choices, numChoices, res, numRes);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, &choices[i]);\n        }\n    }\n}\n
/* \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c */\nfun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        recordSolution(state, res)\n        // \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u043f\u043e\u0438\u0441\u043a\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for (choice in choices) {\n        // \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice)\n            backtrack(state, choices, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice)\n        }\n    }\n}\n
### \u041a\u0430\u0440\u043a\u0430\u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c ###\ndef backtrack(state, choices, res)\n    # \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if is_solution?(state)\n        # \u0417\u0430\u043f\u0438\u0441\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f\n        record_solution(state, res)\n        return\n    end\n\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432\n    for choice in choices\n        # \u041e\u0431\u0440\u0435\u0437\u043a\u0430: \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u0430\n        if is_valid?(state, choice)\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            make_choice(state, choice)\n            backtrack(state, choices, res)\n            # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undo_choice(state, choice)\n        end\n    end\nend\n

\u0422\u0435\u043f\u0435\u0440\u044c, \u043e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u043d\u0430 \u044d\u0442\u043e\u0442 \u043a\u0430\u0440\u043a\u0430\u0441, \u0440\u0435\u0448\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 3. \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 state \u0437\u0434\u0435\u0441\u044c - \u044d\u0442\u043e \u043f\u0443\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0443\u0437\u043b\u043e\u0432, \u0432\u044b\u0431\u043e\u0440 choices - \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0442\u043e\u043c\u043a\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430, \u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 res - \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0442\u0435\u0439:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby preorder_traversal_iii_template.py
def is_solution(state: list[TreeNode]) -> bool:\n    \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\"\"\"\n    return state and state[-1].val == 7\n\ndef record_solution(state: list[TreeNode], res: list[list[TreeNode]]):\n    \"\"\"\u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\"\"\"\n    res.append(list(state))\n\ndef is_valid(state: list[TreeNode], choice: TreeNode) -> bool:\n    \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438\"\"\"\n    return choice is not None and choice.val != 3\n\ndef make_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\"\"\"\n    state.append(choice)\n\ndef undo_choice(state: list[TreeNode], choice: TreeNode):\n    \"\"\"\u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\"\"\"\n    state.pop()\n\ndef backtrack(\n    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3\"\"\"\n    # \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if is_solution(state):\n        # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        record_solution(state, res)\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for choice in choices:\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if is_valid(state, choice):\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            make_choice(state, choice)\n            # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, [choice.left, choice.right], res)\n            # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undo_choice(state, choice)\n
preorder_traversal_iii_template.cpp
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nbool isSolution(vector<TreeNode *> &state) {\n    return !state.empty() && state.back()->val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nvoid recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {\n    res.push_back(state);\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nbool isValid(vector<TreeNode *> &state, TreeNode *choice) {\n    return choice != nullptr && choice->val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid makeChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.push_back(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid undoChoice(vector<TreeNode *> &state, TreeNode *choice) {\n    state.pop_back();\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (TreeNode *choice : choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            vector<TreeNode *> nextChoices{choice->left, choice->right};\n            backtrack(state, nextChoices, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
preorder_traversal_iii_template.java
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nboolean isSolution(List<TreeNode> state) {\n    return !state.isEmpty() && state.get(state.size() - 1).val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.add(new ArrayList<>(state));\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nboolean isValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid makeChoice(List<TreeNode> state, TreeNode choice) {\n    state.add(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid undoChoice(List<TreeNode> state, TreeNode choice) {\n    state.remove(state.size() - 1);\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (TreeNode choice : choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, Arrays.asList(choice.left, choice.right), res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice);\n        }\n    }\n}\n
preorder_traversal_iii_template.cs
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nbool IsSolution(List<TreeNode> state) {\n    return state.Count != 0 && state[^1].val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nvoid RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n    res.Add(new List<TreeNode>(state));\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nbool IsValid(List<TreeNode> state, TreeNode choice) {\n    return choice != null && choice.val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid MakeChoice(List<TreeNode> state, TreeNode choice) {\n    state.Add(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid UndoChoice(List<TreeNode> state, TreeNode choice) {\n    state.RemoveAt(state.Count - 1);\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (IsSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        RecordSolution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    foreach (TreeNode choice in choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (IsValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            MakeChoice(state, choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            Backtrack(state, [choice.left!, choice.right!], res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            UndoChoice(state, choice);\n        }\n    }\n}\n
preorder_traversal_iii_template.go
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfunc isSolution(state *[]*TreeNode) bool {\n    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfunc recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {\n    *res = append(*res, append([]*TreeNode{}, *state...))\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfunc isValid(state *[]*TreeNode, choice *TreeNode) bool {\n    return choice != nil && choice.Val != 3\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunc makeChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = append(*state, choice)\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunc undoChoice(state *[]*TreeNode, choice *TreeNode) {\n    *state = (*state)[:len(*state)-1]\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunc backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if isSolution(state) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res)\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for _, choice := range *choices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if isValid(state, choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            temp := make([]*TreeNode, 0)\n            temp = append(temp, choice.Left, choice.Right)\n            backtrackIII(state, &temp, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice)\n        }\n    }\n}\n
preorder_traversal_iii_template.swift
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfunc isSolution(state: [TreeNode]) -> Bool {\n    !state.isEmpty && state.last!.val == 7\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfunc recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {\n    res.append(state)\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfunc isValid(state: [TreeNode], choice: TreeNode?) -> Bool {\n    choice != nil && choice!.val != 3\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunc makeChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.append(choice)\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunc undoChoice(state: inout [TreeNode], choice: TreeNode) {\n    state.removeLast()\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunc backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if isSolution(state: state) {\n        recordSolution(state: state, res: &res)\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for choice in choices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if isValid(state: state, choice: choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state: &state, choice: choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state: &state, choice: choice)\n        }\n    }\n}\n
preorder_traversal_iii_template.js
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfunction isSolution(state) {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfunction recordSolution(state, res) {\n    res.push([...state]);\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfunction isValid(state, choice) {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunction makeChoice(state, choice) {\n    state.push(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunction undoChoice(state) {\n    state.pop();\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunction backtrack(state, choices, res) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (const choice of choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, [choice.left, choice.right], res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state);\n        }\n    }\n}\n
preorder_traversal_iii_template.ts
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfunction isSolution(state: TreeNode[]): boolean {\n    return state && state[state.length - 1]?.val === 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfunction recordSolution(state: TreeNode[], res: TreeNode[][]): void {\n    res.push([...state]);\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfunction isValid(state: TreeNode[], choice: TreeNode): boolean {\n    return choice !== null && choice.val !== 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunction makeChoice(state: TreeNode[], choice: TreeNode): void {\n    state.push(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfunction undoChoice(state: TreeNode[]): void {\n    state.pop();\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfunction backtrack(\n    state: TreeNode[],\n    choices: TreeNode[],\n    res: TreeNode[][]\n): void {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (const choice of choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, [choice.left, choice.right], res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state);\n        }\n    }\n}\n
preorder_traversal_iii_template.dart
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nbool isSolution(List<TreeNode> state) {\n  return state.isNotEmpty && state.last.val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nvoid recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {\n  res.add(List.from(state));\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nbool isValid(List<TreeNode> state, TreeNode? choice) {\n  return choice != null && choice.val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid makeChoice(List<TreeNode> state, TreeNode? choice) {\n  state.add(choice!);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid undoChoice(List<TreeNode> state, TreeNode? choice) {\n  state.removeLast();\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid backtrack(\n  List<TreeNode> state,\n  List<TreeNode?> choices,\n  List<List<TreeNode>> res,\n) {\n  // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n  if (isSolution(state)) {\n    // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    recordSolution(state, res);\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for (TreeNode? choice in choices) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n    if (isValid(state, choice)) {\n      // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      makeChoice(state, choice);\n      // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, [choice!.left, choice.right], res);\n      // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      undoChoice(state, choice);\n    }\n  }\n}\n
preorder_traversal_iii_template.rs
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {\n    return !state.is_empty() && state.last().unwrap().borrow().val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfn record_solution(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    res.push(state.clone());\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {\n    return choice.is_some() && choice.unwrap().borrow().val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {\n    state.push(choice);\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {\n    state.pop();\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfn backtrack(\n    state: &mut Vec<Rc<RefCell<TreeNode>>>,\n    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,\n    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,\n) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if is_solution(state) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        record_solution(state, res);\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for &choice in choices.iter() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if is_valid(state, choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            make_choice(state, choice.unwrap().clone());\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(\n                state,\n                &vec![\n                    choice.unwrap().borrow().left.as_ref(),\n                    choice.unwrap().borrow().right.as_ref(),\n                ],\n                res,\n            );\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undo_choice(state, choice.unwrap().clone());\n        }\n    }\n}\n
preorder_traversal_iii_template.c
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nbool isSolution(void) {\n    return pathSize > 0 && path[pathSize - 1]->val == 7;\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nvoid recordSolution(void) {\n    for (int i = 0; i < pathSize; i++) {\n        res[resSize][i] = path[i];\n    }\n    resSize++;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nbool isValid(TreeNode *choice) {\n    return choice != NULL && choice->val != 3;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid makeChoice(TreeNode *choice) {\n    path[pathSize++] = choice;\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nvoid undoChoice(void) {\n    pathSize--;\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nvoid backtrack(TreeNode *choices[2]) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution()) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution();\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < 2; i++) {\n        TreeNode *choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            TreeNode *nextChoices[2] = {choice->left, choice->right};\n            backtrack(nextChoices);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice();\n        }\n    }\n}\n
preorder_traversal_iii_template.kt
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c */\nfun isSolution(state: MutableList<TreeNode?>): Boolean {\n    return state.isNotEmpty() && state[state.size - 1]?._val == 7\n}\n\n/* \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 */\nfun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {\n    res.add(state!!.toMutableList())\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c \u043b\u0438 \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 */\nfun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {\n    return choice != null && choice._val != 3\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.add(choice)\n}\n\n/* \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 */\nfun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {\n    state.removeLast()\n}\n\n/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 */\nfun backtrack(\n    state: MutableList<TreeNode?>,\n    choices: MutableList<TreeNode?>,\n    res: MutableList<MutableList<TreeNode?>?>\n) {\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n    if (isSolution(state)) {\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n        recordSolution(state, res)\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (choice in choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n        if (isValid(state, choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            makeChoice(state, choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, mutableListOf(choice!!.left, choice.right), res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            undoChoice(state, choice)\n        }\n    }\n}\n
preorder_traversal_iii_template.rb
=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n# ## \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n# ## \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n# ## \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n# ## \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n# ## \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n=begin\nFile: preorder_traversal_iii_template.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c ###\ndef is_solution?(state)\n  !state.empty? && state.last.val == 7\nend\n\n# ## \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 ###\ndef record_solution(state, res)\n  res << state.dup\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 ###\ndef is_valid?(state, choice)\n  choice && choice.val != 3\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 ###\ndef make_choice(state, choice)\n  state << choice\nend\n\n# ## \u0412\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 ###\ndef undo_choice(state, choice)\n  state.pop\nend\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u043f\u0440\u0438\u043c\u0435\u0440 3 ###\ndef backtrack(state, choices, res)\n  # \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c\n  record_solution(state, res) if is_solution?(state)\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for choice in choices\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u0430\n    if is_valid?(state, choice)\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      make_choice(state, choice)\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, [choice.left, choice.right], res)\n      # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      undo_choice(state, choice)\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u0441\u043b\u0435 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(7\\) \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 return \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-4 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 return \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0438 \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-4 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 return

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430, \u0432\u0435\u0440\u0441\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u0430\u0440\u043a\u0430\u0441\u0430 backtracking \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u043e\u0439, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u043b\u0443\u0447\u0448\u0435\u0439 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 backtracking \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u0442\u043e\u0433\u043e \u043a\u0430\u0440\u043a\u0430\u0441\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c state \u0438 choices \u043f\u043e\u0434 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u0430\u0440\u043a\u0430\u0441\u0430.

"},{"location":"chapter_backtracking/backtracking_algorithm/#1314","title":"13.1.4 \u00a0 \u0427\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b","text":"

\u0427\u0442\u043e\u0431\u044b \u044f\u0441\u043d\u0435\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u0434\u044b\u0442\u043e\u0436\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u0432 backtracking \u0438 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0438\u0445 \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u043c 3, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 13-1.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 13-1 \u00a0 \u0427\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 backtracking

\u0422\u0435\u0440\u043c\u0438\u043d \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u041f\u0440\u0438\u043c\u0435\u0440 3 \u0420\u0435\u0448\u0435\u043d\u0438\u0435 (solution) \u0420\u0435\u0448\u0435\u043d\u0438\u0435 - \u044d\u0442\u043e \u043e\u0442\u0432\u0435\u0442, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0439 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c \u0437\u0430\u0434\u0430\u0447\u0438; \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0434\u043d\u043e \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0412\u0441\u0435 \u043f\u0443\u0442\u0438 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u0443\u0437\u043b\u0430 \\(7\\) , \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 (constraint) \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0435\u0437\u043a\u0438 \u041f\u0443\u0442\u044c \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0443\u0437\u043b\u044b \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(3\\) \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (state) \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0443\u0436\u0435 \u0441\u0434\u0435\u043b\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u044b \u0422\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0443\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0443\u0437\u043b\u043e\u0432 path \u041f\u043e\u043f\u044b\u0442\u043a\u0430 (attempt) \u041f\u043e\u043f\u044b\u0442\u043a\u0430 - \u044d\u0442\u043e \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0432\u044b\u0431\u043e\u0440, \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0445\u043e\u0434 \u043a \u043b\u0435\u0432\u043e\u043c\u0443 \u0438\u043b\u0438 \u043f\u0440\u0430\u0432\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043c\u043a\u0443, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0432 path \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u0440\u0430\u0432\u043d\u043e \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \\(7\\) \u041e\u0442\u043a\u0430\u0442 (backtracking) \u041e\u0442\u043a\u0430\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u043c\u0435\u043d\u0443 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043a \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u043d\u043d\u0435\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0435\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u0417\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0440\u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u0447\u0435\u0440\u0435\u0437 \u043b\u0438\u0441\u0442, \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u0438\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(3\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u0438\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 (pruning) \u041e\u0431\u0440\u0435\u0437\u043a\u0430 - \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439 \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0435\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439, \u043f\u043e\u0432\u044b\u0448\u0430\u044e\u0449\u0438\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u041f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(3\\) \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u044d\u0442\u043e\u0439 \u0432\u0435\u0442\u0432\u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f

Tip

\u0422\u0430\u043a\u0438\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u043a\u0430\u043a \u0437\u0430\u0434\u0430\u0447\u0430, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c\u0438 \u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 backtracking, \u043d\u043e \u0438 \u0432 divide and conquer, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0436\u0430\u0434\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0435\u043c\u0430\u0445.

"},{"location":"chapter_backtracking/backtracking_algorithm/#1315","title":"13.1.5 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0442 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c. \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u0438 \u0440\u0430\u0437\u0443\u043c\u043d\u043e\u0439 \u043e\u0431\u0440\u0435\u0437\u043a\u0435 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432\u0435\u0441\u044c\u043c\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

\u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0438\u043b\u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c backtracking \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u043e\u0439.

\u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 backtracking \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0437\u0430\u0434\u0430\u0447 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0435\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0432\u044b\u0431\u043e\u0440\u044b \u043f\u0440\u0438\u0432\u0435\u0434\u0443\u0442 \u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0432\u043e\u043f\u0440\u043e\u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 , \u0438 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u0432\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438.

"},{"location":"chapter_backtracking/backtracking_algorithm/#1316-backtracking","title":"13.1.6 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 backtracking","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0437\u0430\u0434\u0430\u0447 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0435\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0438 \u0437\u0430\u0434\u0430\u0447 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.

\u041f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438: \u0446\u0435\u043b\u044c\u044e \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c.

\u0417\u0430\u0434\u0430\u0447\u0438 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0435\u043d\u0438\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439: \u0446\u0435\u043b\u044c\u044e \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u0432\u0441\u0435\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c.

\u0417\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438: \u0446\u0435\u043b\u044c\u044e \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0438\u0441\u043a \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 \u043f\u0440\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u0445.

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 backtracking \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f.

"},{"location":"chapter_backtracking/n_queens_problem/","title":"13.4 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445","text":"

Question

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c \u0448\u0430\u0445\u043c\u0430\u0442 \u0444\u0435\u0440\u0437\u044c \u043c\u043e\u0436\u0435\u0442 \u0430\u0442\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0433\u0443\u0440\u044b, \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u0435\u0441\u044f \u0441 \u043d\u0438\u043c \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u0432 \u043e\u0434\u043d\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0438\u043b\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438. \u0414\u0430\u043d\u044b \\(n\\) \u0444\u0435\u0440\u0437\u0435\u0439 \u0438 \u0448\u0430\u0445\u043c\u0430\u0442\u043d\u0430\u044f \u0434\u043e\u0441\u043a\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n \\times n\\) ; \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043a\u0438\u0435 \u0440\u0430\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0438 \u043e\u0434\u043d\u0430 \u043f\u0430\u0440\u0430 \u0444\u0435\u0440\u0437\u0435\u0439 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0430\u0442\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-15, \u043f\u0440\u0438 \\(n = 4\\) \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f. \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f backtracking \u0434\u043e\u0441\u043a\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n \\times n\\) \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \\(n^2\\) \u043a\u043b\u0435\u0442\u043e\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u0432\u044b\u0431\u043e\u0440\u044b choices . \u041f\u043e \u043c\u0435\u0440\u0435 \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0444\u0435\u0440\u0437\u0435\u0439 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u043e\u0441\u043a\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 state .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-15 \u00a0 \u0420\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e 4 \u0444\u0435\u0440\u0437\u044f\u0445

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-16 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0442\u0440\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0435\u0440\u0437\u0435\u0439 \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u0432 \u043e\u0434\u043d\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 \u0438\u043b\u0438 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0431\u044b\u0432\u0430\u044e\u0442 \u0434\u0432\u0443\u0445 \u0442\u0438\u043f\u043e\u0432: \u0433\u043b\u0430\u0432\u043d\u0430\u044f \\ \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0430\u044f / .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-16 \u00a0 \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445

"},{"location":"chapter_backtracking/n_queens_problem/#1","title":"1. \u00a0 \u041f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f","text":"

\u0427\u0438\u0441\u043b\u043e \u0444\u0435\u0440\u0437\u0435\u0439 \u0438 \u0447\u0438\u0441\u043b\u043e \u0441\u0442\u0440\u043e\u043a \u0434\u043e\u0441\u043a\u0438 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e \u0438 \u0440\u0430\u0432\u043d\u043e \\(n\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0432\u043e\u0434: \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u0434\u043e\u0441\u043a\u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e \u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0435\u0440\u0437\u044f.

\u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e: \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0444\u0435\u0440\u0437\u044e \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044f\u044f.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-17 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e 4 \u0444\u0435\u0440\u0437\u044f\u0445. \u0418\u0437-\u0437\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043d\u0435\u043c \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0434\u043b\u044f \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438, \u0430 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b, \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c, \u0431\u044b\u043b\u0438 \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-17 \u00a0 \u041f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0441\u0430\u043c\u0430 \u043f\u043e \u0441\u0435\u0431\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0440\u043e\u043b\u044c \u043e\u0431\u0440\u0435\u0437\u043a\u0438 , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u043e\u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0444\u0435\u0440\u0437\u0435\u0439.

"},{"location":"chapter_backtracking/n_queens_problem/#2","title":"2. \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c","text":"

\u0427\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044e \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u0443\u043b\u0435\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 cols \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435. \u041f\u0435\u0440\u0435\u0434 \u043a\u0430\u0436\u0434\u044b\u043c \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u0435\u043c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c cols \u0434\u043b\u044f \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u0443\u0436\u0435 \u0437\u0430\u043d\u044f\u0442\u044b\u0445 \u0444\u0435\u0440\u0437\u044f\u043c\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 cols \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0442\u043a\u0430\u0442\u0430.

Tip

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043d\u0430\u0447\u0430\u043b\u043e \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0443\u0433\u043b\u0443, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0440\u0430\u0441\u0442\u0443\u0442 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 - \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e.

\u041a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c? \u041f\u0443\u0441\u0442\u044c \u043a\u043b\u0435\u0442\u043a\u0430 \u043d\u0430 \u0434\u043e\u0441\u043a\u0435 \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b \\((row, col)\\) . \u0412\u044b\u0431\u0440\u0430\u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044c \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043b\u0435\u0442\u043e\u043a \u044d\u0442\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043b\u0435\u0442\u043e\u043a \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(row - col\\) \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e.

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u043a\u043b\u0435\u0442\u043e\u043a \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e \\(row_1 - col_1 = row_2 - col_2\\) , \u0442\u043e \u043e\u043d\u0438 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043b\u0435\u0436\u0430\u0442 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043c\u043e\u0436\u043d\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 diags1 , \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-18, \u043e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438.

\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043a\u043b\u0435\u0442\u043e\u043a \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(row + col\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u043c \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043c\u0430\u0441\u0441\u0438\u0432 diags2 .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-18 \u00a0 \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c \u0438 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044f\u043c

"},{"location":"chapter_backtracking/n_queens_problem/#3","title":"3. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0432 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n\\) \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \\(row - col\\) \u0440\u0430\u0432\u0435\u043d \\([-n + 1, n - 1]\\) , \u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \\(row + col\\) \u0440\u0430\u0432\u0435\u043d \\([0, 2n - 2]\\) . \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0447\u0438\u0441\u043b\u043e \u0433\u043b\u0430\u0432\u043d\u044b\u0445 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u0445 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0435\u0439 \u0440\u0430\u0432\u043d\u043e \\(2n - 1\\) , \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 diags1 \u0438 diags2 \u0442\u043e\u0436\u0435 \u0440\u0430\u0432\u043d\u044b \\(2n - 1\\) .

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby n_queens.py
def backtrack(\n    row: int,\n    n: int,\n    state: list[list[str]],\n    res: list[list[list[str]]],\n    cols: list[bool],\n    diags1: list[bool],\n    diags2: list[bool],\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439\"\"\"\n    # \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if row == n:\n        res.append([list(row) for row in state])\n        return\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for col in range(n):\n        # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        diag1 = row - col + n - 1\n        diag2 = row + col\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if not cols[col] and not diags1[diag1] and not diags2[diag2]:\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\"\n            cols[col] = diags1[diag1] = diags2[diag2] = True\n            # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            # \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\"\n            cols[col] = diags1[diag1] = diags2[diag2] = False\n\ndef n_queens(n: int) -> list[list[list[str]]]:\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    state = [[\"#\" for _ in range(n)] for _ in range(n)]\n    cols = [False] * n  # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    diags1 = [False] * (2 * n - 1)  # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    diags2 = [False] * (2 * n - 1)  # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    res = []\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n
n_queens.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nvoid backtrack(int row, int n, vector<vector<string>> &state, vector<vector<vector<string>>> &res, vector<bool> &cols,\n               vector<bool> &diags1, vector<bool> &diags2) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row == n) {\n        res.push_back(state);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nvector<vector<vector<string>>> nQueens(int n) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    vector<vector<string>> state(n, vector<string>(n, \"#\"));\n    vector<bool> cols(n, false);           // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    vector<bool> diags1(2 * n - 1, false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    vector<bool> diags2(2 * n - 1, false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    vector<vector<vector<string>>> res;\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
n_queens.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nvoid backtrack(int row, int n, List<List<String>> state, List<List<List<String>>> res,\n        boolean[] cols, boolean[] diags1, boolean[] diags2) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row == n) {\n        List<List<String>> copyState = new ArrayList<>();\n        for (List<String> sRow : state) {\n            copyState.add(new ArrayList<>(sRow));\n        }\n        res.add(copyState);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state.get(row).set(col, \"Q\");\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state.get(row).set(col, \"#\");\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nList<List<List<String>>> nQueens(int n) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    List<List<String>> state = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<String> row = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            row.add(\"#\");\n        }\n        state.add(row);\n    }\n    boolean[] cols = new boolean[n]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    boolean[] diags1 = new boolean[2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    boolean[] diags2 = new boolean[2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    List<List<List<String>>> res = new ArrayList<>();\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
n_queens.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nvoid Backtrack(int row, int n, List<List<string>> state, List<List<List<string>>> res,\n        bool[] cols, bool[] diags1, bool[] diags2) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row == n) {\n        List<List<string>> copyState = [];\n        foreach (List<string> sRow in state) {\n            copyState.Add(new List<string>(sRow));\n        }\n        res.Add(copyState);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\";\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            Backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\";\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nList<List<List<string>>> NQueens(int n) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    List<List<string>> state = [];\n    for (int i = 0; i < n; i++) {\n        List<string> row = [];\n        for (int j = 0; j < n; j++) {\n            row.Add(\"#\");\n        }\n        state.Add(row);\n    }\n    bool[] cols = new bool[n]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    bool[] diags1 = new bool[2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    bool[] diags2 = new bool[2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    List<List<List<string>>> res = [];\n\n    Backtrack(0, n, state, res, cols, diags1, diags2);\n\n    return res;\n}\n
n_queens.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfunc backtrack(row, n int, state *[][]string, res *[][][]string, cols, diags1, diags2 *[]bool) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if row == n {\n        newState := make([][]string, len(*state))\n        for i, _ := range newState {\n            newState[i] = make([]string, len((*state)[0]))\n            copy(newState[i], (*state)[i])\n\n        }\n        *res = append(*res, newState)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for col := 0; col < n; col++ {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        diag1 := row - col + n - 1\n        diag2 := row + col\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if !(*cols)[col] && !(*diags1)[diag1] && !(*diags2)[diag2] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            (*state)[row][col] = \"Q\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = true, true, true\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row+1, n, state, res, cols, diags1, diags2)\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            (*state)[row][col] = \"#\"\n            (*cols)[col], (*diags1)[diag1], (*diags2)[diag2] = false, false, false\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfunc nQueens(n int) [][][]string {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    state := make([][]string, n)\n    for i := 0; i < n; i++ {\n        row := make([]string, n)\n        for i := 0; i < n; i++ {\n            row[i] = \"#\"\n        }\n        state[i] = row\n    }\n    // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    cols := make([]bool, n)\n    diags1 := make([]bool, 2*n-1)\n    diags2 := make([]bool, 2*n-1)\n    res := make([][][]string, 0)\n    backtrack(0, n, &state, &res, &cols, &diags1, &diags2)\n    return res\n}\n
n_queens.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfunc backtrack(row: Int, n: Int, state: inout [[String]], res: inout [[[String]]], cols: inout [Bool], diags1: inout [Bool], diags2: inout [Bool]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if row == n {\n        res.append(state)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for col in 0 ..< n {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        let diag1 = row - col + n - 1\n        let diag2 = row + col\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\"\n            cols[col] = true\n            diags1[diag1] = true\n            diags2[diag2] = true\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row: row + 1, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\"\n            cols[col] = false\n            diags1[diag1] = false\n            diags2[diag2] = false\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfunc nQueens(n: Int) -> [[[String]]] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    var state = Array(repeating: Array(repeating: \"#\", count: n), count: n)\n    var cols = Array(repeating: false, count: n) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    var diags1 = Array(repeating: false, count: 2 * n - 1) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    var diags2 = Array(repeating: false, count: 2 * n - 1) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    var res: [[[String]]] = []\n\n    backtrack(row: 0, n: n, state: &state, res: &res, cols: &cols, diags1: &diags1, diags2: &diags2)\n\n    return res\n}\n
n_queens.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfunction backtrack(row, n, state, res, cols, diags1, diags2) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfunction nQueens(n) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    const diags1 = Array(2 * n - 1).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    const diags2 = Array(2 * n - 1).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    const res = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
n_queens.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfunction backtrack(\n    row: number,\n    n: number,\n    state: string[][],\n    res: string[][][],\n    cols: boolean[],\n    diags1: boolean[],\n    diags2: boolean[]\n): void {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row === n) {\n        res.push(state.map((row) => row.slice()));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        const diag1 = row - col + n - 1;\n        const diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfunction nQueens(n: number): string[][][] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    const state = Array.from({ length: n }, () => Array(n).fill('#'));\n    const cols = Array(n).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    const diags1 = Array(2 * n - 1).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    const diags2 = Array(2 * n - 1).fill(false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    const res: string[][][] = [];\n\n    backtrack(0, n, state, res, cols, diags1, diags2);\n    return res;\n}\n
n_queens.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nvoid backtrack(\n  int row,\n  int n,\n  List<List<String>> state,\n  List<List<List<String>>> res,\n  List<bool> cols,\n  List<bool> diags1,\n  List<bool> diags2,\n) {\n  // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (row == n) {\n    List<List<String>> copyState = [];\n    for (List<String> sRow in state) {\n      copyState.add(List.from(sRow));\n    }\n    res.add(copyState);\n    return;\n  }\n  // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for (int col = 0; col < n; col++) {\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n    int diag1 = row - col + n - 1;\n    int diag2 = row + col;\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n    if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n      // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n      state[row][col] = \"Q\";\n      cols[col] = true;\n      diags1[diag1] = true;\n      diags2[diag2] = true;\n      // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n      backtrack(row + 1, n, state, res, cols, diags1, diags2);\n      // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n      state[row][col] = \"#\";\n      cols[col] = false;\n      diags1[diag1] = false;\n      diags2[diag2] = false;\n    }\n  }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nList<List<List<String>>> nQueens(int n) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n  List<List<String>> state = List.generate(n, (index) => List.filled(n, \"#\"));\n  List<bool> cols = List.filled(n, false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n  List<bool> diags1 = List.filled(2 * n - 1, false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n  List<bool> diags2 = List.filled(2 * n - 1, false); // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n  List<List<List<String>>> res = [];\n\n  backtrack(0, n, state, res, cols, diags1, diags2);\n\n  return res;\n}\n
n_queens.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfn backtrack(\n    row: usize,\n    n: usize,\n    state: &mut Vec<Vec<String>>,\n    res: &mut Vec<Vec<Vec<String>>>,\n    cols: &mut [bool],\n    diags1: &mut [bool],\n    diags2: &mut [bool],\n) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if row == n {\n        res.push(state.clone());\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for col in 0..n {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        let diag1 = row + n - 1 - col;\n        let diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if !cols[col] && !diags1[diag1] && !diags2[diag2] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (true, true, true);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\".into();\n            (cols[col], diags1[diag1], diags2[diag2]) = (false, false, false);\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfn n_queens(n: usize) -> Vec<Vec<Vec<String>>> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    let mut state: Vec<Vec<String>> = vec![vec![\"#\".to_string(); n]; n];\n    let mut cols = vec![false; n]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    let mut diags1 = vec![false; 2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    let mut diags2 = vec![false; 2 * n - 1]; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    let mut res: Vec<Vec<Vec<String>>> = Vec::new();\n\n    backtrack(\n        0,\n        n,\n        &mut state,\n        &mut res,\n        &mut cols,\n        &mut diags1,\n        &mut diags2,\n    );\n\n    res\n}\n
n_queens.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nvoid backtrack(int row, int n, char state[MAX_SIZE][MAX_SIZE], char ***res, int *resSize, bool cols[MAX_SIZE],\n               bool diags1[2 * MAX_SIZE - 1], bool diags2[2 * MAX_SIZE - 1]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row == n) {\n        res[*resSize] = (char **)malloc(sizeof(char *) * n);\n        for (int i = 0; i < n; ++i) {\n            res[*resSize][i] = (char *)malloc(sizeof(char) * (n + 1));\n            strcpy(res[*resSize][i], state[i]);\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int col = 0; col < n; col++) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        int diag1 = row - col + n - 1;\n        int diag2 = row + col;\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = 'Q';\n            cols[col] = diags1[diag1] = diags2[diag2] = true;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, resSize, cols, diags1, diags2);\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = '#';\n            cols[col] = diags1[diag1] = diags2[diag2] = false;\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nchar ***nQueens(int n, int *returnSize) {\n    char state[MAX_SIZE][MAX_SIZE];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    for (int i = 0; i < n; ++i) {\n        for (int j = 0; j < n; ++j) {\n            state[i][j] = '#';\n        }\n        state[i][n] = '\\0';\n    }\n    bool cols[MAX_SIZE] = {false};           // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    bool diags1[2 * MAX_SIZE - 1] = {false}; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    bool diags2[2 * MAX_SIZE - 1] = {false}; // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n\n    char ***res = (char ***)malloc(sizeof(char **) * MAX_SIZE);\n    *returnSize = 0;\n    backtrack(0, n, state, res, returnSize, cols, diags1, diags2);\n    return res;\n}\n
n_queens.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 */\nfun backtrack(\n    row: Int,\n    n: Int,\n    state: MutableList<MutableList<String>>,\n    res: MutableList<MutableList<MutableList<String>>?>,\n    cols: BooleanArray,\n    diags1: BooleanArray,\n    diags2: BooleanArray\n) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (row == n) {\n        val copyState = mutableListOf<MutableList<String>>()\n        for (sRow in state) {\n            copyState.add(sRow.toMutableList())\n        }\n        res.add(copyState)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (col in 0..<n) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n        val diag1 = row - col + n - 1\n        val diag2 = row + col\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n        if (!cols[col] && !diags1[diag1] && !diags2[diag2]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n            state[row][col] = \"Q\"\n            diags2[diag2] = true\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n            backtrack(row + 1, n, state, res, cols, diags1, diags2)\n            // \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n            state[row][col] = \"#\"\n            diags2[diag2] = false\n            diags1[diag1] = diags2[diag2]\n            cols[col] = diags1[diag1]\n        }\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 */\nfun nQueens(n: Int): MutableList<MutableList<MutableList<String>>?> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n    val state = mutableListOf<MutableList<String>>()\n    for (i in 0..<n) {\n        val row = mutableListOf<String>()\n        for (j in 0..<n) {\n            row.add(\"#\")\n        }\n        state.add(row)\n    }\n    val cols = BooleanArray(n) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n    val diags1 = BooleanArray(2 * n - 1) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    val diags2 = BooleanArray(2 * n - 1) // \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n    val res = mutableListOf<MutableList<MutableList<String>>?>()\n\n    backtrack(0, n, state, res, cols, diags1, diags2)\n\n    return res\n}\n
n_queens.rb
=begin\nFile: n_queens.rb\nCreated Time: 2024-05-21\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for col in 0...n\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n=begin\nFile: n_queens.rb\nCreated Time: 2024-05-21\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: n \u0444\u0435\u0440\u0437\u0435\u0439 ###\ndef backtrack(row, n, state, res, cols, diags1, diags2)\n  # \u041a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0443\u0436\u0435 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u044b, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if row == n\n    res << state.map { |row| row.dup }\n    return\n  end\n\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for col in 0...n\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0433\u043b\u0430\u0432\u043d\u0443\u044e \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u0443\u044e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435\n    diag1 = row - col + n - 1\n    diag2 = row + col\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435, \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u0438 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438 \u044d\u0442\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439\n    if !cols[col] && !diags1[diag1] && !diags2[diag2]\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0435\u0440\u0437\u044f \u0432 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443\n      state[row][col] = \"Q\"\n      cols[col] = diags1[diag1] = diags2[diag2] = true\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438\n      backtrack(row + 1, n, state, res, cols, diags1, diags2)\n      # \u041e\u0442\u043a\u0430\u0442: \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443 \u043a\u0430\u043a \u043f\u0443\u0441\u0442\u0443\u044e\n      state[row][col] = \"#\"\n      cols[col] = diags1[diag1] = diags2[diag2] = false\n    end\n  end\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e n \u0444\u0435\u0440\u0437\u044f\u0445 ###\ndef n_queens(n)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u043a\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 n*n, \u0433\u0434\u0435 'Q' \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0444\u0435\u0440\u0437\u044f, \u0430 '#' \u2014 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u043b\u0435\u0442\u043a\u0443\n  state = Array.new(n) { Array.new(n, \"#\") }\n  cols = Array.new(n, false) # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0444\u0435\u0440\u0437\u044c \u0432 \u0441\u0442\u043e\u043b\u0431\u0446\u0435\n  diags1 = Array.new(2 * n - 1, false) # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n  diags2 = Array.new(2 * n - 1, false) # \u041e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0444\u0435\u0440\u0437\u044f \u043d\u0430 \u043f\u043e\u0431\u043e\u0447\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438\n  res = []\n  backtrack(0, n, state, res, cols, diags1, diags2)\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u0444\u0435\u0440\u0437\u0435\u0439 \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e \\(n\\) \u0440\u0430\u0437, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c, \u0442\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\(n\\), \\(n-1\\), \\(\\dots\\), \\(2\\), \\(1\\) \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \\(O(n!)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 state \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435 \u0432 res , \u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(n^2)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n! \\cdot n^2)\\) . \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u043b\u0443\u0447\u0448\u0435 \u044d\u0442\u043e\u0439 \u043e\u0446\u0435\u043d\u043a\u0438.

\u041c\u0430\u0441\u0441\u0438\u0432 state \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \\(O(n^2)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u044b cols , diags1 \u0438 diags2 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043f\u043e \\(O(n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430. \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) , \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(n)\\) \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0442\u0435\u043a\u0430. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) .

"},{"location":"chapter_backtracking/permutations_problem/","title":"13.2 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445","text":"

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c. \u0415\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0438) \u043d\u0430\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u044d\u0442\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 13-2 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0438\u043c \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 13-2 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a

\u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \\([1]\\) \\([1]\\) \\([1, 2]\\) \\([1, 2], [2, 1]\\) \\([1, 2, 3]\\) \\([1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]\\)"},{"location":"chapter_backtracking/permutations_problem/#1321","title":"13.2.1 \u00a0 \u0421\u043b\u0443\u0447\u0430\u0439 \u0431\u0435\u0437 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043d\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f backtracking \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u0432. \u041f\u0443\u0441\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0430\u0432\u0435\u043d \\([1, 2, 3]\\) ; \u0435\u0441\u043b\u0438 \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0435\u0440\u0435\u043c \\(1\\) , \u0437\u0430\u0442\u0435\u043c \\(3\\) , \u0430 \u043f\u043e\u0442\u043e\u043c \\(2\\) , \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443 \\([1, 3, 2]\\) . \u041e\u0442\u043a\u0430\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u043c\u0435\u043d\u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u043e\u0439 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430 backtracking \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 choices \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0432\u0441\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 state - \u0438\u0437 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u043c\u043e\u043c\u0435\u043d\u0442\u0443. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 state \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-5, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432 \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 state . \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u043a\u043e\u0440\u043d\u044f, \u043f\u043e\u0441\u043b\u0435 \u0442\u0440\u0435\u0445 \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430 \u043c\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u043c \u0432 \u043b\u0438\u0441\u0442\u044c\u044f, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043b\u0438\u0441\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-5 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a

"},{"location":"chapter_backtracking/permutations_problem/#1","title":"1. \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0432\u0432\u0435\u0434\u0435\u043c \u0431\u0443\u043b\u0435\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 selected , \u0433\u0434\u0435 selected[i] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0431\u044b\u043b \u043b\u0438 \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d choices[i] , \u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u043e\u0431\u0440\u0435\u0437\u043a\u0443.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-6, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043c\u044b \u0432\u044b\u0431\u0435\u0440\u0435\u043c 1 , \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c - 3 , \u0430 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u043c - 2 , \u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u0435\u0447\u044c \u0432\u0435\u0442\u0432\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 1 , \u0430 \u0432 \u0442\u0440\u0435\u0442\u044c\u0435\u043c - \u0432\u0435\u0442\u0432\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 1 \u0438 3 .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-6 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0440\u0435\u0437\u043a\u0438 \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445

\u0418\u0437 \u0440\u0438\u0441\u0443\u043d\u043a\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0442\u0430\u043a\u0430\u044f \u043e\u0431\u0440\u0435\u0437\u043a\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \\(O(n^n)\\) \u0434\u043e \\(O(n!)\\) .

"},{"location":"chapter_backtracking/permutations_problem/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \"\u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0438\" \u0432 \u0448\u0430\u0431\u043b\u043e\u043d\u0435 backtracking. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0439 \u043e\u0431\u044a\u0435\u043c \u043a\u043e\u0434\u0430, \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438\u0437 \u043a\u0430\u0440\u043a\u0430\u0441\u0430, \u0430 \u0440\u0430\u0441\u043a\u0440\u043e\u0435\u043c \u0438\u0445 \u043f\u0440\u044f\u043c\u043e \u0432\u043d\u0443\u0442\u0440\u0438 backtrack() :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_i.py
def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I\"\"\"\n    # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i, choice in enumerate(choices):\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if not selected[i]:\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = True\n            state.append(choice)\n            # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res)\n            # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = False\n            state.pop()\n\ndef permutations_i(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
permutations_i.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.push_back(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvector<vector<int>> permutationsI(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
permutations_i.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.add(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nList<List<Integer>> permutationsI(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
permutations_i.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.Add(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            Backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nList<List<int>> PermutationsI(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
permutations_i.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunc backtrackI(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if !(*selected)[i] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrackI(state, choices, selected, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunc permutationsI(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackI(&state, &nums, &selected, &res)\n    return res\n}\n
permutations_i.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (i, choice) in choices.enumerated() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if !selected[i] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true\n            state.append(choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunc permutationsI(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
permutations_i.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunction backtrack(state, choices, selected, res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    choices.forEach((choice, i) => {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunction permutationsI(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
permutations_i.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    choices.forEach((choice, i) => {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfunction permutationsI(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
permutations_i.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n    if (!selected[i]) {\n      // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = true;\n      state.add(choice);\n      // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res);\n      // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nList<List<int>> permutationsI(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
permutations_i.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if !selected[i] {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state.clone(), choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfn permutations_i(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
permutations_i.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nint **permutationsI(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
permutations_i.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n        if (!selected[i]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = true\n            state.add(choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I */\nfun permutationsI(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
permutations_i.rb
=begin\nFile: permutations_i.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I ###\ndef backtrack(state, choices, selected, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  choices.each_with_index do |choice, i|\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n    unless selected[i]\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = true\n      state << choice\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res)\n      # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n=begin\nFile: permutations_i.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I ###\ndef backtrack(state, choices, selected, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  choices.each_with_index do |choice, i|\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\n    unless selected[i]\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = true\n      state << choice\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res)\n      # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n# ## \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 I ###\ndef permutations_i(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_backtracking/permutations_problem/#1322","title":"13.2.2 \u00a0 \u0423\u0447\u0435\u0442 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u0432\u0441\u0435 \u043d\u0435\u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438.

\u041f\u0443\u0441\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0430\u0432\u0435\u043d \\([1, 1, 2]\\) . \u0427\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0434\u0432\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(1\\) , \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u0438\u0437 \u043d\u0438\u0445 \u043a\u0430\u043a \\(\\hat{1}\\) .

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-7, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043c\u0435\u0442\u043e\u0434 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0435\u0439\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-7 \u00a0 \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438

\u041a\u0430\u043a \u0436\u0435 \u0443\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438? \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 - \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0445\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430. \u041d\u043e \u044d\u0442\u043e \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0438\u0437\u044f\u0449\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u044e\u0449\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b, \u0432\u043e\u043e\u0431\u0449\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c: \u0438\u0445 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438 \u043e\u0442\u0441\u0435\u043a\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

"},{"location":"chapter_backtracking/permutations_problem/#1_1","title":"1. \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 13-8: \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(1\\) \u0438\u043b\u0438 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(\\hat{1}\\) - \u044d\u0442\u043e \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432, \u0431\u0443\u0434\u0443\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0435\u0442\u0432\u044c \\(\\hat{1}\\) \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u0435\u0447\u044c.

\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(2\\) , \u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u043e\u0440\u044b \\(1\\) \u0438 \\(\\hat{1}\\) \u0441\u043d\u043e\u0432\u0430 \u0441\u043e\u0437\u0434\u0430\u0434\u0443\u0442 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u0435\u0442\u0432\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0435\u0442\u0432\u044c \\(\\hat{1}\\) \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u0435\u0447\u044c.

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u0438\u0440\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-8 \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a

"},{"location":"chapter_backtracking/permutations_problem/#2_1","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u0437\u0430\u0432\u043e\u0434\u0438\u0442\u044c \u0445\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e duplicated , \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0432 \u044d\u0442\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435, \u0438 \u043e\u0442\u0441\u0435\u043a\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044b:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby permutations_ii.py
def backtrack(\n    state: list[int], choices: list[int], selected: list[bool], res: list[list[int]]\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II\"\"\"\n    # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if len(state) == len(choices):\n        res.append(list(state))\n        return\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    duplicated = set[int]()\n    for i, choice in enumerate(choices):\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if not selected[i] and choice not in duplicated:\n            # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.add(choice)  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = True\n            state.append(choice)\n            # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res)\n            # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = False\n            state.pop()\n\ndef permutations_ii(nums: list[int]) -> list[list[int]]:\n    \"\"\"\u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II\"\"\"\n    res = []\n    backtrack(state=[], choices=nums, selected=[False] * len(nums), res=res)\n    return res\n
permutations_ii.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvoid backtrack(vector<int> &state, const vector<int> &choices, vector<bool> &selected, vector<vector<int>> &res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size() == choices.size()) {\n        res.push_back(state);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    unordered_set<int> duplicated;\n    for (int i = 0; i < choices.size(); i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && duplicated.find(choice) == duplicated.end()) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.emplace(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.push_back(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop_back();\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvector<vector<int>> permutationsII(vector<int> nums) {\n    vector<int> state;\n    vector<bool> selected(nums.size(), false);\n    vector<vector<int>> res;\n    backtrack(state, nums, selected, res);\n    return res;\n}\n
permutations_ii.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvoid backtrack(List<Integer> state, int[] choices, boolean[] selected, List<List<Integer>> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size() == choices.length) {\n        res.add(new ArrayList<Integer>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    Set<Integer> duplicated = new HashSet<Integer>();\n    for (int i = 0; i < choices.length; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.add(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.add(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.remove(state.size() - 1);\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nList<List<Integer>> permutationsII(int[] nums) {\n    List<List<Integer>> res = new ArrayList<List<Integer>>();\n    backtrack(new ArrayList<Integer>(), nums, new boolean[nums.length], res);\n    return res;\n}\n
permutations_ii.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvoid Backtrack(List<int> state, int[] choices, bool[] selected, List<List<int>> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.Count == choices.Length) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    HashSet<int> duplicated = [];\n    for (int i = 0; i < choices.Length; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated.Contains(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.Add(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.Add(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            Backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.RemoveAt(state.Count - 1);\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nList<List<int>> PermutationsII(int[] nums) {\n    List<List<int>> res = [];\n    Backtrack([], nums, new bool[nums.Length], res);\n    return res;\n}\n
permutations_ii.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunc backtrackII(state *[]int, choices *[]int, selected *[]bool, res *[][]int) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if len(*state) == len(*choices) {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    duplicated := make(map[int]struct{}, 0)\n    for i := 0; i < len(*choices); i++ {\n        choice := (*choices)[i]\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if _, ok := duplicated[choice]; !ok && !(*selected)[i] {\n            // \u041f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n            duplicated[choice] = struct{}{}\n            (*selected)[i] = true\n            *state = append(*state, choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrackII(state, choices, selected, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            (*selected)[i] = false\n            *state = (*state)[:len(*state)-1]\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunc permutationsII(nums []int) [][]int {\n    res := make([][]int, 0)\n    state := make([]int, 0)\n    selected := make([]bool, len(nums))\n    backtrackII(&state, &nums, &selected, &res)\n    return res\n}\n
permutations_ii.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunc backtrack(state: inout [Int], choices: [Int], selected: inout [Bool], res: inout [[Int]]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if state.count == choices.count {\n        res.append(state)\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    var duplicated: Set<Int> = []\n    for (i, choice) in choices.enumerated() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if !selected[i], !duplicated.contains(choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.insert(choice) // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true\n            state.append(choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state: &state, choices: choices, selected: &selected, res: &res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false\n            state.removeLast()\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunc permutationsII(nums: [Int]) -> [[Int]] {\n    var state: [Int] = []\n    var selected = Array(repeating: false, count: nums.count)\n    var res: [[Int]] = []\n    backtrack(state: &state, choices: nums, selected: &selected, res: &res)\n    return res\n}\n
permutations_ii.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunction backtrack(state, choices, selected, res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.add(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunction permutationsII(nums) {\n    const res = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
permutations_ii.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunction backtrack(\n    state: number[],\n    choices: number[],\n    selected: boolean[],\n    res: number[][]\n): void {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.length === choices.length) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    const duplicated = new Set();\n    choices.forEach((choice, i) => {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated.has(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.add(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    });\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfunction permutationsII(nums: number[]): number[][] {\n    const res: number[][] = [];\n    backtrack([], nums, Array(nums.length).fill(false), res);\n    return res;\n}\n
permutations_ii.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvoid backtrack(\n  List<int> state,\n  List<int> choices,\n  List<bool> selected,\n  List<List<int>> res,\n) {\n  // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (state.length == choices.length) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  Set<int> duplicated = {};\n  for (int i = 0; i < choices.length; i++) {\n    int choice = choices[i];\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n    if (!selected[i] && !duplicated.contains(choice)) {\n      // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      duplicated.add(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n      selected[i] = true;\n      state.add(choice);\n      // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res);\n      // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false;\n      state.removeLast();\n    }\n  }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nList<List<int>> permutationsII(List<int> nums) {\n  List<List<int>> res = [];\n  backtrack([], nums, List.filled(nums.length, false), res);\n  return res;\n}\n
permutations_ii.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfn backtrack(mut state: Vec<i32>, choices: &[i32], selected: &mut [bool], res: &mut Vec<Vec<i32>>) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if state.len() == choices.len() {\n        res.push(state);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    let mut duplicated = HashSet::<i32>::new();\n    for i in 0..choices.len() {\n        let choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if !selected[i] && !duplicated.contains(&choice) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.insert(choice); // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state.push(choice);\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state.clone(), choices, selected, res);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n            state.pop();\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfn permutations_ii(nums: &mut [i32]) -> Vec<Vec<i32>> {\n    let mut res = Vec::new();\n    backtrack(Vec::new(), nums, &mut vec![false; nums.len()], &mut res);\n    res\n}\n
permutations_ii.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nvoid backtrack(int *state, int stateSize, int *choices, int choicesSize, bool *selected, int **res, int *resSize) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (stateSize == choicesSize) {\n        res[*resSize] = (int *)malloc(choicesSize * sizeof(int));\n        for (int i = 0; i < choicesSize; i++) {\n            res[*resSize][i] = state[i];\n        }\n        (*resSize)++;\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    bool duplicated[MAX_SIZE] = {false};\n    for (int i = 0; i < choicesSize; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated[choice]) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated[choice] = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true;\n            state[stateSize] = choice;\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, stateSize + 1, choices, choicesSize, selected, res, resSize);\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false;\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nint **permutationsII(int *nums, int numsSize, int *returnSize) {\n    int *state = (int *)malloc(numsSize * sizeof(int));\n    bool *selected = (bool *)malloc(numsSize * sizeof(bool));\n    for (int i = 0; i < numsSize; i++) {\n        selected[i] = false;\n    }\n    int **res = (int **)malloc(MAX_SIZE * sizeof(int *));\n    *returnSize = 0;\n\n    backtrack(state, 0, nums, numsSize, selected, res, returnSize);\n\n    free(state);\n    free(selected);\n\n    return res;\n}\n
permutations_ii.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfun backtrack(\n    state: MutableList<Int>,\n    choices: IntArray,\n    selected: BooleanArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (state.size == choices.size) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    val duplicated = HashSet<Int>()\n    for (i in choices.indices) {\n        val choice = choices[i]\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n        if (!selected[i] && !duplicated.contains(choice)) {\n            // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            duplicated.add(choice) // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            selected[i] = true\n            state.add(choice)\n            // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n            backtrack(state, choices, selected, res)\n            // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n            selected[i] = false\n            state.removeAt(state.size - 1)\n        }\n    }\n}\n\n/* \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II */\nfun permutationsII(nums: IntArray): MutableList<MutableList<Int>?> {\n    val res = mutableListOf<MutableList<Int>?>()\n    backtrack(mutableListOf(), nums, BooleanArray(nums.size), res)\n    return res\n}\n
permutations_ii.rb
=begin\nFile: permutations_ii.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II ###\ndef backtrack(state, choices, selected, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n    if !selected[i] && !duplicated.include?(choice)\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res)\n      # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n=begin\nFile: permutations_ii.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II ###\ndef backtrack(state, choices, selected, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u0430 \u0447\u0438\u0441\u043b\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if state.length == choices.length\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  duplicated = Set.new\n  choices.each_with_index do |choice, i|\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\n    if !selected[i] && !duplicated.include?(choice)\n      # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      duplicated.add(choice)\n      selected[i] = true\n      state << choice\n      # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n      backtrack(state, choices, selected, res)\n      # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n      selected[i] = false\n      state.pop\n    end\n  end\nend\n\n# ## \u0412\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 II ###\ndef permutations_ii(nums)\n  res = []\n  backtrack([], nums, Array.new(nums.length, false), res)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0415\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u043f\u0430\u0440\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b, \u0442\u043e \u0438\u0437 \\(n\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \\(n!\\) \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a; \u043f\u0440\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u0447\u0442\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n!n)\\) .

\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) , \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(n)\\) \u0441\u0442\u0435\u043a\u043e\u0432\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u041c\u0430\u0441\u0441\u0438\u0432 selected \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430. \u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0434\u043e \\(n\\) \u0445\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 duplicated , \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \\(O(n^2)\\) \u043f\u0430\u043c\u044f\u0442\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) .

"},{"location":"chapter_backtracking/permutations_problem/#3","title":"3. \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0432\u0438\u0434\u043e\u0432 \u043e\u0431\u0440\u0435\u0437\u043a\u0438","text":"

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0445\u043e\u0442\u044f \u0438 selected , \u0438 duplicated \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0435\u0437\u043a\u0438, \u0438\u0445 \u0446\u0435\u043b\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-9 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0432\u0443\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043e\u0431\u0440\u0435\u0437\u043a\u0438. \u041f\u043e\u043c\u043d\u0438\u0442\u0435, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443, \u0430 \u043f\u0443\u0442\u044c \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u043b\u0438\u0441\u0442\u0430 \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u043e\u0434\u043d\u0443 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-9 \u00a0 \u041e\u0431\u043b\u0430\u0441\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u0434\u0432\u0443\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u043e\u0431\u0440\u0435\u0437\u043a\u0438

"},{"location":"chapter_backtracking/subset_sum_problem/","title":"13.3 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0441\u0443\u043c\u043c\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432","text":""},{"location":"chapter_backtracking/subset_sum_problem/#1331","title":"13.3.1 \u00a0 \u0421\u043b\u0443\u0447\u0430\u0439 \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b nums \u0438 \u0446\u0435\u043b\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 target . \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438, \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0432\u043d\u0430 target . \u0412\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043d\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u044d\u0442\u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u0430; \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \\(\\{3, 4, 5\\}\\) \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \\(9\\) \u0440\u0435\u0448\u0435\u043d\u0438\u044f\u043c\u0438 \u0431\u0443\u0434\u0443\u0442 \\(\\{3, 3, 3\\}\\) \u0438 \\(\\{4, 5\\}\\) . \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043e\u0431\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430.

"},{"location":"chapter_backtracking/subset_sum_problem/#1","title":"1. \u00a0 \u041e\u0442\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u043e\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445","text":"

\u041a\u0430\u043a \u0438 \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 \u043a\u0430\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \"\u0441\u0443\u043c\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\"; \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u0430 \u0441\u0443\u043c\u043c\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u043e\u0439 target , \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.

\u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430\u0445 \u0432 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u0431\u0443\u043b\u0435\u0432 \u0441\u043f\u0438\u0441\u043e\u043a selected \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0442\u043e\u0433\u043e, \u0431\u044b\u043b \u043b\u0438 \u0432\u044b\u0431\u0440\u0430\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u041c\u043e\u0436\u043d\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u0440\u0435\u0448\u0435\u043d\u0438\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i_naive.py
def backtrack(\n    state: list[int],\n    target: int,\n    total: int,\n    choices: list[int],\n    res: list[list[int]],\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if total == target:\n        res.append(list(state))\n        return\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i in range(len(choices)):\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if total + choices[i] > target:\n            continue\n        # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.append(choices[i])\n        # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res)\n        # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop()\n\ndef subset_sum_i_naive(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438)\"\"\"\n    state = []  # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    total = 0  # \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    res = []  # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res)\n    return res\n
subset_sum_i_naive.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(vector<int> &state, int target, int total, vector<int> &choices, vector<vector<int>> &res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total == target) {\n        res.push_back(state);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (size_t i = 0; i < choices.size(); i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.push_back(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop_back();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nvector<vector<int>> subsetSumINaive(vector<int> &nums, int target) {\n    vector<int> state;       // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    int total = 0;           // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    vector<vector<int>> res; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
subset_sum_i_naive.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(List<Integer> state, int target, int total, int[] choices, List<List<Integer>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total == target) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nList<List<Integer>> subsetSumINaive(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    int total = 0; // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    List<List<Integer>> res = new ArrayList<>(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
subset_sum_i_naive.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid Backtrack(List<int> state, int target, int total, int[] choices, List<List<int>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total == target) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choices.Length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.Add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        Backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nList<List<int>> SubsetSumINaive(int[] nums, int target) {\n    List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    int total = 0; // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    Backtrack(state, target, total, nums, res);\n    return res;\n}\n
subset_sum_i_naive.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc backtrackSubsetSumINaive(total, target int, state, choices *[]int, res *[][]int) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == total {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i := 0; i < len(*choices); i++ {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if total+(*choices)[i] > target {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        *state = append(*state, (*choices)[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrackSubsetSumINaive(total+(*choices)[i], target, state, choices, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfunc subsetSumINaive(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    total := 0              // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    res := make([][]int, 0) // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrackSubsetSumINaive(total, target, &state, &nums, &res)\n    return res\n}\n
subset_sum_i_naive.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc backtrack(state: inout [Int], target: Int, total: Int, choices: [Int], res: inout [[Int]]) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if total == target {\n        res.append(state)\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i in choices.indices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if total + choices[i] > target {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.append(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state: &state, target: target, total: total + choices[i], choices: choices, res: &res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeLast()\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfunc subsetSumINaive(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    let total = 0 // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    var res: [[Int]] = [] // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state: &state, target: target, total: total, choices: nums, res: &res)\n    return res\n}\n
subset_sum_i_naive.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction backtrack(state, target, total, choices, res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (let i = 0; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfunction subsetSumINaive(nums, target) {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    const total = 0; // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
subset_sum_i_naive.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    total: number,\n    choices: number[],\n    res: number[][]\n): void {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total === target) {\n        res.push([...state]);\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (let i = 0; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfunction subsetSumINaive(nums: number[], target: number): number[][] {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    const total = 0; // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res);\n    return res;\n}\n
subset_sum_i_naive.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  int total,\n  List<int> choices,\n  List<List<int>> res,\n) {\n  // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (total == target) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for (int i = 0; i < choices.length; i++) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n    if (total + choices[i] > target) {\n      continue;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n    state.add(choices[i]);\n    // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target, total + choices[i], choices, res);\n    // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.removeLast();\n  }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nList<List<int>> subsetSumINaive(List<int> nums, int target) {\n  List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  int total = 0; // \u0421\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n  List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, total, nums, res);\n  return res;\n}\n
subset_sum_i_naive.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    total: i32,\n    choices: &[i32],\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if total == target {\n        res.push(state.clone());\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for i in 0..choices.len() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if total + choices[i] > target {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfn subset_sum_i_naive(nums: &[i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    let total = 0; // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    let mut res = Vec::new(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(&mut state, target, total, nums, &mut res);\n    res\n}\n
subset_sum_i_naive.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(int target, int total, int *choices, int choicesSize) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total == target) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < choicesSize; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state[stateSize++] = choices[i];\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(target, total + choices[i], choices, choicesSize);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        stateSize--;\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nvoid subsetSumINaive(int *nums, int numsSize, int target) {\n    resSize = 0; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0443\u043b\u0435\u043c\n    backtrack(target, 0, nums, numsSize);\n}\n
subset_sum_i_naive.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    total: Int,\n    choices: IntArray,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (total == target) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (i in choices.indices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n        if (total + choices[i] > target) {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n        state.add(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target, total + choices[i], choices, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) */\nfun subsetSumINaive(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    val total = 0 // \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    val res = mutableListOf<MutableList<Int>?>() // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, total, nums, res)\n    return res\n}\n
subset_sum_i_naive.rb
=begin\nFile: subset_sum_i_naive.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I ###\ndef backtrack(state, target, total, choices, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for i in 0...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n    next if total + choices[i] > target\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target, total + choices[i], choices, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n=begin\nFile: subset_sum_i_naive.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I ###\ndef backtrack(state, target, total, choices, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if total == target\n    res << state.dup\n    return\n  end\n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for i in 0...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0432\u044b\u0431\u043e\u0440\n    next if total + choices[i] > target\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 total\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target, total + choices[i], choices, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438) ###\ndef subset_sum_i_naive(nums, target)\n  state = [] # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  total = 0 # \u0421\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  res = [] # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, total, nums, res)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0415\u0441\u043b\u0438 \u043f\u043e\u0434\u0430\u0442\u044c \u043d\u0430 \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432 \\([3, 4, 5]\\) \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(9\\) , \u0442\u043e \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \\([3, 3, 3], [4, 5], [5, 4]\\) . \u0425\u043e\u0442\u044f \u0432\u0441\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0441 \u0441\u0443\u043c\u043c\u043e\u0439 \\(9\\) \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u044b, \u0441\u0440\u0435\u0434\u0438 \u043d\u0438\u0445 \u0432\u0441\u0435 \u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b: \\([4, 5]\\) \u0438 \\([5, 4]\\) .

\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u044b\u0431\u043e\u0440\u0430, \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u0434\u043b\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043d\u0435 \u0432\u0430\u0436\u0435\u043d. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-10, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(4\\) , \u0430 \u0437\u0430\u0442\u0435\u043c \\(5\\) , \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(5\\) , \u0430 \u0437\u0430\u0442\u0435\u043c \\(4\\) - \u044d\u0442\u043e \u0440\u0430\u0437\u043d\u044b\u0435 \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u043d\u043e \u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-10 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 \u0438 \u043e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e \u0432\u044b\u0445\u043e\u0434\u0443 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443

\u0427\u0442\u043e\u0431\u044b \u0443\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430, \u043e\u0434\u043d\u0430 \u0438\u0437 \u043f\u0440\u044f\u043c\u044b\u0445 \u0438\u0434\u0435\u0439 - \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b \u0443\u0436\u0435 \u0438\u0437 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432. \u041d\u043e \u044d\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043c\u0430\u043b\u043e\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u043e \u0434\u0432\u0443\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c.

"},{"location":"chapter_backtracking/subset_sum_problem/#2","title":"2. \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432","text":"

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u043e\u0438\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432 \u043f\u0440\u044f\u043c\u043e \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u0440\u0435\u0437\u043a\u0438. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 13-11: \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a.

  1. \u0415\u0441\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0438 \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0430\u0445 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\(3\\) \u0438 \\(4\\) , \u0442\u043e \u0431\u0443\u0434\u0443\u0442 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432\u0441\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0438 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u0430\u043a \\([3, 4, \\dots]\\) .
  2. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(4\\) , \u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \\(3\\) , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \\([4, 3, \\dots]\\) \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0442 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430, \u0443\u0436\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u0448\u0430\u0433\u0435 1. .

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u044b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u0440\u043e\u0431\u0443\u044e\u0442\u0441\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u0435\u043c \u043f\u0440\u0430\u0432\u0435\u0435 \u0432\u0435\u0442\u0432\u044c, \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0435\u0442\u0432\u0435\u0439 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u043e.

  1. \u0412 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0440\u0430\u0443\u043d\u0434\u0430\u0445 \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \\(3\\) \u0438 \\(5\\) , \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \\([3, 5, \\dots]\\) .
  2. \u0412 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0440\u0430\u0443\u043d\u0434\u0430\u0445 \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \\(4\\) \u0438 \\(5\\) , \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \\([4, 5, \\dots]\\) .
  3. \u0415\u0441\u043b\u0438 \u0436\u0435 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \\(5\\) , \u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \\(3\\) \u0438 \\(4\\) , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \\([5, 3, \\dots]\\) \u0438 \\([5, 4, \\dots]\\) \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0442 \u0441\u043b\u0443\u0447\u0430\u0438, \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432 \u0448\u0430\u0433\u0430\u0445 1. \u0438 2. .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-11 \u00a0 \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0438\u0437-\u0437\u0430 \u0440\u0430\u0437\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u0430

\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434 \\([x_1, x_2, \\dots, x_n]\\) , \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u0432 \u0445\u043e\u0434\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0430\u0432\u043d\u0430 \\([x_{i_1}, x_{i_2}, \\dots, x_{i_m}]\\) , \u0442\u043e \u043e\u043d\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044e \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) ; \u0432\u0441\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u0432, \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0435 \u044d\u0442\u043e\u043c\u0443 \u0443\u0441\u043b\u043e\u0432\u0438\u044e, \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u0430\u043c \u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0442\u0441\u0435\u043a\u0430\u0442\u044c\u0441\u044f.

"},{"location":"chapter_backtracking/subset_sum_problem/#3","title":"3. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u0431\u0440\u0435\u0437\u043a\u0443, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e start , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u043e\u0431\u0445\u043e\u0434\u0430. \u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(x_i\\) \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0443\u043d\u0434 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \\(i\\). \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u0432\u0441\u0435\u0433\u0434\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044e \\(i_1 \\leq i_2 \\leq \\dots \\leq i_m\\) , \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043a\u0430\u0436\u0434\u043e\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437.

\u041f\u043e\u043c\u0438\u043c\u043e \u044d\u0442\u043e\u0433\u043e, \u043c\u044b \u0432\u043d\u0435\u0441\u0435\u043c \u0432 \u043a\u043e\u0434 \u0435\u0449\u0435 \u0434\u0432\u0430 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_i.py
def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for i in range(start, len(choices)):\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0:\n            break\n        # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.append(choices[i])\n        # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res)\n        # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop()\n\ndef subset_sum_i(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I\"\"\"\n    state = []  # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort()  # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    start = 0  # \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    res = []  # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res)\n    return res\n
subset_sum_i.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (int i = start; i < choices.size(); i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push_back(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop_back();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvector<vector<int>> subsetSumI(vector<int> &nums, int target) {\n    vector<int> state;              // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    sort(nums.begin(), nums.end()); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0;                  // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    vector<vector<int>> res;        // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_i.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (int i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nList<List<Integer>> subsetSumI(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    Arrays.sort(nums); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<List<Integer>> res = new ArrayList<>(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_i.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (int i = start; i < choices.Length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.Add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        Backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nList<List<int>> SubsetSumI(int[] nums, int target) {\n    List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    Array.Sort(nums); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_i.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc backtrackSubsetSumI(start, target int, state, choices *[]int, res *[][]int) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for i := start; i < len(*choices); i++ {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        *state = append(*state, (*choices)[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrackSubsetSumI(i, target-(*choices)[i], state, choices, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc subsetSumI(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    sort.Ints(nums)         // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    start := 0              // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    res := make([][]int, 0) // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrackSubsetSumI(start, target, &state, &nums, &res)\n    return res\n}\n
subset_sum_i.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for i in choices.indices.dropFirst(start) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.append(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i, res: &res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeLast()\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunc subsetSumI(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    let nums = nums.sorted() // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    let start = 0 // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    var res: [[Int]] = [] // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
subset_sum_i.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction backtrack(state, target, choices, start, res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (let i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction subsetSumI(nums, target) {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort((a, b) => a - b); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    const start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_i.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (let i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfunction subsetSumI(nums: number[], target: number): number[][] {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort((a, b) => a - b); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    const start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_i.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  for (int i = start; i < choices.length; i++) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state.add(choices[i]);\n    // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i, res);\n    // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.removeLast();\n  }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nList<List<int>> subsetSumI(List<int> nums, int target) {\n  List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  nums.sort(); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n  int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n  List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
subset_sum_i.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for i in start..choices.len() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfn subset_sum_i(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort(); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    let start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    let mut res = Vec::new(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
subset_sum_i.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        for (int i = 0; i < stateSize; ++i) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (int i = start; i < choicesSize; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(target - choices[i], choices, choicesSize, i);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        stateSize--;\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nvoid subsetSumI(int *nums, int numsSize, int target) {\n    qsort(nums, numsSize, sizeof(int), cmp); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0;                           // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    backtrack(target, nums, numsSize, start);\n}\n
subset_sum_i.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    for (i in start..<choices.size) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.add(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I */\nfun subsetSumI(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort() // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    val start = 0 // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    val res = mutableListOf<MutableList<Int>?>() // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
subset_sum_i.rb
=begin\nFile: subset_sum_i.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I ###\ndef backtrack(state, target, choices, start, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  for i in start...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    break if target - choices[i] < 0\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n=begin\nFile: subset_sum_i.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I ###\ndef backtrack(state, target, choices, start, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if target.zero?\n    res << state.dup\n    return\n  end\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  for i in start...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    break if target - choices[i] < 0\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I ###\ndef subset_sum_i(nums, target)\n  state = [] # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  nums.sort! # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n  start = 0 # \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n  res = [] # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, nums, start, res)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-12 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 backtracking \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\([3, 4, 5]\\) \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \\(9\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-12 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 backtracking \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0441\u0443\u043c\u043c\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 I

"},{"location":"chapter_backtracking/subset_sum_problem/#1332","title":"13.3.2 \u00a0 \u0423\u0447\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b nums \u0438 \u0446\u0435\u043b\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 target . \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438, \u0441\u0443\u043c\u043c\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0432\u043d\u0430 target . \u0412\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u044d\u0442\u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u0430; \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439.

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0432\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0438 \u044d\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0434\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \\([4, \\hat{4}, 5]\\) \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(9\\) , \u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0432\u0435\u0440\u043d\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \\([4, 5], [\\hat{4}, 5]\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c.

\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0434\u0443\u0431\u043b\u0435\u0439 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0440\u0430\u0443\u043d\u0434\u0435. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-13 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0440\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0432\u044b\u0431\u043e\u0440\u0430, \u0438 \u0434\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0430\u0432\u043d\u044b \\(4\\) ; \u0438\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u0432\u0435 \u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0435\u0441\u044f \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430. \u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(4\\) \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0442\u043e\u0436\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u044e\u0442 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-13 \u00a0 \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0438\u0437-\u0437\u0430 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432

"},{"location":"chapter_backtracking/subset_sum_problem/#1_1","title":"1. \u00a0 \u041e\u0431\u0440\u0435\u0437\u043a\u0430 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043d\u0443\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0440\u0430\u0432\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043d\u0438\u0445 \u0432\u044b\u0431\u0438\u0440\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0438\u0437\u044f\u0449\u043d\u043e: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0430\u0441\u0441\u0438\u0432 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0440\u0430\u0432\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u0442\u043e\u044f\u0442 \u0440\u044f\u0434\u043e\u043c. \u0417\u043d\u0430\u0447\u0438\u0442, \u0435\u0441\u043b\u0438 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u0441\u043e\u0441\u0435\u0434\u043d\u0435\u043c\u0443 \u0441\u043b\u0435\u0432\u0430, \u0442\u043e \u044d\u0442\u043e\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0443\u0436\u0435 \u0431\u044b\u043b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d, \u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c.

\u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u044d\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e start : \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0431\u043e\u0440\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(x_i\\) \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0430\u0443\u043d\u0434 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \\(i + 1\\) . \u0422\u0430\u043a \u043c\u044b \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.

"},{"location":"chapter_backtracking/subset_sum_problem/#2_1","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby subset_sum_ii.py
def backtrack(\n    state: list[int], target: int, choices: list[int], start: int, res: list[list[int]]\n):\n    \"\"\"\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0:\n        res.append(list(state))\n        return\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for i in range(start, len(choices)):\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0:\n            break\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if i > start and choices[i] == choices[i - 1]:\n            continue\n        # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.append(choices[i])\n        # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop()\n\ndef subset_sum_ii(nums: list[int], target: int) -> list[list[int]]:\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II\"\"\"\n    state = []  # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort()  # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    start = 0  # \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    res = []  # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res)\n    return res\n
subset_sum_ii.cpp
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid backtrack(vector<int> &state, int target, vector<int> &choices, int start, vector<vector<int>> &res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.push_back(state);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (int i = start; i < choices.size(); i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push_back(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop_back();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvector<vector<int>> subsetSumII(vector<int> &nums, int target) {\n    vector<int> state;              // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    sort(nums.begin(), nums.end()); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0;                  // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    vector<vector<int>> res;        // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_ii.java
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid backtrack(List<Integer> state, int target, int[] choices, int start, List<List<Integer>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.add(new ArrayList<>(state));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (int i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.remove(state.size() - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nList<List<Integer>> subsetSumII(int[] nums, int target) {\n    List<Integer> state = new ArrayList<>(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    Arrays.sort(nums); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<List<Integer>> res = new ArrayList<>(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_ii.cs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid Backtrack(List<int> state, int target, int[] choices, int start, List<List<int>> res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.Add(new List<int>(state));\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (int i = start; i < choices.Length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.Add(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        Backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.RemoveAt(state.Count - 1);\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nList<List<int>> SubsetSumII(int[] nums, int target) {\n    List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    Array.Sort(nums); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    Backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_ii.go
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunc backtrackSubsetSumII(start, target int, state, choices *[]int, res *[][]int) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        newState := append([]int{}, *state...)\n        *res = append(*res, newState)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for i := start; i < len(*choices); i++ {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target-(*choices)[i] < 0 {\n            break\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if i > start && (*choices)[i] == (*choices)[i-1] {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        *state = append(*state, (*choices)[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrackSubsetSumII(i+1, target-(*choices)[i], state, choices, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        *state = (*state)[:len(*state)-1]\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunc subsetSumII(nums []int, target int) [][]int {\n    state := make([]int, 0) // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    sort.Ints(nums)         // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    start := 0              // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    res := make([][]int, 0) // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrackSubsetSumII(start, target, &state, &nums, &res)\n    return res\n}\n
subset_sum_ii.swift
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunc backtrack(state: inout [Int], target: Int, choices: [Int], start: Int, res: inout [[Int]]) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        res.append(state)\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for i in choices.indices.dropFirst(start) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0 {\n            break\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if i > start, choices[i] == choices[i - 1] {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.append(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state: &state, target: target - choices[i], choices: choices, start: i + 1, res: &res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeLast()\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunc subsetSumII(nums: [Int], target: Int) -> [[Int]] {\n    var state: [Int] = [] // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    let nums = nums.sorted() // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    let start = 0 // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    var res: [[Int]] = [] // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state: &state, target: target, choices: nums, start: start, res: &res)\n    return res\n}\n
subset_sum_ii.js
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunction backtrack(state, target, choices, start, res) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (let i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunction subsetSumII(nums, target) {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort((a, b) => a - b); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    const start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_ii.ts
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunction backtrack(\n    state: number[],\n    target: number,\n    choices: number[],\n    start: number,\n    res: number[][]\n): void {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target === 0) {\n        res.push([...state]);\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (let i = start; i < choices.length; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] === choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfunction subsetSumII(nums: number[], target: number): number[][] {\n    const state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort((a, b) => a - b); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    const start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    const res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res);\n    return res;\n}\n
subset_sum_ii.dart
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid backtrack(\n  List<int> state,\n  int target,\n  List<int> choices,\n  int start,\n  List<List<int>> res,\n) {\n  // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if (target == 0) {\n    res.add(List.from(state));\n    return;\n  }\n  // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  for (int i = start; i < choices.length; i++) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    if (target - choices[i] < 0) {\n      break;\n    }\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n    if (i > start && choices[i] == choices[i - 1]) {\n      continue;\n    }\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state.add(choices[i]);\n    // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i + 1, res);\n    // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.removeLast();\n  }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nList<List<int>> subsetSumII(List<int> nums, int target) {\n  List<int> state = []; // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  nums.sort(); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n  int start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n  List<List<int>> res = []; // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, nums, start, res);\n  return res;\n}\n
subset_sum_ii.rs
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfn backtrack(\n    state: &mut Vec<i32>,\n    target: i32,\n    choices: &[i32],\n    start: usize,\n    res: &mut Vec<Vec<i32>>,\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if target == 0 {\n        res.push(state.clone());\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for i in start..choices.len() {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if target - choices[i] < 0 {\n            break;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if i > start && choices[i] == choices[i - 1] {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.push(choices[i]);\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.pop();\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfn subset_sum_ii(nums: &mut [i32], target: i32) -> Vec<Vec<i32>> {\n    let mut state = Vec::new(); // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort(); // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    let start = 0; // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    let mut res = Vec::new(); // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(&mut state, target, nums, start, &mut res);\n    res\n}\n
subset_sum_ii.c
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid backtrack(int target, int *choices, int choicesSize, int start) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        for (int i = 0; i < stateSize; i++) {\n            res[resSize][i] = state[i];\n        }\n        resColSizes[resSize++] = stateSize;\n        return;\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (int i = start; i < choicesSize; i++) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (target - choices[i] < 0) {\n            continue;\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state[stateSize] = choices[i];\n        stateSize++;\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(target - choices[i], choices, choicesSize, i + 1);\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        stateSize--;\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nvoid subsetSumII(int *nums, int numsSize, int target) {\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    qsort(nums, numsSize, sizeof(int), cmp);\n    // \u041d\u0430\u0447\u0430\u0442\u044c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\n    backtrack(target, nums, numsSize, 0);\n}\n
subset_sum_ii.kt
/* \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfun backtrack(\n    state: MutableList<Int>,\n    target: Int,\n    choices: IntArray,\n    start: Int,\n    res: MutableList<MutableList<Int>?>\n) {\n    // \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n    if (target == 0) {\n        res.add(state.toMutableList())\n        return\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    for (i in start..<choices.size) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n        // \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n        if (target - choices[i] < 0) {\n            break\n        }\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n        if (i > start && choices[i] == choices[i - 1]) {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n        state.add(choices[i])\n        // \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n        backtrack(state, target - choices[i], choices, i + 1, res)\n        // \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        state.removeAt(state.size - 1)\n    }\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II */\nfun subsetSumII(nums: IntArray, target: Int): MutableList<MutableList<Int>?> {\n    val state = mutableListOf<Int>() // \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n    nums.sort() // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n    val start = 0 // \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n    val res = mutableListOf<MutableList<Int>?>() // \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n    backtrack(state, target, nums, start, res)\n    return res\n}\n
subset_sum_ii.rb
=begin\nFile: subset_sum_ii.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II ###\ndef backtrack(state, target, choices, start, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  for i in start...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    break if target - choices[i] < 0\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n    next if i > start && choices[i] == choices[i - 1]\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n=begin\nFile: subset_sum_ii.rb\nCreated Time: 2024-05-22\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430: \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II ###\ndef backtrack(state, target, choices, start, res)\n  # \u0415\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 target, \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\n  if target.zero?\n    res << state.dup\n    return\n  end\n\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0432\u044b\u0431\u043e\u0440\u0430\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 2: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\n  # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 3: \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434 \u0441 start, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  for i in start...choices.length\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 1: \u0435\u0441\u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 target, \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0446\u0438\u043a\u043b\n    # \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435, \u0438 \u0441\u0443\u043c\u043c\u0430 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 target\n    break if target - choices[i] < 0\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435 4: \u0435\u0441\u043b\u0438 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0440\u0430\u0432\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043b\u0435\u0432\u0430, \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0442\u0432\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u0442\u0441\u044f, \u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c\n    next if i > start && choices[i] == choices[i - 1]\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c target \u0438 start\n    state << choices[i]\n    # \u041f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0432\u044b\u0431\u043e\u0440\u0443\n    backtrack(state, target - choices[i], choices, i + 1, res)\n    # \u041e\u0442\u043a\u0430\u0442: \u043e\u0442\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    state.pop\n  end\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u0443\u043c\u043c\u044b \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II ###\ndef subset_sum_ii(nums, target)\n  state = [] # \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 (\u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e)\n  nums.sort! # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c nums\n  start = 0 # \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430\n  res = [] # \u0421\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 (\u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432)\n  backtrack(state, target, nums, start, res)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 13-14 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 backtracking \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\([4, 4, 5]\\) \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \\(9\\) . \u0412 \u043d\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0442\u044b\u0440\u0435 \u0432\u0438\u0434\u0430 \u043e\u0431\u0440\u0435\u0437\u043a\u0438. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0438\u0441\u0443\u043d\u043e\u043a \u0441 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u043c\u0438 \u0432 \u043a\u043e\u0434\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0442\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u044b\u0439 \u0442\u0438\u043f \u043e\u0431\u0440\u0435\u0437\u043a\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 13-14 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 backtracking \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0441\u0443\u043c\u043c\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432 II

"},{"location":"chapter_backtracking/summary/","title":"13.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_backtracking/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_backtracking/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u041a\u0430\u043a \u043f\u043e\u043d\u044f\u0442\u044c \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439?

\u0412 \u0446\u0435\u043b\u043e\u043c backtracking - \u044d\u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f\", \u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \"\u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\".

"},{"location":"chapter_computational_complexity/","title":"\u0413\u043b\u0430\u0432\u0430 2. \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":"

Abstract

\u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u043e\u0431\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e-\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0432\u043e\u0434\u043d\u0438\u043a\u0443 \u0432 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0439 \u0432\u0441\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.

\u041e\u043d \u0432\u0435\u0434\u0435\u0442 \u043d\u0430\u0441 \u0432\u0433\u043b\u0443\u0431\u044c \u0434\u0432\u0443\u0445 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0439 - \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u043f\u043e\u043c\u043e\u0433\u0430\u044f \u0438\u0441\u043a\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u044f\u0449\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.

"},{"location":"chapter_computational_complexity/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_computational_complexity/iteration_and_recursion/","title":"2.2 \u00a0 \u0418\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f","text":"

\u0412 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0437\u0430\u0434\u0430\u0447\u0443, \u0438 \u044d\u0442\u043e \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043a \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0434\u0430\u0432\u0430\u0439 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c\u0441\u044f, \u043a\u0430\u043a \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430\u0445 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0435\u0435\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447, \u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u0434\u0432\u0443\u043c\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438: \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#221","title":"2.2.1 \u00a0 \u0418\u0442\u0435\u0440\u0430\u0446\u0438\u044f","text":"

\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u044f (iteration) - \u044d\u0442\u043e \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430, \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u0440\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438 \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f, \u043f\u043e\u043a\u0430 \u044d\u0442\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1-for","title":"1. \u00a0 \u0426\u0438\u043a\u043b for","text":"

\u0426\u0438\u043a\u043b for - \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0444\u043e\u0440\u043c \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043e\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0432 \u0442\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445, \u043a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u0439 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435.

\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0441\u0443\u043c\u043c\u044b \\(1 + 2 + \\dots + n\\) \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0446\u0438\u043a\u043b\u0430 for , \u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 res . \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 Python range(a, b) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \"\u043b\u0435\u0432\u043e-\u0437\u0430\u043c\u043a\u043d\u0443\u0442\u043e\u043c\u0443, \u043f\u0440\u0430\u0432\u043e-\u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443\" \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0443, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \\(a, a + 1, \\dots, b-1\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
def for_loop(n: int) -> int:\n    \"\"\"\u0426\u0438\u043a\u043b for\"\"\"\n    res = 0\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        res += i\n    return res\n
iteration.cpp
/* \u0426\u0438\u043a\u043b for */\nint forLoop(int n) {\n    int res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        res += i;\n    }\n    return res;\n}\n
iteration.java
/* \u0426\u0438\u043a\u043b for */\nint forLoop(int n) {\n    int res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
iteration.cs
/* \u0426\u0438\u043a\u043b for */\nint ForLoop(int n) {\n    int res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
iteration.go
/* \u0426\u0438\u043a\u043b for */\nfunc forLoop(n int) int {\n    res := 0\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        res += i\n    }\n    return res\n}\n
iteration.swift
/* \u0426\u0438\u043a\u043b for */\nfunc forLoop(n: Int) -> Int {\n    var res = 0\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        res += i\n    }\n    return res\n}\n
iteration.js
/* \u0426\u0438\u043a\u043b for */\nfunction forLoop(n) {\n    let res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
iteration.ts
/* \u0426\u0438\u043a\u043b for */\nfunction forLoop(n: number): number {\n    let res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
iteration.dart
/* \u0426\u0438\u043a\u043b for */\nint forLoop(int n) {\n  int res = 0;\n  // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    res += i;\n  }\n  return res;\n}\n
iteration.rs
/* \u0426\u0438\u043a\u043b for */\nfn for_loop(n: i32) -> i32 {\n    let mut res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for i in 1..=n {\n        res += i;\n    }\n    res\n}\n
iteration.c
/* \u0426\u0438\u043a\u043b for */\nint forLoop(int n) {\n    int res = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        res += i;\n    }\n    return res;\n}\n
iteration.kt
/* \u0426\u0438\u043a\u043b for */\nfun forLoop(n: Int): Int {\n    var res = 0\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        res += i\n    }\n    return res\n}\n
iteration.rb
=begin\nFile: iteration.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com), Cy (9738314@gmail.com)\n=end\n\n# ## \u0426\u0438\u043a\u043b for ###\ndef for_loop(n)\n  res = 0\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-1 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-1 \u00a0 \u0411\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

\u0427\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0443 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \"\u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\". \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0442\u0430\u043a\u0443\u044e \"\u043b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\". \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043d \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2-while","title":"2. \u00a0 \u0426\u0438\u043a\u043b while","text":"

\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u0446\u0438\u043a\u043b\u0443 for , \u0446\u0438\u043a\u043b while \u0442\u043e\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438. \u0412 \u0446\u0438\u043a\u043b\u0435 while \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435: \u0435\u0441\u043b\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0438\u0441\u0442\u0438\u043d\u043d\u043e, \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f, \u0438\u043d\u0430\u0447\u0435 \u0446\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f.

\u041d\u0438\u0436\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0446\u0438\u043a\u043b while \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u044b \\(1 + 2 + \\dots + n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
def while_loop(n: int) -> int:\n    \"\"\"\u0426\u0438\u043a\u043b while\"\"\"\n    res = 0\n    i = 1  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while i <= n:\n        res += i\n        i += 1  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    return res\n
iteration.cpp
/* \u0426\u0438\u043a\u043b while */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.java
/* \u0426\u0438\u043a\u043b while */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.cs
/* \u0426\u0438\u043a\u043b while */\nint WhileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i += 1; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.go
/* \u0426\u0438\u043a\u043b while */\nfunc whileLoop(n int) int {\n    res := 0\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    i := 1\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    for i <= n {\n        res += i\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++\n    }\n    return res\n}\n
iteration.swift
/* \u0426\u0438\u043a\u043b while */\nfunc whileLoop(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while i <= n {\n        res += i\n        i += 1 // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res\n}\n
iteration.js
/* \u0426\u0438\u043a\u043b while */\nfunction whileLoop(n) {\n    let res = 0;\n    let i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.ts
/* \u0426\u0438\u043a\u043b while */\nfunction whileLoop(n: number): number {\n    let res = 0;\n    let i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.dart
/* \u0426\u0438\u043a\u043b while */\nint whileLoop(int n) {\n  int res = 0;\n  int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n  // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  while (i <= n) {\n    res += i;\n    i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n  }\n  return res;\n}\n
iteration.rs
/* \u0426\u0438\u043a\u043b while */\nfn while_loop(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while i <= n {\n        res += i;\n        i += 1; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    res\n}\n
iteration.c
/* \u0426\u0438\u043a\u043b while */\nint whileLoop(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i;\n        i++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res;\n}\n
iteration.kt
/* \u0426\u0438\u043a\u043b while */\nfun whileLoop(n: Int): Int {\n    var res = 0\n    var i = 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n    while (i <= n) {\n        res += i\n        i++ // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    }\n    return res\n}\n
iteration.rb
=begin\nFile: iteration.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com), Cy (9738314@gmail.com)\n=end\n\n# ## \u0426\u0438\u043a\u043b for ###\ndef for_loop(n)\n  res = 0\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n\n# ## \u0426\u0438\u043a\u043b while ###\ndef while_loop(n)\n  res = 0\n  i = 1 # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n  end\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

**\u0426\u0438\u043a\u043b while \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0432\u043e\u0431\u043e\u0434\u043e\u0439, \u0447\u0435\u043c \u0446\u0438\u043a\u043b for **. \u0412 \u0446\u0438\u043a\u043b\u0435 while \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0448\u0430\u0433\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u0443\u0441\u043b\u043e\u0432\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \\(i\\) \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u0437\u0430 \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434, \u0438 \u0442\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0443\u0436\u0435 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0434\u043e\u0431\u043d\u043e \u0432\u044b\u0440\u0430\u0436\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0446\u0438\u043a\u043b for :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
def while_loop_ii(n: int) -> int:\n    \"\"\"\u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435)\"\"\"\n    res = 0\n    i = 1  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while i <= n:\n        res += i\n        # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i += 1\n        i *= 2\n    return res\n
iteration.cpp
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
iteration.java
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
iteration.cs
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nint WhileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i += 1; \n        i *= 2;\n    }\n    return res;\n}\n
iteration.go
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfunc whileLoopII(n int) int {\n    res := 0\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    i := 1\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    for i <= n {\n        res += i\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++\n        i *= 2\n    }\n    return res\n}\n
iteration.swift
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfunc whileLoopII(n: Int) -> Int {\n    var res = 0\n    var i = 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while i <= n {\n        res += i\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i += 1\n        i *= 2\n    }\n    return res\n}\n
iteration.js
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfunction whileLoopII(n) {\n    let res = 0;\n    let i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
iteration.ts
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfunction whileLoopII(n: number): number {\n    let res = 0;\n    let i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
iteration.dart
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nint whileLoopII(int n) {\n  int res = 0;\n  int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n  // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n  while (i <= n) {\n    res += i;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    i++;\n    i *= 2;\n  }\n  return res;\n}\n
iteration.rs
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfn while_loop_ii(n: i32) -> i32 {\n    let mut res = 0;\n    let mut i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while i <= n {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i += 1;\n        i *= 2;\n    }\n    res\n}\n
iteration.c
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nint whileLoopII(int n) {\n    int res = 0;\n    int i = 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i;\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++;\n        i *= 2;\n    }\n    return res;\n}\n
iteration.kt
/* \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) */\nfun whileLoopII(n: Int): Int {\n    var res = 0\n    var i = 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n    while (i <= n) {\n        res += i\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n        i++\n        i *= 2\n    }\n    return res\n}\n
iteration.rb
=begin\nFile: iteration.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com), Cy (9738314@gmail.com)\n=end\n\n# ## \u0426\u0438\u043a\u043b for ###\ndef for_loop(n)\n  res = 0\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n\n# ## \u0426\u0438\u043a\u043b while ###\ndef while_loop(n)\n  res = 0\n  i = 1 # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n  end\n\n  res\nend\n\n# ## \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) ###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n  while i <= n\n    res += i\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412 \u0446\u0435\u043b\u043e\u043c \u043a\u043e\u0434 \u0441 for \u043e\u0431\u044b\u0447\u043d\u043e \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u0435\u0435, \u0430 while \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043e\u043a; \u043e\u0431\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b. \u0412\u044b\u0431\u043e\u0440 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c\u0441\u044f \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3","title":"3. \u00a0 \u0412\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b","text":"

\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0432\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0432 \u0434\u0440\u0443\u0433\u0443\u044e; \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0446\u0438\u043a\u043b\u0430 for :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby iteration.py
def nested_for_loop(n: int) -> str:\n    \"\"\"\u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for\"\"\"\n    res = \"\"\n    # \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for i in range(1, n + 1):\n        # \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for j in range(1, n + 1):\n            res += f\"({i}, {j}), \"\n    return res\n
iteration.cpp
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nstring nestedForLoop(int n) {\n    ostringstream res;\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; ++i) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; ++j) {\n            res << \"(\" << i << \", \" << j << \"), \";\n        }\n    }\n    return res.str();\n}\n
iteration.java
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nString nestedForLoop(int n) {\n    StringBuilder res = new StringBuilder();\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.append(\"(\" + i + \", \" + j + \"), \");\n        }\n    }\n    return res.toString();\n}\n
iteration.cs
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nstring NestedForLoop(int n) {\n    StringBuilder res = new();\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            res.Append($\"({i}, {j}), \");\n        }\n    }\n    return res.ToString();\n}\n
iteration.go
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfunc nestedForLoop(n int) string {\n    res := \"\"\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= n; j++ {\n            // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n            res += fmt.Sprintf(\"(%d, %d), \", i, j)\n        }\n    }\n    return res\n}\n
iteration.swift
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfunc nestedForLoop(n: Int) -> String {\n    var res = \"\"\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for i in 1 ... n {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for j in 1 ... n {\n            res.append(\"(\\(i), \\(j)), \")\n        }\n    }\n    return res\n}\n
iteration.js
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfunction nestedForLoop(n) {\n    let res = '';\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
iteration.ts
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfunction nestedForLoop(n: number): string {\n    let res = '';\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (let i = 1; i <= n; i++) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (let j = 1; j <= n; j++) {\n            res += `(${i}, ${j}), `;\n        }\n    }\n    return res;\n}\n
iteration.dart
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nString nestedForLoop(int n) {\n  String res = \"\";\n  // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n  for (int i = 1; i <= n; i++) {\n    // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n    for (int j = 1; j <= n; j++) {\n      res += \"($i, $j), \";\n    }\n  }\n  return res;\n}\n
iteration.rs
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfn nested_for_loop(n: i32) -> String {\n    let mut res = vec![];\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for i in 1..=n {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for j in 1..=n {\n            res.push(format!(\"({}, {}), \", i, j));\n        }\n    }\n    res.join(\"\")\n}\n
iteration.c
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nchar *nestedForLoop(int n) {\n    // n * n \u2014 \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0442\u043e\u0447\u0435\u043a, \u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \"(i, j), \" \u0440\u0430\u0432\u043d\u0430 6+10*2, \u043f\u043b\u044e\u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043c\u0435\u0441\u0442\u043e \u0434\u043b\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u044e\u0449\u0435\u0433\u043e \u043d\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \\0\n    int size = n * n * 26 + 1;\n    char *res = malloc(size * sizeof(char));\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (int i = 1; i <= n; i++) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (int j = 1; j <= n; j++) {\n            char tmp[26];\n            snprintf(tmp, sizeof(tmp), \"(%d, %d), \", i, j);\n            strncat(res, tmp, size - strlen(res) - 1);\n        }\n    }\n    return res;\n}\n
iteration.kt
/* \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for */\nfun nestedForLoop(n: Int): String {\n    val res = StringBuilder()\n    // \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n    for (i in 1..n) {\n        // \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n        for (j in 1..n) {\n            res.append(\" ($i, $j), \")\n        }\n    }\n    return res.toString()\n}\n
iteration.rb
=begin\nFile: iteration.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com), Cy (9738314@gmail.com)\n=end\n\n# ## \u0426\u0438\u043a\u043b for ###\ndef for_loop(n)\n  res = 0\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  for i in 1..n\n    res += i\n  end\n\n  res\nend\n\n# ## \u0426\u0438\u043a\u043b while ###\ndef while_loop(n)\n  res = 0\n  i = 1 # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 2, ..., n-1, n\n  while i <= n\n    res += i\n    i += 1 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n  end\n\n  res\nend\n\n# ## \u0426\u0438\u043a\u043b while (\u0434\u0432\u043e\u0439\u043d\u043e\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435) ###\ndef while_loop_ii(n)\n  res = 0\n  i = 1 # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439\n\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 1, 4, 10, ...\n  while i <= n\n    res += i\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e\n    i += 1\n    i *= 2\n  end\n\n  res\nend\n\n# ## \u0414\u0432\u043e\u0439\u043d\u043e\u0439 \u0446\u0438\u043a\u043b for ###\ndef nested_for_loop(n)\n  res = \"\"\n\n  # \u0426\u0438\u043a\u043b \u043f\u043e i = 1, 2, ..., n-1, n\n  for i in 1..n\n    # \u0426\u0438\u043a\u043b \u043f\u043e j = 1, 2, ..., n-1, n\n    for j in 1..n\n      res += \"(#{i}, #{j}), \"\n    end\n  end\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-2 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0431\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-2 \u00a0 \u0411\u043b\u043e\u043a-\u0441\u0445\u0435\u043c\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430

\u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \\(n^2\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \"\u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438\".

\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u0446\u0438\u043a\u043b\u044b, \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u043d\u043e\u0432\u043e\u0435 \u0432\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0435 \"\u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438\", \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e \"\u043a\u0443\u0431\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438\", \"\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438\" \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#222","title":"2.2.2 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f","text":"

\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f (recursion) - \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0440\u0435\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443, \u0432\u044b\u0437\u044b\u0432\u0430\u044f \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043e\u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0435 \u0444\u0430\u0437\u044b.

  1. \u0421\u043f\u0443\u0441\u043a: \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0432\u0441\u0435 \u0433\u043b\u0443\u0431\u0436\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f, \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \"\u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\".
  2. \u041f\u043e\u0434\u044a\u0435\u043c: \u043f\u043e\u0441\u043b\u0435 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043d\u0438\u044f \"\u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\" \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0441\u0430\u043c\u043e\u0439 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0432\u0435\u0440\u0445, \u0441\u043e\u0431\u0438\u0440\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.

  1. \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043e\u0442 \"\u0441\u043f\u0443\u0441\u043a\u0430\" \u043a \"\u043f\u043e\u0434\u044a\u0435\u043c\u0443\".
  2. \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432: \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \"\u0441\u043f\u0443\u0441\u043a\u0443\", \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u0443 \u0441\u0435\u0431\u044f, \u043e\u0431\u044b\u0447\u043d\u043e \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c\u0438 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438.
  3. \u0412\u043e\u0437\u0432\u0440\u0430\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430: \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \"\u043f\u043e\u0434\u044a\u0435\u043c\u0443\", \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c\u0443.

\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434: \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e recur(n) , \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \\(1 + 2 + \\dots + n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
def recur(n: int) -> int:\n    \"\"\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f\"\"\"\n    # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 1:\n        return 1\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    res = recur(n - 1)\n    # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res\n
recursion.cpp
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint recur(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 1)\n        return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    int res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.java
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint recur(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 1)\n        return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    int res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.cs
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint Recur(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 1)\n        return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    int res = Recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.go
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc recur(n int) int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 1 {\n        return 1\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    res := recur(n - 1)\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res\n}\n
recursion.swift
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc recur(n: Int) -> Int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 1 {\n        return 1\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    let res = recur(n: n - 1)\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res\n}\n
recursion.js
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction recur(n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n === 1) return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    const res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.ts
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction recur(n: number): number {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n === 1) return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    const res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.dart
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint recur(int n) {\n  // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  if (n == 1) return 1;\n  // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  int res = recur(n - 1);\n  // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  return n + res;\n}\n
recursion.rs
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfn recur(n: i32) -> i32 {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 1 {\n        return 1;\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    let res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    n + res\n}\n
recursion.c
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint recur(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 1)\n        return 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    int res = recur(n - 1);\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res;\n}\n
recursion.kt
/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfun recur(n: Int): Int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 1)\n        return 1\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0448\u0430\u0433: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    val res = recur(n - 1)\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    return n + res\n}\n
recursion.rb
=begin\nFile: recursion.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef recur(n)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return 1 if n == 1\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  res = recur(n - 1)\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  n + res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-3 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-3 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

\u0425\u043e\u0442\u044f \u0441 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043e\u043d\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0434\u0432\u0435 \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u044b \u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447.

\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u0432\u044b\u0448\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043a\u0430\u043a \\(f(n) = 1 + 2 + \\dots + n\\) .

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#1","title":"1. \u00a0 \u0421\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432","text":"

\u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u0430 \u0441\u0435\u0431\u044f, \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u0430\u0434\u0440\u0435\u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e. \u042d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0434\u0432\u0443\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u044f\u043c.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-4, \u0434\u043e \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \\(n\\) \u0435\u0449\u0435 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0432\u0448\u0438\u0445\u0441\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u0430 \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-4 \u00a0 \u0413\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043d\u0430\u044f \u044f\u0437\u044b\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430, \u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0433\u043b\u0443\u0431\u043e\u043a\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043e\u0448\u0438\u0431\u043a\u0435 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#2","title":"2. \u00a0 \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f","text":"

\u0418\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0432 \u0441\u0430\u043c\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438 \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c , \u0442\u043e \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440 \u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043f\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u043d\u0430 \u0431\u044b\u043b\u0430 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c\u0430 \u0441 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439. \u0422\u0430\u043a\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439 (tail recursion).

\u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \\(1 + 2 + \\dots + n\\) \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u0443\u044e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
def tail_recur(n, res):\n    \"\"\"\u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f\"\"\"\n    # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 0:\n        return res\n    # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tail_recur(n - 1, res + n)\n
recursion.cpp
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint tailRecur(int n, int res) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 0)\n        return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n);\n}\n
recursion.java
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint tailRecur(int n, int res) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 0)\n        return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n);\n}\n
recursion.cs
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint TailRecur(int n, int res) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 0)\n        return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return TailRecur(n - 1, res + n);\n}\n
recursion.go
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc tailRecur(n int, res int) int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 0 {\n        return res\n    }\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n-1, res+n)\n}\n
recursion.swift
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc tailRecur(n: Int, res: Int) -> Int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 0 {\n        return res\n    }\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n: n - 1, res: res + n)\n}\n
recursion.js
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction tailRecur(n, res) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n === 0) return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n);\n}\n
recursion.ts
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction tailRecur(n: number, res: number): number {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n === 0) return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n);\n}\n
recursion.dart
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint tailRecur(int n, int res) {\n  // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  if (n == 0) return res;\n  // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  return tailRecur(n - 1, res + n);\n}\n
recursion.rs
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfn tail_recur(n: i32, res: i32) -> i32 {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if n == 0 {\n        return res;\n    }\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    tail_recur(n - 1, res + n)\n}\n
recursion.c
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint tailRecur(int n, int res) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 0)\n        return res;\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n);\n}\n
recursion.kt
/* \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\ntailrec fun tailRecur(n: Int, res: Int): Int {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e tailrec, \u0447\u0442\u043e\u0431\u044b \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (n == 0)\n        return res\n    // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    return tailRecur(n - 1, res + n)\n}\n
recursion.rb
=begin\nFile: recursion.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef recur(n)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return 1 if n == 1\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  res = recur(n - 1)\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  n + res\nend\n\n# ## \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 ###\ndef for_loop_recur(n)\n  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n  stack = []\n  res = 0\n\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  for i in n.downto(0)\n    # \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n    stack << i\n  end\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n\n# ## \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef tail_recur(n, res)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return res if n == 0\n  # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  tail_recur(n - 1, res + n)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-5. \u0415\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u0443\u044e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0438 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u0443\u044e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u0442\u043e \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0442\u043e\u0447\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0443 \u043d\u0438\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-5 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u043d\u043e\u0433\u0438\u0435 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u044b \u0438 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440\u044b \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Python \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0442\u0430\u043a\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e-\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0435 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e \u0441\u0442\u0435\u043a\u0430.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#3_1","title":"3. \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438","text":"

\u041f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\", \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0447\u0430\u0441\u0442\u043e \u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f \u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0438\u0442\u0430\u0435\u043c\u044b\u0439 \u043a\u043e\u0434, \u0447\u0435\u043c \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f. \u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \"\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438\".

Question

\u0414\u0430\u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438 \\(0, 1, 1, 2, 3, 5, 8, 13, \\dots\\) ; \u043d\u0430\u0439\u0434\u0438 \\(n\\)-\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u044d\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438.

\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \\(n\\)-\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438 \u043a\u0430\u043a \\(f(n)\\) . \u0422\u043e\u0433\u0434\u0430 \u043d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u0432\u0430 \u0432\u044b\u0432\u043e\u0434\u0430.

\u0421\u043b\u0435\u0434\u0443\u044f \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u043e\u043c\u0443 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 \u043a\u0430\u043a \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043a\u043e\u0434. \u0412\u044b\u0437\u043e\u0432 fib(n) \u0434\u0430\u0441\u0442 \u043d\u0430\u043c \\(n\\)-\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
def fib(n: int) -> int:\n    \"\"\"\u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f\"\"\"\n    # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if n == 1 or n == 2:\n        return n - 1\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    res = fib(n - 1) + fib(n - 2)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res\n
recursion.cpp
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint fib(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.java
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint fib(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.cs
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint Fib(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    int res = Fib(n - 1) + Fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.go
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc fib(n int) int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    res := fib(n-1) + fib(n-2)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res\n}\n
recursion.swift
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunc fib(n: Int) -> Int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    let res = fib(n: n - 1) + fib(n: n - 2)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res\n}\n
recursion.js
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction fib(n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.ts
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfunction fib(n: number): number {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n === 1 || n === 2) return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    const res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.dart
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint fib(int n) {\n  // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n  if (n == 1 || n == 2) return n - 1;\n  // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n  int res = fib(n - 1) + fib(n - 2);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n  return res;\n}\n
recursion.rs
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfn fib(n: i32) -> i32 {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if n == 1 || n == 2 {\n        return n - 1;\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    let res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    res\n}\n
recursion.c
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nint fib(int n) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    int res = fib(n - 1) + fib(n - 2);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res;\n}\n
recursion.kt
/* \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f */\nfun fib(n: Int): Int {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n    if (n == 1 || n == 2)\n        return n - 1\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n    val res = fib(n - 1) + fib(n - 2)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n    return res\n}\n
recursion.rb
=begin\nFile: recursion.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef recur(n)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return 1 if n == 1\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  res = recur(n - 1)\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  n + res\nend\n\n# ## \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 ###\ndef for_loop_recur(n)\n  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n  stack = []\n  res = 0\n\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  for i in n.downto(0)\n    # \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n    stack << i\n  end\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n\n# ## \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef tail_recur(n, res)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return res if n == 0\n  # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  tail_recur(n - 1, res + n)\nend\n\n# ## \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef fib(n)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f: f(1) = 0, f(2) = 1\n  return n - 1 if n == 1 || n == 2\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 f(n) = f(n-1) + f(n-2)\n  res = fib(n - 1) + fib(n - 2)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 f(n)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u0434, \u0432\u043d\u0443\u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u0430, \u0430 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u0434\u0438\u043d \u0432\u044b\u0437\u043e\u0432 \u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u0434\u0432\u0435 \u0432\u0435\u0442\u0432\u0438 \u0432\u044b\u0437\u043e\u0432\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-6, \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0432 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 (recursion tree) \u0433\u043b\u0443\u0431\u0438\u043d\u043e\u0439 \\(n\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-6 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0432\u043e\u043f\u043b\u043e\u0449\u0430\u0435\u0442 \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0443 \"\u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438\", \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439-\u0438-\u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 \u0441\u0442\u043e\u043b\u044c \u0432\u0430\u0436\u043d\u0430.

"},{"location":"chapter_computational_complexity/iteration_and_recursion/#223","title":"2.2.3 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432","text":"

\u041e\u0431\u043e\u0431\u0449\u0430\u044f \u0432\u0441\u0435 \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u0432\u044b\u0448\u0435, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u043e\u0441\u0442\u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 2-1 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438

\u0418\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 \u0441\u0430\u043c\u0430 \u0441\u0435\u0431\u044f \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u041e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u0448\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0435\u0442 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0440\u0430\u0441\u0445\u043e\u0434\u043e\u0432 \u043d\u0430 \u0432\u044b\u0437\u043e\u0432\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u041a\u0430\u0436\u0434\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u0430\u043a\u043b\u0430\u0434\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u041e\u0431\u044b\u0447\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438 \u041d\u0430\u043a\u043e\u043f\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 \u0432 \u043a\u0430\u0434\u0440\u0430\u0445 \u0441\u0442\u0435\u043a\u0430 \u041f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0425\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447, \u043a\u043e\u0434 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u0435\u043d \u0438 \u043b\u0435\u0433\u043a\u043e \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0425\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432, \u0433\u0440\u0430\u0444\u043e\u0432, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439-\u0438-\u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439, \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\u0430 \u0438 \u0442. \u0434.; \u043a\u043e\u0434 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u043c \u0438 \u044f\u0441\u043d\u044b\u043c

Tip

\u0415\u0441\u043b\u0438 \u0442\u0435\u0431\u0435 \u0441\u043b\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435, \u043c\u043e\u0436\u0435\u0448\u044c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u043c\u0443 \u043f\u043e\u0441\u043b\u0435 \u0447\u0442\u0435\u043d\u0438\u044f \u0433\u043b\u0430\u0432\u044b \u043e \"\u0441\u0442\u0435\u043a\u0435\".

\u041a\u0430\u043a\u043e\u0432\u0430 \u0436\u0435 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439? \u0415\u0441\u043b\u0438 \u0441\u043d\u043e\u0432\u0430 \u0432\u0437\u044f\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432\u044b\u0448\u0435, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u0444\u0430\u0437\u0435 \"\u043f\u043e\u0434\u044a\u0435\u043c\u0430\" \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0432\u043e\u0439, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439, \u0438 \u0442\u0430\u043a\u043e\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0441\u0442\u0435\u043a\u0430 \"\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0443\u0448\u0435\u043b\".

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0442\u0430\u043a\u0438\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u043a\u0430\u043a \"\u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432\" \u0438 \"\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430\", \u0443\u0436\u0435 \u043f\u0440\u044f\u043c\u043e \u043d\u0430\u043c\u0435\u043a\u0430\u044e\u0442 \u043d\u0430 \u0442\u0435\u0441\u043d\u0443\u044e \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439 \u0438 \u0441\u0442\u0435\u043a\u043e\u043c.

  1. \u0421\u043f\u0443\u0441\u043a: \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \u0434\u043b\u044f \u043d\u0435\u0435 \u043d\u043e\u0432\u044b\u0439 \u043a\u0430\u0434\u0440 \u0441\u0442\u0435\u043a\u0430 \u0432 \"\u0441\u0442\u0435\u043a\u0435 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\", \u0447\u0442\u043e\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435, \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b, \u0430\u0434\u0440\u0435\u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435.
  2. \u041f\u043e\u0434\u044a\u0435\u043c: \u043a\u043e\u0433\u0434\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u0430\u0434\u0440 \u0441\u0442\u0435\u043a\u0430 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0437 \"\u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\", \u0430 \u0441\u0440\u0435\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f.

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0432 \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u0444\u043e\u0440\u043c\u0443:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby recursion.py
def for_loop_recur(n: int) -> int:\n    \"\"\"\u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439\"\"\"\n    # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    stack = []\n    res = 0\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for i in range(n, 0, -1):\n        # \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.append(i)\n    # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while stack:\n        # \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop()\n    # res = 1+2+3+...+n\n    return res\n
recursion.cpp
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nint forLoopRecur(int n) {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    stack<int> stack;\n    int res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (int i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (!stack.empty()) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.top();\n        stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.java
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nint forLoopRecur(int n) {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    Stack<Integer> stack = new Stack<>();\n    int res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (int i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (!stack.isEmpty()) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.cs
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nint ForLoopRecur(int n) {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    Stack<int> stack = new();\n    int res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (int i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.Push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (stack.Count > 0) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.Pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.go
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfunc forLoopRecur(n int) int {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    stack := list.New()\n    res := 0\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for i := n; i > 0; i-- {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.PushBack(i)\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    for stack.Len() != 0 {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.Back().Value.(int)\n        stack.Remove(stack.Back())\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
recursion.swift
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfunc forLoopRecur(n: Int) -> Int {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    var stack: [Int] = []\n    var res = 0\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for i in (1 ... n).reversed() {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.append(i)\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while !stack.isEmpty {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.removeLast()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
recursion.js
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfunction forLoopRecur(n) {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    const stack = [];\n    let res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (let i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (stack.length) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.ts
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfunction forLoopRecur(n: number): number {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    const stack: number[] = [];\n    let res: number = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (let i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (stack.length) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop();\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.dart
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nint forLoopRecur(int n) {\n  // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n  List<int> stack = [];\n  int res = 0;\n  // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  for (int i = n; i > 0; i--) {\n    // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n    stack.add(i);\n  }\n  // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  while (!stack.isEmpty) {\n    // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n    res += stack.removeLast();\n  }\n  // res = 1+2+3+...+n\n  return res;\n}\n
recursion.rs
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfn for_loop_recur(n: i32) -> i32 {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    let mut stack = Vec::new();\n    let mut res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for i in (1..=n).rev() {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i);\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while !stack.is_empty() {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop().unwrap();\n    }\n    // res = 1+2+3+...+n\n    res\n}\n
recursion.c
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nint forLoopRecur(int n) {\n    int stack[1000]; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u0430\n    int top = -1;    // \u0418\u043d\u0434\u0435\u043a\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430\n    int res = 0;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (int i = n; i > 0; i--) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack[1 + top++] = i;\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (top >= 0) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack[top--];\n    }\n    // res = 1+2+3+...+n\n    return res;\n}\n
recursion.kt
/* \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 */\nfun forLoopRecur(n: Int): Int {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n    val stack = Stack<Int>()\n    var res = 0\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0448\u0430\u0433: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n    for (i in n downTo 0) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n        stack.push(i)\n    }\n    // \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n    while (stack.isNotEmpty()) {\n        // \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0432\u043e\u0437\u0432\u0440\u0430\u0442\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\n        res += stack.pop()\n    }\n    // res = 1+2+3+...+n\n    return res\n}\n
recursion.rb
=begin\nFile: recursion.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f ###\ndef recur(n)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  return 1 if n == 1\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  res = recur(n - 1)\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  n + res\nend\n\n# ## \u0418\u043c\u0438\u0442\u0430\u0446\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 ###\ndef for_loop_recur(n)\n  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044f\u0432\u043d\u044b\u0439 \u0441\u0442\u0435\u043a \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432\n  stack = []\n  res = 0\n\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\n  for i in n.downto(0)\n    # \u0418\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u00ab\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0435\u043a\n    stack << i\n  end\n  # \u0412\u043e\u0437\u0432\u0440\u0430\u0442: \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\n  while !stack.empty?\n    res += stack.pop\n  end\n\n  # res = 1+2+3+...+n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043a\u043e\u0434, \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0432 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u043a\u043e\u0434 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0425\u043e\u0442\u044f \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044f \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u044b \u0434\u0440\u0443\u0433 \u0432 \u0434\u0440\u0443\u0433\u0430, \u044d\u0442\u043e \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c.

\u0418\u0442\u0430\u043a, \u0432\u044b\u0431\u043e\u0440 \u043c\u0435\u0436\u0434\u0443 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0435\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043f\u0440\u0438\u0440\u043e\u0434\u044b \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u0412 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u0440\u0430\u0439\u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u0432\u0437\u0432\u0435\u0448\u0438\u0432\u0430\u0442\u044c \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b \u043e\u0431\u043e\u0438\u0445 \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432 \u0438 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0430.

"},{"location":"chapter_computational_complexity/performance_evaluation/","title":"2.1 \u00a0 \u041e\u0446\u0435\u043d\u043a\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432","text":"

\u041f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043c\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0442\u0440\u0435\u043c\u0438\u043c\u0441\u044f \u043a \u0434\u0432\u0443\u043c \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u0446\u0435\u043b\u0435\u0439.

  1. \u041d\u0430\u0439\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438: \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u043d\u0430\u0434\u0435\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.
  2. \u041d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0438 \u043d\u0430\u043c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c.

\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f, \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0433\u043b\u0430\u0432\u043d\u044b\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u043c \u043e\u0446\u0435\u043d\u043a\u0438 \u0435\u0433\u043e \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430. \u041e\u043d\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f.

\u041a\u043e\u0440\u043e\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0430\u0448\u0430 \u0446\u0435\u043b\u044c - \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \"\u0438 \u0431\u044b\u0441\u0442\u0440\u044b, \u0438 \u044d\u043a\u043e\u043d\u043e\u043c\u043d\u044b \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438\". \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430\u044f \u043e\u0446\u0435\u043d\u043a\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043a\u0440\u0430\u0439\u043d\u0435 \u0432\u0430\u0436\u043d\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438.

\u041c\u0435\u0442\u043e\u0434\u044b \u043e\u0446\u0435\u043d\u043a\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430: \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0446\u0435\u043d\u043a\u0430.

"},{"location":"chapter_computational_complexity/performance_evaluation/#211","title":"2.1.1 \u00a0 \u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c A \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c B, \u043e\u0431\u0430 \u0440\u0435\u0448\u0430\u044e\u0442 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0437\u0430\u0434\u0430\u0447\u0443, \u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0438\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c. \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 - \u0432\u0437\u044f\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0431\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0438 \u043e\u0431\u044a\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0422\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0446\u0435\u043d\u043a\u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0440\u0435\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0438 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.

\u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0442\u0440\u0443\u0434\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u0444\u0430\u043a\u0442\u043e\u0440\u043e\u0432 \u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b. \u0410\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u0430\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u043c\u0435\u0435\u0442 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430, \u043e\u043d \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u044f\u0434\u0435\u0440\u043d\u044b\u0445 CPU; \u0435\u0441\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u043d\u0442\u0435\u043d\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043e\u043d \u043f\u043e\u043a\u0430\u0436\u0435\u0442 \u0441\u0435\u0431\u044f \u043b\u0443\u0447\u0448\u0435 \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0430\u0448\u0438\u043d\u0430\u0445 \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0430\u0448\u0438\u043d\u0430\u0445 \u0438 \u0443\u0441\u0440\u0435\u0434\u043d\u044f\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b, \u0430 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u044d\u0442\u043e \u043d\u0435\u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e.

\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043f\u043e\u043b\u043d\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432. \u041f\u043e \u043c\u0435\u0440\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043c\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0441\u0442\u0438 \u0441\u0435\u0431\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0435\u043c\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B; \u043d\u043e \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043e\u0431\u044a\u0435\u043c\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043c\u043e\u0433\u0443\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u043c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0432\u043e\u0434\u043e\u0432 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u043e\u0432, \u0430 \u044d\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.

"},{"location":"chapter_computational_complexity/performance_evaluation/#212","title":"2.1.2 \u00a0 \u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043e\u0446\u0435\u043d\u043a\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439. \u0422\u0430\u043a\u043e\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 (asymptotic complexity analysis), \u0438\u043b\u0438 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438.

\u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0435\u0441\u0443\u0440\u0441\u0430\u043c\u0438, \u0442\u0440\u0435\u0431\u0443\u0435\u043c\u044b\u043c\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443, \u0438 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041e\u043d \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u0440\u043e\u0441\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443, \u043f\u043e \u043c\u0435\u0440\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0432\u0443\u0447\u0438\u0442 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u044f\u0436\u0435\u043b\u043e\u0432\u0435\u0441\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043b\u0435\u0437\u043d\u043e \u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 \u0442\u0440\u0438 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0438\u0434\u0435\u0438.

\u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0443\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0447\u0442\u043e \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u0445.

Tip

\u0415\u0441\u043b\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0442\u0435\u0431\u0435 \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043d\u044b\u043c, \u043d\u0435 \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0430\u0439: \u043c\u044b \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445.

\u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u0435\u0442 \u043d\u0430\u043c \"\u043b\u0438\u043d\u0435\u0439\u043a\u0443\" \u0434\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044f \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439.

\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c - \u044d\u0442\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0445 \u043e\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u043c \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0442\u0440\u0443\u0434\u043d\u044b\u043c. \u0421 \u044d\u0442\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0430\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043d\u0435 \u043b\u0443\u0447\u0448\u0438\u0439 \u0441\u0430\u043c\u044b\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0434\u043b\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u0430. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u0435\u043c \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043f\u043e\u0447\u0442\u0438 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0437\u0430\u0442\u0440\u043e\u043d\u0443\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0435\u0433\u043e \u0440\u0430\u0431\u043e\u0442\u044b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438.

\u0412 \u0438\u0442\u043e\u0433\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0434\u043e \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0433\u0440\u0443\u0436\u0435\u043d\u0438\u044f \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.

"},{"location":"chapter_computational_complexity/space_complexity/","title":"2.4 \u00a0 \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c","text":"

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (space complexity) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c, \u043f\u043e \u043c\u0435\u0440\u0435 \u0440\u043e\u0441\u0442\u0430 \u043e\u0431\u044a\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043c\u0435\u0441\u0442\u043e \"\u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\" \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \"\u043e\u0431\u044a\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\".

"},{"location":"chapter_computational_complexity/space_complexity/#241","title":"2.4.1 \u00a0 \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0435 \u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c","text":"

\u041f\u0430\u043c\u044f\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b, \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0447\u0430\u0441\u0442\u0435\u0439.

\u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432 \u0440\u0430\u0441\u0447\u0435\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \"\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\" \u0438 \"\u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\".

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0442\u0438.

\u041f\u0440\u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u043c \u0442\u0440\u0438 \u0447\u0430\u0441\u0442\u0438: \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u043e\u0432 \u0441\u0442\u0435\u043a\u0430 \u0438 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-15.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-15 \u00a0 \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c

\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
class Node:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441\"\"\"\n    def __init__(self, x: int):\n        self.val: int = x              # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        self.next: Node | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\ndef function() -> int:\n    \"\"\"\u0424\u0443\u043d\u043a\u0446\u0438\u044f\"\"\"\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0\n\ndef algorithm(n) -> int:  # \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    A = 0                 # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430, \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0437\u0430\u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0431\u0443\u043a\u0432\u043e\u0439)\n    b = 0                 # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    node = Node(0)        # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    c = function()        # \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return A + b + c      # \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 */\nstruct Node {\n    int val;\n    Node *next;\n    Node(int x) : val(x), next(nullptr) {}\n};\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0;\n}\n\nint algorithm(int n) {        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const int a = 0;          // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    int b = 0;                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    Node* node = new Node(0); // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    int c = func();           // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;         // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node {\n    int val;\n    Node next;\n    Node(int x) { val = x; }\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0;\n}\n\nint algorithm(int n) {        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    final int a = 0;          // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    int b = 0;                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    Node node = new Node(0);  // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    int c = function();       // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;         // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node(int x) {\n    int val = x;\n    Node next;\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint Function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0;\n}\n\nint Algorithm(int n) {        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const int a = 0;          // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    int b = 0;                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    Node node = new(0);       // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    int c = Function();       // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;         // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 */\ntype node struct {\n    val  int\n    next *node\n}\n\n/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 node */\nfunc newNode(val int) *node {\n    return &node{val: val}\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc function() int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0\n}\n\nfunc algorithm(n int) int { // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const a = 0             // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    b := 0                  // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    newNode(0)              // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    c := function()         // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c        // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node {\n    var val: Int\n    var next: Node?\n\n    init(x: Int) {\n        val = x\n    }\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc function() -> Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0\n}\n\nfunc algorithm(n: Int) -> Int { // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    let a = 0             // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    var b = 0             // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    let node = Node(x: 0) // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    let c = function()    // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c      // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node {\n    val;\n    next;\n    constructor(val) {\n        this.val = val === undefined ? 0 : val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = null;                       // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunction constFunc() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\nfunction algorithm(n) {       // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const a = 0;              // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    let b = 0;                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    const node = new Node(0); // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    const c = constFunc();    // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;         // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node {\n    val: number;\n    next: Node | null;\n    constructor(val?: number) {\n        this.val = val === undefined ? 0 : val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.next = null;                       // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n    }\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunction constFunc(): number {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\nfunction algorithm(n: number): number { // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const a = 0;                        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    let b = 0;                          // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    const node = new Node(0);           // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    const c = constFunc();              // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;                   // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node {\n  int val;\n  Node next;\n  Node(this.val, [this.next]);\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint function() {\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n  return 0;\n}\n\nint algorithm(int n) {  // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n  const int a = 0;      // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n  int b = 0;            // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n  Node node = Node(0);  // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n  int c = function();   // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n  return a + b + c;     // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 */\nstruct Node {\n    val: i32,\n    next: Option<Rc<RefCell<Node>>>,\n}\n\n/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 Node */\nimpl Node {\n    fn new(val: i32) -> Self {\n        Self { val: val, next: None }\n    }\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfn function() -> i32 {      \n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0;\n}\n\nfn algorithm(n: i32) -> i32 {       // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const a: i32 = 0;               // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    let mut b = 0;                  // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    let node = Node::new(0);        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    let c = function();             // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;               // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0;\n}\n\nint algorithm(int n) { // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    const int a = 0;   // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    int b = 0;         // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    int c = func();    // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c;  // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
/* \u041a\u043b\u0430\u0441\u0441 */\nclass Node(var _val: Int) {\n    var next: Node? = null\n}\n\n/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfun function(): Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0\n}\n\nfun algorithm(n: Int): Int { // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    val a = 0                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    var b = 0                // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    val node = Node(0)       // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    val c = function()       // \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    return a + b + c         // \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n}\n
### \u041a\u043b\u0430\u0441\u0441 ###\nclass Node\n    attr_accessor :val      # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    attr_accessor :next     # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n\n    def initialize(x)\n        @val = x\n    end\nend\n\n### \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    0\nend\n\n### \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c ###\ndef algorithm(n)        # \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\n    a = 0               # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430)\n    b = 0               # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f)\n    node = Node.new(0)  # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 (\u043e\u0431\u044a\u0435\u043a\u0442)\n    c = function        # \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0432\u044b\u0437\u043e\u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438)\n    a + b + c           # \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435\nend\n
"},{"location":"chapter_computational_complexity/space_complexity/#242","title":"2.4.2 \u00a0 \u041c\u0435\u0442\u043e\u0434 \u0432\u044b\u0432\u043e\u0434\u0430","text":"

\u041c\u0435\u0442\u043e\u0434 \u0432\u044b\u0432\u043e\u0434\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432 \u0446\u0435\u043b\u043e\u043c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u0435\u043d \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u043c\u0443 \u0430\u043d\u0430\u043b\u0438\u0437\u0443: \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043a\u0442 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430, \u0441 \"\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439\" \u043d\u0430 \"\u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430\".

\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u0443\u0434\u0448\u0443\u044e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043f\u0430\u043c\u044f\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0436\u0435\u0441\u0442\u043a\u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c: \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0430\u043c\u044f\u0442\u0438.

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434. \u0421\u043b\u043e\u0432\u043e \"\u0445\u0443\u0434\u0448\u0430\u044f\" \u0432 \"\u0445\u0443\u0434\u0448\u0435\u0439 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438\" \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.

  1. \u041e\u0440\u0438\u0435\u043d\u0442\u0438\u0440 \u043d\u0430 \u0445\u0443\u0434\u0448\u0438\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435: \u043a\u043e\u0433\u0434\u0430 \\(n < 10\\) , \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) ; \u043d\u043e \u043a\u043e\u0433\u0434\u0430 \\(n > 10\\) , \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0445\u0443\u0434\u0448\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) .
  2. \u041e\u0440\u0438\u0435\u043d\u0442\u0438\u0440 \u043d\u0430 \u043f\u0438\u043a\u043e\u0432\u043e\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(1)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430; \u043f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u043e\u043d\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0445\u0443\u0434\u0448\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) .
PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
def algorithm(n: int):\n    a = 0               # O(1)\n    b = [0] * 10000     # O(1)\n    if n > 10:\n        nums = [0] * n  # O(n)\n
void algorithm(int n) {\n    int a = 0;               // O(1)\n    vector<int> b(10000);    // O(1)\n    if (n > 10)\n        vector<int> nums(n); // O(n)\n}\n
void algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10)\n        int[] nums = new int[n]; // O(n)\n}\n
void Algorithm(int n) {\n    int a = 0;                   // O(1)\n    int[] b = new int[10000];    // O(1)\n    if (n > 10) {\n        int[] nums = new int[n]; // O(n)\n    }\n}\n
func algorithm(n int) {\n    a := 0                      // O(1)\n    b := make([]int, 10000)     // O(1)\n    var nums []int\n    if n > 10 {\n        nums := make([]int, n)  // O(n)\n    }\n    fmt.Println(a, b, nums)\n}\n
func algorithm(n: Int) {\n    let a = 0 // O(1)\n    let b = Array(repeating: 0, count: 10000) // O(1)\n    if n > 10 {\n        let nums = Array(repeating: 0, count: n) // O(n)\n    }\n}\n
function algorithm(n) {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
function algorithm(n: number): void {\n    const a = 0;                   // O(1)\n    const b = new Array(10000);    // O(1)\n    if (n > 10) {\n        const nums = new Array(n); // O(n)\n    }\n}\n
void algorithm(int n) {\n  int a = 0;                            // O(1)\n  List<int> b = List.filled(10000, 0);  // O(1)\n  if (n > 10) {\n    List<int> nums = List.filled(n, 0); // O(n)\n  }\n}\n
fn algorithm(n: i32) {\n    let a = 0;                              // O(1)\n    let b = [0; 10000];                     // O(1)\n    if n > 10 {\n        let nums = vec![0; n as usize];     // O(n)\n    }\n}\n
void algorithm(int n) {\n    int a = 0;               // O(1)\n    int b[10000];            // O(1)\n    if (n > 10)\n        int nums[n] = {0};   // O(n)\n}\n
fun algorithm(n: Int) {\n    val a = 0                    // O(1)\n    val b = IntArray(10000)      // O(1)\n    if (n > 10) {\n        val nums = IntArray(n)   // O(n)\n    }\n}\n
def algorithm(n)\n    a = 0                           # O(1)\n    b = Array.new(10000)            # O(1)\n    nums = Array.new(n) if n > 10   # O(n)\nend\n

\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043a\u0430\u0434\u0440\u043e\u0432 \u0441\u0442\u0435\u043a\u0430. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
def function() -> int:\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n\ndef loop(n: int):\n    \"\"\"\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1)\"\"\"\n    for _ in range(n):\n        function()\n\ndef recur(n: int):\n    \"\"\"\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n)\"\"\"\n    if n == 1:\n        return\n    return recur(n - 1)\n
int func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
int function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
int Function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nvoid Loop(int n) {\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nint Recur(int n) {\n    if (n == 1) return 1;\n    return Recur(n - 1);\n}\n
func function() int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n}\n\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfunc loop(n int) {\n    for i := 0; i < n; i++ {\n        function()\n    }\n}\n\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfunc recur(n int) {\n    if n == 1 {\n        return\n    }\n    recur(n - 1)\n}\n
@discardableResult\nfunc function() -> Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n}\n\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfunc loop(n: Int) {\n    for _ in 0 ..< n {\n        function()\n    }\n}\n\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfunc recur(n: Int) {\n    if n == 1 {\n        return\n    }\n    recur(n: n - 1)\n}\n
function constFunc() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfunction loop(n) {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfunction recur(n) {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
function constFunc(): number {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfunction loop(n: number): void {\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfunction recur(n: number): void {\n    if (n === 1) return;\n    return recur(n - 1);\n}\n
int function() {\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nvoid loop(int n) {\n  for (int i = 0; i < n; i++) {\n    function();\n  }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nvoid recur(int n) {\n  if (n == 1) return;\n  recur(n - 1);\n}\n
fn function() -> i32 {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfn loop(n: i32) {\n    for i in 0..n {\n        function();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfn recur(n: i32) {\n    if n == 1 {\n        return;\n    }\n    recur(n - 1);\n}\n
int func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nvoid loop(int n) {\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nvoid recur(int n) {\n    if (n == 1) return;\n    recur(n - 1);\n}\n
fun function(): Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) */\nfun loop(n: Int) {\n    for (i in 0..<n) {\n        function()\n    }\n}\n/* \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) */\nfun recur(n: Int) {\n    if (n == 1) return\n    return recur(n - 1)\n}\n
def function\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    0\nend\n\n### \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0446\u0438\u043a\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 O(1) ###\ndef loop(n)\n    (0...n).each { function }\nend\n\n### \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 O(n) ###\ndef recur(n)\n    return if n == 1\n    recur(n - 1)\nend\n

\u0424\u0443\u043d\u043a\u0446\u0438\u0438 loop() \u0438 recur() \u0438\u043c\u0435\u044e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\) , \u043d\u043e \u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f.

"},{"location":"chapter_computational_complexity/space_complexity/#243","title":"2.4.3 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b","text":"

\u041f\u0443\u0441\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u0435\u043d \\(n\\) . \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-16 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 (\u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435\u0439 \u043a \u0431\u043e\u043b\u044c\u0448\u0435\u0439).

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n^2) < O(2^n) \\newline \\text{\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f} < \\text{\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f} < \\text{\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f} < \\text{\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f} < \\text{\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f} \\end{aligned} \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-16 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438

"},{"location":"chapter_computational_complexity/space_complexity/#1-o1","title":"1. \u00a0 \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\)","text":"

\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) .

\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u0430\u043c\u044f\u0442\u044c, \u0437\u0430\u043d\u044f\u0442\u0430\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0432\u044b\u0437\u043e\u0432\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432\u043d\u0443\u0442\u0440\u0438 \u0446\u0438\u043a\u043b\u0430, \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d\u0430 \u043d\u0435 \u043d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \\(O(1)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def function() -> int:\n    \"\"\"\u0424\u0443\u043d\u043a\u0446\u0438\u044f\"\"\"\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n\ndef constant(n: int):\n    \"\"\"\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    a = 0\n    nums = [0] * 10000\n    node = ListNode(0)\n    # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for _ in range(n):\n        c = 0\n    # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for _ in range(n):\n        function()\n
space_complexity.cpp
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid constant(int n) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const int a = 0;\n    int b = 0;\n    vector<int> nums(10000);\n    ListNode node(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
space_complexity.java
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid constant(int n) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    final int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new ListNode(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        function();\n    }\n}\n
space_complexity.cs
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint Function() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid Constant(int n) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    int a = 0;\n    int b = 0;\n    int[] nums = new int[10000];\n    ListNode node = new(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        Function();\n    }\n}\n
space_complexity.go
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc function() int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438...\n    return 0\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc spaceConstant(n int) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const a = 0\n    b := 0\n    nums := make([]int, 10000)\n    node := newNode(0)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    var c int\n    for i := 0; i < n; i++ {\n        c = 0\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for i := 0; i < n; i++ {\n        function()\n    }\n    b += 0\n    c += 0\n    nums[0] = 0\n    node.val = 0\n}\n
space_complexity.swift
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\n@discardableResult\nfunc function() -> Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc constant(n: Int) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    let a = 0\n    var b = 0\n    let nums = Array(repeating: 0, count: 10000)\n    let node = ListNode(x: 0)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for _ in 0 ..< n {\n        let c = 0\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for _ in 0 ..< n {\n        function()\n    }\n}\n
space_complexity.js
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunction constFunc() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction constant(n) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
space_complexity.ts
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfunction constFunc(): number {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction constant(n: number): void {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const a = 0;\n    const b = 0;\n    const nums = new Array(10000);\n    const node = new ListNode(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (let i = 0; i < n; i++) {\n        const c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (let i = 0; i < n; i++) {\n        constFunc();\n    }\n}\n
space_complexity.dart
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint function() {\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid constant(int n) {\n  // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  final int a = 0;\n  int b = 0;\n  List<int> nums = List.filled(10000, 0);\n  ListNode node = ListNode(0);\n  // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  for (var i = 0; i < n; i++) {\n    int c = 0;\n  }\n  // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  for (var i = 0; i < n; i++) {\n    function();\n  }\n}\n
space_complexity.rs
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfn function() -> i32 {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\n#[allow(unused)]\nfn constant(n: i32) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const A: i32 = 0;\n    let b = 0;\n    let nums = vec![0; 10000];\n    let node = ListNode::new(0);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for i in 0..n {\n        let c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for i in 0..n {\n        function();\n    }\n}\n
space_complexity.c
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nint func() {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0;\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid constant(int n) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    const int a = 0;\n    int b = 0;\n    int nums[1000];\n    ListNode *node = newListNode(0);\n    free(node);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        int c = 0;\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        func();\n    }\n}\n
space_complexity.kt
/* \u0424\u0443\u043d\u043a\u0446\u0438\u044f */\nfun function(): Int {\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return 0\n}\n\n/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun constant(n: Int) {\n    // \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    val a = 0\n    var b = 0\n    val nums = Array(10000) { 0 }\n    val node = ListNode(0)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (i in 0..<n) {\n        val c = 0\n    }\n    // \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n    for (i in 0..<n) {\n        function()\n    }\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_computational_complexity/space_complexity/#2-on","title":"2. \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\)","text":"

\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432, \u0441\u0442\u0435\u043a\u043e\u0432, \u043e\u0447\u0435\u0440\u0435\u0434\u0435\u0439 \u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440, \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \\(n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def linear(n: int):\n    \"\"\"\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    nums = [0] * n\n    # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    hmap = dict[int, str]()\n    for i in range(n):\n        hmap[i] = str(i)\n
space_complexity.cpp
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid linear(int n) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    vector<int> nums(n);\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    vector<ListNode> nodes;\n    for (int i = 0; i < n; i++) {\n        nodes.push_back(ListNode(i));\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    unordered_map<int, string> map;\n    for (int i = 0; i < n; i++) {\n        map[i] = to_string(i);\n    }\n}\n
space_complexity.java
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid linear(int n) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    int[] nums = new int[n];\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    List<ListNode> nodes = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        nodes.add(new ListNode(i));\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    Map<Integer, String> map = new HashMap<>();\n    for (int i = 0; i < n; i++) {\n        map.put(i, String.valueOf(i));\n    }\n}\n
space_complexity.cs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid Linear(int n) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    int[] nums = new int[n];\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    List<ListNode> nodes = [];\n    for (int i = 0; i < n; i++) {\n        nodes.Add(new ListNode(i));\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    Dictionary<int, string> map = [];\n    for (int i = 0; i < n; i++) {\n        map.Add(i, i.ToString());\n    }\n}\n
space_complexity.go
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc spaceLinear(n int) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    _ = make([]int, n)\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    var nodes []*node\n    for i := 0; i < n; i++ {\n        nodes = append(nodes, newNode(i))\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    m := make(map[int]string, n)\n    for i := 0; i < n; i++ {\n        m[i] = strconv.Itoa(i)\n    }\n}\n
space_complexity.swift
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc linear(n: Int) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let nums = Array(repeating: 0, count: n)\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let nodes = (0 ..< n).map { ListNode(x: $0) }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let map = Dictionary(uniqueKeysWithValues: (0 ..< n).map { ($0, \"\\($0)\") })\n}\n
space_complexity.js
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linear(n) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const nums = new Array(n);\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const nodes = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
space_complexity.ts
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linear(n: number): void {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const nums = new Array(n);\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const nodes: ListNode[] = [];\n    for (let i = 0; i < n; i++) {\n        nodes.push(new ListNode(i));\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    const map = new Map();\n    for (let i = 0; i < n; i++) {\n        map.set(i, i.toString());\n    }\n}\n
space_complexity.dart
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid linear(int n) {\n  // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  List<int> nums = List.filled(n, 0);\n  // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  List<ListNode> nodes = [];\n  for (var i = 0; i < n; i++) {\n    nodes.add(ListNode(i));\n  }\n  // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  Map<int, String> map = HashMap();\n  for (var i = 0; i < n; i++) {\n    map.putIfAbsent(i, () => i.toString());\n  }\n}\n
space_complexity.rs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\n#[allow(unused)]\nfn linear(n: i32) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let mut nums = vec![0; n as usize];\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let mut nodes = Vec::new();\n    for i in 0..n {\n        nodes.push(ListNode::new(i))\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    let mut map = HashMap::new();\n    for i in 0..n {\n        map.insert(i, i.to_string());\n    }\n}\n
space_complexity.c
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 uthash.h\n} HashTable;\n\n/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid linear(int n) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    int *nums = malloc(sizeof(int) * n);\n    free(nums);\n\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    ListNode **nodes = malloc(sizeof(ListNode *) * n);\n    for (int i = 0; i < n; i++) {\n        nodes[i] = newListNode(i);\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        free(nodes[i]);\n    }\n    free(nodes);\n\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    HashTable *h = NULL;\n    for (int i = 0; i < n; i++) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = i;\n        tmp->val = i;\n        HASH_ADD_INT(h, key, tmp);\n    }\n\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438\n    HashTable *curr, *tmp;\n    HASH_ITER(hh, h, curr, tmp) {\n        HASH_DEL(h, curr);\n        free(curr);\n    }\n}\n
space_complexity.kt
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun linear(n: Int) {\n    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    val nums = Array(n) { 0 }\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    val nodes = mutableListOf<ListNode>()\n    for (i in 0..<n) {\n        nodes.add(ListNode(i))\n    }\n    // \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n    val map = mutableMapOf<Int, String>()\n    for (i in 0..<n) {\n        map[i] = i.toString()\n    }\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  nums = Array.new(n, 0)\n\n  # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-17, \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \\(n\\) \u0435\u0449\u0435 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0432\u0448\u0438\u0445\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 linear_recur() , \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \\(O(n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043a\u0430\u0434\u0440\u043e\u0432 \u0441\u0442\u0435\u043a\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def linear_recur(n: int):\n    \"\"\"\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    print(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n =\", n)\n    if n == 1:\n        return\n    linear_recur(n - 1)\n
space_complexity.cpp
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nvoid linearRecur(int n) {\n    cout << \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = \" << n << endl;\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
space_complexity.java
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nvoid linearRecur(int n) {\n    System.out.println(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = \" + n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
space_complexity.cs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nvoid LinearRecur(int n) {\n    Console.WriteLine(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = \" + n);\n    if (n == 1) return;\n    LinearRecur(n - 1);\n}\n
space_complexity.go
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc spaceLinearRecur(n int) {\n    fmt.Println(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n =\", n)\n    if n == 1 {\n        return\n    }\n    spaceLinearRecur(n - 1)\n}\n
space_complexity.swift
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc linearRecur(n: Int) {\n    print(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = \\(n)\")\n    if n == 1 {\n        return\n    }\n    linearRecur(n: n - 1)\n}\n
space_complexity.js
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction linearRecur(n) {\n    console.log(`\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
space_complexity.ts
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction linearRecur(n: number): void {\n    console.log(`\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = ${n}`);\n    if (n === 1) return;\n    linearRecur(n - 1);\n}\n
space_complexity.dart
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nvoid linearRecur(int n) {\n  print('\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = $n');\n  if (n == 1) return;\n  linearRecur(n - 1);\n}\n
space_complexity.rs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn linear_recur(n: i32) {\n    println!(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = {}\", n);\n    if n == 1 {\n        return;\n    };\n    linear_recur(n - 1);\n}\n
space_complexity.c
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nvoid linearRecur(int n) {\n    printf(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = %d\\r\\n\", n);\n    if (n == 1)\n        return;\n    linearRecur(n - 1);\n}\n
space_complexity.kt
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun linearRecur(n: Int) {\n    println(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = $n\")\n    if (n == 1)\n        return\n    linearRecur(n - 1)\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  nums = Array.new(n, 0)\n\n  # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef linear_recur(n)\n  puts \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-17 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439

"},{"location":"chapter_computational_complexity/space_complexity/#3-on2","title":"3. \u00a0 \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n^2)\\)","text":"

\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443 \u043c\u0430\u0442\u0440\u0438\u0446 \u0438 \u0433\u0440\u0430\u0444\u043e\u0432, \u0433\u0434\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \\(n\\) \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c\u044e:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def quadratic(n: int):\n    \"\"\"\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    # \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    num_matrix = [[0] * n for _ in range(n)]\n
space_complexity.cpp
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid quadratic(int n) {\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    vector<vector<int>> numMatrix;\n    for (int i = 0; i < n; i++) {\n        vector<int> tmp;\n        for (int j = 0; j < n; j++) {\n            tmp.push_back(0);\n        }\n        numMatrix.push_back(tmp);\n    }\n}\n
space_complexity.java
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid quadratic(int n) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    int[][] numMatrix = new int[n][n];\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    List<List<Integer>> numList = new ArrayList<>();\n    for (int i = 0; i < n; i++) {\n        List<Integer> tmp = new ArrayList<>();\n        for (int j = 0; j < n; j++) {\n            tmp.add(0);\n        }\n        numList.add(tmp);\n    }\n}\n
space_complexity.cs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid Quadratic(int n) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    int[,] numMatrix = new int[n, n];\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    List<List<int>> numList = [];\n    for (int i = 0; i < n; i++) {\n        List<int> tmp = [];\n        for (int j = 0; j < n; j++) {\n            tmp.Add(0);\n        }\n        numList.Add(tmp);\n    }\n}\n
space_complexity.go
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc spaceQuadratic(n int) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    numMatrix := make([][]int, n)\n    for i := 0; i < n; i++ {\n        numMatrix[i] = make([]int, n)\n    }\n}\n
space_complexity.swift
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc quadratic(n: Int) {\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    let numList = Array(repeating: Array(repeating: 0, count: n), count: n)\n}\n
space_complexity.js
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction quadratic(n) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
space_complexity.ts
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction quadratic(n: number): void {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    const numMatrix = Array(n)\n        .fill(null)\n        .map(() => Array(n).fill(null));\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    const numList = [];\n    for (let i = 0; i < n; i++) {\n        const tmp = [];\n        for (let j = 0; j < n; j++) {\n            tmp.push(0);\n        }\n        numList.push(tmp);\n    }\n}\n
space_complexity.dart
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid quadratic(int n) {\n  // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n  List<List<int>> numMatrix = List.generate(n, (_) => List.filled(n, 0));\n  // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n  List<List<int>> numList = [];\n  for (var i = 0; i < n; i++) {\n    List<int> tmp = [];\n    for (int j = 0; j < n; j++) {\n      tmp.add(0);\n    }\n    numList.add(tmp);\n  }\n}\n
space_complexity.rs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\n#[allow(unused)]\nfn quadratic(n: i32) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    let num_matrix = vec![vec![0; n as usize]; n as usize];\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    let mut num_list = Vec::new();\n    for i in 0..n {\n        let mut tmp = Vec::new();\n        for j in 0..n {\n            tmp.push(0);\n        }\n        num_list.push(tmp);\n    }\n}\n
space_complexity.c
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nvoid quadratic(int n) {\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    int **numMatrix = malloc(sizeof(int *) * n);\n    for (int i = 0; i < n; i++) {\n        int *tmp = malloc(sizeof(int) * n);\n        for (int j = 0; j < n; j++) {\n            tmp[j] = 0;\n        }\n        numMatrix[i] = tmp;\n    }\n\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438\n    for (int i = 0; i < n; i++) {\n        free(numMatrix[i]);\n    }\n    free(numMatrix);\n}\n
space_complexity.kt
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun quadratic(n: Int) {\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    val numMatrix = arrayOfNulls<Array<Int>?>(n)\n    // \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n    val numList = mutableListOf<MutableList<Int>>()\n    for (i in 0..<n) {\n        val tmp = mutableListOf<Int>()\n        for (j in 0..<n) {\n            tmp.add(0)\n        }\n        numList.add(tmp)\n    }\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  nums = Array.new(n, 0)\n\n  # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef linear_recur(n)\n  puts \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  # \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n  Array.new(n) { Array.new(n, 0) }\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-18, \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) , \u0438 \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b \\(n\\) , \\(n-1\\) , \\(\\dots\\) , \\(2\\) , \\(1\\) ; \u0435\u0433\u043e \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0434\u043b\u0438\u043d\u0430 \u0440\u0430\u0432\u043d\u0430 \\(n / 2\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0441\u0443\u043c\u043c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \\(O(n^2)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def quadratic_recur(n: int) -> int:\n    \"\"\"\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    if n <= 0:\n        return 0\n    # \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n    nums = [0] * n\n    return quadratic_recur(n - 1)\n
space_complexity.cpp
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    vector<int> nums(n);\n    cout << \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = \" << n << \" , \u0434\u043b\u0438\u043d\u0430 nums = \" << nums.size() << endl;\n    return quadraticRecur(n - 1);\n}\n
space_complexity.java
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    // \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n    int[] nums = new int[n];\n    System.out.println(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = \" + n + \", \u0434\u043b\u0438\u043d\u0430 nums = \" + nums.length);\n    return quadraticRecur(n - 1);\n}\n
space_complexity.cs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint QuadraticRecur(int n) {\n    if (n <= 0) return 0;\n    int[] nums = new int[n];\n    Console.WriteLine(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = \" + n + \", \u0434\u043b\u0438\u043d\u0430 nums = \" + nums.Length);\n    return QuadraticRecur(n - 1);\n}\n
space_complexity.go
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc spaceQuadraticRecur(n int) int {\n    if n <= 0 {\n        return 0\n    }\n    nums := make([]int, n)\n    fmt.Printf(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = %d, \u0434\u043b\u0438\u043d\u0430 nums = %d\\n\", n, len(nums))\n    return spaceQuadraticRecur(n - 1)\n}\n
space_complexity.swift
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\n@discardableResult\nfunc quadraticRecur(n: Int) -> Int {\n    if n <= 0 {\n        return 0\n    }\n    // \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n    let nums = Array(repeating: 0, count: n)\n    print(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = \\(n), \u0434\u043b\u0438\u043d\u0430 nums = \\(nums.count)\")\n    return quadraticRecur(n: n - 1)\n}\n
space_complexity.js
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction quadraticRecur(n) {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = ${n} \u0434\u043b\u0438\u043d\u0430 nums = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
space_complexity.ts
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction quadraticRecur(n: number): number {\n    if (n <= 0) return 0;\n    const nums = new Array(n);\n    console.log(`\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = ${n} \u0434\u043b\u0438\u043d\u0430 nums = ${nums.length}`);\n    return quadraticRecur(n - 1);\n}\n
space_complexity.dart
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint quadraticRecur(int n) {\n  if (n <= 0) return 0;\n  List<int> nums = List.filled(n, 0);\n  print('\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = $n \u0434\u043b\u0438\u043d\u0430 nums = ${nums.length}');\n  return quadraticRecur(n - 1);\n}\n
space_complexity.rs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn quadratic_recur(n: i32) -> i32 {\n    if n <= 0 {\n        return 0;\n    };\n    // \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n    let nums = vec![0; n as usize];\n    println!(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = {} , \u0434\u043b\u0438\u043d\u0430 nums = {}\", n, nums.len());\n    return quadratic_recur(n - 1);\n}\n
space_complexity.c
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint quadraticRecur(int n) {\n    if (n <= 0)\n        return 0;\n    int *nums = malloc(sizeof(int) * n);\n    printf(\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = %d, \u0434\u043b\u0438\u043d\u0430 nums = %d\\r\\n\", n, n);\n    int res = quadraticRecur(n - 1);\n    free(nums);\n    return res;\n}\n
space_complexity.kt
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\ntailrec fun quadraticRecur(n: Int): Int {\n    if (n <= 0)\n        return 0\n    // \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n    val nums = Array(n) { 0 }\n    println(\"\u0412 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 n = $n \u0434\u043b\u0438\u043d\u0430 nums = ${nums.size}\")\n    return quadraticRecur(n - 1)\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  nums = Array.new(n, 0)\n\n  # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef linear_recur(n)\n  puts \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  # \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n  Array.new(n) { Array.new(n, 0) }\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-18 \u00a0 \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c\u0430\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439

"},{"location":"chapter_computational_complexity/space_complexity/#4-o2n","title":"4. \u00a0 \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(2^n)\\)","text":"

\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0443 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432. \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 2-19: \"\u043f\u043e\u043b\u043d\u043e\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\" \u0441 \\(n\\) \u0443\u0440\u043e\u0432\u043d\u044f\u043c\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \\(2^n - 1\\) \u0443\u0437\u043b\u043e\u0432 \u0438 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(2^n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby space_complexity.py
def build_tree(n: int) -> TreeNode | None:\n    \"\"\"\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430)\"\"\"\n    if n == 0:\n        return None\n    root = TreeNode(0)\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n    return root\n
space_complexity.cpp
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return nullptr;\n    TreeNode *root = new TreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
space_complexity.java
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nTreeNode buildTree(int n) {\n    if (n == 0)\n        return null;\n    TreeNode root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
space_complexity.cs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nTreeNode? BuildTree(int n) {\n    if (n == 0) return null;\n    TreeNode root = new(0) {\n        left = BuildTree(n - 1),\n        right = BuildTree(n - 1)\n    };\n    return root;\n}\n
space_complexity.go
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfunc buildTree(n int) *TreeNode {\n    if n == 0 {\n        return nil\n    }\n    root := NewTreeNode(0)\n    root.Left = buildTree(n - 1)\n    root.Right = buildTree(n - 1)\n    return root\n}\n
space_complexity.swift
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfunc buildTree(n: Int) -> TreeNode? {\n    if n == 0 {\n        return nil\n    }\n    let root = TreeNode(x: 0)\n    root.left = buildTree(n: n - 1)\n    root.right = buildTree(n: n - 1)\n    return root\n}\n
space_complexity.js
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfunction buildTree(n) {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
space_complexity.ts
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfunction buildTree(n: number): TreeNode | null {\n    if (n === 0) return null;\n    const root = new TreeNode(0);\n    root.left = buildTree(n - 1);\n    root.right = buildTree(n - 1);\n    return root;\n}\n
space_complexity.dart
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nTreeNode? buildTree(int n) {\n  if (n == 0) return null;\n  TreeNode root = TreeNode(0);\n  root.left = buildTree(n - 1);\n  root.right = buildTree(n - 1);\n  return root;\n}\n
space_complexity.rs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfn build_tree(n: i32) -> Option<Rc<RefCell<TreeNode>>> {\n    if n == 0 {\n        return None;\n    };\n    let root = TreeNode::new(0);\n    root.borrow_mut().left = build_tree(n - 1);\n    root.borrow_mut().right = build_tree(n - 1);\n    return Some(root);\n}\n
space_complexity.c
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nTreeNode *buildTree(int n) {\n    if (n == 0)\n        return NULL;\n    TreeNode *root = newTreeNode(0);\n    root->left = buildTree(n - 1);\n    root->right = buildTree(n - 1);\n    return root;\n}\n
space_complexity.kt
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) */\nfun buildTree(n: Int): TreeNode? {\n    if (n == 0)\n        return null\n    val root = TreeNode(0)\n    root.left = buildTree(n - 1)\n    root.right = buildTree(n - 1)\n    return root\n}\n
space_complexity.rb
=begin\nFile: space_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u0424\u0443\u043d\u043a\u0446\u0438\u044f ###\ndef function\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n  0\nend\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  # \u041a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  a = 0\n  nums = [0] * 10000\n  node = ListNode.new\n\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { c = 0 }\n  # \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 O(1) \u043f\u0430\u043c\u044f\u0442\u0438\n  (0...n).each { function }\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  # \u0421\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  nums = Array.new(n, 0)\n\n  # \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u0438\u043d\u044b n \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n) \u043f\u0430\u043c\u044f\u0442\u0438\n  hmap = {}\n  for i in 0...n\n    hmap[i] = i.to_s\n  end\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef linear_recur(n)\n  puts \"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044f n = #{n}\"\n  return if n == 1\n  linear_recur(n - 1)\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  # \u0414\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 O(n^2) \u043f\u0430\u043c\u044f\u0442\u0438\n  Array.new(n) { Array.new(n, 0) }\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef quadratic_recur(n)\n  return 0 unless n > 0\n\n  # \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums \u0440\u0430\u0432\u043d\u0430 n, n-1, ..., 2, 1\n  nums = Array.new(n, 0)\n  quadratic_recur(n - 1)\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430) ###\ndef build_tree(n)\n  return if n == 0\n\n  TreeNode.new.tap do |root|\n    root.left = build_tree(n - 1)\n    root.right = build_tree(n - 1)\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-19 \u00a0 \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c\u0430\u044f \u043f\u043e\u043b\u043d\u044b\u043c \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c

"},{"location":"chapter_computational_complexity/space_complexity/#5-olog-n","title":"5. \u00a0 \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(\\log n)\\)","text":"

\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\". \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b \\(n\\) \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043f\u043e\u043f\u043e\u043b\u0430\u043c \u043f\u043e \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0441\u043e\u0442\u044b \\(\\log n\\) \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \\(O(\\log n)\\) \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043a\u0430\u0434\u0440\u043e\u0432 \u0441\u0442\u0435\u043a\u0430.

\u0415\u0449\u0435 \u043e\u0434\u0438\u043d \u043f\u0440\u0438\u043c\u0435\u0440 - \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d\u043e \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(n\\) , \u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u0446\u0438\u0444\u0440 \u0440\u0430\u0432\u043d\u043e \\(\\lfloor \\log_{10} n \\rfloor + 1\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u0442\u043e\u0436\u0435 \u0440\u0430\u0432\u043d\u0430 \\(\\lfloor \\log_{10} n \\rfloor + 1\\) , \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(\\log_{10} n + 1) = O(\\log n)\\) .

"},{"location":"chapter_computational_complexity/space_complexity/#244","title":"2.4.4 \u00a0 \u041a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441 \u043c\u0435\u0436\u0434\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c","text":"

\u0412 \u0438\u0434\u0435\u0430\u043b\u0435 \u043d\u0430\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b, \u0447\u0442\u043e\u0431\u044b \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f, \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0431\u044b\u043b\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0440\u0435\u043c\u044f, \u0438 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0442\u0440\u0443\u0434\u043d\u043e.

\u0421\u043d\u0438\u0436\u0435\u043d\u0438\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043d\u043e\u0439 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0438 \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442. \u041f\u043e\u0434\u0445\u043e\u0434, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0436\u0435\u0440\u0442\u0432\u0443\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0440\u0430\u0434\u0438 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u043e\u0431\u043c\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f\"; \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u043e\u0431\u043c\u0435\u043d \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\".

\u0412\u044b\u0431\u043e\u0440 \u043c\u0435\u0436\u0434\u0443 \u044d\u0442\u0438\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0438\u0434\u0435\u044f\u043c\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u043d\u0430\u0441 \u0432\u0430\u0436\u043d\u0435\u0435. \u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u0440\u0435\u043c\u044f \u0446\u0435\u043d\u043d\u0435\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \"\u043e\u0431\u043c\u0435\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f\" \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0430\u0449\u0435. \u041d\u043e \u043f\u0440\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0442\u043e\u0436\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u0440\u0430\u0439\u043d\u0435 \u0432\u0430\u0436\u043d\u044b\u043c.

"},{"location":"chapter_computational_complexity/summary/","title":"2.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_computational_complexity/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":"

\u041e\u0446\u0435\u043d\u043a\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

"},{"location":"chapter_computational_complexity/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 \\(O(1)\\)?

\u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e-\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e \\(O(1)\\) . \u041e\u0434\u043d\u0430\u043a\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 Java, Python, C++, Go, C# \u0438 \u0434\u0440\u0443\u0433\u0438\u0435) \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u043e\u0439 \\(O(n)\\) .

Q: \u0412 \u0447\u0435\u043c \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043c\u0438 function \u0438 method?

\u0424\u0443\u043d\u043a\u0446\u0438\u044f (function) \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e, \u0438 \u0432\u0441\u0435 \u0435\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u044e\u0442\u0441\u044f \u044f\u0432\u043d\u043e. \u041c\u0435\u0442\u043e\u0434 (method) \u0441\u0432\u044f\u0437\u0430\u043d \u0441 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c, \u043d\u0435\u044f\u0432\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0435\u0433\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442, \u0438 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u043c\u0438\u0441\u044f \u0432 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u0435 \u043a\u043b\u0430\u0441\u0441\u0430.

\u041d\u0438\u0436\u0435 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

Q: \u041e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u043b\u0438 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \"\u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438\" \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u043d\u044f\u0442\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438?

\u041d\u0435\u0442, \u044d\u0442\u0430 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u0440\u043e\u0441\u0442\u0430, \u0430 \u043d\u0435 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u0437\u0430\u043d\u044f\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430.

\u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \\(n = 8\\) , \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0440\u0438\u0432\u044b\u0445 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u043c\u0438. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u043a\u0440\u0438\u0432\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u0447\u043b\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u0436\u0438\u043c\u0430\u0435\u0442 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u043e \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e \u0443\u0434\u043e\u0431\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0430.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u043c, \u043a\u0430\u043a\u043e\u0432\u0430 \"\u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u0430\u044f\" \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430, \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u044b, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f \\(n = 8\\) . \u041d\u043e \u0434\u043b\u044f \\(n = 8^5\\) \u0432\u044b\u0431\u043e\u0440 \u0443\u0436\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u0435\u043d: \u0432 \u044d\u0442\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0434\u043e\u043c\u0438\u043d\u0438\u0440\u0443\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044f \u0440\u043e\u0441\u0442\u0430.

Q: \u0411\u044b\u0432\u0430\u044e\u0442 \u043b\u0438 \u0441\u043b\u0443\u0447\u0430\u0438, \u043a\u043e\u0433\u0434\u0430 \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0436\u0435\u0440\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0440\u0430\u0434\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0438\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c \u0440\u0430\u0434\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438?

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0432 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442 \u043e\u0431\u043c\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432 \u0431\u0430\u0437\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u0442\u0440\u043e\u044f\u0442 B+ \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0438\u043b\u0438 \u0445\u0435\u0448-\u0438\u043d\u0434\u0435\u043a\u0441\u044b, \u0440\u0430\u0441\u0445\u043e\u0434\u0443\u044f \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438 \u0440\u0430\u0434\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0443\u0440\u043e\u0432\u043d\u044f \\(O(\\log n)\\) \u0438\u043b\u0438 \u0434\u0430\u0436\u0435 \\(O(1)\\).

\u0412 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445, \u0433\u0434\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0434\u043e\u0440\u043e\u0433\u0430, \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043c\u043e\u0433\u0443\u0442 \u0436\u0435\u0440\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0440\u0430\u0434\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 embedded-\u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u043f\u0430\u043c\u044f\u0442\u044c \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u0430 \u043e\u0447\u0435\u043d\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u044b \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446 \u0438 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u044d\u043a\u043e\u043d\u043e\u043c\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0446\u0435\u043d\u043e\u0439 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430.

"},{"location":"chapter_computational_complexity/time_complexity/","title":"2.3 \u00a0 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c","text":"

\u0412\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u0438 \u0442\u043e\u0447\u043d\u043e \u043e\u0442\u0440\u0430\u0436\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0442\u043e\u0447\u043d\u043e \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u043a\u043e\u0434\u0430, \u043a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c?

  1. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u043e\u0440\u0443\u0434\u043e\u0432\u0430\u043d\u0438\u044f, \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u0443\u044e \u0441\u0440\u0435\u0434\u0443 \u0438 \u0442.\u0434., \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432\u0441\u0435 \u044d\u0442\u0438 \u0444\u0430\u043a\u0442\u043e\u0440\u044b \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430.
  2. \u041e\u0446\u0435\u043d\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f + \u0442\u0440\u0435\u0431\u0443\u0435\u0442 1 \u043d\u0441 , \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f * \u0442\u0440\u0435\u0431\u0443\u0435\u0442 10 \u043d\u0441 , \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u044b\u0432\u043e\u0434\u0430 print() \u0442\u0440\u0435\u0431\u0443\u0435\u0442 5 \u043d\u0441 \u0438 \u0442.\u0434.
  3. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u043a\u043e\u0434\u0435 \u0438 \u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u0435\u043d \\(n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
# \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\ndef algorithm(n: int):\n    a = 2      # 1 \u043d\u0441\n    a = a + 1  # 1 \u043d\u0441\n    a = a * 2  # 10 \u043d\u0441\n    # \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for _ in range(n):  # 1 \u043d\u0441\n        print(0)        # 5 \u043d\u0441\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nvoid algorithm(int n) {\n    int a = 2;  // 1 \u043d\u0441\n    a = a + 1;  // 1 \u043d\u0441\n    a = a * 2;  // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {  // 1 \u043d\u0441\n        cout << 0 << endl;         // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nvoid algorithm(int n) {\n    int a = 2;  // 1 \u043d\u0441\n    a = a + 1;  // 1 \u043d\u0441\n    a = a * 2;  // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {  // 1 \u043d\u0441\n        System.out.println(0);     // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nvoid Algorithm(int n) {\n    int a = 2;  // 1 \u043d\u0441\n    a = a + 1;  // 1 \u043d\u0441\n    a = a * 2;  // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {  // 1 \u043d\u0441\n        Console.WriteLine(0);      // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfunc algorithm(n int) {\n    a := 2     // 1 \u043d\u0441\n    a = a + 1  // 1 \u043d\u0441\n    a = a * 2  // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for i := 0; i < n; i++ {  // 1 \u043d\u0441\n        fmt.Println(a)        // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfunc algorithm(n: Int) {\n    var a = 2 // 1 \u043d\u0441\n    a = a + 1 // 1 \u043d\u0441\n    a = a * 2 // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for _ in 0 ..< n { // 1 \u043d\u0441\n        print(0) // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfunction algorithm(n) {\n    var a = 2; // 1 \u043d\u0441\n    a = a + 1; // 1 \u043d\u0441\n    a = a * 2; // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for(let i = 0; i < n; i++) { // 1 \u043d\u0441\n        console.log(0); // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfunction algorithm(n: number): void {\n    var a: number = 2; // 1 \u043d\u0441\n    a = a + 1; // 1 \u043d\u0441\n    a = a * 2; // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for(let i = 0; i < n; i++) { // 1 \u043d\u0441\n        console.log(0); // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nvoid algorithm(int n) {\n  int a = 2; // 1 \u043d\u0441\n  a = a + 1; // 1 \u043d\u0441\n  a = a * 2; // 10 \u043d\u0441\n  // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n  for (int i = 0; i < n; i++) { // 1 \u043d\u0441\n    print(0); // 5 \u043d\u0441\n  }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfn algorithm(n: i32) {\n    let mut a = 2;      // 1 \u043d\u0441\n    a = a + 1;          // 1 \u043d\u0441\n    a = a * 2;          // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for _ in 0..n {     // 1 \u043d\u0441\n        println!(\"{}\", 0);  // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nvoid algorithm(int n) {\n    int a = 2;  // 1 \u043d\u0441\n    a = a + 1;  // 1 \u043d\u0441\n    a = a * 2;  // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {   // 1 \u043d\u0441\n        printf(\"%d\", 0);            // 5 \u043d\u0441\n    }\n}\n
// \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\nfun algorithm(n: Int) {\n    var a = 2 // 1 \u043d\u0441\n    a = a + 1 // 1 \u043d\u0441\n    a = a * 2 // 10 \u043d\u0441\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (i in 0..<n) {  // 1 \u043d\u0441\n        println(0)      // 5 \u043d\u0441\n    }\n}\n
# \u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\ndef algorithm(n)\n    a = 2       # 1 \u043d\u0441\n    a = a + 1   # 1 \u043d\u0441\n    a = a * 2   # 10 \u043d\u0441\n    # \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    (0...n).each do # 1 \u043d\u0441\n        puts 0      # 5 \u043d\u0441\n    end\nend\n

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c\u0443 \u0432\u044b\u0448\u0435 \u043c\u0435\u0442\u043e\u0434\u0443, \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0440\u0430\u0432\u043d\u043e \\((6n + 12)\\) \u043d\u0441 :

\\[ 1 + 1 + 10 + (1 + 5) \\times n = 6n + 12 \\]

\u041d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u043d\u0435\u0440\u0430\u0437\u0443\u043c\u043d\u043e, \u0438 \u043d\u0435\u0440\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u043e. \u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u043c\u044b \u043d\u0435 \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u043e\u0446\u0435\u043d\u043a\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u043b\u0430\u0442\u0444\u043e\u0440\u043c\u0430\u0445. \u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043d\u0430\u043c \u0442\u0440\u0443\u0434\u043d\u043e \u0443\u0437\u043d\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0430 \u044d\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u0435\u0442 \u043e\u0446\u0435\u043d\u043a\u0443.

"},{"location":"chapter_computational_complexity/time_complexity/#231","title":"2.3.1 \u00a0 \u041f\u043e\u0434\u0441\u0447\u0435\u0442 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u0438 \u0440\u043e\u0441\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438","text":"

\u0410\u043d\u0430\u043b\u0438\u0437 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u0435\u0442 \u043d\u0435 \u0441\u0430\u043c\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u0430 \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044e \u0440\u043e\u0441\u0442\u0430 \u044d\u0442\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e \u043c\u0435\u0440\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044a\u0435\u043c\u0430 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041f\u043e\u043d\u044f\u0442\u0438\u0435 \"\u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u0438 \u0440\u043e\u0441\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438\" \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u0435\u043d \\(n\\) , \u0438 \u0434\u0430\u043d\u044b \u0442\u0440\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A , B \u0438 C :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\ndef algorithm_A(n: int):\n    print(0)\n# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\ndef algorithm_B(n: int):\n    for _ in range(n):\n        print(0)\n# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\ndef algorithm_C(n: int):\n    for _ in range(1000000):\n        print(0)\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_A(int n) {\n    cout << 0 << endl;\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        cout << 0 << endl;\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        cout << 0 << endl;\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_A(int n) {\n    System.out.println(0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        System.out.println(0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        System.out.println(0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid AlgorithmA(int n) {\n    Console.WriteLine(0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nvoid AlgorithmB(int n) {\n    for (int i = 0; i < n; i++) {\n        Console.WriteLine(0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid AlgorithmC(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        Console.WriteLine(0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunc algorithm_A(n int) {\n    fmt.Println(0)\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfunc algorithm_B(n int) {\n    for i := 0; i < n; i++ {\n        fmt.Println(0)\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunc algorithm_C(n int) {\n    for i := 0; i < 1000000; i++ {\n        fmt.Println(0)\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunc algorithmA(n: Int) {\n    print(0)\n}\n\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfunc algorithmB(n: Int) {\n    for _ in 0 ..< n {\n        print(0)\n    }\n}\n\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunc algorithmC(n: Int) {\n    for _ in 0 ..< 1_000_000 {\n        print(0)\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunction algorithm_A(n) {\n    console.log(0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfunction algorithm_B(n) {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunction algorithm_C(n) {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunction algorithm_A(n: number): void {\n    console.log(0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfunction algorithm_B(n: number): void {\n    for (let i = 0; i < n; i++) {\n        console.log(0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfunction algorithm_C(n: number): void {\n    for (let i = 0; i < 1000000; i++) {\n        console.log(0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithmA(int n) {\n  print(0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nvoid algorithmB(int n) {\n  for (int i = 0; i < n; i++) {\n    print(0);\n  }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithmC(int n) {\n  for (int i = 0; i < 1000000; i++) {\n    print(0);\n  }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfn algorithm_A(n: i32) {\n    println!(\"{}\", 0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfn algorithm_B(n: i32) {\n    for _ in 0..n {\n        println!(\"{}\", 0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfn algorithm_C(n: i32) {\n    for _ in 0..1000000 {\n        println!(\"{}\", 0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_A(int n) {\n    printf(\"%d\", 0);\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nvoid algorithm_B(int n) {\n    for (int i = 0; i < n; i++) {\n        printf(\"%d\", 0);\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nvoid algorithm_C(int n) {\n    for (int i = 0; i < 1000000; i++) {\n        printf(\"%d\", 0);\n    }\n}\n
// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfun algoritm_A(n: Int) {\n    println(0)\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\nfun algorithm_B(n: Int) {\n    for (i in 0..<n){\n        println(0)\n    }\n}\n// \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\nfun algorithm_C(n: Int) {\n    for (i in 0..<1000000) {\n        println(0)\n    }\n}\n
# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 A: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\ndef algorithm_A(n)\n    puts 0\nend\n\n# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 B: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f\ndef algorithm_B(n)\n    (0...n).each { puts 0 }\nend\n\n# \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 C: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f\ndef algorithm_C(n)\n    (0...1_000_000).each { puts 0 }\nend\n

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-7 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0440\u0435\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0432\u044b\u0448\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-7 \u00a0 \u0422\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u0438 \u0440\u043e\u0441\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 A, B \u0438 C

\u041a\u0430\u043a\u0438\u0435 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0438\u043c\u0435\u0435\u0442 \u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430?

"},{"location":"chapter_computational_complexity/time_complexity/#232","title":"2.3.2 \u00a0 \u0410\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438","text":"

\u0414\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u0432\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \\(n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
def algorithm(n: int):\n    a = 1      # +1\n    a = a + 1  # +1\n    a = a * 2  # +1\n    # \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for i in range(n):  # +1\n        print(0)        # +1\n
void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) { // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        cout << 0 << endl;    // +1\n    }\n}\n
void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) { // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        System.out.println(0);    // +1\n    }\n}\n
void Algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {   // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        Console.WriteLine(0);   // +1\n    }\n}\n
func algorithm(n int) {\n    a := 1      // +1\n    a = a + 1   // +1\n    a = a * 2   // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for i := 0; i < n; i++ {   // +1\n        fmt.Println(a)         // +1\n    }\n}\n
func algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for _ in 0 ..< n { // +1\n        print(0) // +1\n    }\n}\n
function algorithm(n) {\n    var a = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for(let i = 0; i < n; i++){ // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        console.log(0); // +1\n    }\n}\n
function algorithm(n: number): void{\n    var a: number = 1; // +1\n    a += 1; // +1\n    a *= 2; // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for(let i = 0; i < n; i++){ // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        console.log(0); // +1\n    }\n}\n
void algorithm(int n) {\n  int a = 1; // +1\n  a = a + 1; // +1\n  a = a * 2; // +1\n  // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n  for (int i = 0; i < n; i++) { // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n    print(0); // +1\n  }\n}\n
fn algorithm(n: i32) {\n    let mut a = 1;   // +1\n    a = a + 1;      // +1\n    a = a * 2;      // +1\n\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for _ in 0..n { // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        println!(\"{}\", 0); // +1\n    }\n}\n
void algorithm(int n) {\n    int a = 1;  // +1\n    a = a + 1;  // +1\n    a = a * 2;  // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (int i = 0; i < n; i++) {   // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        printf(\"%d\", 0);            // +1\n    }\n}\n
fun algorithm(n: Int) {\n    var a = 1 // +1\n    a = a + 1 // +1\n    a = a * 2 // +1\n    // \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    for (i in 0..<n) { // +1 (\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f i ++)\n        println(0) // +1\n    }\n}\n
def algorithm(n)\n    a = 1       # +1\n    a = a + 1   # +1\n    a = a * 2   # +1\n    # \u0426\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f n \u0440\u0430\u0437\n    (0...n).each do # +1\n        puts 0      # +1\n    end\nend\n

\u041f\u0443\u0441\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(T(n)\\) ; \u0442\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0439 \u0432\u044b\u0448\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e:

\\[ T(n) = 3 + 2n \\]

\\(T(n)\\) - \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u0430 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0442\u0435\u043d\u0434\u0435\u043d\u0446\u0438\u044f \u0440\u043e\u0441\u0442\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u043b\u0438\u043d\u0435\u0439\u043d\u0430, \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0435\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439.

\u041b\u0438\u043d\u0435\u0439\u043d\u0443\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u043a \\(O(n)\\) ; \u044d\u0442\u043e\u0442 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 Big \\(O\\) (big-\\(O\\) notation) \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u0443 (asymptotic upper bound) \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \\(T(n)\\) .

\u041f\u043e \u0441\u0443\u0442\u0438 \u0430\u043d\u0430\u043b\u0438\u0437 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 - \u044d\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \"\u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \\(T(n)\\)\", \u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435.

\u0410\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438

\u0415\u0441\u043b\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(c\\) \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(n_0\\) , \u0442\u0430\u043a\u0438\u0435 \u0447\u0442\u043e \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \\(n > n_0\\) \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(T(n) \\leq c \\cdot f(n)\\) , \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \\(f(n)\\) \u0437\u0430\u0434\u0430\u0435\u0442 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u043b\u044f \\(T(n)\\) ; \u044d\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(T(n) = O(f(n))\\) .

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-8, \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u0443 - \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0430\u0439\u0442\u0438 \u0442\u0430\u043a\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \\(f(n)\\) , \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u0442\u0440\u0435\u043c\u043b\u0435\u043d\u0438\u0438 \\(n\\) \u043a \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \\(T(n)\\) \u0438 \\(f(n)\\) \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0440\u043e\u0441\u0442\u0430 \u0438 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u043c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u043e\u043c \\(c\\).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-8 \u00a0 \u0410\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438

"},{"location":"chapter_computational_complexity/time_complexity/#233","title":"2.3.3 \u00a0 \u041c\u0435\u0442\u043e\u0434 \u0432\u044b\u0432\u043e\u0434\u0430","text":"

\u041c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0444\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e, \u0438 \u0435\u0441\u043b\u0438 \u0442\u044b \u043f\u043e\u043d\u044f\u043b \u0435\u0433\u043e \u043d\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u043f\u0435\u0440\u0435\u0436\u0438\u0432\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u043e\u0438\u0442. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u0441\u0430\u043c \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0432\u043e\u0434\u0430, \u0430 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u043f\u043e\u0447\u0443\u0432\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u043c\u044b\u0441\u043b.

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e, \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b\u0438 \\(f(n)\\) , \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(f(n))\\) . \u041d\u043e \u043a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0430\u043c\u0443 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \\(f(n)\\) ? \u0412 \u0446\u0435\u043b\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0448\u0430\u0433\u043e\u0432: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0437\u0430\u0442\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u0443.

"},{"location":"chapter_computational_complexity/time_complexity/#1-1","title":"1. \u00a0 \u0428\u0430\u0433 1: \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439","text":"

\u0414\u043b\u044f \u043a\u043e\u0434\u0430 \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437. \u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \\(c \\cdot f(n)\\) \u0432\u044b\u0448\u0435 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0439 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \\(c\\) \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c \u0443\u0433\u043e\u0434\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u044b \u0438 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u044b\u0435 \u0447\u043b\u0435\u043d\u044b \u0432 \u0447\u0438\u0441\u043b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \\(T(n)\\) \u043c\u043e\u0436\u043d\u043e \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430, \u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0443\u043f\u0440\u043e\u0449\u0430\u044e\u0449\u0438\u0435 \u043f\u0440\u0438\u0435\u043c\u044b \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430.

  1. \u0418\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0432 \\(T(n)\\). \u041e\u043d\u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \\(n\\) , \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043d\u0435 \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c.
  2. \u041e\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0432\u0441\u0435 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0438\u043a\u043b\u044b \u043d\u0430 \\(2n\\) \u0440\u0430\u0437 \u0438\u043b\u0438 \\(5n + 1\\) \u0440\u0430\u0437 \u043c\u043e\u0436\u043d\u043e \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0434\u043e \\(n\\) \u0440\u0430\u0437, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043f\u0435\u0440\u0435\u0434 \\(n\\) \u043d\u0435 \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c.
  3. \u041f\u0440\u0438 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435. \u041e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0447\u0438\u0441\u043b\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0446\u0438\u043a\u043b\u043e\u0432; \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0446\u0438\u043a\u043b\u0430 \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0440\u0438\u0435\u043c\u044b \u0438\u0437 \u043f\u0443\u043d\u043a\u0442\u043e\u0432 1. \u0438 2. .

\u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u0435\u043c\u044b \u0438 \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
def algorithm(n: int):\n    a = 1      # +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n  # +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    # +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for i in range(5 * n + 1):\n        print(0)\n    # +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for i in range(2 * n):\n        for j in range(n + 1):\n            print(0)\n
void algorithm(int n) {\n    int a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        cout << 0 << endl;\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            cout << 0 << endl;\n        }\n    }\n}\n
void algorithm(int n) {\n    int a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        System.out.println(0);\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            System.out.println(0);\n        }\n    }\n}\n
void Algorithm(int n) {\n    int a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        Console.WriteLine(0);\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            Console.WriteLine(0);\n        }\n    }\n}\n
func algorithm(n int) {\n    a := 1     // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for i := 0; i < 5 * n + 1; i++ {\n        fmt.Println(0)\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for i := 0; i < 2 * n; i++ {\n        for j := 0; j < n + 1; j++ {\n            fmt.Println(0)\n        }\n    }\n}\n
func algorithm(n: Int) {\n    var a = 1 // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for _ in 0 ..< (5 * n + 1) {\n        print(0)\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for _ in 0 ..< (2 * n) {\n        for _ in 0 ..< (n + 1) {\n            print(0)\n        }\n    }\n}\n
function algorithm(n) {\n    let a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
function algorithm(n: number): void {\n    let a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (let i = 0; i < 5 * n + 1; i++) {\n        console.log(0);\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (let i = 0; i < 2 * n; i++) {\n        for (let j = 0; j < n + 1; j++) {\n            console.log(0);\n        }\n    }\n}\n
void algorithm(int n) {\n  int a = 1; // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n  a = a + n; // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n  // +n (\u043f\u0440\u0438\u0435\u043c 2)\n  for (int i = 0; i < 5 * n + 1; i++) {\n    print(0);\n  }\n  // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n  for (int i = 0; i < 2 * n; i++) {\n    for (int j = 0; j < n + 1; j++) {\n      print(0);\n    }\n  }\n}\n
fn algorithm(n: i32) {\n    let mut a = 1;     // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;        // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for i in 0..(5 * n + 1) {\n        println!(\"{}\", 0);\n    }\n\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for i in 0..(2 * n) {\n        for j in 0..(n + 1) {\n            println!(\"{}\", 0);\n        }\n    }\n}\n
void algorithm(int n) {\n    int a = 1;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n;  // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (int i = 0; i < 5 * n + 1; i++) {\n        printf(\"%d\", 0);\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (int i = 0; i < 2 * n; i++) {\n        for (int j = 0; j < n + 1; j++) {\n            printf(\"%d\", 0);\n        }\n    }\n}\n
fun algorithm(n: Int) {\n    var a = 1   // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n   // +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    // +n (\u043f\u0440\u0438\u0435\u043c 2)\n    for (i in 0..<5 * n + 1) {\n        println(0)\n    }\n    // +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    for (i in 0..<2 * n) {\n        for (j in 0..<n + 1) {\n            println(0)\n        }\n    }\n}\n
def algorithm(n)\n    a = 1       # +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    a = a + n   # +0 (\u043f\u0440\u0438\u0435\u043c 1)\n    # +n (\u043f\u0440\u0438\u0435\u043c 2)\n    (0...(5 * n + 1)).each do { puts 0 }\n    # +n*n (\u043f\u0440\u0438\u0435\u043c 3)\n    (0...(2 * n)).each do\n        (0...(n + 1)).each do { puts 0 }\n    end\nend\n

\u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u0434\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u0435\u043c\u043e\u0432; \u0432 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) .

\\[ \\begin{aligned} T(n) & = 2n(n + 1) + (5n + 1) + 2 & \\text{\u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442 (-.-|||)} \\newline & = 2n^2 + 7n + 3 \\newline T(n) & = n^2 + n & \\text{\u043b\u0435\u043d\u0438\u0432\u044b\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442 (o.O)} \\end{aligned} \\]"},{"location":"chapter_computational_complexity/time_complexity/#2-2","title":"2. \u00a0 \u0428\u0430\u0433 2: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b","text":"

**\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0440\u0448\u0438\u043c \u043f\u043e \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0447\u043b\u0435\u043d\u043e\u043c \u0432 \\(T(n)\\) **. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u0442\u0440\u0435\u043c\u043b\u0435\u043d\u0438\u0438 \\(n\\) \u043a \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0441\u0442\u0430\u0440\u0448\u0438\u0439 \u0447\u043b\u0435\u043d \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0434\u043e\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0430 \u0432\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u043b\u0435\u043d\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043d\u0435\u0431\u0440\u0435\u0447\u044c.

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 2-2 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432. \u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u043d\u044b \u043f\u0440\u0435\u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u043d\u044b\u043c\u0438, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u043d\u0443\u0442\u044c \u0432\u044b\u0432\u043e\u0434: \"\u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u043d\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a\". \u041a\u043e\u0433\u0434\u0430 \\(n\\) \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u043a \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u0438, \u044d\u0442\u0438 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u043d\u0435\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 2-2 \u00a0 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439

\u0427\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \\(T(n)\\) \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(f(n))\\) \\(100000\\) \\(O(1)\\) \\(3n + 2\\) \\(O(n)\\) \\(2n^2 + 3n + 2\\) \\(O(n^2)\\) \\(n^3 + 10000n^2\\) \\(O(n^3)\\) \\(2^n + 10000n^{10000}\\) \\(O(2^n)\\)"},{"location":"chapter_computational_complexity/time_complexity/#234","title":"2.3.4 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b","text":"

\u041f\u0443\u0441\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u0435\u043d \\(n\\) ; \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-9 (\u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435\u0439 \u043a \u0431\u043e\u043b\u044c\u0448\u0435\u0439).

\\[ \\begin{aligned} O(1) < O(\\log n) < O(n) < O(n \\log n) < O(n^2) < O(2^n) < O(n!) \\newline \\text{\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f} < \\text{\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f} < \\text{\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f} < \\text{\u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f} < \\text{\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f} < \\text{\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f} < \\text{\u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f} \\end{aligned} \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-9 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438

"},{"location":"chapter_computational_complexity/time_complexity/#1-o1","title":"1. \u00a0 \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\)","text":"

\u0427\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0438 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \\(n\\) .

\u0412 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0445\u043e\u0442\u044f \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 size \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u043e\u043d\u043e \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def constant(n: int) -> int:\n    \"\"\"\u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    count = 0\n    size = 100000\n    for _ in range(size):\n        count += 1\n    return count\n
time_complexity.cpp
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
time_complexity.java
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
time_complexity.cs
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint Constant(int n) {\n    int count = 0;\n    int size = 100000;\n    for (int i = 0; i < size; i++)\n        count++;\n    return count;\n}\n
time_complexity.go
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc constant(n int) int {\n    count := 0\n    size := 100000\n    for i := 0; i < size; i++ {\n        count++\n    }\n    return count\n}\n
time_complexity.swift
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc constant(n: Int) -> Int {\n    var count = 0\n    let size = 100_000\n    for _ in 0 ..< size {\n        count += 1\n    }\n    return count\n}\n
time_complexity.js
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction constant(n) {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
time_complexity.ts
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction constant(n: number): number {\n    let count = 0;\n    const size = 100000;\n    for (let i = 0; i < size; i++) count++;\n    return count;\n}\n
time_complexity.dart
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint constant(int n) {\n  int count = 0;\n  int size = 100000;\n  for (var i = 0; i < size; i++) {\n    count++;\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfn constant(n: i32) -> i32 {\n    _ = n;\n    let mut count = 0;\n    let size = 100_000;\n    for _ in 0..size {\n        count += 1;\n    }\n    count\n}\n
time_complexity.c
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint constant(int n) {\n    int count = 0;\n    int size = 100000;\n    int i = 0;\n    for (int i = 0; i < size; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun constant(n: Int): Int {\n    var count = 0\n    val size = 100000\n    for (i in 0..<size)\n        count++\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_computational_complexity/time_complexity/#2-on","title":"2. \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\)","text":"

\u0427\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0438 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u0442\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) . \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0445 \u0446\u0438\u043a\u043b\u0430\u0445:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def linear(n: int) -> int:\n    \"\"\"\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    count = 0\n    for _ in range(n):\n        count += 1\n    return count\n
time_complexity.cpp
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
time_complexity.java
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
time_complexity.cs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint Linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++)\n        count++;\n    return count;\n}\n
time_complexity.go
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc linear(n int) int {\n    count := 0\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
time_complexity.swift
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc linear(n: Int) -> Int {\n    var count = 0\n    for _ in 0 ..< n {\n        count += 1\n    }\n    return count\n}\n
time_complexity.js
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linear(n) {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
time_complexity.ts
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linear(n: number): number {\n    let count = 0;\n    for (let i = 0; i < n; i++) count++;\n    return count;\n}\n
time_complexity.dart
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linear(int n) {\n  int count = 0;\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfn linear(n: i32) -> i32 {\n    let mut count = 0;\n    for _ in 0..n {\n        count += 1;\n    }\n    count\n}\n
time_complexity.c
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linear(int n) {\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun linear(n: Int): Int {\n    var count = 0\n    for (i in 0..<n)\n        count++\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u044e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\) , \u0433\u0434\u0435 \\(n\\) - \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043a\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def array_traversal(nums: list[int]) -> int:\n    \"\"\"\u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430)\"\"\"\n    count = 0\n    # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for num in nums:\n        count += 1\n    return count\n
time_complexity.cpp
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nint arrayTraversal(vector<int> &nums) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.java
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nint arrayTraversal(int[] nums) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int num : nums) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.cs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nint ArrayTraversal(int[] nums) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    foreach (int num in nums) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.go
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfunc arrayTraversal(nums []int) int {\n    count := 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for range nums {\n        count++\n    }\n    return count\n}\n
time_complexity.swift
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfunc arrayTraversal(nums: [Int]) -> Int {\n    var count = 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for _ in nums {\n        count += 1\n    }\n    return count\n}\n
time_complexity.js
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfunction arrayTraversal(nums) {\n    let count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.ts
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfunction arrayTraversal(nums: number[]): number {\n    let count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (let i = 0; i < nums.length; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.dart
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nint arrayTraversal(List<int> nums) {\n  int count = 0;\n  // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for (var _num in nums) {\n    count++;\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfn array_traversal(nums: &[i32]) -> i32 {\n    let mut count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for _ in nums {\n        count += 1;\n    }\n    count\n}\n
time_complexity.c
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nint arrayTraversal(int *nums, int n) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) */\nfun arrayTraversal(nums: IntArray): Int {\n    var count = 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (num in nums) {\n        count++\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u0438\u043f\u0430 \u0432\u0445\u043e\u0434\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \\(n\\) \u0441\u0430\u043c\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445; \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u0443\u0436\u0438\u0442 \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\(n\\) .

"},{"location":"chapter_computational_complexity/time_complexity/#3-on2","title":"3. \u00a0 \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n^2)\\)","text":"

\u0427\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0438 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0441\u0442\u0435\u0442 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) . \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430\u0445: \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0446\u0438\u043a\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(n^2)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def quadratic(n: int) -> int:\n    \"\"\"\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    count = 0\n    # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for i in range(n):\n        for j in range(n):\n            count += 1\n    return count\n
time_complexity.cpp
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint quadratic(int n) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.java
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint quadratic(int n) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.cs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint Quadratic(int n) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.go
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc quadratic(n int) int {\n    count := 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for i := 0; i < n; i++ {\n        for j := 0; j < n; j++ {\n            count++\n        }\n    }\n    return count\n}\n
time_complexity.swift
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc quadratic(n: Int) -> Int {\n    var count = 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for _ in 0 ..< n {\n        for _ in 0 ..< n {\n            count += 1\n        }\n    }\n    return count\n}\n
time_complexity.js
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction quadratic(n) {\n    let count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.ts
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction quadratic(n: number): number {\n    let count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.dart
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint quadratic(int n) {\n  int count = 0;\n  // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for (int i = 0; i < n; i++) {\n    for (int j = 0; j < n; j++) {\n      count++;\n    }\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfn quadratic(n: i32) -> i32 {\n    let mut count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for _ in 0..n {\n        for _ in 0..n {\n            count += 1;\n        }\n    }\n    count\n}\n
time_complexity.c
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint quadratic(int n) {\n    int count = 0;\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < n; j++) {\n            count++;\n        }\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun quadratic(n: Int): Int {\n    var count = 0\n    // \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n    for (i in 0..<n) {\n        for (j in 0..<n) {\n            count++\n        }\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-10 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0442\u0440\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438: \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f, \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0438 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-10 \u00a0 \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f, \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0438 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443: \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(n - 1\\) \u0440\u0430\u0437, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(n-1\\) , \\(n-2\\) , \\(\\dots\\) , \\(2\\) , \\(1\\) \u0440\u0430\u0437, \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u044d\u0442\u043e \\(n / 2\\) \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O((n - 1) n / 2) = O(n^2)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def bubble_sort(nums: list[int]) -> int:\n    \"\"\"\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430)\"\"\"\n    count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in range(len(nums) - 1, 0, -1):\n        # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                tmp: int = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3  # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n    return count\n
time_complexity.cpp
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nint bubbleSort(vector<int> &nums) {\n    int count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.java
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nint bubbleSort(int[] nums) {\n    int count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.cs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nint BubbleSort(int[] nums) {\n    int count = 0;  // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                count += 3;  // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.go
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfunc bubbleSort(nums []int) int {\n    count := 0 // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                tmp := nums[j]\n                nums[j] = nums[j+1]\n                nums[j+1] = tmp\n                count += 3 // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count\n}\n
time_complexity.swift
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfunc bubbleSort(nums: inout [Int]) -> Int {\n    var count = 0 // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = tmp\n                count += 3 // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count\n}\n
time_complexity.js
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfunction bubbleSort(nums) {\n    let count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.ts
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfunction bubbleSort(nums: number[]): number {\n    let count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.dart
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nint bubbleSort(List<int> nums) {\n  int count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n  // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for (var i = nums.length - 1; i > 0; i--) {\n    // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for (var j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      }\n    }\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfn bubble_sort(nums: &mut [i32]) -> i32 {\n    let mut count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    count\n}\n
time_complexity.c
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nint bubbleSort(int *nums, int n) {\n    int count = 0; // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = n - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                count += 3; // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) */\nfun bubbleSort(nums: IntArray): Int {\n    var count = 0 // \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                count += 3 // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n            }\n        }\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_computational_complexity/time_complexity/#4-o2n","title":"4. \u00a0 \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(2^n)\\)","text":"

\u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u043e\u0441\u0442\u0430 \u0432 \u0431\u0438\u043e\u043b\u043e\u0433\u0438\u0438 - \"\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u0435\u0442\u043e\u043a\": \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0435\u0441\u0442\u044c 1 \u043a\u043b\u0435\u0442\u043a\u0430, \u043f\u043e\u0441\u043b\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0445 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f 2, \u043f\u043e\u0441\u043b\u0435 \u0434\u0432\u0443\u0445 \u0434\u0435\u043b\u0435\u043d\u0438\u0439 - 4 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435; \u043f\u043e\u0441\u043b\u0435 \\(n\\) \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0435\u0442\u043e\u043a \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \\(2^n\\) .

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-11 \u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043a\u043b\u0435\u0442\u043e\u043a; \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(2^n)\\) . \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(n\\) \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 count \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u0439.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def exponential(n: int) -> int:\n    \"\"\"\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    count = 0\n    base = 1\n    # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in range(n):\n        for _ in range(base):\n            count += 1\n        base *= 2\n    # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n
time_complexity.cpp
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.java
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint exponential(int n) {\n    int count = 0, base = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.cs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint Exponential(int n) {\n    int count = 0, bas = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.go
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc exponential(n int) int {\n    count, base := 0, 1\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for i := 0; i < n; i++ {\n        for j := 0; j < base; j++ {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
time_complexity.swift
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc exponential(n: Int) -> Int {\n    var count = 0\n    var base = 1\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0 ..< n {\n        for _ in 0 ..< base {\n            count += 1\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
time_complexity.js
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction exponential(n) {\n    let count = 0,\n        base = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.ts
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction exponential(n: number): number {\n    let count = 0,\n        base = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (let i = 0; i < n; i++) {\n        for (let j = 0; j < base; j++) {\n            count++;\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.dart
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint exponential(int n) {\n  int count = 0, base = 1;\n  // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  for (var i = 0; i < n; i++) {\n    for (var j = 0; j < base; j++) {\n      count++;\n    }\n    base *= 2;\n  }\n  // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  return count;\n}\n
time_complexity.rs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn exponential(n: i32) -> i32 {\n    let mut count = 0;\n    let mut base = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for _ in 0..n {\n        for _ in 0..base {\n            count += 1\n        }\n        base *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    count\n}\n
time_complexity.c
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint exponential(int n) {\n    int count = 0;\n    int bas = 1;\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (int i = 0; i < n; i++) {\n        for (int j = 0; j < bas; j++) {\n            count++;\n        }\n        bas *= 2;\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count;\n}\n
time_complexity.kt
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun exponential(n: Int): Int {\n    var count = 0\n    var base = 1\n    // \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n    for (i in 0..<n) {\n        for (j in 0..<base) {\n            count++\n        }\n        base *= 2\n    }\n    // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-11 \u00a0 \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435 \u0438 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \\(n\\) \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0439:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def exp_recur(n: int) -> int:\n    \"\"\"\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    if n == 1:\n        return 1\n    return exp_recur(n - 1) + exp_recur(n - 1) + 1\n
time_complexity.cpp
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.java
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.cs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint ExpRecur(int n) {\n    if (n == 1) return 1;\n    return ExpRecur(n - 1) + ExpRecur(n - 1) + 1;\n}\n
time_complexity.go
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc expRecur(n int) int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n-1) + expRecur(n-1) + 1\n}\n
time_complexity.swift
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc expRecur(n: Int) -> Int {\n    if n == 1 {\n        return 1\n    }\n    return expRecur(n: n - 1) + expRecur(n: n - 1) + 1\n}\n
time_complexity.js
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction expRecur(n) {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.ts
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction expRecur(n: number): number {\n    if (n === 1) return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.dart
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint expRecur(int n) {\n  if (n == 1) return 1;\n  return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.rs
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn exp_recur(n: i32) -> i32 {\n    if n == 1 {\n        return 1;\n    }\n    exp_recur(n - 1) + exp_recur(n - 1) + 1\n}\n
time_complexity.c
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint expRecur(int n) {\n    if (n == 1)\n        return 1;\n    return expRecur(n - 1) + expRecur(n - 1) + 1;\n}\n
time_complexity.kt
/* \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun expRecur(n: Int): Int {\n    if (n == 1) {\n        return 1\n    }\n    return expRecur(n - 1) + expRecur(n - 1) + 1\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u043e\u0441\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043e\u0447\u0435\u043d\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u0430\u0445 (\u0433\u0440\u0443\u0431\u0430\u044f \u0441\u0438\u043b\u0430, backtracking \u0438 \u0442.\u0434.). \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0430 \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0435\u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u0430, \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0436\u0430\u0434\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u044b.

"},{"location":"chapter_computational_complexity/time_complexity/#5-olog-n","title":"5. \u00a0 \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(\\log n)\\)","text":"

\u0412 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439, \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \"\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0443\u043d\u0434 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0432\u0434\u0432\u043e\u0435\". \u041f\u0443\u0441\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u0435\u043d \\(n\\) ; \u0442\u0430\u043a \u043a\u0430\u043a \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0434\u0432\u043e\u0435, \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e \\(\\log_2 n\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 \u043a \\(2^n\\) .

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-12 \u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \"\u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0443\u043d\u0434 \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u0435 \u0432\u0434\u0432\u043e\u0435\"; \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(\\log_2 n)\\) \u0438 \u043a\u0440\u0430\u0442\u043a\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(O(\\log n)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def logarithmic(n: int) -> int:\n    \"\"\"\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    count = 0\n    while n > 1:\n        n = n / 2\n        count += 1\n    return count\n
time_complexity.cpp
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.java
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.cs
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint Logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n /= 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.go
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc logarithmic(n int) int {\n    count := 0\n    for n > 1 {\n        n = n / 2\n        count++\n    }\n    return count\n}\n
time_complexity.swift
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc logarithmic(n: Int) -> Int {\n    var count = 0\n    var n = n\n    while n > 1 {\n        n = n / 2\n        count += 1\n    }\n    return count\n}\n
time_complexity.js
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction logarithmic(n) {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.ts
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction logarithmic(n: number): number {\n    let count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.dart
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logarithmic(int n) {\n  int count = 0;\n  while (n > 1) {\n    n = n ~/ 2;\n    count++;\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn logarithmic(mut n: i32) -> i32 {\n    let mut count = 0;\n    while n > 1 {\n        n = n / 2;\n        count += 1;\n    }\n    count\n}\n
time_complexity.c
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logarithmic(int n) {\n    int count = 0;\n    while (n > 1) {\n        n = n / 2;\n        count++;\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun logarithmic(n: Int): Int {\n    var n1 = n\n    var count = 0\n    while (n1 > 1) {\n        n1 /= 2\n        count++\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-12 \u00a0 \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0442\u0430\u043a\u0436\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445. \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0441\u043e\u0442\u043e\u0439 \\(\\log_2 n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def log_recur(n: int) -> int:\n    \"\"\"\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    if n <= 1:\n        return 0\n    return log_recur(n / 2) + 1\n
time_complexity.cpp
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
time_complexity.java
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
time_complexity.cs
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint LogRecur(int n) {\n    if (n <= 1) return 0;\n    return LogRecur(n / 2) + 1;\n}\n
time_complexity.go
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc logRecur(n int) int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n/2) + 1\n}\n
time_complexity.swift
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc logRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 0\n    }\n    return logRecur(n: n / 2) + 1\n}\n
time_complexity.js
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction logRecur(n) {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
time_complexity.ts
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction logRecur(n: number): number {\n    if (n <= 1) return 0;\n    return logRecur(n / 2) + 1;\n}\n
time_complexity.dart
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logRecur(int n) {\n  if (n <= 1) return 0;\n  return logRecur(n ~/ 2) + 1;\n}\n
time_complexity.rs
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 0;\n    }\n    log_recur(n / 2) + 1\n}\n
time_complexity.c
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint logRecur(int n) {\n    if (n <= 1)\n        return 0;\n    return logRecur(n / 2) + 1;\n}\n
time_complexity.kt
/* \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun logRecur(n: Int): Int {\n    if (n <= 1)\n        return 0\n    return logRecur(n / 2) + 1\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\", \u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u0434\u0435\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u043e \u043d\u0430 \u043c\u043d\u043e\u0433\u043e\" \u0438 \"\u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0435\". \u041e\u043d\u0430 \u0440\u0430\u0441\u0442\u0435\u0442 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e, \u0443\u0441\u0442\u0443\u043f\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0439.

\u041a\u0430\u043a\u043e\u0432\u043e \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u0443 \\(O(\\log n)\\) ?

\u0422\u043e\u0447\u043d\u0435\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \\(m\\) \u0447\u0430\u0441\u0442\u0435\u0439\" \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \\(O(\\log_m n)\\) . \u0410 \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043a \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u044e \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0430 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0430\u0432\u043d\u044b\u0435 \u043f\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438:

\\[ O(\\log_m n) = O(\\log_k n / \\log_k m) = O(\\log_k n) \\]

\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \\(m\\) \u043c\u043e\u0436\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c \u0431\u0435\u0437 \u0432\u043b\u0438\u044f\u043d\u0438\u044f \u043d\u0430 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \\(m\\) \u0438 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a \\(O(\\log n)\\) .

"},{"location":"chapter_computational_complexity/time_complexity/#6-on-log-n","title":"6. \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n \\log n)\\)","text":"

\u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430\u0445, \u043a\u043e\u0433\u0434\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0445 \u0443\u0440\u043e\u0432\u043d\u0435\u0439 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0440\u0430\u0432\u043d\u0430 \\(O(\\log n)\\) \u0438 \\(O(n)\\) . \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def linear_log_recur(n: int) -> int:\n    \"\"\"\u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\"\"\"\n    if n <= 1:\n        return 1\n    # \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043d\u0430\u0434\u0432\u043e\u0435: \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0434\u0432\u043e\u0435\n    count = linear_log_recur(n // 2) + linear_log_recur(n // 2)\n    # \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 n \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439\n    for _ in range(n):\n        count += 1\n    return count\n
time_complexity.cpp
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.java
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.cs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint LinearLogRecur(int n) {\n    if (n <= 1) return 1;\n    int count = LinearLogRecur(n / 2) + LinearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.go
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc linearLogRecur(n int) int {\n    if n <= 1 {\n        return 1\n    }\n    count := linearLogRecur(n/2) + linearLogRecur(n/2)\n    for i := 0; i < n; i++ {\n        count++\n    }\n    return count\n}\n
time_complexity.swift
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunc linearLogRecur(n: Int) -> Int {\n    if n <= 1 {\n        return 1\n    }\n    var count = linearLogRecur(n: n / 2) + linearLogRecur(n: n / 2)\n    for _ in stride(from: 0, to: n, by: 1) {\n        count += 1\n    }\n    return count\n}\n
time_complexity.js
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linearLogRecur(n) {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.ts
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfunction linearLogRecur(n: number): number {\n    if (n <= 1) return 1;\n    let count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (let i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.dart
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linearLogRecur(int n) {\n  if (n <= 1) return 1;\n  int count = linearLogRecur(n ~/ 2) + linearLogRecur(n ~/ 2);\n  for (var i = 0; i < n; i++) {\n    count++;\n  }\n  return count;\n}\n
time_complexity.rs
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfn linear_log_recur(n: i32) -> i32 {\n    if n <= 1 {\n        return 1;\n    }\n    let mut count = linear_log_recur(n / 2) + linear_log_recur(n / 2);\n    for _ in 0..n {\n        count += 1;\n    }\n    return count;\n}\n
time_complexity.c
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nint linearLogRecur(int n) {\n    if (n <= 1)\n        return 1;\n    int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);\n    for (int i = 0; i < n; i++) {\n        count++;\n    }\n    return count;\n}\n
time_complexity.kt
/* \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c */\nfun linearLogRecur(n: Int): Int {\n    if (n <= 1)\n        return 1\n    var count = linearLogRecur(n / 2) + linearLogRecur(n / 2)\n    for (i in 0..<n) {\n        count++\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-13 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e, \u043a\u0430\u043a \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u041e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0430\u0432\u043d\u043e \\(n\\) , \u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u0438\u043c\u0435\u0435\u0442 \\(\\log_2 n + 1\\) \u0443\u0440\u043e\u0432\u043d\u0435\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-13 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) , \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0443 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c, \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0442.\u0434.

"},{"location":"chapter_computational_complexity/time_complexity/#7-on","title":"7. \u00a0 \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n!)\\)","text":"

\u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \"\u0432\u0441\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438\". \u0415\u0441\u043b\u0438 \u0434\u0430\u043d\u044b \\(n\\) \u043f\u043e\u043f\u0430\u0440\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u043d\u043e\u0432\u043e\u043a \u0440\u0430\u0432\u043d\u043e:

\\[ n! = n \\times (n - 1) \\times (n - 2) \\times \\dots \\times 2 \\times 1 \\]

\u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 2-14 \u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435, \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0430 \\(n\\) \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u043d\u0430 \u0432\u0442\u043e\u0440\u043e\u043c - \u043d\u0430 \\(n - 1\\) \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435, \u043f\u043e\u043a\u0430 \u043d\u0430 \\(n\\) -\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0432\u0435\u0442\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby time_complexity.py
def factorial_recur(n: int) -> int:\n    \"\"\"\u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f)\"\"\"\n    if n == 0:\n        return 1\n    count = 0\n    # \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for _ in range(n):\n        count += factorial_recur(n - 1)\n    return count\n
time_complexity.cpp
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.java
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.cs
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint FactorialRecur(int n) {\n    if (n == 0) return 1;\n    int count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (int i = 0; i < n; i++) {\n        count += FactorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.go
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc factorialRecur(n int) int {\n    if n == 0 {\n        return 1\n    }\n    count := 0\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for i := 0; i < n; i++ {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
time_complexity.swift
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunc factorialRecur(n: Int) -> Int {\n    if n == 0 {\n        return 1\n    }\n    var count = 0\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for _ in 0 ..< n {\n        count += factorialRecur(n: n - 1)\n    }\n    return count\n}\n
time_complexity.js
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction factorialRecur(n) {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.ts
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfunction factorialRecur(n: number): number {\n    if (n === 0) return 1;\n    let count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (let i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.dart
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint factorialRecur(int n) {\n  if (n == 0) return 1;\n  int count = 0;\n  // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n  for (var i = 0; i < n; i++) {\n    count += factorialRecur(n - 1);\n  }\n  return count;\n}\n
time_complexity.rs
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfn factorial_recur(n: i32) -> i32 {\n    if n == 0 {\n        return 1;\n    }\n    let mut count = 0;\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for _ in 0..n {\n        count += factorial_recur(n - 1);\n    }\n    count\n}\n
time_complexity.c
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nint factorialRecur(int n) {\n    if (n == 0)\n        return 1;\n    int count = 0;\n    for (int i = 0; i < n; i++) {\n        count += factorialRecur(n - 1);\n    }\n    return count;\n}\n
time_complexity.kt
/* \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) */\nfun factorialRecur(n: Int): Int {\n    if (n == 0)\n        return 1\n    var count = 0\n    // \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n    for (i in 0..<n) {\n        count += factorialRecur(n - 1)\n    }\n    return count\n}\n
time_complexity.rb
=begin\nFile: time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef constant(n)\n  count = 0\n  size = 100000\n\n  (0...size).each { count += 1 }\n\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear(n)\n  count = 0\n  (0...n).each { count += 1 }\n  count\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043e\u0431\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) ###\ndef array_traversal(nums)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  for num in nums\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef quadratic(n)\n  count = 0\n\n  # \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 n\n  for i in 0...n\n    for j in 0...n\n      count += 1\n    end\n  end\n\n  count\nend\n\n# ## \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430) ###\ndef bubble_sort(nums)\n  count = 0  # \u0421\u0447\u0435\u0442\u0447\u0438\u043a\n\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (nums.length - 1).downto(0)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        tmp = nums[j]\n        nums[j] = nums[j + 1]\n        nums[j + 1] = tmp\n        count += 3 # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438\n      end\n    end\n  end\n\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exponential(n)\n  count, base = 0, 1\n\n  # \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a\u043b\u0435\u0442\u043a\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430\u0434\u0432\u043e\u0435, \u043e\u0431\u0440\u0430\u0437\u0443\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c 1, 2, 4, 8, ..., 2^(n-1)\n  (0...n).each do\n    (0...base).each { count += 1 }\n    base *= 2\n  end\n\n  # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1\n  count\nend\n\n# ## \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef exp_recur(n)\n  return 1 if n == 1\n  exp_recur(n - 1) + exp_recur(n - 1) + 1\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef logarithmic(n)\n  count = 0\n\n  while n > 1\n    n /= 2\n    count += 1\n  end\n\n  count\nend\n\n# ## \u041b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef log_recur(n)\n  return 0 unless n > 1\n  log_recur(n / 2) + 1\nend\n\n# ## \u041b\u0438\u043d\u0435\u0439\u043d\u043e-\u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c ###\ndef linear_log_recur(n)\n  return 1 unless n > 1\n\n  count = linear_log_recur(n / 2) + linear_log_recur(n / 2)\n  (0...n).each { count += 1 }\n\n  count\nend\n\n# ## \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c (\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f) ###\ndef factorial_recur(n)\n  return 1 if n == 0\n\n  count = 0\n  # \u0418\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f n\n  (0...n).each { count += factorial_recur(n - 1) }\n\n  count\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 2-14 \u00a0 \u0424\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0438 \\(n \\geq 4\\) \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(n! > 2^n\\) , \u0444\u0430\u043a\u0442\u043e\u0440\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u0442\u0435\u0442 \u0435\u0449\u0435 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f, \u0438 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \\(n\\) \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u0430.

"},{"location":"chapter_computational_complexity/time_complexity/#235","title":"2.3.5 \u00a0 \u0425\u0443\u0434\u0448\u0430\u044f, \u043b\u0443\u0447\u0448\u0430\u044f \u0438 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c","text":"

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0447\u0430\u0441\u0442\u043e \u043d\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430, \u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u0434\u0430\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u0447\u0438\u0441\u0435\u043b \u043e\u0442 \\(1\\) \u0434\u043e \\(n\\) , \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437; \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u043d. \u0417\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(1\\) . \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b.

\"\u0425\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\" \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u043e\u0442\u0430\u0446\u0438\u0435\u0439 Big \\(O\\) . \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \"\u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\" \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043d\u0438\u0436\u043d\u0435\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \\(\\Omega\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby worst_best_time_complexity.py
def random_numbers(n: int) -> list[int]:\n    \"\"\"\u0421\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 1, 2, ..., n \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\"\"\"\n    # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums =: 1, 2, 3, ..., n\n    nums = [i for i in range(1, n + 1)]\n    # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    random.shuffle(nums)\n    return nums\n\ndef find_one(nums: list[int]) -> int:\n    \"\"\"\u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums\"\"\"\n    for i in range(len(nums)):\n        # \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        # \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if nums[i] == 1:\n            return i\n    return -1\n
worst_best_time_complexity.cpp
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nvector<int> randomNumbers(int n) {\n    vector<int> nums(n);\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e seed\n    unsigned seed = chrono::system_clock::now().time_since_epoch().count();\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    shuffle(nums.begin(), nums.end(), default_random_engine(seed));\n    return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nint findOne(vector<int> &nums) {\n    for (int i = 0; i < nums.size(); i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
worst_best_time_complexity.java
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nint[] randomNumbers(int n) {\n    Integer[] nums = new Integer[n];\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    Collections.shuffle(Arrays.asList(nums));\n    // Integer[] -> int[]\n    int[] res = new int[n];\n    for (int i = 0; i < n; i++) {\n        res[i] = nums[i];\n    }\n    return res;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nint findOne(int[] nums) {\n    for (int i = 0; i < nums.length; i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
worst_best_time_complexity.cs
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nint[] RandomNumbers(int n) {\n    int[] nums = new int[n];\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int i = 0; i < nums.Length; i++) {\n        int index = new Random().Next(i, nums.Length);\n        (nums[i], nums[index]) = (nums[index], nums[i]);\n    }\n    return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nint FindOne(int[] nums) {\n    for (int i = 0; i < nums.Length; i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
worst_best_time_complexity.go
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfunc randomNumbers(n int) []int {\n    nums := make([]int, n)\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for i := 0; i < n; i++ {\n        nums[i] = i + 1\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    rand.Shuffle(len(nums), func(i, j int) {\n        nums[i], nums[j] = nums[j], nums[i]\n    })\n    return nums\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfunc findOne(nums []int) int {\n    for i := 0; i < len(nums); i++ {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
worst_best_time_complexity.swift
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfunc randomNumbers(n: Int) -> [Int] {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    var nums = Array(1 ... n)\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums.shuffle()\n    return nums\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfunc findOne(nums: [Int]) -> Int {\n    for i in nums.indices {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if nums[i] == 1 {\n            return i\n        }\n    }\n    return -1\n}\n
worst_best_time_complexity.js
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfunction randomNumbers(n) {\n    const nums = Array(n);\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfunction findOne(nums) {\n    for (let i = 0; i < nums.length; i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
worst_best_time_complexity.ts
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfunction randomNumbers(n: number): number[] {\n    const nums = Array(n);\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (let i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (let i = 0; i < n; i++) {\n        const r = Math.floor(Math.random() * (i + 1));\n        const temp = nums[i];\n        nums[i] = nums[r];\n        nums[r] = temp;\n    }\n    return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfunction findOne(nums: number[]): number {\n    for (let i = 0; i < nums.length; i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] === 1) {\n            return i;\n        }\n    }\n    return -1;\n}\n
worst_best_time_complexity.dart
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nList<int> randomNumbers(int n) {\n  final nums = List.filled(n, 0);\n  // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n  for (var i = 0; i < n; i++) {\n    nums[i] = i + 1;\n  }\n  // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  nums.shuffle();\n\n  return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nint findOne(List<int> nums) {\n  for (var i = 0; i < nums.length; i++) {\n    // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n    // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    if (nums[i] == 1) return i;\n  }\n\n  return -1;\n}\n
worst_best_time_complexity.rs
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfn random_numbers(n: i32) -> Vec<i32> {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    let mut nums = (1..=n).collect::<Vec<i32>>();\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums.shuffle(&mut thread_rng());\n    nums\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfn find_one(nums: &[i32]) -> Option<usize> {\n    for i in 0..nums.len() {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if nums[i] == 1 {\n            return Some(i);\n        }\n    }\n    None\n}\n
worst_best_time_complexity.c
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nint *randomNumbers(int n) {\n    // \u0412\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0432 \u043a\u0443\u0447\u0435 (\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b: \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0432\u043d\u043e n, \u0442\u0438\u043f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u2014 int)\n    int *nums = (int *)malloc(n * sizeof(int));\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (int i = 0; i < n; i++) {\n        nums[i] = i + 1;\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    for (int i = n - 1; i > 0; i--) {\n        int j = rand() % (i + 1);\n        int temp = nums[i];\n        nums[i] = nums[j];\n        nums[j] = temp;\n    }\n    return nums;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nint findOne(int *nums, int n) {\n    for (int i = 0; i < n; i++) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] == 1)\n            return i;\n    }\n    return -1;\n}\n
worst_best_time_complexity.kt
/* \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 { 1, 2, ..., n } \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 */\nfun randomNumbers(n: Int): Array<Int?> {\n    val nums = IntArray(n)\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums = { 1, 2, 3, ..., n }\n    for (i in 0..<n) {\n        nums[i] = i + 1\n    }\n    // \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums.shuffle()\n    val res = arrayOfNulls<Int>(n)\n    for (i in 0..<n) {\n        res[i] = nums[i]\n    }\n    return res\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums */\nfun findOne(nums: Array<Int?>): Int {\n    for (i in nums.indices) {\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n        // \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n        if (nums[i] == 1)\n            return i\n    }\n    return -1\n}\n
worst_best_time_complexity.rb
=begin\nFile: worst_best_time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438: 1, 2, ..., n \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 ###\ndef random_numbers(n)\n  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  nums.shuffle!\nend\n\n=begin\nFile: worst_best_time_complexity.rb\nCreated Time: 2024-03-30\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438: 1, 2, ..., n \u0432 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 ###\ndef random_numbers(n)\n  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums =: 1, 2, 3, ..., n\n  nums = Array.new(n) { |i| i + 1 }\n  # \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  nums.shuffle!\nend\n\n# ## \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0447\u0438\u0441\u043b\u0430 1 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 nums ###\ndef find_one(nums)\n  for i in 0...nums.length\n    # \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(1)\n    # \u041a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 1 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    return i if nums[i] == 1\n  end\n\n  -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u044b \u0440\u0435\u0434\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043b\u0443\u0447\u0448\u0443\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043d\u0430 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u0441 \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u0432 \u0437\u0430\u0431\u043b\u0443\u0436\u0434\u0435\u043d\u0438\u0435. \u0425\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u043d\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0437\u0430\u0434\u0430\u0435\u0442 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0443\u044e \u043e\u0446\u0435\u043d\u043a\u0443 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c.

\u0418\u0437 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0445\u0443\u0434\u0448\u0430\u044f \u0438 \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \"\u043e\u0441\u043e\u0431\u044b\u0445 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f\u0445 \u0434\u0430\u043d\u043d\u044b\u0445\"; \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0438\u0437\u043a\u043e\u0439, \u0438 \u043e\u043d\u0438 \u043d\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u041d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \\(\\Theta\\) .

\u0414\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043f\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u043d, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(1\\) \u043d\u0430 \u043b\u044e\u0431\u043e\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0430; \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0441\u0440\u0435\u0434\u043d\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0435 \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \\(n / 2\\) , \u0430 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(\\Theta(n / 2) = \\Theta(n)\\) .

\u041d\u043e \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u044e\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0447\u0430\u0441\u0442\u043e \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0440\u0443\u0434\u043d\u043e \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u043d\u0430 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0445\u0443\u0434\u0448\u0443\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u043e\u0446\u0435\u043d\u043a\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

\u041f\u043e\u0447\u0435\u043c\u0443 \u0441\u0438\u043c\u0432\u043e\u043b \\(\\Theta\\) \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u0440\u0435\u0434\u043a\u043e?

\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b \\(O\\) \u0437\u0432\u0443\u0447\u0438\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u043e, \u0438 \u043c\u044b \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u0435\u0434\u043d\u0435\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u041d\u043e \u0441\u0442\u0440\u043e\u0433\u043e \u0433\u043e\u0432\u043e\u0440\u044f, \u044d\u0442\u043e \u043d\u0435\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e. \u0412 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u0438 \u0432 \u0434\u0440\u0443\u0433\u0438\u0445 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430\u0445, \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u0441\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432\u0440\u043e\u0434\u0435 \"\u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\)\", \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0439 \u0435\u0433\u043e \u043a\u0430\u043a \\(\\Theta(n)\\) .

"},{"location":"chapter_data_structure/","title":"\u0413\u043b\u0430\u0432\u0430 3. \u00a0 \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445","text":"

Abstract

\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u044b \u043f\u0440\u043e\u0447\u043d\u043e\u043c\u0443 \u0438 \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e\u043c\u0443 \u043a\u0430\u0440\u043a\u0430\u0441\u0443.

\u041e\u043d\u0438 \u0437\u0430\u0434\u0430\u044e\u0442 \u043f\u043b\u0430\u043d \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0439 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043d\u0430 \u044d\u0442\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u0431\u0440\u0435\u0442\u0430\u044e\u0442 \u0436\u0438\u0437\u043d\u044c.

"},{"location":"chapter_data_structure/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_data_structure/basic_data_types/","title":"3.2 \u00a0 \u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0433\u043e\u0432\u043e\u0440\u0438\u043c \u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435, \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u044f\u0442 \u043d\u0430 \u0443\u043c \u0442\u0435\u043a\u0441\u0442, \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0432\u0438\u0434\u0435\u043e, \u0437\u0432\u0443\u043a, 3D-\u043c\u043e\u0434\u0435\u043b\u0438 \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0444\u043e\u0440\u043c\u044b. \u0425\u043e\u0442\u044f \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u0432\u0441\u0435 \u043e\u043d\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.

\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 - \u044d\u0442\u043e \u0442\u0438\u043f\u044b, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 CPU \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e; \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u043e\u043d\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435.

\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0435. \u041e\u0434\u0438\u043d \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u0440\u0430\u0432\u0435\u043d \\(1\\) \u0431\u0438\u0442\u0443. \u0412 \u043f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c \\(1\\) \u0431\u0430\u0439\u0442 (byte) \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \\(8\\) \u0431\u0438\u0442\u043e\u0432 (bit).

\u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043e\u0431\u044a\u0435\u043c\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0433\u043e \u0438\u043c\u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430. \u041d\u0438\u0436\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Java.

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 3-1 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u044b \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 Java. \u0417\u0430\u0443\u0447\u0438\u0432\u0430\u0442\u044c \u044d\u0442\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043d\u0430\u0438\u0437\u0443\u0441\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e; \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 3-1 \u00a0 \u041e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445

\u0422\u0438\u043f \u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u041e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438 \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0426\u0435\u043b\u044b\u0435 byte 1 \u0431\u0430\u0439\u0442 \\(-2^7\\) (\\(-128\\)) \\(2^7 - 1\\) (\\(127\\)) \\(0\\) short 2 \u0431\u0430\u0439\u0442\u0430 \\(-2^{15}\\) \\(2^{15} - 1\\) \\(0\\) int 4 \u0431\u0430\u0439\u0442\u0430 \\(-2^{31}\\) \\(2^{31} - 1\\) \\(0\\) long 8 \u0431\u0430\u0439\u0442 \\(-2^{63}\\) \\(2^{63} - 1\\) \\(0\\) \u0412\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 float 4 \u0431\u0430\u0439\u0442\u0430 \\(1.175 \\times 10^{-38}\\) \\(3.403 \\times 10^{38}\\) \\(0.0\\text{f}\\) double 8 \u0431\u0430\u0439\u0442 \\(2.225 \\times 10^{-308}\\) \\(1.798 \\times 10^{308}\\) \\(0.0\\) \u0421\u0438\u043c\u0432\u043e\u043b\u044b char 2 \u0431\u0430\u0439\u0442\u0430 \\(0\\) \\(2^{16} - 1\\) \\(0\\) \u041b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 bool 1 \u0431\u0430\u0439\u0442 \\(\\text{false}\\) \\(\\text{true}\\) \\(\\text{false}\\)

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u0432\u044b\u0448\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043a \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u0442\u0438\u043f\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 Java. \u0412 \u043a\u0430\u0436\u0434\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0441\u0432\u043e\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f.

\u041a\u0430\u043a\u043e\u0432\u0430 \u0436\u0435 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445? \u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 - \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435. \u041f\u043e\u0434\u043b\u0435\u0436\u0430\u0449\u0435\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0444\u0440\u0430\u0437\u0435 - \"\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\", \u0430 \u043d\u0435 \"\u0434\u0430\u043d\u043d\u044b\u0435\".

\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \"\u0440\u044f\u0434 \u0447\u0438\u0441\u0435\u043b\", \u0442\u043e \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c \u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0435\u0434\u0441\u0442\u0432\u0430 \u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043c\u0435\u0436\u0434\u0443 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u0430 \u0432\u043e\u0442 \u0442\u043e, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 - \u0446\u0435\u043b\u044b\u0435 int , \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0435 float \u0438\u043b\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b char , - \u043a \"\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445\" \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442.

\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u044e\u0442 \"\u0442\u0438\u043f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e\" \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u044e\u0442 \"\u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438\" \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043a\u043e\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043c\u0430\u0441\u0441\u0438\u0432) \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u043a\u043b\u044e\u0447\u0430\u044f int , float , char , bool \u0438 \u0442.\u0434.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nnumbers: list[int] = [0] * 5\ndecimals: list[float] = [0.0] * 5\n# \u0412 Python \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043d\u0430 \u0434\u0435\u043b\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0434\u043b\u0438\u043d\u044b 1\ncharacters: list[str] = ['0'] * 5\nbools: list[bool] = [False] * 5\n# \u0421\u043f\u0438\u0441\u043a\u0438 Python \u043c\u043e\u0433\u0443\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u044b\ndata = [0, 0.0, 'a', False, ListNode(0)]\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nint numbers[5];\nfloat decimals[5];\nchar characters[5];\nbool bools[5];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nboolean[] bools = new boolean[5];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nint[] numbers = new int[5];\nfloat[] decimals = new float[5];\nchar[] characters = new char[5];\nbool[] bools = new bool[5];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nvar numbers = [5]int{}\nvar decimals = [5]float64{}\nvar characters = [5]byte{}\nvar bools = [5]bool{}\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nlet numbers = Array(repeating: 0, count: 5)\nlet decimals = Array(repeating: 0.0, count: 5)\nlet characters: [Character] = Array(repeating: \"a\", count: 5)\nlet bools = Array(repeating: false, count: 5)\n
// \u041c\u0430\u0441\u0441\u0438\u0432\u044b JavaScript \u043c\u043e\u0433\u0443\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u044b\nconst array = [0, 0.0, 'a', false];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nconst numbers: number[] = [];\nconst characters: string[] = [];\nconst bools: boolean[] = [];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nList<int> numbers = List.filled(5, 0);\nList<double> decimals = List.filled(5, 0.0);\nList<String> characters = List.filled(5, 'a');\nList<bool> bools = List.filled(5, false);\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nlet numbers: Vec<i32> = vec![0; 5];\nlet decimals: Vec<f32> = vec![0.0; 5];\nlet characters: Vec<char> = vec!['0'; 5];\nlet bools: Vec<bool> = vec![false; 5];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nint numbers[10];\nfloat decimals[10];\nchar characters[10];\nbool bools[10];\n
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\nval numbers = IntArray(5)\nval decinals = FloatArray(5)\nval characters = CharArray(5)\nval bools = BooleanArray(5)\n
# \u0421\u043f\u0438\u0441\u043a\u0438 Ruby \u043c\u043e\u0433\u0443\u0442 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u043e\u0431\u044a\u0435\u043a\u0442\u044b\ndata = [0, 0.0, 'a', false, ListNode(0)]\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%D1%81%D0%B2%D1%8F%D0%B7%D0%BD%D1%8B%D0%B9%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%D1%83%D0%B7%D0%B5%D0%BB%D0%BA%D0%BB%D0%B0%D1%81%D1%81%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%D0%97%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%B0%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D1%81%D0%BB%D0%B5%D0%B4%D1%83%D1%8E%D1%89%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BC%D0%B0%D1%81%D1%81%D0%B8%D0%B2%20%D1%81%20%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5%D0%BC%20%D0%BD%D0%B5%D1%81%D0%BA%D0%BE%D0%BB%D1%8C%D0%BA%D0%B8%D1%85%20%D0%B1%D0%B0%D0%B7%D0%BE%D0%B2%D1%8B%D1%85%20%D1%82%D0%B8%D0%BF%D0%BE%D0%B2%20%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85%0A%20%20%20%20numbers%20%3D%20%5B0%5D%20%2A%205%0A%20%20%20%20decimals%20%3D%20%5B0.0%5D%20%2A%205%0A%20%20%20%20%23%20%D0%92%20Python%20%D1%81%D0%B8%D0%BC%D0%B2%D0%BE%D0%BB%D1%8B%20%D0%BD%D0%B0%20%D1%81%D0%B0%D0%BC%D0%BE%D0%BC%20%D0%B4%D0%B5%D0%BB%D0%B5%20%D1%8F%D0%B2%D0%BB%D1%8F%D1%8E%D1%82%D1%81%D1%8F%20%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B0%D0%BC%D0%B8%20%D0%B4%D0%BB%D0%B8%D0%BD%D1%8B%201%0A%20%20%20%20characters%20%3D%20%5B%270%27%5D%20%2A%205%0A%20%20%20%20bools%20%3D%20%5BFalse%5D%20%2A%205%0A%20%20%20%20%23%20%D0%A1%D0%BF%D0%B8%D1%81%D0%BA%D0%B8%20%D0%B2%20Python%20%D0%BC%D0%BE%D0%B3%D1%83%D1%82%20%D1%81%D0%B2%D0%BE%D0%B1%D0%BE%D0%B4%D0%BD%D0%BE%20%D1%85%D1%80%D0%B0%D0%BD%D0%B8%D1%82%D1%8C%20%D1%80%D0%B0%D0%B7%D0%BB%D0%B8%D1%87%D0%BD%D1%8B%D0%B5%20%D0%B1%D0%B0%D0%B7%D0%BE%D0%B2%D1%8B%D0%B5%20%D1%82%D0%B8%D0%BF%D1%8B%20%D0%B4%D0%B0%D0%BD%D0%BD%D1%8B%D1%85%20%D0%B8%20%D1%81%D1%81%D1%8B%D0%BB%D0%BA%D0%B8%20%D0%BD%D0%B0%20%D0%BE%D0%B1%D1%8A%D0%B5%D0%BA%D1%82%D1%8B%0A%20%20%20%20data%20%3D%20%5B0%2C%200.0%2C%20%27a%27%2C%20False%2C%20ListNode%280%29%5D&cumulative=false&curInstr=12&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_data_structure/character_encoding/","title":"3.4 \u00a0 \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 *","text":"

\u0412 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0435, \u0438 \u0441\u0438\u043c\u0432\u043e\u043b char \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \"\u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432\", \u0437\u0430\u0434\u0430\u044e\u0449\u0438\u0439 \u0432\u0437\u0430\u0438\u043c\u043d\u043e-\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043a\u0430\u0436\u0434\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c. \u0418\u043c\u0435\u044f \u0442\u0430\u043a\u043e\u0439 \u043d\u0430\u0431\u043e\u0440, \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0435.

"},{"location":"chapter_data_structure/character_encoding/#341-ascii","title":"3.4.1 \u00a0 \u041d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 ASCII","text":"

\u041a\u043e\u0434 ASCII - \u044d\u0442\u043e \u0441\u0430\u043c\u044b\u0439 \u0440\u0430\u043d\u043d\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432; \u0435\u0433\u043e \u043f\u043e\u043b\u043d\u043e\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 - American Standard Code for Information Interchange (\u0430\u043c\u0435\u0440\u0438\u043a\u0430\u043d\u0441\u043a\u0438\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 \u043a\u043e\u0434 \u043e\u0431\u043c\u0435\u043d\u0430 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439). \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 7 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0431\u0438\u0442\u043e\u0432 (\u043d\u0438\u0436\u043d\u0438\u0435 7 \u0431\u0438\u0442\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430) \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 128 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-6, ASCII \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0437\u0430\u0433\u043b\u0430\u0432\u043d\u044b\u0435 \u0438 \u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0435 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0435 \u0431\u0443\u043a\u0432\u044b, \u0446\u0438\u0444\u0440\u044b 0 ~ 9, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u043d\u0430\u043a\u0438 \u043f\u0440\u0435\u043f\u0438\u043d\u0430\u043d\u0438\u044f \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u044e).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-6 \u00a0 \u0422\u0430\u0431\u043b\u0438\u0446\u0430 ASCII

\u041e\u0434\u043d\u0430\u043a\u043e \u043a\u043e\u0434 ASCII \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a. \u0421 \u0433\u043b\u043e\u0431\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u044b\u0445 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 EASCII, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0439 \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435 \u044f\u0437\u044b\u043a\u043e\u0432. \u041e\u043d \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 7-\u0431\u0438\u0442\u043d\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u0443 ASCII \u0434\u043e 8 \u0431\u0438\u0442\u043e\u0432 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c 256 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.

\u0412\u043e \u0432\u0441\u0435\u043c \u043c\u0438\u0440\u0435 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043d\u0430\u0431\u043e\u0440\u044b EASCII, \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0440\u0435\u0433\u0438\u043e\u043d\u043e\u0432. \u041f\u0435\u0440\u0432\u044b\u0435 128 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u044d\u0442\u0438\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 ASCII, \u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 128 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0442\u044c \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044f\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432.

"},{"location":"chapter_data_structure/character_encoding/#342-gbk","title":"3.4.2 \u00a0 \u041d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 GBK","text":"

\u041f\u043e\u0437\u0436\u0435 \u043b\u044e\u0434\u0438 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u043b\u0438, \u0447\u0442\u043e \u043a\u043e\u0434\u0430 EASCII \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0445 \u0438\u0435\u0440\u043e\u0433\u043b\u0438\u0444\u043e\u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u043e\u0447\u0442\u0438 \u0441\u0442\u043e \u0442\u044b\u0441\u044f\u0447, \u0430 \u0432 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0443\u0436\u043d\u044b \u0442\u044b\u0441\u044f\u0447\u0438. \u0412 1980 \u0433\u043e\u0434\u0443 \u0413\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432 \u041a\u0438\u0442\u0430\u044f \u0432\u044b\u043f\u0443\u0441\u0442\u0438\u043b\u043e \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 GB2312, \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0439 6763 \u0438\u0435\u0440\u043e\u0433\u043b\u0438\u0444\u0430, \u0447\u0442\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u043b\u043e \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430.

\u041e\u0434\u043d\u0430\u043a\u043e GB2312 \u043d\u0435 \u0443\u043c\u0435\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0440\u0435\u0434\u043a\u0438\u043c\u0438 \u0438\u0435\u0440\u043e\u0433\u043b\u0438\u0444\u0430\u043c\u0438 \u0438 \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u043c\u0438 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u043f\u0438\u0441\u044c\u043c\u0430. \u041d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 GBK - \u044d\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 GB2312, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0435 \u0432 \u043e\u0431\u0449\u0435\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 21886 \u0438\u0435\u0440\u043e\u0433\u043b\u0438\u0444\u043e\u0432. \u0412 \u0441\u0445\u0435\u043c\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f GBK \u0441\u0438\u043c\u0432\u043e\u043b\u044b ASCII \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0431\u0430\u0439\u0442\u043e\u043c, \u0430 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0435 \u0438\u0435\u0440\u043e\u0433\u043b\u0438\u0444\u044b - \u0434\u0432\u0443\u043c\u044f \u0431\u0430\u0439\u0442\u0430\u043c\u0438.

"},{"location":"chapter_data_structure/character_encoding/#343-unicode","title":"3.4.3 \u00a0 \u041d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 Unicode","text":"

\u0421 \u0431\u0443\u0440\u043d\u044b\u043c \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435\u043c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0439 \u0442\u0435\u0445\u043d\u0438\u043a\u0438 \u043d\u0430\u0431\u043e\u0440\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u044b \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0447\u0430\u043b\u0438 \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043d\u043e\u0436\u0438\u0442\u044c\u0441\u044f, \u0438 \u044d\u0442\u043e \u043f\u043e\u0440\u043e\u0434\u0438\u043b\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0442\u0430\u043a\u0438\u0435 \u043d\u0430\u0431\u043e\u0440\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u043b\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432 \u0438 \u043d\u0435 \u043c\u043e\u0433\u043b\u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0432 \u043c\u043d\u043e\u0433\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u0435. \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u044f\u0437\u044b\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f; \u0435\u0441\u043b\u0438 \u0434\u0432\u0435 \u043c\u0430\u0448\u0438\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u044b, \u043f\u0440\u0438 \u043e\u0431\u043c\u0435\u043d\u0435 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u043b\u0438 \u043a\u0440\u0430\u043a\u043e\u0437\u044f\u0431\u0440\u044b.

\u0418\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0442\u043e\u0439 \u044d\u043f\u043e\u0445\u0438 \u0437\u0430\u0434\u0443\u043c\u0430\u043b\u0438\u0441\u044c: \u0435\u0441\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u043e\u043b\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0438\u0442 \u0432\u0441\u0435 \u044f\u0437\u044b\u043a\u0438 \u0438 \u0437\u043d\u0430\u043a\u0438 \u043c\u0438\u0440\u0430, \u0440\u0430\u0437\u0432\u0435 \u044d\u0442\u043e \u043d\u0435 \u0440\u0435\u0448\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043c\u0435\u0436\u044a\u044f\u0437\u044b\u043a\u043e\u0432\u043e\u0439 \u0441\u0440\u0435\u0434\u044b \u0438 \u0438\u0441\u043a\u0430\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430? \u041f\u043e\u0434 \u0432\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0438\u0434\u0435\u0438 \u0438 \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0438 \u0432\u0441\u0435\u043e\u0431\u044a\u0435\u043c\u043b\u044e\u0449\u0438\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 Unicode.

Unicode \u043f\u043e-\u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u0435\u0434\u0438\u043d\u044b\u0439 \u043a\u043e\u0434\" \u0438 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d \u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u0415\u0433\u043e \u0446\u0435\u043b\u044c - \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0441\u043e \u0432\u0441\u0435\u0433\u043e \u043c\u0438\u0440\u0430 \u0432 \u0435\u0434\u0438\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0441\u0442\u043e\u0432 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u0441 \u0438\u0441\u043a\u0430\u0436\u0435\u043d\u0438\u0435\u043c \u0442\u0435\u043a\u0441\u0442\u0430, \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f\u043c\u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043e\u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0421 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0432 1991 \u0433\u043e\u0434\u0443 Unicode \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u043b\u0441\u044f, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u043d\u043e\u0432\u044b\u0435 \u044f\u0437\u044b\u043a\u0438 \u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b. \u041f\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043d\u0430 \u0441\u0435\u043d\u0442\u044f\u0431\u0440\u044c 2022 \u0433\u043e\u0434\u0430 Unicode \u0443\u0436\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u043b 149186 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0432 \u0442\u043e\u043c \u0447\u0438\u0441\u043b\u0435 \u0431\u0443\u043a\u0432\u044b \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432, \u0437\u043d\u0430\u043a\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u044d\u043c\u043e\u0434\u0437\u0438. \u0412 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u043c \u043d\u0430\u0431\u043e\u0440\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 Unicode \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 2 \u0431\u0430\u0439\u0442\u0430, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0434\u043a\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b - 3 \u0431\u0430\u0439\u0442\u0430 \u0438 \u0434\u0430\u0436\u0435 4 \u0431\u0430\u0439\u0442\u0430.

Unicode - \u044d\u0442\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e \u0441\u0443\u0442\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0443 \u043d\u043e\u043c\u0435\u0440 (\u0442\u0430\u043a \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0443\u044e \"\u043a\u043e\u0434\u043e\u0432\u0443\u044e \u0442\u043e\u0447\u043a\u0443\"), \u043d\u043e \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u044d\u0442\u0438 \u043a\u043e\u0434\u043e\u0432\u044b\u0435 \u0442\u043e\u0447\u043a\u0438 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435. \u0422\u0443\u0442 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u0435\u0441\u043b\u0438 \u0432 \u043e\u0434\u043d\u043e\u043c \u0442\u0435\u043a\u0441\u0442\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u043a\u043e\u0434\u043e\u0432\u044b\u0435 \u0442\u043e\u0447\u043a\u0438 Unicode \u0440\u0430\u0437\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u043a\u0430\u043a \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u044b? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0434\u0430\u043d \u043a\u043e\u0434 \u0434\u043b\u0438\u043d\u043e\u0439 2 \u0431\u0430\u0439\u0442\u0430, \u043a\u0430\u043a \u043f\u043e\u043d\u044f\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u044d\u0442\u043e \u043e\u0434\u043d\u0438\u043c 2-\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c \u0438\u043b\u0438 \u0434\u0432\u0443\u043c\u044f 1-\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u043c\u0438?

\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0439 \u0434\u043b\u0438\u043d\u044b. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-7, \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0432 \"Hello\" \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 1 \u0431\u0430\u0439\u0442, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0432 \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\" \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 2 \u0431\u0430\u0439\u0442\u0430. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \u0431\u0438\u0442\u044b \u043d\u0443\u043b\u044f\u043c\u0438 \u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0432 \"Hello \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\" \u0432 \u0432\u0438\u0434\u0435 2-\u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0445 \u0435\u0434\u0438\u043d\u0438\u0446. \u0422\u043e\u0433\u0434\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0441\u043c\u043e\u0436\u0435\u0442 \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0443 \u043a\u0430\u0436\u0434\u044b\u0435 2 \u0431\u0430\u0439\u0442\u0430 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u0444\u0440\u0430\u0437\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-7 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Unicode

\u041e\u0434\u043d\u0430\u043a\u043e ASCII \u0443\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043d\u0430\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e 1 \u0431\u0430\u0439\u0442\u0430. \u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u0443\u044e \u0432\u044b\u0448\u0435 \u0441\u0445\u0435\u043c\u0443, \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439 \u0442\u0435\u043a\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u0432\u0434\u0432\u043e\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0430\u043c\u044f\u0442\u0438, \u0447\u0435\u043c \u043f\u0440\u0438 ASCII, \u0430 \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Unicode.

"},{"location":"chapter_data_structure/character_encoding/#344-utf-8","title":"3.4.4 \u00a0 \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0430 UTF-8","text":"

\u0421\u0435\u0433\u043e\u0434\u043d\u044f UTF-8 \u0441\u0442\u0430\u043b\u0430 \u0441\u0430\u043c\u044b\u043c \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Unicode \u0432 \u043c\u0438\u0440\u0435. \u042d\u0442\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0430\u044f \u043e\u0442 1 \u0434\u043e 4 \u0431\u0430\u0439\u0442 \u043d\u0430 \u0441\u0438\u043c\u0432\u043e\u043b \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438. \u0421\u0438\u043c\u0432\u043e\u043b\u0430\u043c ASCII \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e 1 \u0431\u0430\u0439\u0442, \u043b\u0430\u0442\u0438\u043d\u0441\u043a\u0438\u043c \u0438 \u0433\u0440\u0435\u0447\u0435\u0441\u043a\u0438\u043c \u0431\u0443\u043a\u0432\u0430\u043c - 2 \u0431\u0430\u0439\u0442\u0430, \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u043c \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c - 3 \u0431\u0430\u0439\u0442\u0430, \u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0440\u0435\u0434\u043a\u0438\u043c \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c - 4 \u0431\u0430\u0439\u0442\u0430.

\u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f UTF-8 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0441\u043b\u043e\u0436\u043d\u044b \u0438 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u0441\u043b\u0443\u0447\u0430\u044f.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-8 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 UTF-8-\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043b\u044f \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\". \u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \\(n\\) \u0431\u0438\u0442\u043e\u0432 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u044b \u0432 \\(1\\) , \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043a\u0430\u043a \\(n\\) , \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0432 \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0434\u0443\u0449\u0438\u0445 \u0435\u0434\u0438\u043d\u0438\u0446.

\u041d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u0442\u0430\u0440\u0448\u0438\u0435 2 \u0431\u0438\u0442\u0430 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0431\u0430\u0439\u0442\u043e\u0432 \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \\(10\\) ? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \\(10\\) \u0438\u0433\u0440\u0430\u0435\u0442 \u0440\u043e\u043b\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0440\u043a\u0435\u0440\u0430. \u0415\u0441\u043b\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0430\u0447\u043d\u0435\u0442 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442 \u0441 \u043d\u0435\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0431\u0430\u0439\u0442\u0430, \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \\(10\\) \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442\u044c \u0430\u043d\u043e\u043c\u0430\u043b\u0438\u044e.

\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432\u044b\u0431\u043e\u0440\u0430 \\(10\\) \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0440\u043a\u0435\u0440\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c UTF-8 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \u0434\u0432\u0430 \u0431\u0438\u0442\u0430, \u0440\u0430\u0432\u043d\u044b\u0435 \\(10\\) . \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u0433\u043e: \u0435\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \u0434\u0432\u0430 \u0431\u0438\u0442\u0430 \u0440\u0430\u0432\u043d\u044b \\(10\\) , \u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c 1 \u0431\u0430\u0439\u0442, \u0442\u043e \u0435\u0441\u0442\u044c \u044d\u0442\u043e ASCII. \u041d\u043e \u0443 ASCII \u0441\u0442\u0430\u0440\u0448\u0438\u0439 \u0431\u0438\u0442 \u043e\u0431\u044f\u0437\u0430\u043d \u0431\u044b\u0442\u044c \\(0\\) , \u0447\u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0442 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-8 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 UTF-8

\u041f\u043e\u043c\u0438\u043c\u043e UTF-8, \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u044b \u0435\u0449\u0435 \u0434\u0432\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0435\u0441\u0442\u0430 UTF-8 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430 \u0434\u043b\u044f \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0438\u043c \u043d\u0443\u0436\u0435\u043d \u0432\u0441\u0435\u0433\u043e 1 \u0431\u0430\u0439\u0442; \u0430 \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0445) UTF-16 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0435\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 2 \u0431\u0430\u0439\u0442\u0430, \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a UTF-8 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c 3 \u0431\u0430\u0439\u0442\u0430.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0443 UTF-8 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0430\u044f \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b \u0438 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0438\u043c\u0435\u043d\u043d\u043e UTF-8.

"},{"location":"chapter_data_structure/character_encoding/#345","title":"3.4.5 \u00a0 \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0441\u0442\u0440\u043e\u043a\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u0434\u043b\u0438\u043d\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a UTF-16 \u0438\u043b\u0438 UTF-32. \u041f\u0440\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0440\u043e\u043a\u0443 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432, \u0438 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430.

\u0412\u043e\u043e\u0431\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0445\u0435\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f - \u043e\u0447\u0435\u043d\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0430\u044f \u0442\u0435\u043c\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0444\u0430\u043a\u0442\u043e\u0440\u043e\u0432.

\u0418\u0437-\u0437\u0430 \u043d\u0435\u0434\u043e\u043e\u0446\u0435\u043d\u043a\u0438 \u043e\u0431\u0449\u0435\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c \u0432\u044b\u0448\u0435 \u044f\u0437\u044b\u043a\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \"\u0441\u0443\u0440\u0440\u043e\u0433\u0430\u0442\u043d\u044b\u0435 \u043f\u0430\u0440\u044b\" \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f Unicode-\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0434\u043b\u0438\u043d\u043e\u0439 \u0431\u043e\u043b\u044c\u0448\u0435 16 \u0431\u0438\u0442. \u042d\u0442\u043e \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \u0441 \u0441\u0443\u0440\u0440\u043e\u0433\u0430\u0442\u043d\u044b\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0438 \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c 2 \u0431\u0430\u0439\u0442\u0430 \u0438\u043b\u0438 4 \u0431\u0430\u0439\u0442\u0430, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0442\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b. \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0441\u0443\u0440\u0440\u043e\u0433\u0430\u0442\u043d\u044b\u0445 \u043f\u0430\u0440 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0447\u0442\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0438 \u043e\u0442\u043b\u0430\u0434\u043a\u0438.

\u041f\u043e \u044d\u0442\u0438\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b\u0438 \u0438\u043d\u044b\u0435 \u0441\u0445\u0435\u043c\u044b \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0448\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u0441\u044f \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a \u0432\u043d\u0443\u0442\u0440\u0438 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u044d\u0442\u043e \u043d\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a \u0432 \u0444\u0430\u0439\u043b\u0430\u0445 \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0438\u0445 \u043f\u043e \u0441\u0435\u0442\u0438. \u041f\u0440\u0438 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u043c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0438 \u0438 \u0441\u0435\u0442\u0435\u0432\u043e\u0439 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0435 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 UTF-8, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0443\u044e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u043e\u043c\u0443 \u043c\u0435\u0441\u0442\u0443.

"},{"location":"chapter_data_structure/classification_of_data_structure/","title":"3.1 \u00a0 \u041a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u041a \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438, \u0441\u0442\u0435\u043a\u0438, \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0434\u0435\u0440\u0435\u0432\u044c\u044f, \u043a\u0443\u0447\u0438 \u0438 \u0433\u0440\u0430\u0444\u044b; \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f\u043c: \"\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\" \u0438 \"\u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\".

"},{"location":"chapter_data_structure/classification_of_data_structure/#311","title":"3.1.1 \u00a0 \u041b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430: \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0438 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f","text":"

\u041b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043e\u0442\u0440\u0430\u0436\u0430\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438; \u0432 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\u0445 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \"\u043f\u0440\u0435\u0434\u043a\u0430\u043c\u0438\" \u0438 \"\u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438\"; \u0433\u0440\u0430\u0444\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0440\u0435\u0431\u0435\u0440 \u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u0441\u0432\u044f\u0437\u0438.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-1, \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u0430: \"\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435\" \u0438 \"\u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435\". \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b \u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u0441\u0442\u0440\u043e\u0435\u043d\u044b \u0432 \u043b\u0438\u043d\u0438\u044e; \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e.

\u041d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0440\u0435\u0432\u043e\u0432\u0438\u0434\u043d\u044b\u0435 \u0438 \u0441\u0435\u0442\u0435\u0432\u044b\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-1 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0438 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445

"},{"location":"chapter_data_structure/classification_of_data_structure/#312","title":"3.1.2 \u00a0 \u0424\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430: \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u0430\u044f \u0438 \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u0430\u044f","text":"

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-2 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043f\u043b\u0430\u043d\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0435\u0440\u043d\u044b\u0439 \u0431\u043b\u043e\u043a \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u043a\u0430\u043a \u043e\u0433\u0440\u043e\u043c\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 Excel, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430.

\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u0431\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-2, \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u0435\u0442 \u043a\u0430\u0436\u0434\u043e\u0439 \u044f\u0447\u0435\u0439\u043a\u0435 \u0432 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u043e\u043c\u0435\u0440, \u0447\u0442\u043e\u0431\u044b \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0447\u0430\u0441\u0442\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u0431\u044b\u043b \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u0434\u0440\u0435\u0441. \u0418\u043c\u0435\u044f \u044d\u0442\u0438 \u0430\u0434\u0440\u0435\u0441\u0430, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u043d\u0430\u0445\u043e\u0434\u044f\u0449\u0438\u043c\u0441\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-2 \u00a0 \u041f\u043b\u0430\u043d\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438, \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0430\u0434\u0440\u0435\u0441 \u043f\u0430\u043c\u044f\u0442\u0438

Tip

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 Excel - \u044d\u0442\u043e \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044f; \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u043f\u0430\u043c\u044f\u0442\u0438 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0442\u0430\u043a\u0438\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u043a\u0430\u043a \u0430\u0434\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e, \u043a\u044d\u0448-\u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b, \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u0430\u044f \u0438 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c.

\u041f\u0430\u043c\u044f\u0442\u044c - \u043e\u0431\u0449\u0438\u0439 \u0440\u0435\u0441\u0443\u0440\u0441 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c. \u041a\u043e\u0433\u0434\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0447\u0430\u0441\u0442\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 \u0437\u0430\u043d\u044f\u0442 \u043e\u0434\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043e\u0439, \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u0430\u043c\u044f\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0430\u0436\u043d\u044b\u043c \u0444\u0430\u043a\u0442\u043e\u0440\u043e\u043c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0438\u043a\u043e\u0432\u043e\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u044b; \u0435\u0441\u043b\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0433\u043e \u043a\u0440\u0443\u043f\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u0430 \u0443\u043c\u0435\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445 \u043f\u0430\u043c\u044f\u0442\u0438.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-3, \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430; \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 (\u043c\u0430\u0441\u0441\u0438\u0432\u044b) \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435 (\u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438). \u0424\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0430 \u043d\u0438\u0436\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0438\u0445 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f, \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f; \u044d\u0442\u0438 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0432\u0437\u0430\u0438\u043c\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u044f\u044e\u0442 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-3 \u00a0 \u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u043c \u0438 \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438\u043b\u0438 \u0438\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0435\u043a\u0438 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432, \u0442\u0430\u043a \u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432; \u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438.

\u041f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u0438\u043d\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \"\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445\". \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \"\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445\". \u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0438\u043e\u0431\u0440\u0435\u0442\u0430\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \"\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u043d\u043e\u0441\u0442\u044c\".

Tip

\u0415\u0441\u043b\u0438 \u0442\u0435\u0431\u0435 \u043f\u043e\u043a\u0430 \u0442\u0440\u0443\u0434\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0433\u043b\u0430\u0432\u0443, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0434\u0435\u043b\u0443.

"},{"location":"chapter_data_structure/number_encoding/","title":"3.3 \u00a0 \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b *","text":"

Tip

\u0412 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u044b, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c *, \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0447\u0442\u0435\u043d\u0438\u044e. \u0415\u0441\u043b\u0438 \u0443 \u0442\u0435\u0431\u044f \u043c\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438\u043b\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0442\u0440\u0443\u0434\u043d\u044b\u043c, \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u043e\u0432.

"},{"location":"chapter_data_structure/number_encoding/#331","title":"3.3.1 \u00a0 \u041f\u0440\u044f\u043c\u043e\u0439, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\u044b","text":"

\u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043c\u044b \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438, \u0447\u0442\u043e \u0432\u0441\u0435 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u0430 \u043e\u0434\u043d\u043e \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d byte \u0440\u0430\u0432\u0435\u043d \\([-128, 127]\\) . \u042d\u0442\u043e \u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e, \u0438 \u0435\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u0430 \u0441 \u043f\u0440\u044f\u043c\u044b\u043c, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u0434\u0430\u043c\u0438.

\u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u0430 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435 \u0432 \u0444\u043e\u0440\u043c\u0435 \"\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430\". \u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0434\u0438\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u043c \u0442\u0440\u0435\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 3-4 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u044f\u043c\u044b\u043c, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u0434\u0430\u043c\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-4 \u00a0 \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u044f\u043c\u044b\u043c, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u0434\u0430\u043c\u0438

\u041f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 (sign-magnitude), \u0445\u043e\u0442\u044f \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u044b\u043c, \u0438\u043c\u0435\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0438 \\(1 + (-2)\\) \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043a\u043e\u0434\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \\(-3\\) , \u0447\u0442\u043e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043d\u0435\u0432\u0435\u0440\u043d\u043e.

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 + 1000 \\; 0010 \\newline & = 1000 \\; 0011 \\newline & \\rightarrow -3 \\end{aligned} \\]

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u044b \u0432\u0432\u0435\u043b\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434 (1's complement). \u0415\u0441\u043b\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \\(1 + (-2)\\) \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043a\u043e\u0434\u0435, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \\(-1\\) .

\\[ \\begin{aligned} & 1 + (-2) \\newline & \\rightarrow 0000 \\; 0001 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} + 1000 \\; 0010 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} \\newline & = 0000 \\; 0001 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} + 1111 \\; 1101 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline & = 1111 \\; 1110 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline & = 1000 \\; 0001 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} \\newline & \\rightarrow -1 \\end{aligned} \\]

\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, **\u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043a\u043e\u0434\u0435 \u0443 \u043d\u0443\u043b\u044f \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f: \\(+0\\) \u0438 \\(-0\\) **. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u0443 \u043d\u043e\u043b\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u043a\u043e\u0434\u0430, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432 \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0438 \u043d\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043d\u043e\u043b\u044c, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0410 \u0435\u0441\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u044c, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430.

\\[ \\begin{aligned} +0 & \\rightarrow 0000 \\; 0000 \\newline -0 & \\rightarrow 1000 \\; 0000 \\end{aligned} \\]

\u041a\u0430\u043a \u0438 \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u043e\u0436\u0435 \u0441\u0442\u0440\u0430\u0434\u0430\u0435\u0442 \u043e\u0442 \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u044b \u0432\u0432\u0435\u043b\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 (2's complement). \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f \u0438\u0437 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439:

\\[ \\begin{aligned} -0 \\rightarrow \\; & 1000 \\; 0000 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} \\newline = \\; & 1111 \\; 1111 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline = 1 \\; & 0000 \\; 0000 \\; \\text{(\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline \\end{aligned} \\]

\u041f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \\(1\\) \u043a \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u043e\u0441, \u043d\u043e \u0434\u043b\u0438\u043d\u0430 \u0442\u0438\u043f\u0430 byte \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0441\u0435\u0433\u043e 8 \u0431\u0438\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0438\u0432\u0448\u0430\u044f\u0441\u044f \u0432 9-\u0439 \u0431\u0438\u0442 \u0435\u0434\u0438\u043d\u0438\u0446\u0430 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f \u0440\u0430\u0432\u0435\u043d \\(0000 \\; 0000\\) \u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u043a\u043e\u0434\u043e\u043c \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f. \u0417\u043d\u0430\u0447\u0438\u0442, \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043d\u043e\u043b\u044c, \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0443\u0441\u0442\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f.

\u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0442\u0438\u043f\u0430 byte \u0440\u0430\u0432\u0435\u043d \\([-128, 127]\\) , \u043e\u0442\u043a\u0443\u0434\u0430 \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u043d\u0435\u0435 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(-128\\) ? \u041c\u044b \u0437\u0430\u043c\u0435\u0447\u0430\u0435\u043c, \u0447\u0442\u043e \u0443 \u0432\u0441\u0435\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438\u0437 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 \\([-127, +127]\\) \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u044f\u043c\u043e\u0439, \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\u044b, \u0430 \u043f\u0440\u044f\u043c\u043e\u0439 \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0434\u0440\u0443\u0433 \u0432 \u0434\u0440\u0443\u0433\u0430.

\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \\(1000 \\; 0000\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c: \u0443 \u043d\u0435\u0433\u043e \u043d\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d \\(0000 \\; 0000\\) . \u042d\u0442\u043e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \\(0\\) , \u0430 \u0435\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0441\u0430\u043c\u0438\u043c. \u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u043e\u0441\u043e\u0431\u044b\u0439 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \\(1000 \\; 0000\\) \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \\(-128\\) . \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \\((-1) + (-127)\\) \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0440\u0430\u0432\u0435\u043d \\(-128\\) .

\\[ \\begin{aligned} & (-127) + (-1) \\newline & \\rightarrow 1111 \\; 1111 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} + 1000 \\; 0001 \\; \\text{(\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434)} \\newline & = 1000 \\; 0000 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} + 1111 \\; 1110 \\; \\text{(\u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline & = 1000 \\; 0001 \\; \\text{(\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434)} + 1111 \\; 1111 \\; \\text{(\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline & = 1000 \\; 0000 \\; \\text{(\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434)} \\newline & \\rightarrow -128 \\end{aligned} \\]

\u0422\u044b, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u0443\u0436\u0435 \u0437\u0430\u043c\u0435\u0442\u0438\u043b, \u0447\u0442\u043e \u0432\u0441\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0431\u044b\u043b\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u042d\u0442\u043e \u043d\u0430\u043c\u0435\u043a\u0430\u0435\u0442 \u043d\u0430 \u0432\u0430\u0436\u043d\u044b\u0439 \u0444\u0430\u043a\u0442: \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u0445\u0435\u043c\u044b \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0430 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435\u043c) \u043f\u0440\u043e\u0449\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435, \u043b\u0435\u0433\u0447\u0435 \u0440\u0430\u0441\u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435.

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u0442\u043e \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u0443\u043c\u0435\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c. \u041a\u043e\u043c\u0431\u0438\u043d\u0438\u0440\u0443\u044f \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0431\u0430\u0437\u043e\u0432\u044b\u043c\u0438 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c\u0438, \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u0435 \\(a - b\\) \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \\(a + (-b)\\) ; \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044e \u0438\u043b\u0438 \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044e.

\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0432\u0435\u0441\u0442\u0438 \u0438\u0442\u043e\u0433, \u043f\u043e\u0447\u0435\u043c\u0443 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434: \u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0438 \u0438 \u0442\u0435 \u0436\u0435 \u0441\u0445\u0435\u043c\u044b \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u0431\u0435\u0437 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u0445\u0435\u043c\u044b \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044f, \u0438 \u0431\u0435\u0437 \u043e\u0441\u043e\u0431\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043d\u0435\u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0438 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043d\u0443\u043b\u044f. \u042d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0430\u043f\u043f\u0430\u0440\u0430\u0442\u043d\u0443\u044e \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0443 \u0438 \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439.

\u0418\u0434\u0435\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u043e\u0447\u0435\u043d\u044c \u0438\u0437\u044f\u0449\u043d\u0430; \u0438\u0437-\u0437\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u043e\u0431\u044a\u0435\u043c\u0443 \u043c\u044b \u043d\u0430 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c\u0441\u044f. \u0415\u0441\u043b\u0438 \u0442\u0435\u0431\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e, \u0441\u0442\u043e\u0438\u0442 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u044d\u0442\u0443 \u0442\u0435\u043c\u0443 \u0433\u043b\u0443\u0431\u0436\u0435.

"},{"location":"chapter_data_structure/number_encoding/#332","title":"3.3.2 \u00a0 \u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439","text":"

\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c: int \u0438 float \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0443\u044e \u0434\u043b\u0438\u043d\u0443, \u043f\u043e 4 \u0431\u0430\u0439\u0442\u0430 , \u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0443 float \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 int ? \u042d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u0430\u0440\u0430\u0434\u043e\u043a\u0441\u0430\u043b\u044c\u043d\u043e, \u0432\u0435\u0434\u044c float \u0434\u043e\u043b\u0436\u0435\u043d \u0435\u0449\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0434\u0440\u043e\u0431\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0432\u0440\u043e\u0434\u0435 \u0431\u044b \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0435.

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 float \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f. \u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0434\u043b\u0438\u043d\u043e\u0439 32 \u0431\u0438\u0442\u0430 \u043a\u0430\u043a:

\\[ b_{31} b_{30} b_{29} \\ldots b_2 b_1 b_0 \\]

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443 IEEE 754, 32-\u0431\u0438\u0442\u043d\u044b\u0439 float \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0442\u0440\u0435\u0445 \u0447\u0430\u0441\u0442\u0435\u0439.

\u0424\u043e\u0440\u043c\u0443\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c\u0443 \u0447\u0438\u0441\u043b\u0443 float, \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

\\[ \\text {val} = (-1)^{b_{31}} \\times 2^{\\left(b_{30} b_{29} \\ldots b_{23}\\right)_2-127} \\times\\left(1 . b_{22} b_{21} \\ldots b_0\\right)_2 \\]

\u0415\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438, \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0442\u0430\u043a\u043e\u0439:

\\[ \\text {val}=(-1)^{\\mathrm{S}} \\times 2^{\\mathrm{E} -127} \\times (1 + \\mathrm{N}) \\]

\u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u0447\u0430\u0441\u0442\u0435\u0439 \u0442\u0430\u043a\u043e\u0432\u044b:

\\[ \\begin{aligned} \\mathrm{S} \\in & \\{ 0, 1\\}, \\quad \\mathrm{E} \\in \\{ 1, 2, \\dots, 254 \\} \\newline (1 + \\mathrm{N}) = & (1 + \\sum_{i=1}^{23} b_{23-i} 2^{-i}) \\subset [1, 2 - 2^{-23}] \\end{aligned} \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 3-5 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f float \u043f\u043e \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0443 IEEE 754

\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 3-5: \u0435\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \\(\\mathrm{S} = 0\\) , \\(\\mathrm{E} = 124\\) , \\(\\mathrm{N} = 2^{-2} + 2^{-3} = 0.375\\) , \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c:

\\[ \\text { val } = (-1)^0 \\times 2^{124 - 127} \\times (1 + 0.375) = 0.171875 \\]

\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 float \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0431\u0438\u0442\u044b \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 int. \u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u043c \u0432\u044b\u0448\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f\u043c, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0434\u043b\u044f float \u0440\u0430\u0432\u043d\u043e \\(2^{254 - 127} \\times (2 - 2^{-23}) \\approx 3.4 \\times 10^{38}\\) ; \u0435\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0431\u0438\u0442 \u0437\u043d\u0430\u043a\u0430, \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e.

\u0425\u043e\u0442\u044f \u0447\u0438\u0441\u043b\u043e \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439 float \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u043e\u0442\u0435\u0440\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438. \u0426\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 \u0442\u0438\u043f int \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u0441\u0435 32 \u0431\u0438\u0442\u0430 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430, \u0438 \u0447\u0438\u0441\u043b\u0430 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e; \u0430 \u0438\u0437-\u0437\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0438\u0442\u043e\u0432 \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0443 float \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0438\u0441\u043b\u043e, \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u043c\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 3-2, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0442\u044b \\(\\mathrm{E} = 0\\) \u0438 \\(\\mathrm{E} = 255\\) \u0438\u043c\u0435\u044e\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0441\u043c\u044b\u0441\u043b \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043d\u0443\u043b\u044f, \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u0438, \\(\\mathrm{NaN}\\) \u0438 \u0442.\u0434.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 3-2 \u00a0 \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0442\u044b

\u041f\u043e\u043b\u0435 \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0442\u044b E \u041f\u043e\u043b\u0435 \u043c\u0430\u043d\u0442\u0438\u0441\u0441\u044b \\(\\mathrm{N} = 0\\) \u041f\u043e\u043b\u0435 \u043c\u0430\u043d\u0442\u0438\u0441\u0441\u044b \\(\\mathrm{N} \\ne 0\\) \u0424\u043e\u0440\u043c\u0443\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \\(0\\) \\(\\pm 0\\) \u0414\u0435\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\((-1)^{\\mathrm{S}} \\times 2^{-126} \\times (0.\\mathrm{N})\\) \\(1, 2, \\dots, 254\\) \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\((-1)^{\\mathrm{S}} \\times 2^{(\\mathrm{E} -127)} \\times (1.\\mathrm{N})\\) \\(255\\) \\(\\pm \\infty\\) \\(\\mathrm{NaN}\\)

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0434\u0435\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u044e\u0442 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0447\u0438\u0441\u0435\u043b \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0442\u043e\u0447\u043a\u043e\u0439. \u041d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0432\u043d\u043e \\(2^{-126}\\) , \u0430 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0434\u0435\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0432\u043d\u043e \\(2^{-126} \\times 2^{-23}\\) .

\u0414\u0432\u043e\u0439\u043d\u0430\u044f \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c double \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 float , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u043d\u0435\u043c.

"},{"location":"chapter_data_structure/summary/","title":"3.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_data_structure/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_data_structure/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0438 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445?

\u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043b\u0435\u0436\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0430 \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \"\u0446\u0435\u043f\u043e\u0447\u043a\u0438 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438\" (\u043e\u0431 \u044d\u0442\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \"\u0425\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438\"): \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0430\u043a\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0430 \u0435\u0441\u043b\u0438 \u0434\u043b\u0438\u043d\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0440\u043e\u0433, \u043e\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d \u0432 \u0434\u0435\u0440\u0435\u0432\u043e (\u043e\u0431\u044b\u0447\u043d\u043e \u0432 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e).

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043e\u0441\u043d\u043e\u0432\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u043e\u0442 \u0431\u0430\u043a\u0435\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043b\u0438\u0431\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043b\u0438\u0431\u043e \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u043b\u0438\u0431\u043e \u0434\u0435\u0440\u0435\u0432\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (\u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u0441\u043f\u0438\u0441\u043a\u0438) \u0438 \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 (\u0434\u0435\u0440\u0435\u0432\u044c\u044f).

Q: \u0414\u043b\u0438\u043d\u0430 \u0442\u0438\u043f\u0430 char \u0440\u0430\u0432\u043d\u0430 1 \u0431\u0430\u0439\u0442\u0443?

\u0414\u043b\u0438\u043d\u0430 \u0442\u0438\u043f\u0430 char \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u043c \u0432 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, Java, JavaScript, TypeScript \u0438 C# \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0443 UTF-16 (\u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u043e\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a Unicode), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u0438\u043d\u0430 char \u0443 \u043d\u0438\u0445 \u0440\u0430\u0432\u043d\u0430 2 \u0431\u0430\u0439\u0442\u0430\u043c.

Q: \u041d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0434\u0432\u0443\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \"\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445\"? \u0412\u0435\u0434\u044c \u0441\u0442\u0435\u043a \u0442\u043e\u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0438 pop, \u0430 \u043e\u043d\u0438 \u044f\u0432\u043d\u043e \"\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435\".

\u0421\u0442\u0435\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u0434\u0430\u043d\u043d\u044b\u043c\u0438, \u043d\u043e \u0441\u0430\u043c\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \"\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439\" (\u0435\u0435 \u0434\u043b\u0438\u043d\u0430 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u0430). \u0425\u043e\u0442\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0438\u0445 \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0430. \u0415\u0441\u043b\u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439, \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u0442\u0430\u0440\u043e\u0433\u043e.

Q: \u041f\u0440\u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0438 \u0441\u0442\u0435\u043a\u0430 (\u043e\u0447\u0435\u0440\u0435\u0434\u0438) \u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440 \u043d\u0435 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u044f\u0432\u043d\u043e, \u043f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u0435\u0433\u043e \u043e\u0442\u043d\u043e\u0441\u044f\u0442 \u043a \"\u0441\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445\"?

\u0412 \u044f\u0437\u044b\u043a\u0430\u0445 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u0440\u0443\u0447\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0441\u0442\u0435\u043a\u0430 (\u043e\u0447\u0435\u0440\u0435\u0434\u0438): \u044d\u0442\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u0430\u043c\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0435\u043c\u043a\u043e\u0441\u0442\u044c ArrayList \u0432 Java \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u0430 10. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u0438. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043e \"\u0441\u043f\u0438\u0441\u043a\u0430\u0445\".

Q: \u0415\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0437 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 - \u044d\u0442\u043e \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\", \u0442\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \u0432 \u043f\u0440\u044f\u043c\u043e\u0439, \u043f\u043e \u0438\u0434\u0435\u0435, \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0435\u0441\u0442\u044c 1, \u0437\u0430\u0442\u0435\u043c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\". \u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0432\u0435\u0441\u0442\u0438 \u0432 \u043f\u0440\u044f\u043c\u043e\u0439 \u0442\u0435\u043c \u0436\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\"?

\u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0432\u0437\u0430\u0438\u043c\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u043e\u0432 \u043f\u043e \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435\u043c \"\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\". \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0434\u0438\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f: \u0435\u0441\u043b\u0438 \\(a + b = c\\) , \u0442\u043e \u0433\u043e\u0432\u043e\u0440\u044f\u0442, \u0447\u0442\u043e \\(a\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0447\u0438\u0441\u043b\u0430 \\(b\\) \u0434\u043e \\(c\\) ; \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e, \\(b\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0447\u0438\u0441\u043b\u0430 \\(a\\) \u0434\u043e \\(c\\) .

\u0414\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0434\u043b\u0438\u043d\u044b \\(n = 4\\) \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(0010\\) , \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 (\u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u0437\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0431\u0438\u0442), \u0442\u043e \u0435\u0433\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\":

\\[ 0010 \\rightarrow 1101 \\rightarrow 1110 \\]

\u041c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u043e\u0432 \u0440\u0430\u0432\u043d\u0430 \\(0010 + 1110 = 10000\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \\(1110\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \"\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c\" \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \\(0010\\) \u0434\u043e \\(10000\\) . **\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u0430\u044f \u0432\u044b\u0448\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\" \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e \\(10000\\) **.

\u0422\u043e\u0433\u0434\u0430 \u0447\u0435\u043c\u0443 \u0440\u0430\u0432\u043d\u043e \"\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\" \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \\(1110\\) \u0434\u043e \\(10000\\) ? \u041c\u044b \u0441\u043d\u043e\u0432\u0430 \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\":

\\[ 1110 \\rightarrow 0001 \\rightarrow 0010 \\]

\u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043f\u0440\u044f\u043c\u043e\u0439 \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0437\u0430\u0438\u043c\u043d\u044b\u043c\u0438 \"\u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f\u043c\u0438\" \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u0434\u043e \\(10000\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438 \"\u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 -> \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434\", \u0438 \"\u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 -> \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434\" \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 (\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1).

\u0420\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 \u0438\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430 \\(1110\\) \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0435\u0441\u0442\u044c 1, \u0437\u0430\u0442\u0435\u043c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\":

\\[ 1110 \\rightarrow 1101 \\rightarrow 0010 \\]

\u0412 \u0438\u0442\u043e\u0433\u0435 \u0438 \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c 1\", \u0438 \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0435\u0441\u0442\u044c 1, \u0437\u0430\u0442\u0435\u043c \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\" - \u044d\u0442\u043e \u0434\u0432\u0430 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043e \\(10000\\) .

\u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \"\u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\" \u0441\u0430\u043c\u0430 \u043f\u043e \u0441\u0435\u0431\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e \\(1111\\) (\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e\u0439 \u043a\u043e\u0434 + \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043a\u043e\u0434 = 1111 ); \u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434, \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f 1 \u043a \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u043a\u043e\u0434\u0443, \u0438 \u0435\u0441\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u043e \\(10000\\) .

\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \\(n = 4\\) , \u043d\u043e \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u043e\u0431\u0449\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u043b\u044e\u0431\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.

"},{"location":"chapter_divide_and_conquer/","title":"\u0413\u043b\u0430\u0432\u0430 12. \u00a0 \u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439","text":"

Abstract

\u0421\u043b\u043e\u0436\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0439 \u0437\u0430 \u0441\u043b\u043e\u0435\u043c, \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u043d\u043e\u0432\u043e\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0435 \u043f\u0440\u043e\u0449\u0435.

\u041f\u0440\u0438\u043d\u0446\u0438\u043f \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\" \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0432\u0430\u0436\u043d\u044b\u0439 \u0444\u0430\u043a\u0442: \u0435\u0441\u043b\u0438 \u043d\u0430\u0447\u0430\u0442\u044c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e, \u043c\u043d\u043e\u0433\u043e\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0431\u044b\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u044b\u043c.

"},{"location":"chapter_divide_and_conquer/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_divide_and_conquer/binary_search_recur/","title":"12.2 \u00a0 \u041f\u043e\u0438\u0441\u043a\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer","text":"

\u041c\u044b \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \\(O(\\log n)\\) \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 divide and conquer, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u044f.

\u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.

\u041f\u043e \u0441\u0443\u0442\u0438 divide and conquer \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u043e\u043b\u043d\u043e\u043c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0435 \u0437\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0442\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u043f\u0440\u0438 \u043f\u043e\u0438\u0441\u043a\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 divide and conquer \u0437\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432.

"},{"location":"chapter_divide_and_conquer/binary_search_recur/#1-divide-and-conquer","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 divide and conquer","text":"

\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0433\u043b\u0430\u0432\u0430\u0445 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u043b\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e. \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0435\u0433\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e divide and conquer, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e.

Question

\u0414\u0430\u043d \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 target .

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f divide and conquer \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0443 \u043f\u043e\u0438\u0441\u043a\u0430 \\([i, j]\\) , \u0447\u0435\u0440\u0435\u0437 \\(f(i, j)\\) .

\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(0, n-1)\\) , \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u0430\u043c.

  1. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 \\(m\\) \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \\([i, j]\\) \u0438 \u0441 \u0435\u0435 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430.
  2. \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \u0432\u0434\u0432\u043e\u0435 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430; \u044d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u0438\u0431\u043e \\(f(i, m-1)\\) , \u043b\u0438\u0431\u043e \\(f(m+1, j)\\) .
  3. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433 1. \u0438 \u0448\u0430\u0433 2. , \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0434\u0435\u043d target \u0438\u043b\u0438 \u043f\u043e\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u0443\u0441\u0442\u044b\u043c.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-4 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f divide and conquer \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(6\\) \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-4 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0441\u0442\u0438\u043b\u0435 divide and conquer

\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e dfs() \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(i, j)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_recur.py
def dfs(nums: list[int], target: int, i: int, j: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j)\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i > j:\n        return -1\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    m = (i + j) // 2\n    if nums[m] < target:\n        # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j)\n    elif nums[m] > target:\n        # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1)\n    else:\n        # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m\n\ndef binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\"\"\"\n    n = len(nums)\n    # \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n
binary_search_recur.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nint dfs(vector<int> &nums, int target, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nint binarySearch(vector<int> &nums, int target) {\n    int n = nums.size();\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nint dfs(int[] nums, int target, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nint binarySearch(int[] nums, int target) {\n    int n = nums.length;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nint DFS(int[] nums, int target, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return DFS(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return DFS(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nint BinarySearch(int[] nums, int target) {\n    int n = nums.Length;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return DFS(nums, target, 0, n - 1);\n}\n
binary_search_recur.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfunc dfs(nums []int, target, i, j int) int {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i > j {\n        return -1\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\n    m := i + ((j - i) >> 1)\n    // \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n    if nums[m] < target {\n        // \u0415\u0441\u043b\u0438 \u043c\u0435\u043d\u044c\u0448\u0435, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u0443\u044e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m+1, j)\n    } else if nums[m] > target {\n        // \u0415\u0441\u043b\u0438 \u0431\u043e\u043b\u044c\u0448\u0435, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u0443\u044e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m-1)\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfunc binarySearch(nums []int, target int) int {\n    n := len(nums)\n    return dfs(nums, target, 0, n-1)\n}\n
binary_search_recur.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfunc dfs(nums: [Int], target: Int, i: Int, j: Int) -> Int {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i > j {\n        return -1\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    let m = (i + j) / 2\n    if nums[m] < target {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums: nums, target: target, i: m + 1, j: j)\n    } else if nums[m] > target {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums: nums, target: target, i: i, j: m - 1)\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    dfs(nums: nums, target: target, i: nums.startIndex, j: nums.endIndex - 1)\n}\n
binary_search_recur.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfunction dfs(nums, target, i, j) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfunction binarySearch(nums, target) {\n    const n = nums.length;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfunction dfs(nums: number[], target: number, i: number, j: number): number {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    const m = i + ((j - i) >> 1);\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfunction binarySearch(nums: number[], target: number): number {\n    const n = nums.length;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nint dfs(List<int> nums, int target, int i, int j) {\n  // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  if (i > j) {\n    return -1;\n  }\n  // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n  int m = (i + j) ~/ 2;\n  if (nums[m] < target) {\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n    return dfs(nums, target, m + 1, j);\n  } else if (nums[m] > target) {\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n    return dfs(nums, target, i, m - 1);\n  } else {\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    return m;\n  }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nint binarySearch(List<int> nums, int target) {\n  int n = nums.length;\n  // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n  return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfn dfs(nums: &[i32], target: i32, i: i32, j: i32) -> i32 {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i > j {\n        return -1;\n    }\n    let m: i32 = i + (j - i) / 2;\n    if nums[m as usize] < target {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if nums[m as usize] > target {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    let n = nums.len() as i32;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    dfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nint dfs(int nums[], int target, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    int m = (i + j) / 2;\n    if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        return dfs(nums, target, m + 1, j);\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        return dfs(nums, target, i, m - 1);\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        return m;\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nint binarySearch(int nums[], int target, int numsSize) {\n    int n = numsSize;\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1);\n}\n
binary_search_recur.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) */\nfun dfs(\n    nums: IntArray,\n    target: Int,\n    i: Int,\n    j: Int\n): Int {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i > j) {\n        return -1\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    val m = (i + j) / 2\n    return if (nums[m] < target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n        dfs(nums, target, m + 1, j)\n    } else if (nums[m] > target) {\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n        dfs(nums, target, i, m - 1)\n    } else {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        m\n    }\n}\n\n/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    val n = nums.size\n    // \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n    return dfs(nums, target, 0, n - 1)\n}\n
binary_search_recur.rb
=begin\nFile: binary_search_recur.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) ###\ndef dfs(nums, target, i, j)\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1 if i > j\n\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    return m\n  end\nend\n\n=begin\nFile: binary_search_recur.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u0437\u0430\u0434\u0430\u0447\u0430 f(i, j) ###\ndef dfs(nums, target, i, j)\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u0443\u0441\u0442, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1 if i > j\n\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n  m = (i + j) / 2\n\n  if nums[m] < target\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(m+1, j)\n    return dfs(nums, target, m + 1, j)\n  elsif nums[m] > target\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i, m-1)\n    return dfs(nums, target, i, m - 1)\n  else\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    return m\n  end\nend\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a ###\ndef binary_search(nums, target)\n  n = nums.length\n  # \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 f(0, n-1)\n  dfs(nums, target, 0, n - 1)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/","title":"12.3 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

Question

\u0414\u0430\u043d\u044b \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 preorder \u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 inorder \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u041f\u043e\u0441\u0442\u0440\u043e\u0439\u0442\u0435 \u043f\u043e \u043d\u0438\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u0435\u0433\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u043d\u0435\u0442 \u0443\u0437\u043b\u043e\u0432 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 (\u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-5).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-5 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#1-divide-and-conquer","title":"1. \u00a0 \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0435\u0439 divide and conquer","text":"

\u0418\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 - \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e preorder \u0438 inorder - \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 divide and conquer.

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#2","title":"2. \u00a0 \u041a\u0430\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f","text":"

\u0418\u0437 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0432\u044b\u0448\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 divide and conquer, \u043d\u043e \u043a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e, \u0438\u043c\u0435\u044f \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 preorder \u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 inorder, \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f?

\u041f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0438 preorder , \u0438 inorder \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0442\u0438.

\u041d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 \u0440\u0438\u0441\u0443\u043d\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u0430\u043c.

  1. \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430, \u0440\u0430\u0432\u043d\u044b\u0439 3, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430.
  2. \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 3 \u0432 inorder ; \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c inorder \u043d\u0430 [ 9 | 3 | 1 2 7 ] .
  3. \u041f\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0443 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f inorder \u043d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u0445 \u0440\u0430\u0432\u043d\u043e 1 \u0438 3 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, preorder \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043a\u0430\u043a [ 3 | 9 | 2 1 7 ] .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-6 \u00a0 \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0430\u0445

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#3","title":"3. \u00a0 \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u0432 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0447\u0435\u0440\u0435\u0437 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435","text":"

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c\u0443 \u0432\u044b\u0448\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0443 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f, \u043c\u044b \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u044b \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0432 preorder \u0438 inorder. \u0427\u0442\u043e\u0431\u044b \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u044b, \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439-\u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 12-1, \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 preorder \u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u0432 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0432 inorder .

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 12-1 \u00a0 \u0418\u043d\u0434\u0435\u043a\u0441\u044b \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0430\u0445

\u0418\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432 preorder \u0418\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u0432 inorder \u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \\(i\\) \\([l, r]\\) \u041b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \\(i + 1\\) \\([l, m-1]\\) \u041f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \\(i + 1 + (m - l)\\) \\([m+1, r]\\)

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \\((m-l)\\) \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \"\u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435\"; \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u044d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0438\u0441\u0443\u043d\u043a\u043e\u043c \u043d\u0438\u0436\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-7 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u043d\u044b\u0445 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u0432 \u043a\u043e\u0440\u043d\u044f \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432

"},{"location":"chapter_divide_and_conquer/build_binary_tree_problem/#4","title":"4. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a \\(m\\) , \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 hmap \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 inorder \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u044b:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby build_tree.py
def dfs(\n    preorder: list[int],\n    inorder_map: dict[int, int],\n    i: int,\n    l: int,\n    r: int,\n) -> TreeNode | None:\n    \"\"\"\u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\"\"\"\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if r - l < 0:\n        return None\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    root = TreeNode(preorder[i])\n    # \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    m = inorder_map[preorder[i]]\n    # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n    # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root\n\ndef build_tree(preorder: list[int], inorder: list[int]) -> TreeNode | None:\n    \"\"\"\u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    inorder_map = {val: i for i, val in enumerate(inorder)}\n    root = dfs(preorder, inorder_map, 0, 0, len(inorder) - 1)\n    return root\n
build_tree.cpp
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nTreeNode *dfs(vector<int> &preorder, unordered_map<int, int> &inorderMap, int i, int l, int r) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0)\n        return NULL;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    TreeNode *root = new TreeNode(preorder[i]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    int m = inorderMap[preorder[i]];\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    unordered_map<int, int> inorderMap;\n    for (int i = 0; i < inorder.size(); i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorder.size() - 1);\n    return root;\n}\n
build_tree.java
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nTreeNode dfs(int[] preorder, Map<Integer, Integer> inorderMap, int i, int l, int r) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0)\n        return null;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    TreeNode root = new TreeNode(preorder[i]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    int m = inorderMap.get(preorder[i]);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode buildTree(int[] preorder, int[] inorder) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    Map<Integer, Integer> inorderMap = new HashMap<>();\n    for (int i = 0; i < inorder.length; i++) {\n        inorderMap.put(inorder[i], i);\n    }\n    TreeNode root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
build_tree.cs
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nTreeNode? DFS(int[] preorder, Dictionary<int, int> inorderMap, int i, int l, int r) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0)\n        return null;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    TreeNode root = new(preorder[i]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    int m = inorderMap[preorder[i]];\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = DFS(preorder, inorderMap, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = DFS(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode? BuildTree(int[] preorder, int[] inorder) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    Dictionary<int, int> inorderMap = [];\n    for (int i = 0; i < inorder.Length; i++) {\n        inorderMap.TryAdd(inorder[i], i);\n    }\n    TreeNode? root = DFS(preorder, inorderMap, 0, 0, inorder.Length - 1);\n    return root;\n}\n
build_tree.go
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfunc dfsBuildTree(preorder []int, inorderMap map[int]int, i, l, r int) *TreeNode {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if r-l < 0 {\n        return nil\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    root := NewTreeNode(preorder[i])\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    m := inorderMap[preorder[i]]\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.Left = dfsBuildTree(preorder, inorderMap, i+1, l, m-1)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.Right = dfsBuildTree(preorder, inorderMap, i+1+m-l, m+1, r)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfunc buildTree(preorder, inorder []int) *TreeNode {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    inorderMap := make(map[int]int, len(inorder))\n    for i := 0; i < len(inorder); i++ {\n        inorderMap[inorder[i]] = i\n    }\n\n    root := dfsBuildTree(preorder, inorderMap, 0, 0, len(inorder)-1)\n    return root\n}\n
build_tree.swift
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfunc dfs(preorder: [Int], inorderMap: [Int: Int], i: Int, l: Int, r: Int) -> TreeNode? {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if r - l < 0 {\n        return nil\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    let root = TreeNode(x: preorder[i])\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    let m = inorderMap[preorder[i]]!\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1, l: l, r: m - 1)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder: preorder, inorderMap: inorderMap, i: i + 1 + m - l, l: m + 1, r: r)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfunc buildTree(preorder: [Int], inorder: [Int]) -> TreeNode? {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    let inorderMap = inorder.enumerated().reduce(into: [:]) { $0[$1.element] = $1.offset }\n    return dfs(preorder: preorder, inorderMap: inorderMap, i: inorder.startIndex, l: inorder.startIndex, r: inorder.endIndex - 1)\n}\n
build_tree.js
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfunction dfs(preorder, inorderMap, i, l, r) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0) return null;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    const root = new TreeNode(preorder[i]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    const m = inorderMap.get(preorder[i]);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfunction buildTree(preorder, inorder) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    let inorderMap = new Map();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
build_tree.ts
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfunction dfs(\n    preorder: number[],\n    inorderMap: Map<number, number>,\n    i: number,\n    l: number,\n    r: number\n): TreeNode | null {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0) return null;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    const root: TreeNode = new TreeNode(preorder[i]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    const m = inorderMap.get(preorder[i]);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfunction buildTree(preorder: number[], inorder: number[]): TreeNode | null {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    let inorderMap = new Map<number, number>();\n    for (let i = 0; i < inorder.length; i++) {\n        inorderMap.set(inorder[i], i);\n    }\n    const root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n    return root;\n}\n
build_tree.dart
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nTreeNode? dfs(\n  List<int> preorder,\n  Map<int, int> inorderMap,\n  int i,\n  int l,\n  int r,\n) {\n  // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  if (r - l < 0) {\n    return null;\n  }\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  TreeNode? root = TreeNode(preorder[i]);\n  // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n  int m = inorderMap[preorder[i]]!;\n  // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.left = dfs(preorder, inorderMap, i + 1, l, m - 1);\n  // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode? buildTree(List<int> preorder, List<int> inorder) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n  Map<int, int> inorderMap = {};\n  for (int i = 0; i < inorder.length; i++) {\n    inorderMap[inorder[i]] = i;\n  }\n  TreeNode? root = dfs(preorder, inorderMap, 0, 0, inorder.length - 1);\n  return root;\n}\n
build_tree.rs
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfn dfs(\n    preorder: &[i32],\n    inorder_map: &HashMap<i32, i32>,\n    i: i32,\n    l: i32,\n    r: i32,\n) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if r - l < 0 {\n        return None;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    let root = TreeNode::new(preorder[i as usize]);\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    let m = inorder_map.get(&preorder[i as usize]).unwrap();\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.borrow_mut().left = dfs(preorder, inorder_map, i + 1, l, m - 1);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.borrow_mut().right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    Some(root)\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfn build_tree(preorder: &[i32], inorder: &[i32]) -> Option<Rc<RefCell<TreeNode>>> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    let mut inorder_map: HashMap<i32, i32> = HashMap::new();\n    for i in 0..inorder.len() {\n        inorder_map.insert(inorder[i], i as i32);\n    }\n    let root = dfs(preorder, &inorder_map, 0, 0, inorder.len() as i32 - 1);\n    root\n}\n
build_tree.c
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nTreeNode *dfs(int *preorder, int *inorderMap, int i, int l, int r, int size) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0)\n        return NULL;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    TreeNode *root = (TreeNode *)malloc(sizeof(TreeNode));\n    root->val = preorder[i];\n    root->left = NULL;\n    root->right = NULL;\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    int m = inorderMap[preorder[i]];\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root->left = dfs(preorder, inorderMap, i + 1, l, m - 1, size);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root->right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r, size);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root;\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode *buildTree(int *preorder, int preorderSize, int *inorder, int inorderSize) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    int *inorderMap = (int *)malloc(sizeof(int) * MAX_SIZE);\n    for (int i = 0; i < inorderSize; i++) {\n        inorderMap[inorder[i]] = i;\n    }\n    TreeNode *root = dfs(preorder, inorderMap, 0, 0, inorderSize - 1, inorderSize);\n    free(inorderMap);\n    return root;\n}\n
build_tree.kt
/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 */\nfun dfs(\n    preorder: IntArray,\n    inorderMap: Map<Int?, Int?>,\n    i: Int,\n    l: Int,\n    r: Int\n): TreeNode? {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    if (r - l < 0) return null\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    val root = TreeNode(preorder[i])\n    // \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n    val m = inorderMap[preorder[i]]!!\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.left = dfs(preorder, inorderMap, i + 1, l, m - 1)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    root.right = dfs(preorder, inorderMap, i + 1 + m - l, m + 1, r)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return root\n}\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e */\nfun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n    val inorderMap = HashMap<Int?, Int?>()\n    for (i in inorder.indices) {\n        inorderMap[inorder[i]] = i\n    }\n    val root = dfs(preorder, inorderMap, 0, 0, inorder.size - 1)\n    return root\n}\n
build_tree.rb
=begin\nFile: build_tree.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  return if r - l < 0\n\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  root = TreeNode.new(preorder[i])\n  # \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n  m = inorder_map[preorder[i]]\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  root\nend\n\n=begin\nFile: build_tree.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e: \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 ###\ndef dfs(preorder, inorder_map, i, l, r)\n  # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u043f\u0443\u0441\u0442\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  return if r - l < 0\n\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  root = TreeNode.new(preorder[i])\n  # \u041d\u0430\u0439\u0442\u0438 m, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\n  m = inorder_map[preorder[i]]\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.left = dfs(preorder, inorder_map, i + 1, l, m - 1)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  root.right = dfs(preorder, inorder_map, i + 1 + m - l, m + 1, r)\n\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  root\nend\n\n# ## \u041f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e ###\ndef build_tree(preorder, inorder)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 inorder \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\n  inorder_map = {}\n  inorder.each_with_index { |val, i| inorder_map[val] = i }\n  dfs(preorder, inorder_map, 0, 0, inorder.length - 1)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-8 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430: \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0444\u0430\u0437\u0435 \"\u0441\u043f\u0443\u0441\u043a\u0430\", \u0430 \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0435\u0431\u0440\u043e (\u0441\u0441\u044b\u043b\u043a\u0430) \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0432 \u0444\u0430\u0437\u0435 \"\u043f\u043e\u0434\u044a\u0435\u043c\u0430\".

<1><2><3><4><5><6><7><8><9>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-8 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f preorder \u0438 inorder \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-9.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-9 \u00a0 \u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435

\u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0430\u0432\u043d\u043e \\(n\\) ; \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0437\u043b\u0430 (\u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 dfs() ) \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) .

\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0445\u0440\u0430\u043d\u0438\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 inorder \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) . \u0412 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \\(n\\) \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(n)\\) \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0442\u0435\u043a\u0430. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u0431\u0449\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0436\u0435 \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) .

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/","title":"12.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\"","text":"

\u0420\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439 (divide and conquer) - \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u0438 \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432. \u041e\u0431\u044b\u0447\u043d\u043e \u043e\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0430 \u044d\u0442\u0430\u043f\u0430: \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435\" \u0438 \"\u0440\u0435\u0448\u0435\u043d\u0438\u0435\".

  1. \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 (\u044d\u0442\u0430\u043f \u0434\u0435\u043a\u043e\u043c\u043f\u043e\u0437\u0438\u0446\u0438\u0438): \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u0434\u0432\u0435 \u0438\u043b\u0438 \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u0430 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430.
  2. \u0420\u0435\u0448\u0435\u043d\u0438\u0435 (\u044d\u0442\u0430\u043f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f): \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-1, \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c\" \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\".

  1. \u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435: \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443) \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438), \u043f\u043e\u043a\u0430 \u0432 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 (\u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430).
  2. \u0420\u0435\u0448\u0435\u043d\u0438\u0435: \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0442\u044c \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b (\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447), \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-1 \u00a0 \u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1211-divide-and-conquer","title":"12.1.1 \u00a0 \u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 divide and conquer","text":"

\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u044f\u0442\u044c, \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c divide and conquer, \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0438.

  1. \u0417\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0447\u0430\u0441\u0442\u0438: \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0435 \u0438 \u043f\u043e\u0445\u043e\u0436\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u0440\u0438\u0447\u0435\u043c \u0442\u0430\u043a\u043e\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e.
  2. \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b: \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f, \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u0438 \u043c\u043e\u0433\u0443\u0442 \u0440\u0435\u0448\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e.
  3. \u0420\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c: \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447.

\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0432\u0441\u0435\u043c \u0442\u0440\u0435\u043c \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u044f\u043c.

  1. \u0417\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0447\u0430\u0441\u0442\u0438: \u043c\u0430\u0441\u0441\u0438\u0432 (\u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430) \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438).
  2. \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b: \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e (\u0442\u043e \u0435\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u0443\u044e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e).
  3. \u0420\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c: \u0434\u0432\u0430 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447) \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0432 \u043e\u0434\u0438\u043d \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 (\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438).
"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1212-divide-and-conquer","title":"12.1.2 \u00a0 \u041f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e divide and conquer","text":"

\u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u0438 \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0441\u0430\u043c\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0438 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c, \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c \u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438.

\u0422\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441: \u043f\u043e\u0447\u0435\u043c\u0443 divide and conquer \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438 \u043a\u0430\u043a\u043e\u0432\u0430 \u043b\u043e\u0433\u0438\u043a\u0430 \u044d\u0442\u043e\u0433\u043e \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435? \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043f\u043e\u0447\u0435\u043c\u0443 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u044d\u0442\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0447\u0435\u043c \u043f\u0440\u044f\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438? \u042d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441 \u0434\u0432\u0443\u0445 \u0441\u0442\u043e\u0440\u043e\u043d: \u0447\u0435\u0440\u0435\u0437 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0438 \u0447\u0435\u0440\u0435\u0437 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f.

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1","title":"1. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c\": \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043b\u0438\u043d\u044b \\(n\\) \u0435\u0439 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \\(O(n^2)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-2. \u0422\u043e\u0433\u0434\u0430 \u0441\u0430\u043c\u043e \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u0430\u0439\u043c\u0435\u0442 \\(O((n / 2)^2)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0430 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0435\u0449\u0435 \\(O(n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u0430:

\\[ O(n + (\\frac{n}{2})^2 \\times 2 + n) = O(\\frac{n^2}{2} + 2n) \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-2 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c \u0434\u043e \u0438 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430

\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u043d\u0435\u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u043e, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u0435\u0432\u0430\u044f \u0438 \u043f\u0440\u0430\u0432\u0430\u044f \u0447\u0430\u0441\u0442\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442 \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043e \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0433\u043e:

\\[ \\begin{aligned} n^2 & > \\frac{n^2}{2} + 2n \\newline n^2 - \\frac{n^2}{2} - 2n & > 0 \\newline n(n - 4) & > 0 \\end{aligned} \\]

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \\(n > 4\\) \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043c\u0435\u043d\u044c\u0448\u0435, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u0430\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0432\u0441\u0435 \u0435\u0449\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \\(O(n^2)\\) ; \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043b\u0438\u0448\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c.

\u0415\u0441\u043b\u0438 \u043f\u043e\u0439\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0434\u0435\u043b\u0438\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043f\u043e\u043b\u0430\u043c, \u043f\u043e\u043a\u0430 \u0432 \u043d\u0435\u043c \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0442\u043e \u043c\u044b \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c\", \u0447\u044c\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) .

\u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0439\u0442\u0438 \u0435\u0449\u0435 \u0434\u0430\u043b\u044c\u0448\u0435 \u0438 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c: \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0447\u0435\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \\(k\\) \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432? \u0422\u0430\u043a\u0430\u044f \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \"\u0431\u043b\u043e\u0447\u043d\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443\", \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \\(O(n + k)\\) .

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#2","title":"2. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439","text":"

\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u043c\u044b\u0435 divide and conquer, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u043c\u0438, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0438\u0445 \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, divide and conquer \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043e\u0436\u0435\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043d\u043e \u0438 \u0445\u043e\u0440\u043e\u0448\u043e \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b.

\u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430 \u0432 \u0441\u0440\u0435\u0434\u0435 \u0441 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u044f\u0434\u0440\u0430\u043c\u0438 \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u0430\u043c\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043b\u0443\u0447\u0448\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u044f \u043e\u0431\u0449\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u043d\u0438\u0436\u0435 \"\u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435\" \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c. \u0422\u043e\u0433\u0434\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0440\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0435\u0434\u0438\u043d\u0438\u0446\u0430\u043c, \u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-3 \u00a0 \u041f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435

"},{"location":"chapter_divide_and_conquer/divide_and_conquer/#1213-divide-and-conquer","title":"12.1.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f divide and conquer","text":"

\u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, divide and conquer \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447.

\u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, divide and conquer \u043e\u0447\u0435\u043d\u044c \u0448\u0438\u0440\u043e\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e divide and conquer - \u044d\u0442\u043e \"\u0442\u0438\u0445\u0430\u044f\" \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438\u0434\u0435\u044f, \u0441\u043a\u0440\u044b\u0442\u043e \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u0430\u043c\u044b\u0445 \u0440\u0430\u0437\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.

"},{"location":"chapter_divide_and_conquer/hanota_problem/","title":"12.4 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435","text":"

\u0412 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u044b \u0434\u0435\u043b\u0438\u043b\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u0434\u0432\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u0430\u0436\u0434\u0430\u044f \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0438\u043c\u0435\u043b\u0430 \u0440\u0430\u0437\u043c\u0435\u0440, \u0440\u0430\u0432\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f.

Question

\u0414\u0430\u043d\u044b \u0442\u0440\u0438 \u0441\u0442\u0435\u0440\u0436\u043d\u044f, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u043a A , B \u0438 C . \u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043d\u0430 \u0441\u0442\u0435\u0440\u0436\u043d\u0435 A \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \\(n\\) \u0434\u0438\u0441\u043a\u043e\u0432, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u043a \u0431\u043e\u043b\u044c\u0448\u0435\u043c\u0443. \u041d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u0438 \\(n\\) \u0434\u0438\u0441\u043a\u043e\u0432 \u043d\u0430 \u0441\u0442\u0435\u0440\u0436\u0435\u043d\u044c C , \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0432 \u0438\u0445 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a (\u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-10). \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0434\u0438\u0441\u043a\u043e\u0432 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430.

  1. \u0414\u0438\u0441\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f.
  2. \u0417\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0437 \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a.
  3. \u041c\u0435\u043d\u044c\u0448\u0438\u0439 \u0434\u0438\u0441\u043a \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u043b\u0435\u0436\u0430\u0442\u044c \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0435\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-10 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435

\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(i\\) \u043a\u0430\u043a \\(f(i)\\) . \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \\(f(3)\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f 3 \u0434\u0438\u0441\u043a\u043e\u0432 \u0441\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f A \u043d\u0430 \u0441\u0442\u0435\u0440\u0436\u0435\u043d\u044c C .

"},{"location":"chapter_divide_and_conquer/hanota_problem/#1","title":"1. \u00a0 \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-11, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(1)\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f A \u043d\u0430 \u0441\u0442\u0435\u0440\u0436\u0435\u043d\u044c C .

<1><2>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-11 \u00a0 \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 1

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-12, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(2)\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0434\u0438\u0441\u043a\u0430, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0434\u0438\u0441\u043a \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u043b\u0436\u0435\u043d \u043b\u0435\u0436\u0430\u0442\u044c \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0435\u043c, \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c B \u043a\u0430\u043a \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0441\u0442\u0435\u0440\u0436\u0435\u043d\u044c.

  1. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u0434\u0438\u0441\u043a \u0441 A \u043d\u0430 B .
  2. \u0417\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0434\u0438\u0441\u043a \u0441 A \u043d\u0430 C .
  3. \u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u0434\u0438\u0441\u043a \u0441 B \u043d\u0430 C .
<1><2><3><4>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-12 \u00a0 \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 2

\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(2)\\) \u043c\u043e\u0436\u043d\u043e \u043a\u0440\u0430\u0442\u043a\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0434\u0432\u0430 \u0434\u0438\u0441\u043a\u0430 \u0441 A \u043d\u0430 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B . \u0417\u0434\u0435\u0441\u044c C \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0435\u0432\u044b\u043c \u0441\u0442\u0435\u0440\u0436\u043d\u0435\u043c, \u0430 B - \u0431\u0443\u0444\u0435\u0440\u043d\u044b\u043c \u0441\u0442\u0435\u0440\u0436\u043d\u0435\u043c.

"},{"location":"chapter_divide_and_conquer/hanota_problem/#2","title":"2. \u00a0 \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438","text":"

\u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(f(3)\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0442\u0440\u0438 \u0434\u0438\u0441\u043a\u0430, \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \\(f(1)\\) \u0438 \\(f(2)\\) \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043e\u0439\u0442\u0438 \u043a \u0437\u0430\u0434\u0430\u0447\u0435 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f divide and conquer \u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0434\u0432\u0430 \u0432\u0435\u0440\u0445\u043d\u0438\u0445 \u0434\u0438\u0441\u043a\u0430 \u043d\u0430 A \u043a\u0430\u043a \u0435\u0434\u0438\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u0448\u0430\u0433\u0438, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-13. \u0422\u0430\u043a \u0442\u0440\u0438 \u0434\u0438\u0441\u043a\u0430 \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0441 A \u043d\u0430 C .

  1. \u0421\u0434\u0435\u043b\u0430\u0442\u044c B \u0446\u0435\u043b\u0435\u0432\u044b\u043c \u0441\u0442\u0435\u0440\u0436\u043d\u0435\u043c, \u0430 C \u0431\u0443\u0444\u0435\u0440\u043d\u044b\u043c, \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0434\u0432\u0430 \u0434\u0438\u0441\u043a\u0430 \u0441 A \u043d\u0430 B .
  2. \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0441 A \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043d\u0430 C .
  3. \u0421\u0434\u0435\u043b\u0430\u0442\u044c C \u0446\u0435\u043b\u0435\u0432\u044b\u043c \u0441\u0442\u0435\u0440\u0436\u043d\u0435\u043c, \u0430 A \u0431\u0443\u0444\u0435\u0440\u043d\u044b\u043c, \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0434\u0432\u0430 \u0434\u0438\u0441\u043a\u0430 \u0441 B \u043d\u0430 C .
<1><2><3><4>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-13 \u00a0 \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 3

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \\(f(3)\\) \u043d\u0430 \u0434\u0432\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \\(f(2)\\) \u0438 \u043e\u0434\u043d\u0443 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \\(f(1)\\) . \u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0438 \u0442\u0440\u0438 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0442\u043e\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0430. \u042d\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b \u0438 \u0447\u0442\u043e \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c.

\u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043c\u043e\u0436\u043d\u043e \u0441\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-14 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e divide and conquer \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435: \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \\(f(n)\\) \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \\(f(n-1)\\) \u0438 \u043e\u0434\u043d\u0443 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \\(f(1)\\) , \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0442\u0435\u043c \u0440\u0435\u0448\u0430\u044e\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.

  1. \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \\(n-1\\) \u0434\u0438\u0441\u043a\u043e\u0432 \u0441 A \u043d\u0430 B \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e C .
  2. \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \\(1\\) \u0434\u0438\u0441\u043a \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441 A \u043d\u0430 C .
  3. \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \\(n-1\\) \u0434\u0438\u0441\u043a\u043e\u0432 \u0441 B \u043d\u0430 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e A .

\u0414\u043b\u044f \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \\(f(n-1)\\) \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0442\u043e\u0442 \u0436\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442\u0430 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \\(f(1)\\) . \u0410 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \\(f(1)\\) \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-14 \u00a0 \u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435

"},{"location":"chapter_divide_and_conquer/hanota_problem/#3","title":"3. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0412 \u043a\u043e\u0434\u0435 \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e dfs(i, src, buf, tar) , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442 \\(i\\) \u0432\u0435\u0440\u0445\u043d\u0438\u0445 \u0434\u0438\u0441\u043a\u043e\u0432 \u0441\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f src \u043d\u0430 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0441\u0442\u0435\u0440\u0436\u0435\u043d\u044c tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0443\u0444\u0435\u0440\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u0440\u0436\u043d\u044f buf :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hanota.py
def move(src: list[int], tar: list[int]):\n    \"\"\"\u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a\"\"\"\n    # \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    pan = src.pop()\n    # \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.append(pan)\n\ndef dfs(i: int, src: list[int], buf: list[int], tar: list[int]):\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i)\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if i == 1:\n        move(src, tar)\n        return\n    # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf)\n    # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar)\n    # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar)\n\ndef solve_hanota(A: list[int], B: list[int], C: list[int]):\n    \"\"\"\u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438\"\"\"\n    n = len(A)\n    # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C)\n
hanota.cpp
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nvoid move(vector<int> &src, vector<int> &tar) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    int pan = src.back();\n    src.pop_back();\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.push_back(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nvoid dfs(int i, vector<int> &src, vector<int> &buf, vector<int> &tar) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nvoid solveHanota(vector<int> &A, vector<int> &B, vector<int> &C) {\n    int n = A.size();\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C);\n}\n
hanota.java
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nvoid move(List<Integer> src, List<Integer> tar) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    Integer pan = src.remove(src.size() - 1);\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.add(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nvoid dfs(int i, List<Integer> src, List<Integer> buf, List<Integer> tar) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i == 1) {\n        move(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nvoid solveHanota(List<Integer> A, List<Integer> B, List<Integer> C) {\n    int n = A.size();\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C);\n}\n
hanota.cs
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nvoid Move(List<int> src, List<int> tar) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    int pan = src[^1];\n    src.RemoveAt(src.Count - 1);\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.Add(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nvoid DFS(int i, List<int> src, List<int> buf, List<int> tar) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i == 1) {\n        Move(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    DFS(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    Move(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    DFS(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nvoid SolveHanota(List<int> A, List<int> B, List<int> C) {\n    int n = A.Count;\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    DFS(n, A, B, C);\n}\n
hanota.go
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfunc move(src, tar *list.List) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    pan := src.Back()\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.PushBack(pan.Value)\n    // \u0423\u0431\u0440\u0430\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u0434\u0438\u0441\u043a \u0438\u0437 src\n    src.Remove(pan)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfunc dfsHanota(i int, src, buf, tar *list.List) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if i == 1 {\n        move(src, tar)\n        return\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfsHanota(i-1, src, tar, buf)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfsHanota(i-1, buf, src, tar)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfunc solveHanota(A, B, C *list.List) {\n    n := A.Len()\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfsHanota(n, A, B, C)\n}\n
hanota.swift
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfunc move(src: inout [Int], tar: inout [Int]) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    let pan = src.popLast()!\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.append(pan)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfunc dfs(i: Int, src: inout [Int], buf: inout [Int], tar: inout [Int]) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if i == 1 {\n        move(src: &src, tar: &tar)\n        return\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i: i - 1, src: &src, buf: &tar, tar: &buf)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src: &src, tar: &tar)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i: i - 1, src: &buf, buf: &src, tar: &tar)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfunc solveHanota(A: inout [Int], B: inout [Int], C: inout [Int]) {\n    let n = A.count\n    // \u0425\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(i: n, src: &A, buf: &B, tar: &C)\n}\n
hanota.js
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfunction move(src, tar) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    const pan = src.pop();\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.push(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfunction dfs(i, src, buf, tar) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfunction solveHanota(A, B, C) {\n    const n = A.length;\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C);\n}\n
hanota.ts
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfunction move(src: number[], tar: number[]): void {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    const pan = src.pop();\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.push(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfunction dfs(i: number, src: number[], buf: number[], tar: number[]): void {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i === 1) {\n        move(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfunction solveHanota(A: number[], B: number[], C: number[]): void {\n    const n = A.length;\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C);\n}\n
hanota.dart
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nvoid move(List<int> src, List<int> tar) {\n  // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n  int pan = src.removeLast();\n  // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n  tar.add(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nvoid dfs(int i, List<int> src, List<int> buf, List<int> tar) {\n  // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n  if (i == 1) {\n    move(src, tar);\n    return;\n  }\n  // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n  dfs(i - 1, src, tar, buf);\n  // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n  move(src, tar);\n  // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n  dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nvoid solveHanota(List<int> A, List<int> B, List<int> C) {\n  int n = A.length;\n  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n  dfs(n, A, B, C);\n}\n
hanota.rs
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfn move_pan(src: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    let pan = src.pop().unwrap();\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.push(pan);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfn dfs(i: i32, src: &mut Vec<i32>, buf: &mut Vec<i32>, tar: &mut Vec<i32>) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if i == 1 {\n        move_pan(src, tar);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move_pan(src, tar);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfn solve_hanota(A: &mut Vec<i32>, B: &mut Vec<i32>, C: &mut Vec<i32>) {\n    let n = A.len() as i32;\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C);\n}\n
hanota.c
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nvoid move(int *src, int *srcSize, int *tar, int *tarSize) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    int pan = src[*srcSize - 1];\n    src[*srcSize - 1] = 0;\n    (*srcSize)--;\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar[*tarSize] = pan;\n    (*tarSize)++;\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nvoid dfs(int i, int *src, int *srcSize, int *buf, int *bufSize, int *tar, int *tarSize) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i == 1) {\n        move(src, srcSize, tar, tarSize);\n        return;\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, srcSize, tar, tarSize, buf, bufSize);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, srcSize, tar, tarSize);\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, bufSize, src, srcSize, tar, tarSize);\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nvoid solveHanota(int *A, int *ASize, int *B, int *BSize, int *C, int *CSize) {\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(*ASize, A, ASize, B, BSize, C, CSize);\n}\n
hanota.kt
/* \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a */\nfun move(src: MutableList<Int>, tar: MutableList<Int>) {\n    // \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n    val pan = src.removeAt(src.size - 1)\n    // \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n    tar.add(pan)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) */\nfun dfs(i: Int, src: MutableList<Int>, buf: MutableList<Int>, tar: MutableList<Int>) {\n    // \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n    if (i == 1) {\n        move(src, tar)\n        return\n    }\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n    dfs(i - 1, src, tar, buf)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n    move(src, tar)\n    // \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n    dfs(i - 1, buf, src, tar)\n}\n\n/* \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 */\nfun solveHanota(A: MutableList<Int>, B: MutableList<Int>, C: MutableList<Int>) {\n    val n = A.size\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n    dfs(n, A, B, C)\n}\n
hanota.rb
=begin\nFile: hanota.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a ###\ndef move(src, tar)\n  # \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n  pan = src.pop\n  # \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n  tar << pan\nend\n\n=begin\nFile: hanota.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a ###\ndef move(src, tar)\n  # \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n  pan = src.pop\n  # \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n  tar << pan\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) ###\ndef dfs(i, src, buf, tar)\n  # \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n  dfs(i - 1, src, tar, buf)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n  move(src, tar)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n  dfs(i - 1, buf, src, tar)\nend\n\n=begin\nFile: hanota.rb\nCreated Time: 2024-05-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a ###\ndef move(src, tar)\n  # \u0421\u043d\u044f\u0442\u044c \u0434\u0438\u0441\u043a \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b src\n  pan = src.pop\n  # \u041f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0434\u0438\u0441\u043a \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 tar\n  tar << pan\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 f(i) ###\ndef dfs(i, src, buf, tar)\n  # \u0415\u0441\u043b\u0438 \u0432 src \u043e\u0441\u0442\u0430\u043b\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a, \u0441\u0440\u0430\u0437\u0443 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 tar\n  if i == 1\n    move(src, tar)\n    return\n  end\n\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 src \u0432 buf \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e tar\n  dfs(i - 1, src, tar, buf)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043e\u0434\u0438\u043d \u0434\u0438\u0441\u043a \u0438\u0437 src \u0432 tar\n  move(src, tar)\n  # \u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 f(i-1): \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 i-1 \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 buf \u0432 tar \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e src\n  dfs(i - 1, buf, src, tar)\nend\n\n# ## \u0420\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0438 ###\ndef solve_hanota(_A, _B, _C)\n  n = _A.length\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0435 n \u0434\u0438\u0441\u043a\u043e\u0432 \u0438\u0437 A \u0432 C \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e B\n  dfs(n, _A, _B, _C)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 12-15, \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435 \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u0442 \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0432\u044b\u0441\u043e\u0442\u044b \\(n\\) , \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u0432\u044b\u0437\u043e\u0432\u0443 dfs() ; \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(2^n)\\) , \u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 12-15 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435

Quote

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0425\u0430\u043d\u043e\u0439\u0441\u043a\u043e\u0439 \u0431\u0430\u0448\u043d\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u0434\u0440\u0435\u0432\u043d\u0435\u0439 \u043b\u0435\u0433\u0435\u043d\u0434\u044b. \u0412 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0445\u0440\u0430\u043c\u043e\u0432 \u0434\u0440\u0435\u0432\u043d\u0435\u0439 \u0418\u043d\u0434\u0438\u0438 \u043c\u043e\u043d\u0430\u0445\u0438 \u0438\u043c\u0435\u043b\u0438 \u0442\u0440\u0438 \u0432\u044b\u0441\u043e\u043a\u0438\u0445 \u0430\u043b\u043c\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0435\u0440\u0436\u043d\u044f \u0438 \\(64\\) \u0437\u043e\u043b\u043e\u0442\u044b\u0445 \u0434\u0438\u0441\u043a\u0430 \u0440\u0430\u0437\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430. \u041c\u043e\u043d\u0430\u0445\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u043b\u0438 \u0434\u0438\u0441\u043a\u0438 \u0438 \u0432\u0435\u0440\u0438\u043b\u0438, \u0447\u0442\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u0438\u0441\u043a \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0435\u043d, \u043c\u0438\u0440 \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u043a \u043a\u043e\u043d\u0446\u0443.

\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0431\u044b \u043c\u043e\u043d\u0430\u0445\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u043b\u0438 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0434\u0438\u0441\u043a\u0443 \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0438\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0431\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \\(2^{64} \\approx 1.84\u00d710^{19}\\) \u0441\u0435\u043a\u0443\u043d\u0434, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u043a\u043e\u043b\u043e \\(585\\) \u043c\u0438\u043b\u043b\u0438\u0430\u0440\u0434\u043e\u0432 \u043b\u0435\u0442, \u0447\u0442\u043e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043e\u0446\u0435\u043d\u043a\u0443 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430 \u0412\u0441\u0435\u043b\u0435\u043d\u043d\u043e\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u043b\u0435\u0433\u0435\u043d\u0434\u0430 \u0438 \u0432\u0435\u0440\u043d\u0430, \u043d\u0430\u043c, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e \u0447\u0435\u043c \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u0438\u0442\u044c\u0441\u044f.

"},{"location":"chapter_divide_and_conquer/summary/","title":"12.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_divide_and_conquer/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_dynamic_programming/","title":"\u0413\u043b\u0430\u0432\u0430 14. \u00a0 \u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

Abstract

\u0420\u0443\u0447\u044c\u0438 \u0432\u043f\u0430\u0434\u0430\u044e\u0442 \u0432 \u0440\u0435\u043a\u0438, \u0430 \u0440\u0435\u043a\u0438 \u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043c\u043e\u0440\u0435.

\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u0430\u043b\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0438 \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u0432\u0435\u0434\u0435\u0442 \u043d\u0430\u0441 \u043a \u0435\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044e.

"},{"location":"chapter_dynamic_programming/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_dynamic_programming/dp_problem_features/","title":"14.2 \u00a0 \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0443\u0432\u0438\u0434\u0435\u043b\u0438, \u043a\u0430\u043a \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u0448\u0430\u0435\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0447\u0435\u0440\u0435\u0437 \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 - \u044d\u0442\u043e \u043e\u0431\u0449\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0434\u0445\u043e\u0434, \u043d\u043e \u0432 divide and conquer, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0438 backtracking \u0430\u043a\u0446\u0435\u043d\u0442\u044b \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0422\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u0438 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0435\u0449\u0435 \u0434\u0432\u0443\u043c\u044f \u0432\u0430\u0436\u043d\u044b\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439.

"},{"location":"chapter_dynamic_programming/dp_problem_features/#1421","title":"14.2.1 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430","text":"

\u041d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u043f\u043e\u0434\u044a\u0435\u043c\u0435 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b.

\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435

\u0414\u0430\u043d\u0430 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \u0437\u0430 \u0440\u0430\u0437. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0435\u0435 \u0446\u0435\u043d\u0443 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u043d\u0430 \u044d\u0442\u0443 \u0441\u0442\u0443\u043f\u0435\u043d\u044c. \u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \\(cost\\) , \u0433\u0434\u0435 \\(cost[i]\\) - \u044d\u0442\u043e \u0446\u0435\u043d\u0430 \u0434\u043b\u044f \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\) , \u0430 \\(cost[0]\\) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0437\u0435\u043c\u043b\u0435 (\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438). \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0443\u044e \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u044b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-6, \u0435\u0441\u043b\u0438 \u0446\u0435\u043d\u044b \u0434\u043b\u044f \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439 \\(1\\) , \\(2\\) \u0438 \\(3\\) \u0440\u0430\u0432\u043d\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\(1\\) , \\(10\\) \u0438 \\(1\\) , \u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u0441 \u0437\u0435\u043c\u043b\u0438 \u043d\u0430 \u0442\u0440\u0435\u0442\u044c\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c \u0440\u0430\u0432\u043d\u0430 \\(2\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-6 \u00a0 \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043d\u0430 3-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c

\u041f\u0443\u0441\u0442\u044c \\(dp[i]\\) \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043d\u0430\u043a\u043e\u043f\u043b\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u044c \\(i\\) . \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u044c \\(i\\) \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0439\u0442\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 1\\) \u0438\u043b\u0438 \u0441\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 2\\) , \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(dp[i]\\) \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u0438\u0431\u043e \\(dp[i - 1] + cost[i]\\) , \u043b\u0438\u0431\u043e \\(dp[i - 2] + cost[i]\\) . \u0427\u0442\u043e\u0431\u044b \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432:

\\[ dp[i] = \\min(dp[i-1], dp[i-2]) + cost[i] \\]

\u041e\u0442\u0441\u044e\u0434\u0430 \u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b: \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0438\u0437 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447.

\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439: \u043c\u044b \u0431\u0435\u0440\u0435\u043c \u043b\u0443\u0447\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \\(dp[i-1]\\) \u0438 \\(dp[i-2]\\) \u0438 \u043d\u0430 \u0435\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u043e\u0438\u043c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \\(dp[i]\\) .

\u0410 \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u043b\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0447\u0438\u0441\u043b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0438\u0437 \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430? \u0424\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e \u043e\u043d\u0430 \u043d\u0435 \u043f\u0440\u043e \u043e\u043f\u0442\u0438\u043c\u0443\u043c, \u0430 \u043f\u0440\u043e \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430. \u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u043a\u0430\u043a \"\u043d\u0430\u0439\u0434\u0438\u0442\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432\", \u043c\u044b \u043d\u0435\u043e\u0436\u0438\u0434\u0430\u043d\u043d\u043e \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0445\u043e\u0442\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0441\u0442\u0430\u043b\u0430\u0441\u044c \u043f\u043e \u0441\u0443\u0442\u0438 \u0442\u043e\u0439 \u0436\u0435, \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0442\u0430\u043b\u0430 \u044f\u0432\u043d\u043e\u0439: \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(n\\) \u0440\u0430\u0432\u043d\u043e \u0441\u0443\u043c\u043c\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439 \\(n-1\\) \u0438 \\(n-2\\) . \u0422\u043e \u0435\u0441\u0442\u044c \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0433\u0438\u0431\u043a\u0438\u043c.

\u0417\u043d\u0430\u044f \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\(dp[1] = cost[1]\\) \u0438 \\(dp[2] = cost[2]\\) , \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u0440\u0430\u0437\u0443 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp(cost: list[int]) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp = [0] * (n + 1)\n    # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1], dp[2] = cost[1], cost[2]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in range(3, n + 1):\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    return dp[n]\n
min_cost_climbing_stairs_dp.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minCostClimbingStairsDP(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    vector<int> dp(n + 1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
min_cost_climbing_stairs_dp.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minCostClimbingStairsDP(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[] dp = new int[n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
min_cost_climbing_stairs_dp.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint MinCostClimbingStairsDP(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[] dp = new int[n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = Math.Min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
min_cost_climbing_stairs_dp.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc minCostClimbingStairsDP(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp := make([]int, n+1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i := 3; i <= n; i++ {\n        dp[i] = min(dp[i-1], dp[i-2]) + cost[i]\n    }\n    return dp[n]\n}\n
min_cost_climbing_stairs_dp.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc minCostClimbingStairsDP(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3 ... n {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
min_cost_climbing_stairs_dp.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction minCostClimbingStairsDP(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = new Array(n + 1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
min_cost_climbing_stairs_dp.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction minCostClimbingStairsDP(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = new Array(n + 1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i] = Math.min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    return dp[n];\n}\n
min_cost_climbing_stairs_dp.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minCostClimbingStairsDP(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  List<int> dp = List.filled(n + 1, 0);\n  // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1] = cost[1];\n  dp[2] = cost[2];\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  for (int i = 3; i <= n; i++) {\n    dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i];\n  }\n  return dp[n];\n}\n
min_cost_climbing_stairs_dp.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn min_cost_climbing_stairs_dp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    let mut dp = vec![-1; n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3..=n {\n        dp[i] = cmp::min(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    dp[n]\n}\n
min_cost_climbing_stairs_dp.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minCostClimbingStairsDP(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int *dp = calloc(n + 1, sizeof(int));\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1];\n    dp[2] = cost[2];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = myMin(dp[i - 1], dp[i - 2]) + cost[i];\n    }\n    int res = dp[n];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
min_cost_climbing_stairs_dp.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun minCostClimbingStairsDP(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    val dp = IntArray(n + 1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = cost[1]\n    dp[2] = cost[2]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (i in 3..n) {\n        dp[i] = min(dp[i - 1], dp[i - 2]) + cost[i]\n    }\n    return dp[n]\n}\n
min_cost_climbing_stairs_dp.rb
=begin\nFile: min_cost_climbing_stairs_dp.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp = Array.new(n + 1, 0)\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-7 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-7 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e\u0434\u044a\u0435\u043c\u0430

\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0441\u0436\u0430\u0432 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432 \u043d\u0443\u043b\u0435\u0432\u043e\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0432 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \\(O(n)\\) \u0434\u043e \\(O(1)\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_cost_climbing_stairs_dp.py
def min_cost_climbing_stairs_dp_comp(cost: list[int]) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n = len(cost) - 1\n    if n == 1 or n == 2:\n        return cost[n]\n    a, b = cost[1], cost[2]\n    for i in range(3, n + 1):\n        a, b = b, min(a, b) + cost[i]\n    return b\n
min_cost_climbing_stairs_dp.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minCostClimbingStairsDPComp(vector<int> &cost) {\n    int n = cost.size() - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint MinCostClimbingStairsDPComp(int[] cost) {\n    int n = cost.Length - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = Math.Min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc minCostClimbingStairsDPComp(cost []int) int {\n    n := len(cost) - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    min := func(a, b int) int {\n        if a < b {\n            return a\n        }\n        return b\n    }\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    a, b := cost[1], cost[2]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i := 3; i <= n; i++ {\n        tmp := b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
min_cost_climbing_stairs_dp.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc minCostClimbingStairsDPComp(cost: [Int]) -> Int {\n    let n = cost.count - 1\n    if n == 1 || n == 2 {\n        return cost[n]\n    }\n    var (a, b) = (cost[1], cost[2])\n    for i in 3 ... n {\n        (a, b) = (b, min(a, b) + cost[i])\n    }\n    return b\n}\n
min_cost_climbing_stairs_dp.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction minCostClimbingStairsDPComp(cost) {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction minCostClimbingStairsDPComp(cost: Array<number>): number {\n    const n = cost.length - 1;\n    if (n === 1 || n === 2) {\n        return cost[n];\n    }\n    let a = cost[1],\n        b = cost[2];\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = Math.min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minCostClimbingStairsDPComp(List<int> cost) {\n  int n = cost.length - 1;\n  if (n == 1 || n == 2) return cost[n];\n  int a = cost[1], b = cost[2];\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = min(a, tmp) + cost[i];\n    a = tmp;\n  }\n  return b;\n}\n
min_cost_climbing_stairs_dp.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn min_cost_climbing_stairs_dp_comp(cost: &[i32]) -> i32 {\n    let n = cost.len() - 1;\n    if n == 1 || n == 2 {\n        return cost[n];\n    };\n    let (mut a, mut b) = (cost[1], cost[2]);\n    for i in 3..=n {\n        let tmp = b;\n        b = cmp::min(a, tmp) + cost[i];\n        a = tmp;\n    }\n    b\n}\n
min_cost_climbing_stairs_dp.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minCostClimbingStairsDPComp(int cost[], int costSize) {\n    int n = costSize - 1;\n    if (n == 1 || n == 2)\n        return cost[n];\n    int a = cost[1], b = cost[2];\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = myMin(a, tmp) + cost[i];\n        a = tmp;\n    }\n    return b;\n}\n
min_cost_climbing_stairs_dp.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun minCostClimbingStairsDPComp(cost: IntArray): Int {\n    val n = cost.size - 1\n    if (n == 1 || n == 2) return cost[n]\n    var a = cost[1]\n    var b = cost[2]\n    for (i in 3..n) {\n        val tmp = b\n        b = min(a, tmp) + cost[i]\n        a = tmp\n    }\n    return b\n}\n
min_cost_climbing_stairs_dp.rb
=begin\nFile: min_cost_climbing_stairs_dp.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef min_cost_climbing_stairs_dp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp = Array.new(n + 1, 0)\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1], dp[2] = cost[1], cost[2]\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  (3...(n + 1)).each { |i| dp[i] = [dp[i - 1], dp[i - 2]].min + cost[i] }\n  dp[n]\nend\n\n# \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\ndef min_cost_climbing_stairs_dp_comp(cost)\n  n = cost.length - 1\n  return cost[n] if n == 1 || n == 2\n  a, b = cost[1], cost[2]\n  (3...(n + 1)).each { |i| a, b = b, [a, b].min + cost[i] }\n  b\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/dp_problem_features/#1422","title":"14.2.2 \u00a0 \u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439","text":"

\u041e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439 - \u043e\u0434\u043d\u043e \u0438\u0437 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0445 \u0441\u0432\u043e\u0439\u0441\u0442\u0432, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u043c\u043e\u0436\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u0415\u0433\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0432\u043e: \u0435\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u043d\u043e \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e, \u0442\u043e \u0435\u0433\u043e \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u043d\u0435\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0441\u0435\u0439 \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.

\u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0441\u043d\u043e\u0432\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435. \u0415\u0441\u043b\u0438 \u0434\u0430\u043d\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\(i\\) , \u0442\u043e \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\(i+1\\) \u0438 \\(i+2\\) , \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u044b\u0436\u043a\u0430\u043c \u043d\u0430 \\(1\\) \u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438. \u0427\u0442\u043e\u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u044d\u0442\u0438\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432, \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a\u0438\u043c\u0438 \u0431\u044b\u043b\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0434\u043e \\(i\\) ; \u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u0435 \u0432\u043b\u0438\u044f\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\(i\\) .

\u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0437\u0430\u0434\u0430\u0447\u0443 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435, \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f.

\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c

\u0414\u0430\u043d\u0430 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430 \u0438\u0437 \\(n\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439. \u0417\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u043d\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0434\u0432\u0430 \u0440\u0430\u0443\u043d\u0434\u0430 \u043f\u043e\u0434\u0440\u044f\u0434 \u043f\u0440\u044b\u0433\u0430\u0442\u044c \u043d\u0430 \\(1\\) \u0441\u0442\u0443\u043f\u0435\u043d\u044c. \u0421\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b?

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-8, \u043d\u0430 \u0442\u0440\u0435\u0442\u044c\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \\(2\\) \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f: \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0442\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u0440\u044b\u0436\u043a\u0430\u043c\u0438 \u043d\u0430 \\(1\\) \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044e \u0438 \u043f\u043e\u0442\u043e\u043c\u0443 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-8 \u00a0 \u0427\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 3-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f

\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0431\u044b\u043b \u0441\u0434\u0435\u043b\u0430\u043d \u043f\u0440\u044b\u0436\u043e\u043a \u043d\u0430 \\(1\\) \u0441\u0442\u0443\u043f\u0435\u043d\u044c, \u0442\u043e \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0443\u0436\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u044b\u0433\u043d\u0443\u0442\u044c \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0432\u044b\u0431\u043e\u0440 \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e (\u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u043d\u043e\u043c\u0435\u0440\u0443 \u0441\u0442\u0443\u043f\u0435\u043d\u0438) - \u043e\u043d \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0435\u0449\u0435 \u0438 \u043e\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f (\u0441 \u043a\u0430\u043a\u043e\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \u043c\u044b \u043f\u0440\u0438\u0448\u043b\u0438 \u0432 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437).

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0438\u0434\u0435 \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0443 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439, \u0430 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\(dp[i] = dp[i-1] + dp[i-2]\\) \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \\(dp[i-1]\\) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u0440\u044b\u0436\u043a\u0443 \u043d\u0430 \\(1\\) \u0441\u0442\u0443\u043f\u0435\u043d\u044c, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u0433\u0434\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0440\u0430\u0443\u043d\u0434 \u0442\u043e\u0436\u0435 \u0431\u044b\u043b \u043f\u0440\u044b\u0436\u043a\u043e\u043c \u043d\u0430 \\(1\\) \u0441\u0442\u0443\u043f\u0435\u043d\u044c. \u0422\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432 \\(dp[i]\\) , \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435.

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f: \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\([i, j]\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u043c\u0441\u044f \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\) \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043f\u0440\u044b\u0433\u043d\u0443\u043b\u0438 \u043d\u0430 \\(j\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439, \u0433\u0434\u0435 \\(j \\in \\{1, 2\\}\\) . \u0422\u0430\u043a\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442, \u0431\u044b\u043b \u043b\u0438 \u0432 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u043f\u0440\u044b\u0436\u043e\u043a \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-9, \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \\(dp[i, j]\\) \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043b\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\([i, j]\\) . \u0422\u043e\u0433\u0434\u0430 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

\\[ \\begin{cases} dp[i, 1] = dp[i-1, 2] \\\\ dp[i, 2] = dp[i-2, 1] + dp[i-2, 2] \\end{cases} \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-9 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u0441 \u0443\u0447\u0435\u0442\u043e\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f

\u0412 \u043a\u043e\u043d\u0446\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \\(dp[n, 1] + dp[n, 2]\\) ; \u044d\u0442\u0430 \u0441\u0443\u043c\u043c\u0430 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(n\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_constraint_dp.py
def climbing_stairs_constraint_dp(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    if n == 1 or n == 2:\n        return 1\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp = [[0] * 3 for _ in range(n + 1)]\n    # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1], dp[1][2] = 1, 0\n    dp[2][1], dp[2][2] = 0, 1\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in range(3, n + 1):\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    return dp[n][1] + dp[n][2]\n
climbing_stairs_constraint_dp.cpp
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    vector<vector<int>> dp(n + 1, vector<int>(3, 0));\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.java
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[][] dp = new int[n + 1][3];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.cs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint ClimbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[,] dp = new int[n + 1, 3];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1, 1] = 1;\n    dp[1, 2] = 0;\n    dp[2, 1] = 0;\n    dp[2, 2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i, 1] = dp[i - 1, 2];\n        dp[i, 2] = dp[i - 2, 1] + dp[i - 2, 2];\n    }\n    return dp[n, 1] + dp[n, 2];\n}\n
climbing_stairs_constraint_dp.go
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc climbingStairsConstraintDP(n int) int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp := make([][3]int, n+1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i := 3; i <= n; i++ {\n        dp[i][1] = dp[i-1][2]\n        dp[i][2] = dp[i-2][1] + dp[i-2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.swift
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc climbingStairsConstraintDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return 1\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    var dp = Array(repeating: Array(repeating: 0, count: 3), count: n + 1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3 ... n {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.js
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction climbingStairsConstraintDP(n) {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = Array.from(new Array(n + 1), () => new Array(3));\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.ts
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction climbingStairsConstraintDP(n: number): number {\n    if (n === 1 || n === 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = Array.from({ length: n + 1 }, () => new Array(3));\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.dart
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsConstraintDP(int n) {\n  if (n == 1 || n == 2) {\n    return 1;\n  }\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(3, 0));\n  // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1][1] = 1;\n  dp[1][2] = 0;\n  dp[2][1] = 0;\n  dp[2][2] = 1;\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  for (int i = 3; i <= n; i++) {\n    dp[i][1] = dp[i - 1][2];\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n  }\n  return dp[n][1] + dp[n][2];\n}\n
climbing_stairs_constraint_dp.rs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn climbing_stairs_constraint_dp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return 1;\n    };\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    let mut dp = vec![vec![-1; 3]; n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3..=n {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.c
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsConstraintDP(int n) {\n    if (n == 1 || n == 2) {\n        return 1;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(3, sizeof(int));\n    }\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1;\n    dp[1][2] = 0;\n    dp[2][1] = 0;\n    dp[2][2] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i][1] = dp[i - 1][2];\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2];\n    }\n    int res = dp[n][1] + dp[n][2];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
climbing_stairs_constraint_dp.kt
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun climbingStairsConstraintDP(n: Int): Int {\n    if (n == 1 || n == 2) {\n        return 1\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    val dp = Array(n + 1) { IntArray(3) }\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1][1] = 1\n    dp[1][2] = 0\n    dp[2][1] = 0\n    dp[2][2] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (i in 3..n) {\n        dp[i][1] = dp[i - 1][2]\n        dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n    }\n    return dp[n][1] + dp[n][2]\n}\n
climbing_stairs_constraint_dp.rb
=begin\nFile: climbing_stairs_constraint_dp.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef climbing_stairs_constraint_dp(n)\n  return 1 if n == 1 || n == 2\n\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp = Array.new(n + 1) { Array.new(3, 0) }\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1][1], dp[1][2] = 1, 0\n  dp[2][1], dp[2][2] = 0, 1\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  for i in 3...(n + 1)\n    dp[i][1] = dp[i - 1][2]\n    dp[i][2] = dp[i - 2][1] + dp[i - 2][2]\n  end\n\n  dp[n][1] + dp[n][2]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043d\u043e\u0432\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0443 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \"\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0442 \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e\" \u0431\u044b\u0432\u0430\u0435\u0442 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u0435\u0435.

\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441 \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u0435\u043c \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0438\u0439

\u0414\u0430\u043d\u0430 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430 \u0438\u0437 \\(n\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439. \u0417\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0435\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043f\u0430\u043b\u0438 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u044c \\(i\\) , \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0438\u0435 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(2i\\) , \u0438 \u043d\u0430 \u0432\u0441\u0435\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0448\u0430\u0433\u0430\u0445 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u044c \\(2i\\) \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432 \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u0432\u0443\u0445 \u0440\u0430\u0443\u043d\u0434\u0430\u0445 \u0432\u044b \u043f\u043e\u043f\u0430\u043b\u0438 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(2\\) \u0438 \\(3\\) , \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u0442\u044c \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(4\\) \u0438 \\(6\\) . \u0421\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b?

\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u0440\u044b\u0436\u043e\u043a \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u044b\u0436\u043e\u043a \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u043e\u0435 \u043f\u0440\u0435\u043f\u044f\u0442\u0441\u0442\u0432\u0438\u0435 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u0432\u0441\u0435 \u0431\u0443\u0434\u0443\u0449\u0438\u0435 \u043f\u0440\u044b\u0436\u043a\u0438. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u0442\u0430\u043a\u043e\u0433\u043e \u0442\u0438\u043f\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b\u043c.

\u0412\u043e\u043e\u0431\u0449\u0435, \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0442\u043e\u0440\u043d\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0437\u0430\u0434\u0430\u0447\u0430 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0435\u0440\u0430) \u043d\u0435 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439. \u0414\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u044e\u0442 \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b - \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u0432\u0440\u0438\u0441\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0438\u0441\u043a, \u0433\u0435\u043d\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u0434\u043a\u0440\u0435\u043f\u043b\u0435\u043d\u0438\u0435\u043c \u0438 \u0442.\u0434., - \u0447\u0442\u043e\u0431\u044b \u0437\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u043e\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435.

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/","title":"14.3 \u00a0 \u041f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0412 \u0434\u0432\u0443\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445 \u0431\u044b\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0422\u0435\u043f\u0435\u0440\u044c \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u0434\u0432\u0430 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u0430.

  1. \u041a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f?
  2. \u0421 \u0447\u0435\u0433\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u043a\u0430\u043a \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f?
"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1431","title":"14.3.1 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438","text":"

\u0412 \u0446\u0435\u043b\u043e\u043c, \u0435\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0443 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0439, \u0442\u043e \u043e\u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0441\u0435 \u044d\u0442\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0437 \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u044b\u0432\u0430\u0435\u0442 \u0442\u0440\u0443\u0434\u043d\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0441\u043b\u0430\u0431\u043b\u044f\u0435\u043c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c backtracking (\u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430).

\u0417\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0434\u043b\u044f backtracking, \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \"\u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\". \u0422\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0434\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0443\u0442\u044c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0439.

\u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0435\u0441\u043b\u0438 \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u0435\u0441\u0442\u044c \u0447\u0435\u0442\u043a\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e \u0442\u0430\u043a\u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0442\u043e \u043e\u043d\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 backtracking.

\u041f\u043e\u0432\u0435\u0440\u0445 \u044d\u0442\u043e\u0433\u043e \u0443 \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \"\u043f\u043b\u044e\u0441\u044b\".

\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \"\u043c\u0438\u043d\u0443\u0441\u044b\".

\u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438 \u0438\u043c\u0435\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u044f\u0432\u043d\u044b\u0435 \"\u043f\u043b\u044e\u0441\u044b\", \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0443\u0436\u0435 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f.

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1432","title":"14.3.2 \u00a0 \u042d\u0442\u0430\u043f\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438","text":"

\u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043f\u0440\u0438\u0440\u043e\u0434\u044b \u0438 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0448\u0430\u0433\u0438: \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) , \u0432\u044b\u0432\u043e\u0434 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439 \u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432.

\u0427\u0442\u043e\u0431\u044b \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \"\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438\".

Question

\u0414\u0430\u043d\u0430 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0430\u044f \u0441\u0435\u0442\u043a\u0430 grid \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n \\times m\\) , \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0435\u0435 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u044d\u0442\u0443 \u043a\u043b\u0435\u0442\u043a\u0443. \u0420\u043e\u0431\u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u0442 \u0438\u0437 \u043b\u0435\u0432\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u0438 \u0437\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043c\u043e\u0436\u0435\u0442 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043f\u0440\u0430\u0432\u043e \u0438\u043b\u0438 \u0432\u043d\u0438\u0437, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u0442 \u043f\u0440\u0430\u0432\u043e\u0439 \u043d\u0438\u0436\u043d\u0435\u0439 \u043a\u043b\u0435\u0442\u043a\u0438. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043f\u0443\u0442\u0438 \u043e\u0442 \u043b\u0435\u0432\u043e\u0439 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u0434\u043e \u043f\u0440\u0430\u0432\u043e\u0439 \u043d\u0438\u0436\u043d\u0435\u0439.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-10 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438 \u0440\u0430\u0432\u043d\u0430 \\(13\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-10 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u0435 \u043f\u0443\u0442\u0438

\u0428\u0430\u0433 1: \u043f\u043e\u043d\u044f\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\)

\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u0432\u043d\u0438\u0437 \u0438\u043b\u0438 \u0432\u043f\u0440\u0430\u0432\u043e. \u041f\u0443\u0441\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \u0440\u0430\u0432\u043d\u044b \\([i, j]\\) ; \u0442\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u043b\u0435 \u0448\u0430\u0433\u0430 \u0432\u043d\u0438\u0437 \u0438\u043b\u0438 \u0432\u043f\u0440\u0430\u0432\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u044b\u043c\u0438 \\([i+1, j]\\) \u0438\u043b\u0438 \\([i, j+1]\\) . \u0417\u043d\u0430\u0447\u0438\u0442, \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u0434\u0432\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430: \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\([i, j]\\) .

\u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \\([i, j]\\) , \u0442\u0430\u043a\u043e\u0432\u0430: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438 \u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u043a\u043b\u0435\u0442\u043a\u0438 \\([0, 0]\\) \u0434\u043e \u043a\u043b\u0435\u0442\u043a\u0438 \\([i, j]\\) . \u0415\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \\(dp[i, j]\\) .

\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0443\u044e \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \\(dp\\) , \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-11, \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0435\u0442\u043a\u0438 grid .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-11 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp

Note

\u041a\u0430\u043a \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0442\u0430\u043a \u0438 \u0432 backtracking, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0439. \u041e\u043d\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u0443\u044e \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.

\u041a\u0430\u0436\u0434\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0438 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\) ; \u043a\u0430\u0436\u0434\u0430\u044f \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) . \u041f\u043e \u0441\u0443\u0442\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \\(dp\\) - \u044d\u0442\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438.

\u0428\u0430\u0433 2: \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u043d\u0430 \u0435\u0435 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f

\u0414\u043b\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\([i, j]\\) \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0432\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430: \u043a\u043b\u0435\u0442\u043a\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \\([i-1, j]\\) \u0438 \u043a\u043b\u0435\u0442\u043a\u0430 \u0441\u043b\u0435\u0432\u0430 \\([i, j-1]\\) . \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438 \u0434\u043e \\([i, j]\\) \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u0438\u0437 \u0434\u0432\u0443\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 - \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u043f\u0443\u0442\u0438 \u0434\u043e \\([i-1, j]\\) \u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u043f\u0443\u0442\u0438 \u0434\u043e \\([i, j-1]\\) .

\u041f\u043e \u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-12:

\\[ dp[i, j] = \\min(dp[i-1, j], dp[i, j-1]) + grid[i, j] \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-12 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0438 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f

Note

\u041e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u043d\u0430 \u0443\u0436\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\) , \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u0442\u044c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0438 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0438 \u043d\u0430\u0439\u0442\u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u0437 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443.

\u041a\u0430\u043a \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043d\u0430 \u0435\u0435 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.

\u0428\u0430\u0433 3: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432

\u0412 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u043b\u0435\u0432\u0430, \u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0435 - \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u0432\u0435\u0440\u0445\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \\(i = 0\\) \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \\(j = 0\\) \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-13, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u043b\u0435\u0432\u0430 \u0438 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u0432\u0435\u0440\u0445\u0443, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0446\u0438\u043a\u043b\u0430\u043c\u0438: \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u043f\u043e \u0441\u0442\u0440\u043e\u043a\u0430\u043c, \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 - \u043f\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-13 \u00a0 \u0413\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439

Note

\u0412 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) , \u0430 \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 - \u0434\u043b\u044f \u043e\u0431\u0440\u0435\u0437\u043a\u0438.

\u0421\u043c\u044b\u0441\u043b \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a \u043c\u043e\u043c\u0435\u043d\u0442\u0443 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u0441\u0435 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u043e\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043d\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u0442, \u0443\u0436\u0435 \u0431\u044b\u043b\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u044b \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e.

\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043c\u044b \u0443\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 - \u044d\u0442\u043e \u043c\u044b\u0448\u043b\u0435\u043d\u0438\u0435 \"\u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\", \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f \u0431\u043e\u043b\u0435\u0435 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \"\u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \\(\\rightarrow\\) \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \\(\\rightarrow\\) \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\".

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#1-1","title":"1. \u00a0 \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440","text":"

\u041d\u0430\u0447\u0430\u0432 \u0441\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\([i, j]\\) , \u043c\u044b \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u0435\u0433\u043e \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\([i-1, j]\\) \u0438 \\([i, j-1]\\) . \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
def min_path_sum_dfs(grid: list[list[int]], i: int, j: int) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\"\"\"\n    # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 or j < 0:\n        return inf\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    up = min_path_sum_dfs(grid, i - 1, j)\n    left = min_path_sum_dfs(grid, i, j - 1)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return min(left, up) + grid[i][j]\n
min_path_sum.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint minPathSumDFS(vector<vector<int>> &grid, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint minPathSumDFS(int[][] grid, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint MinPathSumDFS(int[][] grid, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    int up = MinPathSumDFS(grid, i - 1, j);\n    int left = MinPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return Math.Min(left, up) + grid[i][j];\n}\n
min_path_sum.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc minPathSumDFS(grid [][]int, i, j int) int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    up := minPathSumDFS(grid, i-1, j)\n    left := minPathSumDFS(grid, i, j-1)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return int(math.Min(float64(left), float64(up))) + grid[i][j]\n}\n
min_path_sum.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc minPathSumDFS(grid: [[Int]], i: Int, j: Int) -> Int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    let up = minPathSumDFS(grid: grid, i: i - 1, j: j)\n    let left = minPathSumDFS(grid: grid, i: i, j: j - 1)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return min(left, up) + grid[i][j]\n}\n
min_path_sum.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction minPathSumDFS(grid, i, j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction minPathSumDFS(\n    grid: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i === 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    const up = minPathSumDFS(grid, i - 1, j);\n    const left = minPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return Math.min(left, up) + grid[i][j];\n}\n
min_path_sum.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint minPathSumDFS(List<List<int>> grid, int i, int j) {\n  // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  if (i < 0 || j < 0) {\n    // \u0412 Dart \u0442\u0438\u043f int \u2014 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0433\u043e \u00ab\u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u044c\u00bb, \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n  int up = minPathSumDFS(grid, i - 1, j);\n  int left = minPathSumDFS(grid, i, j - 1);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  return min(left, up) + grid[i][j];\n}\n
min_path_sum.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfn min_path_sum_dfs(grid: &Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    let up = min_path_sum_dfs(grid, i - 1, j);\n    let left = min_path_sum_dfs(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    std::cmp::min(left, up) + grid[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint minPathSumDFS(int grid[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    int up = minPathSumDFS(grid, i - 1, j);\n    int left = minPathSumDFS(grid, i, j - 1);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n}\n
min_path_sum.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfun minPathSumDFS(grid: Array<IntArray>, i: Int, j: Int): Int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n    val up = minPathSumDFS(grid, i - 1, j)\n    val left = minPathSumDFS(grid, i, j - 1)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    return min(left, up) + grid[i][j]\n}\n
min_path_sum.rb
=begin\nFile: min_path_sum.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[i][j] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  [left, up].min + grid[i][j]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-14 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0441 \u043a\u043e\u0440\u043d\u0435\u043c \u0432 \\(dp[2, 1]\\) ; \u0432 \u043d\u0435\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u0438 \u0438\u0445 \u0447\u0438\u0441\u043b\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0435\u0437\u043a\u043e \u0440\u0430\u0441\u0442\u0438 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u0441\u0435\u0442\u043a\u0438 grid .

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u043f\u0440\u0438\u0447\u0438\u043d\u0430 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0442\u0430\u043a\u043e\u0432\u0430: \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439 \u043e\u0442 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e \u043e\u0434\u043d\u043e\u0439 \u0438 \u0442\u043e\u0439 \u0436\u0435 \u043a\u043b\u0435\u0442\u043a\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-14 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430

\u0423 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0432\u044b\u0431\u043e\u0440\u0430 - \u0432\u043d\u0438\u0437 \u0438 \u0432\u043f\u0440\u0430\u0432\u043e, \u0430 \u043e\u0442 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043d\u0438\u0436\u043d\u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435\u0433\u043e \\(m + n - 2\\) \u0448\u0430\u0433\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(2^{m + n})\\) , \u0433\u0434\u0435 \\(n\\) \u0438 \\(m\\) - \u0447\u0438\u0441\u043b\u043e \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u0435\u0442\u043a\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u0439 \u043e\u0446\u0435\u043d\u043a\u0435 \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u044c \u043a \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u043c \u0441\u0435\u0442\u043a\u0438: \u0443 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0445 \u043a\u043b\u0435\u0442\u043e\u043a \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0432\u044b\u0431\u043e\u0440, \u0442\u0430\u043a \u0447\u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u0443\u0442\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0435\u043d\u044c\u0448\u0435.

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#2-2","title":"2. \u00a0 \u041c\u0435\u0442\u043e\u0434 2: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439","text":"

\u0412\u0432\u0435\u0434\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438 mem \u0442\u043e\u0433\u043e \u0436\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u0447\u0442\u043e \u0438 \u0441\u0435\u0442\u043a\u0430 grid , \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0438 \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
def min_path_sum_dfs_mem(\n    grid: list[list[int]], mem: list[list[int]], i: int, j: int\n) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439\"\"\"\n    # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 and j == 0:\n        return grid[0][0]\n    # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 or j < 0:\n        return inf\n    # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][j] != -1:\n        return mem[i][j]\n    # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n    left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n    # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n
min_path_sum.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint minPathSumDFSMem(vector<vector<int>> &grid, vector<vector<int>> &mem, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = min(left, up) != INT_MAX ? min(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
min_path_sum.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint minPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Integer.MAX_VALUE;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
min_path_sum.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint MinPathSumDFSMem(int[][] grid, int[][] mem, int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return int.MaxValue;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    int up = MinPathSumDFSMem(grid, mem, i - 1, j);\n    int left = MinPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = Math.Min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
min_path_sum.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc minPathSumDFSMem(grid, mem [][]int, i, j int) int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 && j == 0 {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return math.MaxInt\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    up := minPathSumDFSMem(grid, mem, i-1, j)\n    left := minPathSumDFSMem(grid, mem, i, j-1)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = int(math.Min(float64(left), float64(up))) + grid[i][j]\n    return mem[i][j]\n}\n
min_path_sum.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc minPathSumDFSMem(grid: [[Int]], mem: inout [[Int]], i: Int, j: Int) -> Int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0, j == 0 {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return .max\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][j] != -1 {\n        return mem[i][j]\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    let up = minPathSumDFSMem(grid: grid, mem: &mem, i: i - 1, j: j)\n    let left = minPathSumDFSMem(grid: grid, mem: &mem, i: i, j: j - 1)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
min_path_sum.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction minPathSumDFSMem(grid, mem, i, j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] !== -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
min_path_sum.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction minPathSumDFSMem(\n    grid: Array<Array<number>>,\n    mem: Array<Array<number>>,\n    i: number,\n    j: number\n): number {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i === 0 && j === 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Infinity;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    const up = minPathSumDFSMem(grid, mem, i - 1, j);\n    const left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = Math.min(left, up) + grid[i][j];\n    return mem[i][j];\n}\n
min_path_sum.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint minPathSumDFSMem(List<List<int>> grid, List<List<int>> mem, int i, int j) {\n  // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  if (i == 0 && j == 0) {\n    return grid[0][0];\n  }\n  // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  if (i < 0 || j < 0) {\n    // \u0412 Dart \u0442\u0438\u043f int \u2014 \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430; \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0433\u043e \u00ab\u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u044c\u00bb, \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\n    return BigInt.from(2).pow(31).toInt();\n  }\n  // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  if (mem[i][j] != -1) {\n    return mem[i][j];\n  }\n  // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n  int up = minPathSumDFSMem(grid, mem, i - 1, j);\n  int left = minPathSumDFSMem(grid, mem, i, j - 1);\n  // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  mem[i][j] = min(left, up) + grid[i][j];\n  return mem[i][j];\n}\n
min_path_sum.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfn min_path_sum_dfs_mem(grid: &Vec<Vec<i32>>, mem: &mut Vec<Vec<i32>>, i: i32, j: i32) -> i32 {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if i == 0 && j == 0 {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if i < 0 || j < 0 {\n        return i32::MAX;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i as usize][j as usize] != -1 {\n        return mem[i as usize][j as usize];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    let up = min_path_sum_dfs_mem(grid, mem, i - 1, j);\n    let left = min_path_sum_dfs_mem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i as usize][j as usize] = std::cmp::min(left, up) + grid[i as usize][j as usize];\n    mem[i as usize][j as usize]\n}\n
min_path_sum.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint minPathSumDFSMem(int grid[MAX_SIZE][MAX_SIZE], int mem[MAX_SIZE][MAX_SIZE], int i, int j) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0];\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return INT_MAX;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j];\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    int up = minPathSumDFSMem(grid, mem, i - 1, j);\n    int left = minPathSumDFSMem(grid, mem, i, j - 1);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = myMin(left, up) != INT_MAX ? myMin(left, up) + grid[i][j] : INT_MAX;\n    return mem[i][j];\n}\n
min_path_sum.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfun minPathSumDFSMem(\n    grid: Array<IntArray>,\n    mem: Array<IntArray>,\n    i: Int,\n    j: Int\n): Int {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n    if (i == 0 && j == 0) {\n        return grid[0][0]\n    }\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n    if (i < 0 || j < 0) {\n        return Int.MAX_VALUE\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][j] != -1) {\n        return mem[i][j]\n    }\n    // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n    val up = minPathSumDFSMem(grid, mem, i - 1, j)\n    val left = minPathSumDFSMem(grid, mem, i, j - 1)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n    mem[i][j] = min(left, up) + grid[i][j]\n    return mem[i][j]\n}\n
min_path_sum.rb
=begin\nFile: min_path_sum.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[i][j] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  [left, up].min + grid[i][j]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[0][0] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][j] if mem[i][j] != -1\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-15, \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u0449\u0438\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(nm)\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-15 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#3-3","title":"3. \u00a0 \u041c\u0435\u0442\u043e\u0434 3: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u0418\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
def min_path_sum_dp(grid: list[list[int]]) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [[0] * m for _ in range(n)]\n    dp[0][0] = grid[0][0]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in range(1, m):\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in range(1, n):\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in range(1, n):\n        for j in range(1, m):\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n    return dp[n - 1][m - 1]\n
min_path_sum.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minPathSumDP(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<vector<int>> dp(n, vector<int>(m));\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
min_path_sum.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minPathSumDP(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[][] dp = new int[n][m];\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
min_path_sum.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint MinPathSumDP(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[,] dp = new int[n, m];\n    dp[0, 0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j < m; j++) {\n        dp[0, j] = dp[0, j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i < n; i++) {\n        dp[i, 0] = dp[i - 1, 0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i, j] = Math.Min(dp[i, j - 1], dp[i - 1, j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1, m - 1];\n}\n
min_path_sum.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc minPathSumDP(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([][]int, n)\n    for i := 0; i < n; i++ {\n        dp[i] = make([]int, m)\n    }\n    dp[0][0] = grid[0][0]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j := 1; j < m; j++ {\n        dp[0][j] = dp[0][j-1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i := 1; i < n; i++ {\n        dp[i][0] = dp[i-1][0] + grid[i][0]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i := 1; i < n; i++ {\n        for j := 1; j < m; j++ {\n            dp[i][j] = int(math.Min(float64(dp[i][j-1]), float64(dp[i-1][j]))) + grid[i][j]\n        }\n    }\n    return dp[n-1][m-1]\n}\n
min_path_sum.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc minPathSumDP(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: Array(repeating: 0, count: m), count: n)\n    dp[0][0] = grid[0][0]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in 1 ..< m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in 1 ..< n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1 ..< n {\n        for j in 1 ..< m {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
min_path_sum.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction minPathSumDP(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i < n; i++) {\n        for (let j = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
min_path_sum.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction minPathSumDP(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n }, () =>\n        Array.from({ length: m }, () => 0)\n    );\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (let j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i < n; i++) {\n        for (let j: number = 1; j < m; j++) {\n            dp[i][j] = Math.min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    return dp[n - 1][m - 1];\n}\n
min_path_sum.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minPathSumDP(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<List<int>> dp = List.generate(n, (i) => List.filled(m, 0));\n  dp[0][0] = grid[0][0];\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  for (int j = 1; j < m; j++) {\n    dp[0][j] = dp[0][j - 1] + grid[0][j];\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  for (int i = 1; i < n; i++) {\n    dp[i][0] = dp[i - 1][0] + grid[i][0];\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for (int i = 1; i < n; i++) {\n    for (int j = 1; j < m; j++) {\n      dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n    }\n  }\n  return dp[n - 1][m - 1];\n}\n
min_path_sum.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn min_path_sum_dp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![vec![0; m]; n];\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in 1..m {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in 1..n {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1..n {\n        for j in 1..m {\n            dp[i][j] = std::cmp::min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    dp[n - 1][m - 1]\n}\n
min_path_sum.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint minPathSumDP(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int **dp = malloc(n * sizeof(int *));\n    for (int i = 0; i < n; i++) {\n        dp[i] = calloc(m, sizeof(int));\n    }\n    dp[0][0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j < m; j++) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i < n; i++) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i < n; i++) {\n        for (int j = 1; j < m; j++) {\n            dp[i][j] = myMin(dp[i][j - 1], dp[i - 1][j]) + grid[i][j];\n        }\n    }\n    int res = dp[n - 1][m - 1];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i < n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
min_path_sum.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun minPathSumDP(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = Array(n) { IntArray(m) }\n    dp[0][0] = grid[0][0]\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (j in 1..<m) {\n        dp[0][j] = dp[0][j - 1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (i in 1..<n) {\n        dp[i][0] = dp[i - 1][0] + grid[i][0]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (i in 1..<n) {\n        for (j in 1..<m) {\n            dp[i][j] = min(dp[i][j - 1], dp[i - 1][j]) + grid[i][j]\n        }\n    }\n    return dp[n - 1][m - 1]\n}\n
min_path_sum.rb
=begin\nFile: min_path_sum.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[i][j] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  [left, up].min + grid[i][j]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[0][0] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][j] if mem[i][j] != -1\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-16 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u0435 \u043f\u0443\u0442\u0438. \u041e\u043d \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0432\u0441\u0435\u0439 \u0441\u0435\u0442\u043a\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(nm)\\) .

\u0420\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 dp \u0440\u0430\u0432\u0435\u043d \\(n \\times m\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(nm)\\) .

<1><2><3><4><5><6><7><8><9><10><11><12>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-16 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u043f\u0443\u0442\u0438

"},{"location":"chapter_dynamic_programming/dp_solution_pipeline/#4","title":"4. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u0430\u044f \u043a\u043b\u0435\u0442\u043a\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u043b\u0435\u0432\u0430 \u0438 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u0432\u0435\u0440\u0445\u0443, \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\) \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0435.

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0430\u0441\u0441\u0438\u0432 dp \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430, \u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e \u043c\u0435\u0440\u0435 \u043e\u0431\u0445\u043e\u0434\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby min_path_sum.py
def min_path_sum_dp_comp(grid: list[list[int]]) -> int:\n    \"\"\"\u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n, m = len(grid), len(grid[0])\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [0] * m\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0]\n    for j in range(1, m):\n        dp[j] = dp[j - 1] + grid[0][j]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in range(1, n):\n        # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0]\n        # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in range(1, m):\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n    return dp[m - 1]\n
min_path_sum.cpp
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minPathSumDPComp(vector<vector<int>> &grid) {\n    int n = grid.size(), m = grid[0].size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<int> dp(m);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j < m; j++) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
min_path_sum.java
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minPathSumDPComp(int[][] grid) {\n    int n = grid.length, m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[m];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
min_path_sum.cs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint MinPathSumDPComp(int[][] grid) {\n    int n = grid.Length, m = grid[0].Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[m];\n    dp[0] = grid[0][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j < m; j++) {\n            dp[j] = Math.Min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
min_path_sum.go
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc minPathSumDPComp(grid [][]int) int {\n    n, m := len(grid), len(grid[0])\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([]int, m)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0]\n    for j := 1; j < m; j++ {\n        dp[j] = dp[j-1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i := 1; i < n; i++ {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0]\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j := 1; j < m; j++ {\n            dp[j] = int(math.Min(float64(dp[j-1]), float64(dp[j]))) + grid[i][j]\n        }\n    }\n    return dp[m-1]\n}\n
min_path_sum.swift
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc minPathSumDPComp(grid: [[Int]]) -> Int {\n    let n = grid.count\n    let m = grid[0].count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: 0, count: m)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0]\n    for j in 1 ..< m {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in 1 ..< n {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0]\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in 1 ..< m {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
min_path_sum.js
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction minPathSumDPComp(grid) {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = new Array(m);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (let i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
min_path_sum.ts
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction minPathSumDPComp(grid: Array<Array<number>>): number {\n    const n = grid.length,\n        m = grid[0].length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = new Array(m);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for (let j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (let i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (let j = 1; j < m; j++) {\n            dp[j] = Math.min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    return dp[m - 1];\n}\n
min_path_sum.dart
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minPathSumDPComp(List<List<int>> grid) {\n  int n = grid.length, m = grid[0].length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<int> dp = List.filled(m, 0);\n  dp[0] = grid[0][0];\n  for (int j = 1; j < m; j++) {\n    dp[j] = dp[j - 1] + grid[0][j];\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n  for (int i = 1; i < n; i++) {\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    dp[0] = dp[0] + grid[i][0];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int j = 1; j < m; j++) {\n      dp[j] = min(dp[j - 1], dp[j]) + grid[i][j];\n    }\n  }\n  return dp[m - 1];\n}\n
min_path_sum.rs
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn min_path_sum_dp_comp(grid: &Vec<Vec<i32>>) -> i32 {\n    let (n, m) = (grid.len(), grid[0].len());\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![0; m];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for j in 1..m {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in 1..n {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in 1..m {\n            dp[j] = std::cmp::min(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    dp[m - 1]\n}\n
min_path_sum.c
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint minPathSumDPComp(int grid[MAX_SIZE][MAX_SIZE], int n, int m) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int *dp = calloc(m, sizeof(int));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0];\n    for (int j = 1; j < m; j++) {\n        dp[j] = dp[j - 1] + grid[0][j];\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i < n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0];\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j < m; j++) {\n            dp[j] = myMin(dp[j - 1], dp[j]) + grid[i][j];\n        }\n    }\n    int res = dp[m - 1];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
min_path_sum.kt
/* \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun minPathSumDPComp(grid: Array<IntArray>): Int {\n    val n = grid.size\n    val m = grid[0].size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = IntArray(m)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    dp[0] = grid[0][0]\n    for (j in 1..<m) {\n        dp[j] = dp[j - 1] + grid[0][j]\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (i in 1..<n) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        dp[0] = dp[0] + grid[i][0]\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (j in 1..<m) {\n            dp[j] = min(dp[j - 1], dp[j]) + grid[i][j]\n        }\n    }\n    return dp[m - 1]\n}\n
min_path_sum.rb
=begin\nFile: min_path_sum.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef min_path_sum_dfs(grid, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[i][j] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i-1, j) \u0438 (i, j-1)\n  up = min_path_sum_dfs(grid, i - 1, j)\n  left = min_path_sum_dfs(grid, i, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  [left, up].min + grid[i][j]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef min_path_sum_dfs_mem(grid, mem, i, j)\n  # \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u0432\u0435\u0440\u0445\u043d\u044f\u044f \u043b\u0435\u0432\u0430\u044f \u044f\u0447\u0435\u0439\u043a\u0430, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a\n  return grid[0][0] if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u043b\u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c +\u221e\n  return Float::INFINITY if i < 0 || j < 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][j] if mem[i][j] != -1\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0439 \u0438 \u0432\u0435\u0440\u0445\u043d\u0435\u0439 \u044f\u0447\u0435\u0435\u043a\n  up = min_path_sum_dfs_mem(grid, mem, i - 1, j)\n  left = min_path_sum_dfs_mem(grid, mem, i, j - 1)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u0443\u0433\u043b\u0430 \u0434\u043e (i, j)\n  mem[i][j] = [left, up].min + grid[i][j]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef min_path_sum_dp(grid)\n  n, m = grid.length, grid.first.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n) { Array.new(m, 0) }\n  dp[0][0] = grid[0][0]\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  (1...m).each { |j| dp[0][j] = dp[0][j - 1] + grid[0][j] }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...n).each { |i| dp[i][0] = dp[i - 1][0] + grid[i][0] }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...n\n    for j in 1...m\n      dp[i][j] = [dp[i][j - 1], dp[i - 1][j]].min + grid[i][j]\n    end\n  end\n  dp[n -1][m -1]\nend\n\n# ## \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0443\u0442\u0438: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef min_path_sum_dp_comp(grid)\n  n, m = grid.length, grid.first.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(m, 0)\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  dp[0] = grid[0][0]\n  (1...m).each { |j| dp[j] = dp[j - 1] + grid[0][j] }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n  for i in 1...n\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    dp[0] = dp[0] + grid[i][0]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    (1...m).each { |j| dp[j] = [dp[j - 1], dp[j]].min + grid[i][j] }\n  end\n  dp[m - 1]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/edit_distance_problem/","title":"14.6 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0420\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u041b\u0435\u0432\u0435\u043d\u0448\u0442\u0435\u0439\u043d\u0430, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043f\u0440\u0430\u0432\u043e\u043a, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0445 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0441\u0442\u0440\u043e\u043a. \u041e\u0431\u044b\u0447\u043d\u043e \u043e\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u0430 \u0434\u0432\u0443\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0435\u0439 \u0432 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430.

Question

\u0414\u0430\u043d\u044b \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \\(s\\) \u0438 \\(t\\) . \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \\(s\\) \u0432 \\(t\\) .

\u0414\u043b\u044f \u0441\u0442\u0440\u043e\u043a\u0438 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u0442\u0440\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0438 \u0437\u0430\u043c\u0435\u043d\u0430 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0438\u043c\u0432\u043e\u043b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-27, \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f kitten \u0432 sitting \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f 3 \u0448\u0430\u0433\u0430 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f: 2 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0437\u0430\u043c\u0435\u043d\u044b \u0438 1 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438; \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f hello \u0432 algo \u0442\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f 3 \u0448\u0430\u0433\u0430: 2 \u0437\u0430\u043c\u0435\u043d\u044b \u0438 1 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-27 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

\u0417\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u043e\u0434\u0435\u043b\u044c \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439. \u0421\u0442\u0440\u043e\u043a\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0443\u0437\u043b\u0430\u043c \u0434\u0435\u0440\u0435\u0432\u0430, \u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0443\u043d\u0434 \u0440\u0435\u0448\u0435\u043d\u0438\u044f (\u043e\u0434\u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0435\u0431\u0440\u0443 \u0434\u0435\u0440\u0435\u0432\u0430.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-28, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440, \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u0438\u0437 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c hello \u0432 algo \u043c\u043e\u0436\u043d\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0439.

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0446\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 - \u043d\u0430\u0439\u0442\u0438 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u043e\u043c hello \u0438 \u0443\u0437\u043b\u043e\u043c algo .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-28 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#1","title":"1. \u00a0 \u0418\u0434\u0435\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0428\u0430\u0433 1: \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\)

\u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0434 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \\(s\\) .

\u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e, \u0447\u0442\u043e\u0431\u044b \u0432 \u0445\u043e\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u043b\u0441\u044f; \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438. \u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0440\u043e\u043a \\(s\\) \u0438 \\(t\\) \u0440\u0430\u0432\u043d\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\(n\\) \u0438 \\(m\\) ; \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u043e\u043a, \u0442\u043e \u0435\u0441\u0442\u044c \\(s[n-1]\\) \u0438 \\(t[m-1]\\) .

\u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 (\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f), \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043d\u0430\u0434 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \\(s\\) , \u043c\u0435\u043d\u044f\u0435\u0442 \u0442\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0449\u0435 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0432 \u0441\u0442\u0440\u043e\u043a\u0430\u0445 \\(s\\) \u0438 \\(t\\) . \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0438\u043c\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \\(s\\) \u0438 \\(t\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \\([i, j]\\) .

\u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \\([i, j]\\) , \u0442\u0430\u043a\u043e\u0432\u0430: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0435\u0440\u0432\u044b\u0445 \\(i\\) \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \\(s\\) \u0432 \u043f\u0435\u0440\u0432\u044b\u0435 \\(j\\) \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0441\u0442\u0440\u043e\u043a\u0438 \\(t\\).

\u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \\(dp\\) \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\((i+1) \\times (j+1)\\) .

\u0428\u0430\u0433 2: \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u043d\u0430 \u0435\u0435 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \\(dp[i, j]\\) . \u0415\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b - \u044d\u0442\u043e \\(s[i-1]\\) \u0438 \\(t[j-1]\\) . \u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0442\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u044f, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-29.

  1. \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \\(s[i-1]\\) \u0441\u0438\u043c\u0432\u043e\u043b \\(t[j-1]\\) ; \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \\(dp[i, j-1]\\) .
  2. \u0423\u0434\u0430\u043b\u0438\u0442\u044c \\(s[i-1]\\) ; \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \\(dp[i-1, j]\\) .
  3. \u0417\u0430\u043c\u0435\u043d\u0438\u0442\u044c \\(s[i-1]\\) \u043d\u0430 \\(t[j-1]\\) ; \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \\(dp[i-1, j-1]\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-29 \u00a0 \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u044b \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u044d\u0442\u043e\u043c\u0443 \u0430\u043d\u0430\u043b\u0438\u0437\u0443 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0442\u0430\u043a\u043e\u0432\u0430: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \\(dp[i, j]\\) \u0440\u0430\u0432\u043d\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 - \\(dp[i, j-1]\\) , \\(dp[i-1, j]\\) \u0438 \\(dp[i-1, j-1]\\) - \u043f\u043b\u044e\u0441 \u0446\u0435\u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \\(1\\) . \u0417\u043d\u0430\u0447\u0438\u0442, \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

\\[ dp[i, j] = \\min(dp[i, j-1], dp[i-1, j], dp[i-1, j-1]) + 1 \\]

\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \\(s[i-1]\\) \u0438 \\(t[j-1]\\) \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442, \u0442\u043e \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

\\[ dp[i, j] = dp[i-1, j-1] \\]

\u0428\u0430\u0433 3: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432

\u041a\u043e\u0433\u0434\u0430 \u043e\u0431\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u0443\u0441\u0442\u044b, \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u043e \\(0\\) , \u0442\u043e \u0435\u0441\u0442\u044c \\(dp[0, 0] = 0\\) . \u041a\u043e\u0433\u0434\u0430 \u0441\u0442\u0440\u043e\u043a\u0430 \\(s\\) \u043f\u0443\u0441\u0442\u0430, \u0430 \u0441\u0442\u0440\u043e\u043a\u0430 \\(t\\) \u043d\u0435\u043f\u0443\u0441\u0442\u0430, \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \\(t\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0441\u044f \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(dp[0, j] = j\\) . \u041a\u043e\u0433\u0434\u0430 \u0441\u0442\u0440\u043e\u043a\u0430 \\(s\\) \u043d\u0435\u043f\u0443\u0441\u0442\u0430, \u0430 \u0441\u0442\u0440\u043e\u043a\u0430 \\(t\\) \u043f\u0443\u0441\u0442\u0430, \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e \u0434\u043b\u0438\u043d\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \\(s\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(dp[i, 0] = i\\) .

\u0418\u0437 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \\(dp[i, j]\\) \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0441\u043b\u0435\u0432\u0430, \u0441\u0432\u0435\u0440\u0445\u0443 \u0438 \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\) \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
def edit_distance_dp(s: str, t: str) -> int:\n    \"\"\"\u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n, m = len(s), len(t)\n    dp = [[0] * (m + 1) for _ in range(n + 1)]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in range(1, n + 1):\n        dp[i][0] = i\n    for j in range(1, m + 1):\n        dp[0][j] = j\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in range(1, n + 1):\n        for j in range(1, m + 1):\n            if s[i - 1] == t[j - 1]:\n                # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1]\n            else:\n                # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1\n    return dp[n][m]\n
edit_distance.cpp
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint editDistanceDP(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
edit_distance.java
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint editDistanceDP(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[][] dp = new int[n + 1][m + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = Math.min(Math.min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
edit_distance.cs
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint EditDistanceDP(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[,] dp = new int[n + 1, m + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i <= n; i++) {\n        dp[i, 0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0, j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i, j] = dp[i - 1, j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i, j] = Math.Min(Math.Min(dp[i, j - 1], dp[i - 1, j]), dp[i - 1, j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n, m];\n}\n
edit_distance.go
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc editDistanceDP(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, m+1)\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i := 1; i <= n; i++ {\n        dp[i][0] = i\n    }\n    for j := 1; j <= m; j++ {\n        dp[0][j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i := 1; i <= n; i++ {\n        for j := 1; j <= m; j++ {\n            if s[i-1] == t[j-1] {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i-1][j-1]\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = MinInt(MinInt(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
edit_distance.swift
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc editDistanceDP(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: Array(repeating: 0, count: m + 1), count: n + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in 1 ... n {\n        dp[i][0] = i\n    }\n    for j in 1 ... m {\n        dp[0][j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1 ... n {\n        for j in 1 ... m {\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
edit_distance.js
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction editDistanceDP(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () => new Array(m + 1).fill(0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
edit_distance.ts
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction editDistanceDP(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: m + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (let j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i <= n; i++) {\n        for (let j = 1; j <= m; j++) {\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] =\n                    Math.min(dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    return dp[n][m];\n}\n
edit_distance.dart
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint editDistanceDP(String s, String t) {\n  int n = s.length, m = t.length;\n  List<List<int>> dp = List.generate(n + 1, (_) => List.filled(m + 1, 0));\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  for (int i = 1; i <= n; i++) {\n    dp[i][0] = i;\n  }\n  for (int j = 1; j <= m; j++) {\n    dp[0][j] = j;\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for (int i = 1; i <= n; i++) {\n    for (int j = 1; j <= m; j++) {\n      if (s[i - 1] == t[j - 1]) {\n        // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n        dp[i][j] = dp[i - 1][j - 1];\n      } else {\n        // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n        dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n      }\n    }\n  }\n  return dp[n][m];\n}\n
edit_distance.rs
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn edit_distance_dp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![vec![0; m + 1]; n + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for i in 1..=n {\n        dp[i][0] = i as i32;\n    }\n    for j in 1..m {\n        dp[0][j] = j as i32;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1..=n {\n        for j in 1..=m {\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] =\n                    std::cmp::min(std::cmp::min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    dp[n][m]\n}\n
edit_distance.c
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint editDistanceDP(char *s, char *t, int n, int m) {\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(m + 1, sizeof(int));\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int i = 1; i <= n; i++) {\n        dp[i][0] = i;\n    }\n    for (int j = 1; j <= m; j++) {\n        dp[0][j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int j = 1; j <= m; j++) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1];\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = myMin(myMin(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1;\n            }\n        }\n    }\n    int res = dp[n][m];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
edit_distance.kt
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun editDistanceDP(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = Array(n + 1) { IntArray(m + 1) }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (i in 1..n) {\n        dp[i][0] = i\n    }\n    for (j in 1..m) {\n        dp[0][j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (i in 1..n) {\n        for (j in 1..m) {\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[i][j] = dp[i - 1][j - 1]\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[i][j] = min(min(dp[i][j - 1], dp[i - 1][j]), dp[i - 1][j - 1]) + 1\n            }\n        }\n    }\n    return dp[n][m]\n}\n
edit_distance.rb
=begin\nFile: edit_distance.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef edit_distance_dfs(s, t, i, j)\n  # \u0415\u0441\u043b\u0438 s \u0438 t \u043f\u0443\u0441\u0442\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c 0\n  return 0 if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 s \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 t\n  return j if i == 0\n  # \u0415\u0441\u043b\u0438 t \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 s\n  return i if j == 0\n  # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n  return edit_distance_dfs(s, t, i - 1, j - 1) if s[i - 1] == t[j - 1]\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n  insert = edit_distance_dfs(s, t, i, j - 1)\n  delete = edit_distance_dfs(s, t, i - 1, j)\n  replace = edit_distance_dfs(s, t, i - 1, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\n  [insert, delete, replace].min + 1\nend\n\ndef edit_distance_dfs_mem(s, t, mem, i, j)\n  # \u0415\u0441\u043b\u0438 s \u0438 t \u043f\u0443\u0441\u0442\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c 0\n  return 0 if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 s \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 t\n  return j if i == 0\n  # \u0415\u0441\u043b\u0438 t \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 s\n  return i if j == 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n  return mem[i][j] if mem[i][j] != -1\n  # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n  return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1) if s[i - 1] == t[j - 1]\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n  insert = edit_distance_dfs_mem(s, t, mem, i, j - 1)\n  delete = edit_distance_dfs_mem(s, t, mem, i - 1, j)\n  replace = edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\n  mem[i][j] = [insert, delete, replace].min + 1\nend\n\n# ## \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-30, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435: \u0432 \u043e\u0431\u043e\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u044d\u0442\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0439 \u0441\u0435\u0442\u043a\u0438.

<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-30 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

"},{"location":"chapter_dynamic_programming/edit_distance_problem/#3","title":"3. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \\(dp[i,j]\\) \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \\(dp[i-1, j]\\) , \u0441\u043b\u0435\u0432\u0430 \\(dp[i, j-1]\\) \u0438 \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \\(dp[i-1, j-1]\\) , \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434 \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0442\u0435\u0440\u044f\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443, \u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0432\u0430 \\(dp[i, j-1]\\) . \u0417\u043d\u0430\u0447\u0438\u0442, \u043e\u0431\u0430 \u043d\u0430\u0438\u0432\u043d\u044b\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u043f\u0440\u0438\u0433\u043e\u0434\u043d\u044b.

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e leftup \u0434\u043b\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \\(dp[i-1, j-1]\\) ; \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0445\u043d\u0435\u0435 \u0438 \u043b\u0435\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0422\u043e\u0433\u0434\u0430 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435, \u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby edit_distance.py
def edit_distance_dp_comp(s: str, t: str) -> int:\n    \"\"\"\u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n, m = len(s), len(t)\n    dp = [0] * (m + 1)\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in range(1, m + 1):\n        dp[j] = j\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in range(1, n + 1):\n        # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        leftup = dp[0]  # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] += 1\n        # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in range(1, m + 1):\n            temp = dp[j]\n            if s[i - 1] == t[j - 1]:\n                # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup\n            else:\n                # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = min(dp[j - 1], dp[j], leftup) + 1\n            leftup = temp  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n    return dp[m]\n
edit_distance.cpp
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint editDistanceDPComp(string s, string t) {\n    int n = s.length(), m = t.length();\n    vector<int> dp(m + 1, 0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        int leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m];\n}\n
edit_distance.java
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint editDistanceDPComp(String s, String t) {\n    int n = s.length(), m = t.length();\n    int[] dp = new int[m + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        int leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s.charAt(i - 1) == t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = Math.min(Math.min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m];\n}\n
edit_distance.cs
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint EditDistanceDPComp(string s, string t) {\n    int n = s.Length, m = t.Length;\n    int[] dp = new int[m + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        int leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = Math.Min(Math.Min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m];\n}\n
edit_distance.go
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc editDistanceDPComp(s string, t string) int {\n    n := len(s)\n    m := len(t)\n    dp := make([]int, m+1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j := 1; j <= m; j++ {\n        dp[j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i := 1; i <= n; i++ {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        leftUp := dp[0] // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j := 1; j <= m; j++ {\n            temp := dp[j]\n            if s[i-1] == t[j-1] {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftUp\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = MinInt(MinInt(dp[j-1], dp[j]), leftUp) + 1\n            }\n            leftUp = temp // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m]\n}\n
edit_distance.swift
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc editDistanceDPComp(s: String, t: String) -> Int {\n    let n = s.utf8CString.count\n    let m = t.utf8CString.count\n    var dp = Array(repeating: 0, count: m + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in 1 ... m {\n        dp[j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in 1 ... n {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        var leftup = dp[0] // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in 1 ... m {\n            let temp = dp[j]\n            if s.utf8CString[i - 1] == t.utf8CString[j - 1] {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m]\n}\n
edit_distance.js
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction editDistanceDPComp(s, t) {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (let i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        let leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m];\n}\n
edit_distance.ts
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction editDistanceDPComp(s: string, t: string): number {\n    const n = s.length,\n        m = t.length;\n    const dp = new Array(m + 1).fill(0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (let j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (let i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        let leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (let j = 1; j <= m; j++) {\n            const temp = dp[j];\n            if (s.charAt(i - 1) === t.charAt(j - 1)) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = Math.min(dp[j - 1], dp[j], leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m];\n}\n
edit_distance.dart
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint editDistanceDPComp(String s, String t) {\n  int n = s.length, m = t.length;\n  List<int> dp = List.filled(m + 1, 0);\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  for (int j = 1; j <= m; j++) {\n    dp[j] = j;\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n  for (int i = 1; i <= n; i++) {\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    int leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n    dp[0] = i;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int j = 1; j <= m; j++) {\n      int temp = dp[j];\n      if (s[i - 1] == t[j - 1]) {\n        // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n        dp[j] = leftup;\n      } else {\n        // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n        dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1;\n      }\n      leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n    }\n  }\n  return dp[m];\n}\n
edit_distance.rs
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn edit_distance_dp_comp(s: &str, t: &str) -> i32 {\n    let (n, m) = (s.len(), t.len());\n    let mut dp = vec![0; m + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for j in 1..m {\n        dp[j] = j as i32;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for i in 1..=n {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        let mut leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i as i32;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for j in 1..=m {\n            let temp = dp[j];\n            if s.chars().nth(i - 1) == t.chars().nth(j - 1) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = std::cmp::min(std::cmp::min(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    dp[m]\n}\n
edit_distance.c
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint editDistanceDPComp(char *s, char *t, int n, int m) {\n    int *dp = calloc(m + 1, sizeof(int));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (int j = 1; j <= m; j++) {\n        dp[j] = j;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (int i = 1; i <= n; i++) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        int leftup = dp[0]; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i;\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (int j = 1; j <= m; j++) {\n            int temp = dp[j];\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup;\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = myMin(myMin(dp[j - 1], dp[j]), leftup) + 1;\n            }\n            leftup = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    int res = dp[m];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
edit_distance.kt
/* \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun editDistanceDPComp(s: String, t: String): Int {\n    val n = s.length\n    val m = t.length\n    val dp = IntArray(m + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n    for (j in 1..m) {\n        dp[j] = j\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n    for (i in 1..n) {\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n        var leftup = dp[0] // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n        dp[0] = i\n        // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n        for (j in 1..m) {\n            val temp = dp[j]\n            if (s[i - 1] == t[j - 1]) {\n                // \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n                dp[j] = leftup\n            } else {\n                // \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n                dp[j] = min(min(dp[j - 1], dp[j]), leftup) + 1\n            }\n            leftup = temp // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n        }\n    }\n    return dp[m]\n}\n
edit_distance.rb
=begin\nFile: edit_distance.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef edit_distance_dfs(s, t, i, j)\n  # \u0415\u0441\u043b\u0438 s \u0438 t \u043f\u0443\u0441\u0442\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c 0\n  return 0 if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 s \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 t\n  return j if i == 0\n  # \u0415\u0441\u043b\u0438 t \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 s\n  return i if j == 0\n  # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n  return edit_distance_dfs(s, t, i - 1, j - 1) if s[i - 1] == t[j - 1]\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n  insert = edit_distance_dfs(s, t, i, j - 1)\n  delete = edit_distance_dfs(s, t, i - 1, j)\n  replace = edit_distance_dfs(s, t, i - 1, j - 1)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\n  [insert, delete, replace].min + 1\nend\n\ndef edit_distance_dfs_mem(s, t, mem, i, j)\n  # \u0415\u0441\u043b\u0438 s \u0438 t \u043f\u0443\u0441\u0442\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c 0\n  return 0 if i == 0 && j == 0\n  # \u0415\u0441\u043b\u0438 s \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 t\n  return j if i == 0\n  # \u0415\u0441\u043b\u0438 t \u043f\u0443\u0441\u0442\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u0443 s\n  return i if j == 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n  return mem[i][j] if mem[i][j] != -1\n  # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n  return edit_distance_dfs_mem(s, t, mem, i - 1, j - 1) if s[i - 1] == t[j - 1]\n  # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n  insert = edit_distance_dfs_mem(s, t, mem, i, j - 1)\n  delete = edit_distance_dfs_mem(s, t, mem, i - 1, j)\n  replace = edit_distance_dfs_mem(s, t, mem, i - 1, j - 1)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\n  mem[i][j] = [insert, delete, replace].min + 1\nend\n\n# ## \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef edit_distance_dp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(n + 1) { Array.new(m + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...(n + 1)).each { |i| dp[i][0] = i }\n  (1...(m + 1)).each { |j| dp[0][j] = j }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...(n + 1)\n    for j in 1...(m +1)\n      if s[i - 1] == t[j - 1]\n        # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n        dp[i][j] = dp[i - 1][j - 1]\n      else\n        # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n        dp[i][j] = [dp[i][j - 1], dp[i - 1][j], dp[i - 1][j - 1]].min + 1\n      end\n    end\n  end\n  dp[n][m]\nend\n\n# ## \u0420\u0435\u0434\u0430\u043a\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef edit_distance_dp_comp(s, t)\n  n, m = s.length, t.length\n  dp = Array.new(m + 1, 0)\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430\n  (1...(m + 1)).each { |j| dp[j] = j }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438\n  for i in 1...(n + 1)\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    leftup = dp.first # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i-1, j-1]\n    dp[0] += 1\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for j in 1...(m + 1)\n      temp = dp[j]\n      if s[i - 1] == t[j - 1]\n        # \u0415\u0441\u043b\u0438 \u0434\u0432\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0440\u0430\u0432\u043d\u044b, \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0445\n        dp[j] = leftup\n      else\n        # \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f = \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0437\u0430\u043c\u0435\u043d\u044b + 1\n        dp[j] = [dp[j - 1], dp[j], leftup].min + 1\n      end\n      leftup = temp # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f dp[i-1, j-1] \u0434\u043b\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438\n    end\n  end\n  dp[m]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/","title":"14.1 \u00a0 \u041f\u0435\u0440\u0432\u043e\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c","text":"

\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (dynamic programming) - \u044d\u0442\u043e \u0432\u0430\u0436\u043d\u0430\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043f\u0430\u0440\u0430\u0434\u0438\u0433\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0438 \u0437\u0430 \u0441\u0447\u0435\u0442 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0440\u0435\u0437\u043a\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438.

\u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u043d\u0430\u0447\u043d\u0435\u043c \u0441 \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u0433\u0440\u0443\u0431\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 backtracking, \u0443\u0432\u0438\u0434\u0438\u043c \u0432 \u043d\u0435\u043c \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435

\u0414\u0430\u043d\u0430 \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430 \u0438\u0437 \\(n\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439. \u0417\u0430 \u043e\u0434\u0438\u043d \u0448\u0430\u0433 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438. \u0421\u043a\u043e\u043b\u044c\u043a\u0438\u043c\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b?

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-1, \u0434\u043b\u044f \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u044b \u0438\u0437 \\(3\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \\(3\\) \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-1 \u00a0 \u0427\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 3-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c

\u0426\u0435\u043b\u044c \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 - \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0443\u0431\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e backtracking. \u0415\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0442\u043e \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0442 \u0437\u0435\u043c\u043b\u0438 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0440\u044b\u0436\u043e\u043a \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438; \u0432\u0441\u044f\u043a\u0438\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043d\u0430 \\(1\\) , \u0430 \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u043a\u0438\u0432\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u043c \u044d\u0442\u0443 \u0432\u0435\u0442\u0432\u044c. \u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_backtrack.py
def backtrack(choices: list[int], state: int, n: int, res: list[int]) -> int:\n    \"\"\"\u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\"\"\"\n    # \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if state == n:\n        res[0] += 1\n    # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for choice in choices:\n        # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if state + choice > n:\n            continue\n        # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res)\n        # \u041e\u0442\u043a\u0430\u0442\n\ndef climbing_stairs_backtrack(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433\"\"\"\n    choices = [1, 2]  # \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    state = 0  # \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    res = [0]  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res)\n    return res[0]\n
climbing_stairs_backtrack.cpp
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nvoid backtrack(vector<int> &choices, int state, int n, vector<int> &res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state == n)\n        res[0]++;\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (auto &choice : choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n)\n            continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nint climbingStairsBacktrack(int n) {\n    vector<int> choices = {1, 2}; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    int state = 0;                // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    vector<int> res = {0};        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res);\n    return res[0];\n}\n
climbing_stairs_backtrack.java
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nvoid backtrack(List<Integer> choices, int state, int n, List<Integer> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state == n)\n        res.set(0, res.get(0) + 1);\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (Integer choice : choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n)\n            continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nint climbingStairsBacktrack(int n) {\n    List<Integer> choices = Arrays.asList(1, 2); // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    int state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    List<Integer> res = new ArrayList<>();\n    res.add(0); // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
climbing_stairs_backtrack.cs
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nvoid Backtrack(List<int> choices, int state, int n, List<int> res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state == n)\n        res[0]++;\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    foreach (int choice in choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n)\n            continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        Backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nint ClimbingStairsBacktrack(int n) {\n    List<int> choices = [1, 2]; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    int state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    List<int> res = [0]; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    Backtrack(choices, state, n, res);\n    return res[0];\n}\n
climbing_stairs_backtrack.go
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunc backtrack(choices []int, state, n int, res []int) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if state == n {\n        res[0] = res[0] + 1\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for _, choice := range choices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if state+choice > n {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state+choice, n, res)\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunc climbingStairsBacktrack(n int) int {\n    // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    choices := []int{1, 2}\n    // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    state := 0\n    res := make([]int, 1)\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    res[0] = 0\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
climbing_stairs_backtrack.swift
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunc backtrack(choices: [Int], state: Int, n: Int, res: inout [Int]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if state == n {\n        res[0] += 1\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for choice in choices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if state + choice > n {\n            continue\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices: choices, state: state + choice, n: n, res: &res)\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunc climbingStairsBacktrack(n: Int) -> Int {\n    let choices = [1, 2] // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    let state = 0 // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    var res: [Int] = []\n    res.append(0) // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices: choices, state: state, n: n, res: &res)\n    return res[0]\n}\n
climbing_stairs_backtrack.js
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunction backtrack(choices, state, n, res) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (const choice of choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n) continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunction climbingStairsBacktrack(n) {\n    const choices = [1, 2]; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    const state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    const res = new Map();\n    res.set(0, 0); // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
climbing_stairs_backtrack.ts
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunction backtrack(\n    choices: number[],\n    state: number,\n    n: number,\n    res: Map<0, any>\n): void {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state === n) res.set(0, res.get(0) + 1);\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (const choice of choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n) continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfunction climbingStairsBacktrack(n: number): number {\n    const choices = [1, 2]; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    const state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    const res = new Map();\n    res.set(0, 0); // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res);\n    return res.get(0);\n}\n
climbing_stairs_backtrack.dart
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nvoid backtrack(List<int> choices, int state, int n, List<int> res) {\n  // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n  if (state == n) {\n    res[0]++;\n  }\n  // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for (int choice in choices) {\n    // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n    if (state + choice > n) continue;\n    // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    backtrack(choices, state + choice, n, res);\n    // \u041e\u0442\u043a\u0430\u0442\n  }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nint climbingStairsBacktrack(int n) {\n  List<int> choices = [1, 2]; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n  int state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n  List<int> res = [];\n  res.add(0); // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  backtrack(choices, state, n, res);\n  return res[0];\n}\n
climbing_stairs_backtrack.rs
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfn backtrack(choices: &[i32], state: i32, n: i32, res: &mut [i32]) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if state == n {\n        res[0] = res[0] + 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for &choice in choices {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if state + choice > n {\n            continue;\n        }\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfn climbing_stairs_backtrack(n: usize) -> i32 {\n    let choices = vec![1, 2]; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    let state = 0; // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    let mut res = Vec::new();\n    res.push(0); // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(&choices, state, n as i32, &mut res);\n    res[0]\n}\n
climbing_stairs_backtrack.c
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nvoid backtrack(int *choices, int state, int n, int *res, int len) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state == n)\n        res[0]++;\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (int i = 0; i < len; i++) {\n        int choice = choices[i];\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n)\n            continue;\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res, len);\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nint climbingStairsBacktrack(int n) {\n    int choices[2] = {1, 2}; // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    int state = 0;           // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    int *res = (int *)malloc(sizeof(int));\n    *res = 0; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    int len = sizeof(choices) / sizeof(int);\n    backtrack(choices, state, n, res, len);\n    int result = *res;\n    free(res);\n    return result;\n}\n
climbing_stairs_backtrack.kt
/* \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfun backtrack(\n    choices: MutableList<Int>,\n    state: Int,\n    n: Int,\n    res: MutableList<Int>\n) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n    if (state == n)\n        res[0] = res[0] + 1\n    // \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n    for (choice in choices) {\n        // \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n        if (state + choice > n) continue\n        // \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n        backtrack(choices, state + choice, n, res)\n        // \u041e\u0442\u043a\u0430\u0442\n    }\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 */\nfun climbingStairsBacktrack(n: Int): Int {\n    val choices = mutableListOf(1, 2) // \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    val state = 0 // \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n    val res = mutableListOf<Int>()\n    res.add(0) // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    backtrack(choices, state, n, res)\n    return res[0]\n}\n
climbing_stairs_backtrack.rb
=begin\nFile: climbing_stairs_backtrack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 ###\ndef backtrack(choices, state, n, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n  res[0] += 1 if state == n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for choice in choices\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n    next if state + choice > n\n\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    backtrack(choices, state + choice, n, res)\n  end\n  # \u041e\u0442\u043a\u0430\u0442\nend\n\n=begin\nFile: climbing_stairs_backtrack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0411\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 ###\ndef backtrack(choices, state, n, res)\n  # \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u0434\u044a\u0435\u043c \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 n-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0447\u0438\u0441\u043b\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 1\n  res[0] += 1 if state == n\n  # \u041f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430\n  for choice in choices\n    # \u041e\u0442\u0441\u0435\u0447\u0435\u043d\u0438\u0435: \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430 n-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c\n    next if state + choice > n\n\n    # \u041f\u043e\u043f\u044b\u0442\u043a\u0430: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0431\u043e\u0440 \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\n    backtrack(choices, state + choice, n, res)\n  end\n  # \u041e\u0442\u043a\u0430\u0442\nend\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0431\u044d\u043a\u0442\u0440\u0435\u043a\u0438\u043d\u0433 ###\ndef climbing_stairs_backtrack(n)\n  choices = [1, 2] # \u041c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1 \u0438\u043b\u0438 2 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n  state = 0 # \u041d\u0430\u0447\u0430\u0442\u044c \u043f\u043e\u0434\u044a\u0435\u043c \u0441 0-\u0439 \u0441\u0442\u0443\u043f\u0435\u043d\u0438\n  res = [0] # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c res[0] \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0447\u0438\u0441\u043b\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  backtrack(choices, state, n, res)\n  res.first\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1411-1","title":"14.1.1 \u00a0 \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440","text":"

Backtracking \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u044f\u0432\u043d\u043e \u043d\u0430 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438; \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043e\u043d \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u043e\u043f\u044b\u0442\u043a\u0438 \u0438 \u043e\u0431\u0440\u0435\u0437\u043a\u0443 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043e\u0442\u0432\u0435\u0442\u043e\u0432.

\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u0430\u043a \u043d\u0430 \u0440\u0430\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447. \u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\) \u0440\u0430\u0432\u043d\u043e \\(dp[i]\\) ; \u0442\u043e\u0433\u0434\u0430 \\(dp[i]\\) - \u044d\u0442\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0430 \u0435\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442:

\\[ dp[i-1], dp[i-2], \\dots, dp[2], dp[1] \\]

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0437\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0443\u043d\u0434 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \\(1\\) \u0438\u043b\u0438 \u043d\u0430 \\(2\\) \u0441\u0442\u0443\u043f\u0435\u043d\u0438, \u0441\u0442\u043e\u044f \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\) , \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0440\u0430\u0443\u043d\u0434 \u043c\u044b \u043c\u043e\u0433\u043b\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 1\\) \u0438\u043b\u0438 \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 2\\) . \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043d\u0430 \u0441\u0442\u0443\u043f\u0435\u043d\u044c \\(i\\) \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i -1\\) \u0438\u043b\u0438 \u0441\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 2\\) .

\u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u0430\u0436\u043d\u044b\u0439 \u0432\u044b\u0432\u043e\u0434: \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 1\\) \u043f\u043b\u044e\u0441 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i - 2\\) \u0440\u0430\u0432\u043d\u043e \u0447\u0438\u0441\u043b\u0443 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0434\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0434\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\). \u0424\u043e\u0440\u043c\u0443\u043b\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

\\[ dp[i] = dp[i-1] + dp[i-2] \\]

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043f\u043e\u0434\u044a\u0435\u043c\u0435 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u043c\u0435\u0436\u0434\u0443 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c, \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447. \u042d\u0442\u0430 \u0441\u0432\u044f\u0437\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-2.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-2 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u0447\u0438\u0441\u043b\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432

\u041f\u043e \u0440\u0435\u043a\u0443\u0440\u0440\u0435\u043d\u0442\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u0443\u043b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \\(dp[n]\\) , \u043c\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u043b\u0430\u0433\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u0441\u0443\u043c\u043c\u0443 \u0434\u0432\u0443\u0445 \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 , \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0439\u0434\u0435\u043c \u0434\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \\(dp[1]\\) \u0438 \\(dp[2]\\) . \u0418\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b: \\(dp[1] = 1\\) \u0438 \\(dp[2] = 2\\) , \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \\(1\\) \u0438 \\(2\\) \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430 \\(1\\)-\u044e \u0438 \\(2\\)-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u0438.

\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u0442\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434: \u043a\u0430\u043a \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439 backtracking, \u043e\u043d \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043f\u043e\u0438\u0441\u043a\u0443 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443, \u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs.py
def dfs(i: int) -> int:\n    \"\"\"\u041f\u043e\u0438\u0441\u043a\"\"\"\n    # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 or i == 2:\n        return i\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1) + dfs(i - 2)\n    return count\n\ndef climbing_stairs_dfs(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a\"\"\"\n    return dfs(n)\n
climbing_stairs_dfs.cpp
/* \u041f\u043e\u0438\u0441\u043a */\nint dfs(int i) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
climbing_stairs_dfs.java
/* \u041f\u043e\u0438\u0441\u043a */\nint dfs(int i) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
climbing_stairs_dfs.cs
/* \u041f\u043e\u0438\u0441\u043a */\nint DFS(int i) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1) + DFS(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nint ClimbingStairsDFS(int n) {\n    return DFS(n);\n}\n
climbing_stairs_dfs.go
/* \u041f\u043e\u0438\u0441\u043a */\nfunc dfs(i int) int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfs(i-1) + dfs(i-2)\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfunc climbingStairsDFS(n int) int {\n    return dfs(n)\n}\n
climbing_stairs_dfs.swift
/* \u041f\u043e\u0438\u0441\u043a */\nfunc dfs(i: Int) -> Int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1) + dfs(i: i - 2)\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfunc climbingStairsDFS(n: Int) -> Int {\n    dfs(i: n)\n}\n
climbing_stairs_dfs.js
/* \u041f\u043e\u0438\u0441\u043a */\nfunction dfs(i) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfunction climbingStairsDFS(n) {\n    return dfs(n);\n}\n
climbing_stairs_dfs.ts
/* \u041f\u043e\u0438\u0441\u043a */\nfunction dfs(i: number): number {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i === 1 || i === 2) return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfunction climbingStairsDFS(n: number): number {\n    return dfs(n);\n}\n
climbing_stairs_dfs.dart
/* \u041f\u043e\u0438\u0441\u043a */\nint dfs(int i) {\n  // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  if (i == 1 || i == 2) return i;\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1) + dfs(i - 2);\n  return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nint climbingStairsDFS(int n) {\n  return dfs(n);\n}\n
climbing_stairs_dfs.rs
/* \u041f\u043e\u0438\u0441\u043a */\nfn dfs(i: usize) -> i32 {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1) + dfs(i - 2);\n    count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfn climbing_stairs_dfs(n: usize) -> i32 {\n    dfs(n)\n}\n
climbing_stairs_dfs.c
/* \u041f\u043e\u0438\u0441\u043a */\nint dfs(int i) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1) + dfs(i - 2);\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nint climbingStairsDFS(int n) {\n    return dfs(n);\n}\n
climbing_stairs_dfs.kt
/* \u041f\u043e\u0438\u0441\u043a */\nfun dfs(i: Int): Int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2) return i\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1) + dfs(i - 2)\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a */\nfun climbingStairsDFS(n: Int): Int {\n    return dfs(n)\n}\n
climbing_stairs_dfs.rb
=begin\nFile: climbing_stairs_dfs.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0438\u0441\u043a ###\ndef dfs(i)\n  # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n=begin\nFile: climbing_stairs_dfs.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0438\u0441\u043a ###\ndef dfs(i)\n  # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  return i if i == 1 || i == 2\n  # dp[i] = dp[i-1] + dp[i-2]\n  dfs(i - 1) + dfs(i - 2)\nend\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a ###\ndef climbing_stairs_dfs(n)\n  dfs(n)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-3 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0449\u0435\u0435 \u043f\u0440\u0438 \u043f\u043e\u043b\u043d\u043e\u043c \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0435. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \\(dp[n]\\) \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(n\\) , \u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(2^n)\\) . \u042d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u043e\u0441\u0442 \u0432\u0437\u0440\u044b\u0432\u043e\u043e\u0431\u0440\u0430\u0437\u0435\u043d: \u0435\u0441\u043b\u0438 \u043f\u043e\u0434\u0430\u0442\u044c \u043d\u0430 \u0432\u0445\u043e\u0434 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(n\\) , \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u0434\u043e\u043b\u0433\u0438\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-3 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 14-3, \u0442\u043e \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \"\u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438\". \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \\(dp[9]\\) \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \\(dp[8]\\) \u0438 \\(dp[7]\\) , \u0430 \\(dp[8]\\) - \u0432 \\(dp[7]\\) \u0438 \\(dp[6]\\) ; \u043e\u0431\u0435 \u0432\u0435\u0442\u0432\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0443 \\(dp[7]\\) .

\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u0435, \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u044e\u0442 \u0432\u0441\u0435 \u0431\u043e\u043b\u0435\u0435 \u043c\u0435\u043b\u043a\u0438\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0435\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u0435\u0437 \u043a\u043e\u043d\u0446\u0430. \u041f\u043e\u0434\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0443\u0445\u043e\u0434\u0438\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 \u043d\u0438\u0445.

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1412-2","title":"14.1.2 \u00a0 \u041c\u0435\u0442\u043e\u0434 2: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439","text":"

\u0427\u0442\u043e\u0431\u044b \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u043b\u0430\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043e\u0431\u044a\u044f\u0432\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 mem \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0431\u0443\u0434\u0435\u043c \u043e\u0431\u0440\u0435\u0437\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u043e\u0438\u0441\u043a\u0430.

  1. \u041a\u043e\u0433\u0434\u0430 \\(dp[i]\\) \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043f\u0435\u0440\u0432\u044b\u0435, \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0435\u0433\u043e \u0432 mem[i] \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f.
  2. \u041a\u043e\u0433\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(dp[i]\\) \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043d\u043e\u0432\u0430, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0437 mem[i] \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438.

\u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dfs_mem.py
def dfs(i: int, mem: list[int]) -> int:\n    \"\"\"\u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439\"\"\"\n    # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 or i == 2:\n        return i\n    # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if mem[i] != -1:\n        return mem[i]\n    # dp[i] = dp[i-1] + dp[i-2]\n    count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count\n    return count\n\ndef climbing_stairs_dfs_mem(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439\"\"\"\n    # mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    mem = [-1] * (n + 1)\n    return dfs(n, mem)\n
climbing_stairs_dfs_mem.cpp
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint dfs(int i, vector<int> &mem) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    vector<int> mem(n + 1, -1);\n    return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.java
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint dfs(int i, int[] mem) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    int[] mem = new int[n + 1];\n    Arrays.fill(mem, -1);\n    return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.cs
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint DFS(int i, int[] mem) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = DFS(i - 1, mem) + DFS(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint ClimbingStairsDFSMem(int n) {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    int[] mem = new int[n + 1];\n    Array.Fill(mem, -1);\n    return DFS(n, mem);\n}\n
climbing_stairs_dfs_mem.go
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc dfsMem(i int, mem []int) int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    count := dfsMem(i-1, mem) + dfsMem(i-2, mem)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc climbingStairsDFSMem(n int) int {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    mem := make([]int, n+1)\n    for i := range mem {\n        mem[i] = -1\n    }\n    return dfsMem(n, mem)\n}\n
climbing_stairs_dfs_mem.swift
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc dfs(i: Int, mem: inout [Int]) -> Int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if mem[i] != -1 {\n        return mem[i]\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i: i - 1, mem: &mem) + dfs(i: i - 2, mem: &mem)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc climbingStairsDFSMem(n: Int) -> Int {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    var mem = Array(repeating: -1, count: n + 1)\n    return dfs(i: n, mem: &mem)\n}\n
climbing_stairs_dfs_mem.js
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction dfs(i, mem) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i === 1 || i === 2) return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction climbingStairsDFSMem(n) {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.ts
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction dfs(i: number, mem: number[]): number {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i === 1 || i === 2) return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1) return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    const count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction climbingStairsDFSMem(n: number): number {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    const mem = new Array(n + 1).fill(-1);\n    return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.dart
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint dfs(int i, List<int> mem) {\n  // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  if (i == 1 || i == 2) return i;\n  // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n  if (mem[i] != -1) return mem[i];\n  // dp[i] = dp[i-1] + dp[i-2]\n  int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n  // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n  mem[i] = count;\n  return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint climbingStairsDFSMem(int n) {\n  // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n  List<int> mem = List.filled(n + 1, -1);\n  return dfs(n, mem);\n}\n
climbing_stairs_dfs_mem.rs
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfn dfs(i: usize, mem: &mut [i32]) -> i32 {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if i == 1 || i == 2 {\n        return i as i32;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if mem[i] != -1 {\n        return mem[i];\n    }\n    // dp[i] = dp[i-1] + dp[i-2]\n    let count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfn climbing_stairs_dfs_mem(n: usize) -> i32 {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    let mut mem = vec![-1; n + 1];\n    dfs(n, &mut mem)\n}\n
climbing_stairs_dfs_mem.c
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint dfs(int i, int *mem) {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2)\n        return i;\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1)\n        return mem[i];\n    // dp[i] = dp[i-1] + dp[i-2]\n    int count = dfs(i - 1, mem) + dfs(i - 2, mem);\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count;\n    return count;\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint climbingStairsDFSMem(int n) {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    int *mem = (int *)malloc((n + 1) * sizeof(int));\n    for (int i = 0; i <= n; i++) {\n        mem[i] = -1;\n    }\n    int result = dfs(n, mem);\n    free(mem);\n    return result;\n}\n
climbing_stairs_dfs_mem.kt
/* \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfun dfs(i: Int, mem: IntArray): Int {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if (i == 1 || i == 2) return i\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n    if (mem[i] != -1) return mem[i]\n    // dp[i] = dp[i-1] + dp[i-2]\n    val count = dfs(i - 1, mem) + dfs(i - 2, mem)\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n    mem[i] = count\n    return count\n}\n\n/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfun climbingStairsDFSMem(n: Int): Int {\n    // mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n    val mem = IntArray(n + 1)\n    mem.fill(-1)\n    return dfs(n, mem)\n}\n
climbing_stairs_dfs_mem.rb
=begin\nFile: climbing_stairs_dfs_mem.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef dfs(i, mem)\n  # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  return i if i == 1 || i == 2\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n  mem[i] = count\nend\n\n=begin\nFile: climbing_stairs_dfs_mem.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef dfs(i, mem)\n  # dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n  return i if i == 1 || i == 2\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c dp[i] \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0435\n  return mem[i] if mem[i] != -1\n\n  # dp[i] = dp[i-1] + dp[i-2]\n  count = dfs(i - 1, mem) + dfs(i - 2, mem)\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c dp[i]\n  mem[i] = count\nend\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef climbing_stairs_dfs_mem(n)\n  # mem[i] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 i-\u044e \u0441\u0442\u0443\u043f\u0435\u043d\u044c, -1 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438\n  mem = Array.new(n + 1, -1)\n  dfs(n, mem)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-4, \u043f\u043e\u0441\u043b\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0434\u043e \\(O(n)\\) . \u042d\u0442\u043e \u043e\u0433\u0440\u043e\u043c\u043d\u044b\u0439 \u0441\u043a\u0430\u0447\u043e\u043a \u0432 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-4 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1413-3","title":"14.1.3 \u00a0 \u041c\u0435\u0442\u043e\u0434 3: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 - \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \"\u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\" : \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0441 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 (\u043a\u043e\u0440\u043d\u044f), \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0435 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0435\u043c \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0441 \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u043c (\u043b\u0438\u0441\u0442\u044c\u0435\u0432). \u0417\u0430\u0442\u0435\u043c \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

\u041d\u0430\u043f\u0440\u043e\u0442\u0438\u0432, \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 - \u044d\u0442\u043e \u043c\u0435\u0442\u043e\u0434 \"\u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\" : \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u043c\u044b \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u0441\u0442\u0440\u043e\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u044b\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0442\u0432\u0435\u0442 \u043d\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u043d\u0435\u0442 \u044d\u0442\u0430\u043f\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430, \u0434\u043b\u044f \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432, \u0431\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u043d\u0438\u0436\u0435 \u043a\u043e\u0434\u0435 \u043c\u044b \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447; \u043e\u043d \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0442\u0443 \u0436\u0435 \u0440\u043e\u043b\u044c, \u0447\u0442\u043e \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 mem \u0432 \u043c\u0435\u043c\u043e\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
def climbing_stairs_dp(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    if n == 1 or n == 2:\n        return n\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp = [0] * (n + 1)\n    # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1], dp[2] = 1, 2\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in range(3, n + 1):\n        dp[i] = dp[i - 1] + dp[i - 2]\n    return dp[n]\n
climbing_stairs_dp.cpp
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    vector<int> dp(n + 1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
climbing_stairs_dp.java
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[] dp = new int[n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
climbing_stairs_dp.cs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint ClimbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int[] dp = new int[n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
climbing_stairs_dp.go
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc climbingStairsDP(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp := make([]int, n+1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1\n    dp[2] = 2\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i := 3; i <= n; i++ {\n        dp[i] = dp[i-1] + dp[i-2]\n    }\n    return dp[n]\n}\n
climbing_stairs_dp.swift
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc climbingStairsDP(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    var dp = Array(repeating: 0, count: n + 1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1\n    dp[2] = 2\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3 ... n {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
climbing_stairs_dp.js
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction climbingStairsDP(n) {\n    if (n === 1 || n === 2) return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = new Array(n + 1).fill(-1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
climbing_stairs_dp.ts
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction climbingStairsDP(n: number): number {\n    if (n === 1 || n === 2) return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    const dp = new Array(n + 1).fill(-1);\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (let i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    return dp[n];\n}\n
climbing_stairs_dp.dart
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsDP(int n) {\n  if (n == 1 || n == 2) return n;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  List<int> dp = List.filled(n + 1, 0);\n  // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1] = 1;\n  dp[2] = 2;\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  for (int i = 3; i <= n; i++) {\n    dp[i] = dp[i - 1] + dp[i - 2];\n  }\n  return dp[n];\n}\n
climbing_stairs_dp.rs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn climbing_stairs_dp(n: usize) -> i32 {\n    // dp[1] \u0438 dp[2] \u0443\u0436\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u0445\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    let mut dp = vec![-1; n + 1];\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i in 3..=n {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    dp[n]\n}\n
climbing_stairs_dp.c
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint climbingStairsDP(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    int *dp = (int *)malloc((n + 1) * sizeof(int));\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1;\n    dp[2] = 2;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (int i = 3; i <= n; i++) {\n        dp[i] = dp[i - 1] + dp[i - 2];\n    }\n    int result = dp[n];\n    free(dp);\n    return result;\n}\n
climbing_stairs_dp.kt
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun climbingStairsDP(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    val dp = IntArray(n + 1)\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n    dp[1] = 1\n    dp[2] = 2\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for (i in 3..n) {\n        dp[i] = dp[i - 1] + dp[i - 2]\n    }\n    return dp[n]\n}\n
climbing_stairs_dp.rb
=begin\nFile: climbing_stairs_dp.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp = Array.new(n + 1, 0)\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1], dp[2] = 1, 2\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-5 \u0441\u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u0434\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-5 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u044a\u0435\u043c\u0430 \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435

\u041a\u0430\u043a \u0438 \u0432 backtracking, \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \"\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\" \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u0442\u0430\u043f\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438; \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u043d\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0435 \u0438 \u0435\u0435 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0441\u0442\u0443\u043f\u0435\u043d\u0438 \\(i\\) .

\u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u0432\u0435\u0441\u0442\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0442\u0435\u0440\u043c\u0438\u043d\u043e\u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

"},{"location":"chapter_dynamic_programming/intro_to_dynamic_programming/#1414","title":"14.1.4 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0433 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \\(dp[i]\\) \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \\(dp[i-1]\\) \u0438 \\(dp[i-2]\\) , \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u0432\u0435\u0441\u044c \u043c\u0430\u0441\u0441\u0438\u0432 dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043e\u0442\u0432\u0435\u0442\u043e\u0432 \u0432\u0441\u0435\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 ; \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \"\u043f\u0435\u0440\u0435\u043a\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f\" \u0432\u043f\u0435\u0440\u0435\u0434. \u041a\u043e\u0434 \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby climbing_stairs_dp.py
def climbing_stairs_dp_comp(n: int) -> int:\n    \"\"\"\u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    if n == 1 or n == 2:\n        return n\n    a, b = 1, 2\n    for _ in range(3, n + 1):\n        a, b = b, a + b\n    return b\n
climbing_stairs_dp.cpp
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.java
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.cs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint ClimbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.go
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc climbingStairsDPComp(n int) int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    a, b := 1, 2\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n    for i := 3; i <= n; i++ {\n        a, b = b, a+b\n    }\n    return b\n}\n
climbing_stairs_dp.swift
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc climbingStairsDPComp(n: Int) -> Int {\n    if n == 1 || n == 2 {\n        return n\n    }\n    var a = 1\n    var b = 2\n    for _ in 3 ... n {\n        (a, b) = (b, a + b)\n    }\n    return b\n}\n
climbing_stairs_dp.js
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction climbingStairsDPComp(n) {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.ts
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction climbingStairsDPComp(n: number): number {\n    if (n === 1 || n === 2) return n;\n    let a = 1,\n        b = 2;\n    for (let i = 3; i <= n; i++) {\n        const tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.dart
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint climbingStairsDPComp(int n) {\n  if (n == 1 || n == 2) return n;\n  int a = 1, b = 2;\n  for (int i = 3; i <= n; i++) {\n    int tmp = b;\n    b = a + b;\n    a = tmp;\n  }\n  return b;\n}\n
climbing_stairs_dp.rs
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn climbing_stairs_dp_comp(n: usize) -> i32 {\n    if n == 1 || n == 2 {\n        return n as i32;\n    }\n    let (mut a, mut b) = (1, 2);\n    for _ in 3..=n {\n        let tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    b\n}\n
climbing_stairs_dp.c
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint climbingStairsDPComp(int n) {\n    if (n == 1 || n == 2)\n        return n;\n    int a = 1, b = 2;\n    for (int i = 3; i <= n; i++) {\n        int tmp = b;\n        b = a + b;\n        a = tmp;\n    }\n    return b;\n}\n
climbing_stairs_dp.kt
/* \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun climbingStairsDPComp(n: Int): Int {\n    if (n == 1 || n == 2) return n\n    var a = 1\n    var b = 2\n    for (i in 3..n) {\n        val temp = b\n        b += a\n        a = temp\n    }\n    return b\n}\n
climbing_stairs_dp.rb
=begin\nFile: climbing_stairs_dp.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef climbing_stairs_dp(n)\n  return n  if n == 1 || n == 2\n\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp = Array.new(n + 1, 0)\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435: \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\n  dp[1], dp[2] = 1, 2\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u043d\u044c\u0448\u0438\u0435\n  (3...(n + 1)).each { |i| dp[i] = dp[i - 1] + dp[i - 2] }\n\n  dp[n]\nend\n\n# ## \u041f\u043e\u0434\u044a\u0435\u043c \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef climbing_stairs_dp_comp(n)\n  return n if n == 1 || n == 2\n\n  a, b = 1, 2\n  (3...(n + 1)).each { a, b = b, a + b }\n\n  b\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0418\u0437 \u043a\u043e\u0434\u0430 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043e\u0442\u043a\u0430\u0437\u0430 \u043e\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 dp \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \\(O(n)\\) \u0434\u043e \\(O(1)\\) .

\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043b\u0438\u0448\u044c \u043e\u0442 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u0422\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0438 \u0437\u0430 \u0441\u0447\u0435\u0442 \"\u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438\" \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c. \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u0435\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \"\u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u043c\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438\" \u0438\u043b\u0438 \"\u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c\".

"},{"location":"chapter_dynamic_programming/knapsack_problem/","title":"14.4 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1","text":"

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 - \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u0432\u043e\u0434\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u0434\u043d\u0430 \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0444\u043e\u0440\u043c \u0437\u0430\u0434\u0430\u0447 \u044d\u0442\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u0423 \u043d\u0435\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1, \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435, \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u0438 \u0442.\u0434.

\u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0441\u0430\u043c\u044b\u0439 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 - \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1.

Question

\u0414\u0430\u043d\u044b \\(n\\) \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432. \u0412\u0435\u0441 \\(i\\)-\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u0440\u0430\u0432\u0435\u043d \\(wgt[i-1]\\) , \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(val[i-1]\\) . \u0422\u0430\u043a\u0436\u0435 \u0434\u0430\u043d \u0440\u044e\u043a\u0437\u0430\u043a \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \\(cap\\) . \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u043f\u0440\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438.

\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-17, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043d\u043e\u043c\u0435\u0440 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(i\\) \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \\(1\\) , \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0441 \\(0\\) , \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0443 \\(i\\) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0432\u0435\u0441 \\(wgt[i-1]\\) \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \\(val[i-1]\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-17 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1

\u041d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1 \u043c\u043e\u0436\u043d\u043e \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u0430\u043a \u043d\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u0437 \\(n\\) \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f - \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0438\u043b\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u0434\u0430\u0447\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u0439.

\u0426\u0435\u043b\u044c \u0437\u0430\u0434\u0430\u0447\u0438 - \u043d\u0430\u0439\u0442\u0438 \"\u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u044e\u043a\u0437\u0430\u043a\u0430\", \u0437\u043d\u0430\u0447\u0438\u0442, \u0441 \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0428\u0430\u0433 1: \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\)

\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0434\u0432\u0430 \u0441\u043b\u0443\u0447\u0430\u044f: \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u044e\u043a\u0437\u0430\u043a, \u0442\u043e\u0433\u0434\u0430 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f; \u0438\u043b\u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u044e\u043a\u0437\u0430\u043a, \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u0432\u0448\u0430\u044f\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f. \u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f: \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043d\u043e\u043c\u0435\u0440 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(i\\) \u0438 \u0442\u0435\u043a\u0443\u0449\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \\(c\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\([i, c]\\) .

\u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \\([i, c]\\) , \u0442\u0430\u043a\u043e\u0432\u0430: \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043f\u0435\u0440\u0432\u044b\u0435 \\(i\\) \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0438 \u0440\u044e\u043a\u0437\u0430\u043a \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \\(c\\). \u0415\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \\(dp[i, c]\\) .

\u0418\u0441\u043a\u043e\u043c\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \\(dp[n, cap]\\) , \u0437\u043d\u0430\u0447\u0438\u0442, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \\(dp\\) \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\((n+1) \\times (cap+1)\\) .

\u0428\u0430\u0433 2: \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u043d\u0430 \u0435\u0435 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f

\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0443 \\(i\\) , \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0430\u044f \u0441 \u043f\u0435\u0440\u0432\u044b\u043c\u0438 \\(i-1\\) \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\u043c\u0438. \u0417\u0434\u0435\u0441\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0434\u0432\u0430 \u0441\u043b\u0443\u0447\u0430\u044f.

\u042d\u0442\u043e\u0442 \u0430\u043d\u0430\u043b\u0438\u0437 \u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0437\u0430\u0434\u0430\u0447\u0438: \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \\(dp[i, c]\\) \u0440\u0430\u0432\u043d\u0430 \u043b\u0443\u0447\u0448\u0435\u043c\u0443 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 - \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(i\\) \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(i\\). \u041e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i-1, c - wgt[i-1]] + val[i-1]) \\]

\u041d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u0435\u0441 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(wgt[i - 1]\\) \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043e\u0441\u0442\u0430\u0432\u0448\u0443\u044e\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \\(c\\) , \u0442\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0431\u0440\u0430\u0442\u044c.

\u0428\u0430\u0433 3: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432

\u041a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u043d\u0435\u0442 \u0438\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(0\\) ; \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \\(dp[i, 0]\\) \u0438 \u0432\u0441\u044f \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \\(dp[0, c]\\) \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u0443\u043b\u044f\u043c\u0438.

\u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\([i, c]\\) \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \\([i-1, c]\\) \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443 \\([i-1, c-wgt[i-1]]\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u0432\u0443\u043c\u044f \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u0446\u0438\u043a\u043b\u0430\u043c\u0438 \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u0432\u0441\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \\(dp\\) \u0432 \u043f\u0440\u044f\u043c\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.

\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440, \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.

"},{"location":"chapter_dynamic_programming/knapsack_problem/#1-1","title":"1. \u00a0 \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440","text":"

\u041a\u043e\u0434 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
def knapsack_dfs(wgt: list[int], val: list[int], i: int, c: int) -> int:\n    \"\"\"\u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c:\n        return knapsack_dfs(wgt, val, i - 1, c)\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    no = knapsack_dfs(wgt, val, i - 1, c)\n    yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return max(no, yes)\n
knapsack.cpp
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint knapsackDFS(vector<int> &wgt, vector<int> &val, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return max(no, yes);\n}\n
knapsack.java
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint knapsackDFS(int[] wgt, int[] val, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return Math.max(no, yes);\n}\n
knapsack.cs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint KnapsackDFS(int[] weight, int[] val, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (weight[i - 1] > c) {\n        return KnapsackDFS(weight, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = KnapsackDFS(weight, val, i - 1, c);\n    int yes = KnapsackDFS(weight, val, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return Math.Max(no, yes);\n}\n
knapsack.go
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc knapsackDFS(wgt, val []int, i, c int) int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i-1] > c {\n        return knapsackDFS(wgt, val, i-1, c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    no := knapsackDFS(wgt, val, i-1, c)\n    yes := knapsackDFS(wgt, val, i-1, c-wgt[i-1]) + val[i-1]\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return int(math.Max(float64(no), float64(yes)))\n}\n
knapsack.swift
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc knapsackDFS(wgt: [Int], val: [Int], i: Int, c: Int) -> Int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c {\n        return knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    let no = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c)\n    let yes = knapsackDFS(wgt: wgt, val: val, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return max(no, yes)\n}\n
knapsack.js
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction knapsackDFS(wgt, val, i, c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return Math.max(no, yes);\n}\n
knapsack.ts
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction knapsackDFS(\n    wgt: Array<number>,\n    val: Array<number>,\n    i: number,\n    c: number\n): number {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    const no = knapsackDFS(wgt, val, i - 1, c);\n    const yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return Math.max(no, yes);\n}\n
knapsack.dart
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint knapsackDFS(List<int> wgt, List<int> val, int i, int c) {\n  // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  if (wgt[i - 1] > c) {\n    return knapsackDFS(wgt, val, i - 1, c);\n  }\n  // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  int no = knapsackDFS(wgt, val, i - 1, c);\n  int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n  return max(no, yes);\n}\n
knapsack.rs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfn knapsack_dfs(wgt: &[i32], val: &[i32], i: usize, c: usize) -> i32 {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    let no = knapsack_dfs(wgt, val, i - 1, c);\n    let yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    std::cmp::max(no, yes)\n}\n
knapsack.c
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint knapsackDFS(int wgt[], int val[], int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, val, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFS(wgt, val, i - 1, c);\n    int yes = knapsackDFS(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return myMax(no, yes);\n}\n
knapsack.kt
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfun knapsackDFS(\n    wgt: IntArray,\n    _val: IntArray,\n    i: Int,\n    c: Int\n): Int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFS(wgt, _val, i - 1, c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    val no = knapsackDFS(wgt, _val, i - 1, c)\n    val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    return max(no, yes)\n}\n
knapsack.rb
=begin\nFile: knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n  [no, yes].max\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-18, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0434\u0432\u0435 \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 - \"\u043d\u0435 \u0431\u0440\u0430\u0442\u044c\" \u0438 \"\u0431\u0440\u0430\u0442\u044c\", \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(2^n)\\) .

\u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0432 \u043d\u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438, \u043b\u0435\u0433\u043a\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \\(dp[1, 10]\\) \u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0445. \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0440\u0430\u0441\u0442\u0435\u0442, \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u0432\u0435\u043b\u0438\u043a\u0430, \u0430 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u043a\u043e\u0433\u0434\u0430 \u043c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0432\u0435\u0441\u043e\u043c, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0438\u0445\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 \u0431\u044b\u0441\u0442\u0440\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-18 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1

"},{"location":"chapter_dynamic_programming/knapsack_problem/#2-2","title":"2. \u00a0 \u041c\u0435\u0442\u043e\u0434 2: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439","text":"

\u0427\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u0430\u044f \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f\u0441\u044f \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u043b\u0430\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0430\u043c\u044f\u0442\u0438 mem \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447, \u0433\u0434\u0435 mem[i][c] \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \\(dp[i, c]\\) .

\u041f\u043e\u0441\u043b\u0435 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e\u043c \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447 , \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\times cap)\\) . \u041a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
def knapsack_dfs_mem(\n    wgt: list[int], val: list[int], mem: list[list[int]], i: int, c: int\n) -> int:\n    \"\"\"\u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 or c == 0:\n        return 0\n    # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][c] != -1:\n        return mem[i][c]\n    # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c:\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n    yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n    # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n
knapsack.cpp
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint knapsackDFSMem(vector<int> &wgt, vector<int> &val, vector<vector<int>> &mem, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = max(no, yes);\n    return mem[i][c];\n}\n
knapsack.java
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint knapsackDFSMem(int[] wgt, int[] val, int[][] mem, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
knapsack.cs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint KnapsackDFSMem(int[] weight, int[] val, int[][] mem, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (weight[i - 1] > c) {\n        return KnapsackDFSMem(weight, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = KnapsackDFSMem(weight, val, mem, i - 1, c);\n    int yes = KnapsackDFSMem(weight, val, mem, i - 1, c - weight[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = Math.Max(no, yes);\n    return mem[i][c];\n}\n
knapsack.go
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc knapsackDFSMem(wgt, val []int, mem [][]int, i, c int) int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i-1] > c {\n        return knapsackDFSMem(wgt, val, mem, i-1, c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    no := knapsackDFSMem(wgt, val, mem, i-1, c)\n    yes := knapsackDFSMem(wgt, val, mem, i-1, c-wgt[i-1]) + val[i-1]\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n    mem[i][c] = int(math.Max(float64(no), float64(yes)))\n    return mem[i][c]\n}\n
knapsack.swift
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunc knapsackDFSMem(wgt: [Int], val: [Int], mem: inout [[Int]], i: Int, c: Int) -> Int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][c] != -1 {\n        return mem[i][c]\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c {\n        return knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    let no = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c)\n    let yes = knapsackDFSMem(wgt: wgt, val: val, mem: &mem, i: i - 1, c: c - wgt[i - 1]) + val[i - 1]\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
knapsack.js
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction knapsackDFSMem(wgt, val, mem, i, c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
knapsack.ts
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfunction knapsackDFSMem(\n    wgt: Array<number>,\n    val: Array<number>,\n    mem: Array<Array<number>>,\n    i: number,\n    c: number\n): number {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i === 0 || c === 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] !== -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    const no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n    const yes =\n        knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = Math.max(no, yes);\n    return mem[i][c];\n}\n
knapsack.dart
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint knapsackDFSMem(\n  List<int> wgt,\n  List<int> val,\n  List<List<int>> mem,\n  int i,\n  int c,\n) {\n  // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  if (i == 0 || c == 0) {\n    return 0;\n  }\n  // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  if (mem[i][c] != -1) {\n    return mem[i][c];\n  }\n  // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  if (wgt[i - 1] > c) {\n    return knapsackDFSMem(wgt, val, mem, i - 1, c);\n  }\n  // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  int no = knapsackDFSMem(wgt, val, mem, i - 1, c);\n  int yes = knapsackDFSMem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n  // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  mem[i][c] = max(no, yes);\n  return mem[i][c];\n}\n
knapsack.rs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfn knapsack_dfs_mem(wgt: &[i32], val: &[i32], mem: &mut Vec<Vec<i32>>, i: usize, c: usize) -> i32 {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if i == 0 || c == 0 {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if mem[i][c] != -1 {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if wgt[i - 1] > c as i32 {\n        return knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    let no = knapsack_dfs_mem(wgt, val, mem, i - 1, c);\n    let yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1] as usize) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = std::cmp::max(no, yes);\n    mem[i][c]\n}\n
knapsack.c
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nint knapsackDFSMem(int wgt[], int val[], int memCols, int **mem, int i, int c) {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0;\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] != -1) {\n        return mem[i][c];\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    int no = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c);\n    int yes = knapsackDFSMem(wgt, val, memCols, mem, i - 1, c - wgt[i - 1]) + val[i - 1];\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = myMax(no, yes);\n    return mem[i][c];\n}\n
knapsack.kt
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 */\nfun knapsackDFSMem(\n    wgt: IntArray,\n    _val: IntArray,\n    mem: Array<IntArray>,\n    i: Int,\n    c: Int\n): Int {\n    // \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n    if (i == 0 || c == 0) {\n        return 0\n    }\n    // \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    if (mem[i][c] != -1) {\n        return mem[i][c]\n    }\n    // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n    if (wgt[i - 1] > c) {\n        return knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n    val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)\n    val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]\n    // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n    mem[i][c] = max(no, yes)\n    return mem[i][c]\n}\n
knapsack.rb
=begin\nFile: knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n  [no, yes].max\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][c] if mem[i][c] != -1\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  mem[i][c] = [no, yes].max\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-19 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0432\u0435\u0442\u0432\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b\u043b\u0438 \u043e\u0442\u0441\u0435\u0447\u0435\u043d\u044b \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-19 \u00a0 \u0414\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1

"},{"location":"chapter_dynamic_programming/knapsack_problem/#3-3","title":"3. \u00a0 \u041c\u0435\u0442\u043e\u0434 3: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0437\u0434\u0435\u0441\u044c - \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430\u043c\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
def knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n = len(wgt)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
knapsack.cpp
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint knapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
knapsack.java
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint knapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
knapsack.cs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint KnapsackDP(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (weight[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i, c] = Math.Max(dp[i - 1, c - weight[i - 1]] + val[i - 1], dp[i - 1, c]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
knapsack.go
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc knapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i-1][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
knapsack.swift
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc knapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
knapsack.js
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction knapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array(n + 1)\n        .fill(0)\n        .map(() => Array(cap + 1).fill(0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
knapsack.ts
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction knapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
knapsack.dart
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint knapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
knapsack.rs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = std::cmp::max(\n                    dp[i - 1][c],\n                    dp[i - 1][c - wgt[i - 1] as usize] + val[i - 1],\n                );\n            }\n        }\n    }\n    dp[n][cap]\n}\n
knapsack.c
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint knapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
knapsack.kt
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun knapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
knapsack.rb
=begin\nFile: knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n  [no, yes].max\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][c] if mem[i][c] != -1\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  mem[i][c] = [no, yes].max\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-20, \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430 dp , \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u044b \\(O(n \\times cap)\\) .

<1><2><3><4><5><6><7><8><9><10><11><12><13><14>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-20 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1

"},{"location":"chapter_dynamic_programming/knapsack_problem/#4","title":"4. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \"\u043f\u0435\u0440\u0435\u043a\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f\" \u0432\u043f\u0435\u0440\u0435\u0434, \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \\(O(n^2)\\) \u0434\u043e \\(O(n)\\) .

\u0415\u0441\u043b\u0438 \u043f\u043e\u0439\u0442\u0438 \u0434\u0430\u043b\u044c\u0448\u0435, \u043c\u043e\u0436\u043d\u043e \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c: \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043c\u0430\u0441\u0441\u0438\u0432? \u041d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043a\u043b\u0435\u0442\u043a\u0438 \u043f\u0440\u044f\u043c\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0438 \u043a\u043b\u0435\u0442\u043a\u0438 \u0441\u043b\u0435\u0432\u0430 \u0441\u0432\u0435\u0440\u0445\u0443. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u043c\u0430\u0441\u0441\u0438\u0432, \u0438 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 \u0441\u0442\u0440\u043e\u043a\u0438 \\(i\\) \u043e\u043d \u0435\u0449\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u0438 \\(i-1\\) .

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-21 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043e\u0442 \u0441\u0442\u0440\u043e\u043a\u0438 \\(i = 1\\) \u043a \u0441\u0442\u0440\u043e\u043a\u0435 \\(i = 2\\) \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0441 \u0440\u0430\u0437\u043d\u0438\u0446\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u043f\u0440\u044f\u043c\u044b\u043c \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u043e\u0431\u0445\u043e\u0434\u043e\u043c.

<1><2><3><4><5><6>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-21 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043b\u044f \u0440\u044e\u043a\u0437\u0430\u043a\u0430 0-1

\u0412 \u043a\u043e\u0434\u0435 \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 dp , \u0430 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby knapsack.py
def knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n = len(wgt)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [0] * (cap + 1)\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        # \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for c in range(cap, 0, -1):\n            if wgt[i - 1] > c:\n                # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c]\n            else:\n                # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
knapsack.cpp
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint knapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<int> dp(cap + 1, 0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
knapsack.java
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint knapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
knapsack.cs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint KnapsackDPComp(int[] weight, int[] val, int cap) {\n    int n = weight.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (int c = cap; c > 0; c--) {\n            if (weight[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.Max(dp[c], dp[c - weight[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
knapsack.go
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc knapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([]int, cap+1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for c := cap; c >= 1; c-- {\n            if wgt[i-1] <= c {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
knapsack.swift
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc knapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for c in (1 ... cap).reversed() {\n            if wgt[i - 1] <= c {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
knapsack.js
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction knapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array(cap + 1).fill(0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
knapsack.ts
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction knapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array(cap + 1).fill(0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (let c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
knapsack.dart
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint knapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n    for (int c = cap; c >= 1; c--) {\n      if (wgt[i - 1] <= c) {\n        // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
knapsack.rs
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![0; cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for c in (1..=cap).rev() {\n            if wgt[i - 1] <= c as i32 {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
knapsack.c
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint knapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (int c = cap; c >= 1; c--) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
knapsack.kt
/* \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun knapsackDPComp(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = IntArray(cap + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        // \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n        for (c in cap downTo 1) {\n            if (wgt[i - 1] <= c) {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
knapsack.rb
=begin\nFile: knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef knapsack_dfs(wgt, val, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs(wgt, val, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs(wgt, val, i - 1, c)\n  yes = knapsack_dfs(wgt, val, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445\n  [no, yes].max\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u043f\u043e\u0438\u0441\u043a \u0441 \u043c\u0435\u043c\u043e\u0438\u0437\u0430\u0446\u0438\u0435\u0439 ###\ndef knapsack_dfs_mem(wgt, val, mem, i, c)\n  # \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0438\u043b\u0438 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043c\u0435\u0441\u0442\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c 0\n  return 0 if i == 0 || c == 0\n  # \u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0443\u0436\u0435 \u0435\u0441\u0442\u044c, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return mem[i][c] if mem[i][c] != -1\n  # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u043a\u043b\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\n  return knapsack_dfs_mem(wgt, val, mem, i - 1, c) if wgt[i - 1] > c\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u043a\u043b\u0430\u0434\u0443\u0442 \u0438 \u043a\u043b\u0430\u0434\u0443\u0442\n  no = knapsack_dfs_mem(wgt, val, mem, i - 1, c)\n  yes = knapsack_dfs_mem(wgt, val, mem, i - 1, c - wgt[i - 1]) + val[i - 1]\n  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\u044e \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439\n  mem[i][c] = [no, yes].max\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = [dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n\n# ## \u0420\u044e\u043a\u0437\u0430\u043a 0-1: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(cap + 1, 0)\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    # \u041e\u0431\u0445\u043e\u0434 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435\n    for c in cap.downto(1)\n      if wgt[i - 1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[c] = dp[c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/summary/","title":"14.7 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_dynamic_programming/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":"

\u0417\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0440\u0435\u0434\u0430\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/","title":"14.5 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435","text":"

\u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0435\u0448\u0438\u043c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 - \u043f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a, \u0430 \u0437\u0430\u0442\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043e\u0434\u043d\u0443 \u0438\u0437 \u0435\u0433\u043e \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0444\u043e\u0440\u043c: \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442.

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1451","title":"14.5.1 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435","text":"

Question

\u0414\u0430\u043d\u044b \\(n\\) \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432. \u0412\u0435\u0441 \\(i\\)-\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u0440\u0430\u0432\u0435\u043d \\(wgt[i-1]\\) , \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(val[i-1]\\) . \u0422\u0430\u043a\u0436\u0435 \u0434\u0430\u043d \u0440\u044e\u043a\u0437\u0430\u043a \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \\(cap\\) . \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u043f\u0440\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-22.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-22 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1","title":"1. \u00a0 \u0418\u0434\u0435\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1; \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u043d\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e.

\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\([i, c]\\) \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u0432\u0443\u043c\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438.

\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0438\u0434:

\\[ dp[i, c] = \\max(dp[i-1, c], dp[i, c - wgt[i-1]] + val[i-1]) \\]"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0415\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043a\u043e\u0434 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441 \u043a\u043e\u0434\u043e\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1, \u0442\u043e \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0434\u0435\u0442\u0430\u043b\u044c: \u0432\u043c\u0435\u0441\u0442\u043e \\(i-1\\) \u043f\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \\(i\\) ; \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
def unbounded_knapsack_dp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n = len(wgt)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [[0] * (cap + 1) for _ in range(n + 1)]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            else:\n                # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n    return dp[n][cap]\n
unbounded_knapsack.cpp
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint unboundedKnapsackDP(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<vector<int>> dp(n + 1, vector<int>(cap + 1, 0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
unbounded_knapsack.java
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint unboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[][] dp = new int[n + 1][cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
unbounded_knapsack.cs
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[,] dp = new int[n + 1, cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i, c] = dp[i - 1, c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n, cap];\n}\n
unbounded_knapsack.go
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc unboundedKnapsackDP(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, cap+1)\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i-1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = int(math.Max(float64(dp[i-1][c]), float64(dp[i][c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
unbounded_knapsack.swift
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc unboundedKnapsackDP(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: Array(repeating: 0, count: cap + 1), count: n + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
unbounded_knapsack.js
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction unboundedKnapsackDP(wgt, val, cap) {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
unbounded_knapsack.ts
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction unboundedKnapsackDP(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: cap + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = Math.max(\n                    dp[i - 1][c],\n                    dp[i][c - wgt[i - 1]] + val[i - 1]\n                );\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
unbounded_knapsack.dart
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint unboundedKnapsackDP(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(cap + 1, 0));\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c];\n      } else {\n        // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[n][cap];\n}\n
unbounded_knapsack.rs
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn unbounded_knapsack_dp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![vec![0; cap + 1]; n + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = std::cmp::max(dp[i - 1][c], dp[i][c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    return dp[n][cap];\n}\n
unbounded_knapsack.c
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint unboundedKnapsackDP(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(cap + 1, sizeof(int));\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = myMax(dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[n][cap];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    return res;\n}\n
unbounded_knapsack.kt
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {\n    val n = wgt.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = Array(n + 1) { IntArray(cap + 1) }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][c] = dp[i - 1][c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[n][cap]\n}\n
unbounded_knapsack.rb
=begin\nFile: unbounded_knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3","title":"3. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u043b\u0435\u0432\u0430 \u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0441\u0432\u0435\u0440\u0445\u0443, \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e.

\u042d\u0442\u043e\u0442 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430 \u043a\u0430\u043a \u0440\u0430\u0437 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1. \u0420\u0430\u0437\u043d\u0438\u0446\u0443 \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c \u043f\u043e \u0440\u0438\u0441\u0443\u043d\u043a\u0443 \u043d\u0438\u0436\u0435.

<1><2><3><4><5><6>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-23 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0430

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0437\u0434\u0435\u0441\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442: \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0431\u0440\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 dp :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby unbounded_knapsack.py
def unbounded_knapsack_dp_comp(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n = len(wgt)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [0] * (cap + 1)\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n        for c in range(1, cap + 1):\n            if wgt[i - 1] > c:\n                # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c]\n            else:\n                # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n    return dp[cap]\n
unbounded_knapsack.cpp
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint unboundedKnapsackDPComp(vector<int> &wgt, vector<int> &val, int cap) {\n    int n = wgt.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<int> dp(cap + 1, 0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
unbounded_knapsack.java
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint unboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
unbounded_knapsack.cs
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {\n    int n = wgt.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
unbounded_knapsack.go
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc unboundedKnapsackDPComp(wgt, val []int, cap int) int {\n    n := len(wgt)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([]int, cap+1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        for c := 1; c <= cap; c++ {\n            if wgt[i-1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = int(math.Max(float64(dp[c]), float64(dp[c-wgt[i-1]]+val[i-1])))\n            }\n        }\n    }\n    return dp[cap]\n}\n
unbounded_knapsack.swift
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc unboundedKnapsackDPComp(wgt: [Int], val: [Int], cap: Int) -> Int {\n    let n = wgt.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: 0, count: cap + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for c in 1 ... cap {\n            if wgt[i - 1] > c {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
unbounded_knapsack.js
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction unboundedKnapsackDPComp(wgt, val, cap) {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
unbounded_knapsack.ts
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction unboundedKnapsackDPComp(\n    wgt: Array<number>,\n    val: Array<number>,\n    cap: number\n): number {\n    const n = wgt.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: cap + 1 }, () => 0);\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = Math.max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    return dp[cap];\n}\n
unbounded_knapsack.dart
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint unboundedKnapsackDPComp(List<int> wgt, List<int> val, int cap) {\n  int n = wgt.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<int> dp = List.filled(cap + 1, 0);\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int c = 1; c <= cap; c++) {\n      if (wgt[i - 1] > c) {\n        // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[c] = dp[c];\n      } else {\n        // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[c] = max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n      }\n    }\n  }\n  return dp[cap];\n}\n
unbounded_knapsack.rs
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn unbounded_knapsack_dp_comp(wgt: &[i32], val: &[i32], cap: usize) -> i32 {\n    let n = wgt.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![0; cap + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for c in 1..=cap {\n            if wgt[i - 1] > c as i32 {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = std::cmp::max(dp[c], dp[c - wgt[i - 1] as usize] + val[i - 1]);\n            }\n        }\n    }\n    dp[cap]\n}\n
unbounded_knapsack.c
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint unboundedKnapsackDPComp(int wgt[], int val[], int cap, int wgtSize) {\n    int n = wgtSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int *dp = calloc(cap + 1, sizeof(int));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int c = 1; c <= cap; c++) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c];\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = myMax(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);\n            }\n        }\n    }\n    int res = dp[cap];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
unbounded_knapsack.kt
/* \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun unboundedKnapsackDPComp(\n    wgt: IntArray,\n    _val: IntArray,\n    cap: Int\n): Int {\n    val n = wgt.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = IntArray(cap + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (c in 1..cap) {\n            if (wgt[i - 1] > c) {\n                // \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[c] = dp[c]\n            } else {\n                // \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n                dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])\n            }\n        }\n    }\n    return dp[cap]\n}\n
unbounded_knapsack.rb
=begin\nFile: unbounded_knapsack.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef unbounded_knapsack_dp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(cap + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for c in 1...(cap + 1)\n      if wgt[i - 1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][c] = dp[i - 1][c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[i][c] = [dp[i - 1][c], dp[i][c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[n][cap]\nend\n\n# ## \u041f\u043e\u043b\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ##3\ndef unbounded_knapsack_dp_comp(wgt, val, cap)\n  n = wgt.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(cap + 1, 0)\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n    for c in 1...(cap + 1)\n      if wgt[i -1] > c\n        # \u0415\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[c] = dp[c]\n      else\n        # \u0411\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 i\n        dp[c] = [dp[c], dp[c - wgt[i - 1]] + val[i - 1]].max\n      end\n    end\n  end\n  dp[cap]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1452","title":"14.5.2 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442","text":"

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0446\u0435\u043b\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0437\u0430\u0434\u0430\u0447 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432, \u0438 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0442\u0430\u043a\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u0439 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442.

Question

\u0414\u0430\u043d\u044b \\(n\\) \u0432\u0438\u0434\u043e\u0432 \u043c\u043e\u043d\u0435\u0442, \u043d\u043e\u043c\u0438\u043d\u0430\u043b \u043c\u043e\u043d\u0435\u0442\u044b \\(i\\) \u0440\u0430\u0432\u0435\u043d \\(coins[i - 1]\\) , \u0430 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0440\u0430\u0432\u043d\u0430 \\(amt\\) . \u041c\u043e\u043d\u0435\u0442\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e. \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u043d\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0443\u043c\u043c\u0443. \u0415\u0441\u043b\u0438 \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0441\u0443\u043c\u043c\u0443 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \\(-1\\) . \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-24.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-24 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_1","title":"1. \u00a0 \u0418\u0434\u0435\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0417\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 ; \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f.

\u0428\u0430\u0433 1: \u043f\u0440\u043e\u0434\u0443\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \\(dp\\)

\u041f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044e \\([i, a]\\) , \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u043d\u0435\u0442 \u0438\u0437 \u043f\u0435\u0440\u0432\u044b\u0445 \\(i\\) \u0432\u0438\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0441\u0443\u043c\u043c\u0443 \\(a\\). \u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(dp[i, a]\\) .

\u0420\u0430\u0437\u043c\u0435\u0440 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(dp\\) \u0440\u0430\u0432\u0435\u043d \\((n+1) \\times (amt+1)\\) .

\u0428\u0430\u0433 2: \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0438 \u043d\u0430 \u0435\u0435 \u043e\u0441\u043d\u043e\u0432\u0435 \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u0437\u0434\u0435\u0441\u044c \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043e\u0442\u043b\u0438\u0447\u0438\u044f \u0432 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.

\\[ dp[i, a] = \\min(dp[i-1, a], dp[i, a - coins[i-1]] + 1) \\]

\u0428\u0430\u0433 3: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432

\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u043d\u0435\u0442 \u0434\u043b\u044f \u0435\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u0440\u0430\u0432\u043d\u043e \\(0\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \\(dp[i, 0]\\) \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0443\u043b\u044f\u043c\u0438.

\u041a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u0435\u0442 \u043d\u0435\u0442, \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0443\u044e \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \\(> 0\\) ; \u044d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u0427\u0442\u043e\u0431\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u044f \\(\\min()\\) \u0432 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u044c \u0438 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u043d\u0435\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0443\u0434\u043e\u0431\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \\(+ \\infty\\) ; \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0441\u044e \u043f\u0435\u0440\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \\(dp[0, a]\\) \u043d\u0443\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \\(+ \\infty\\) .

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_1","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"

\u0411\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0433\u043e\u0442\u043e\u0432\u0443\u044e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \\(+ \\infty\\) \u0434\u043b\u044f \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0435 \u043d\u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0442\u0438\u043f\u0430 int . \u041d\u043e \u0442\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0440\u0438\u0441\u043a \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f: \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \\(+ 1\\) \u0432 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0447\u0438\u0441\u043b\u043e.

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0447\u0438\u0441\u043b\u043e \\(amt + 1\\) \u043a\u0430\u043a \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0434\u043b\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u0441\u0443\u043c\u043c\u044b \\(amt\\) \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0435\u043c \\(amt\\) \u043c\u043e\u043d\u0435\u0442. \u041f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0440\u0430\u0432\u043d\u043e \u043b\u0438 \\(dp[n, amt]\\) \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \\(amt + 1\\) ; \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \\(-1\\) , \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0443\u043c\u043c\u0443. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
def coin_change_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for a in range(1, amt + 1):\n        dp[0][a] = MAX\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n    return dp[n][amt] if dp[n][amt] != MAX else -1\n
coin_change.cpp
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.java
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeDP(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.cs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint CoinChangeDP(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int a = 1; a <= amt; a++) {\n        dp[0, a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i, a] = Math.Min(dp[i - 1, a], dp[i, a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n, amt] != MAX ? dp[n, amt] : -1;\n}\n
coin_change.go
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc coinChangeDP(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for a := 1; a <= amt; a++ {\n        dp[0][a] = max\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = int(math.Min(float64(dp[i-1][a]), float64(dp[i][a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt]\n    }\n    return -1\n}\n
coin_change.swift
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc coinChangeDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for a in 1 ... amt {\n        dp[0][a] = MAX\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[n][amt] != MAX ? dp[n][amt] : -1\n}\n
coin_change.js
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction coinChangeDP(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.ts
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction coinChangeDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (let a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = Math.min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[n][amt] !== MAX ? dp[n][amt] : -1;\n}\n
coin_change.dart
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeDP(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  for (int a = 1; a <= amt; a++) {\n    dp[0][a] = MAX;\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[n][amt] != MAX ? dp[n][amt] : -1;\n}\n
coin_change.rs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn coin_change_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for a in 1..=amt {\n        dp[0][a] = max;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = std::cmp::min(dp[i - 1][a], dp[i][a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[n][amt] != max {\n        return dp[n][amt] as i32;\n    } else {\n        -1\n    }\n}\n
coin_change.c
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (int a = 1; a <= amt; a++) {\n        dp[0][a] = MAX;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = myMin(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[n][amt] != MAX ? dp[n][amt] : -1;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
coin_change.kt
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun coinChangeDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    for (a in 1..amt) {\n        dp[0][a] = MAX\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = min(dp[i - 1][a], dp[i][a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[n][amt] != MAX) dp[n][amt] else -1\n}\n
coin_change.rb
=begin\nFile: coin_change.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-25, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435.

<1><2><3><4><5><6><7><8><9><10><11><12><13><14><15>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-25 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_1","title":"3. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0438 \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change.py
def coin_change_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n = len(coins)\n    MAX = amt + 1\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [MAX] * (amt + 1)\n    dp[0] = 0\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            else:\n                # \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n    return dp[amt] if dp[amt] != MAX else -1\n
coin_change.cpp
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<int> dp(amt + 1, MAX);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.java
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[amt + 1];\n    Arrays.fill(dp, MAX);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.cs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint CoinChangeDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[amt + 1];\n    Array.Fill(dp, MAX);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = Math.Min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.go
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc coinChangeDPComp(coins []int, amt int) int {\n    n := len(coins)\n    max := amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([]int, amt+1)\n    for i := 1; i <= amt; i++ {\n        dp[i] = max\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        // \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = int(math.Min(float64(dp[a]), float64(dp[a-coins[i-1]]+1)))\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt]\n    }\n    return -1\n}\n
coin_change.swift
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc coinChangeDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    let MAX = amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: MAX, count: amt + 1)\n    dp[0] = 0\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return dp[amt] != MAX ? dp[amt] : -1\n}\n
coin_change.js
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction coinChangeDPComp(coins, amt) {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.ts
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction coinChangeDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    const MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: amt + 1 }, () => MAX);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = Math.min(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    return dp[amt] !== MAX ? dp[amt] : -1;\n}\n
coin_change.dart
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  int MAX = amt + 1;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<int> dp = List.filled(amt + 1, MAX);\n  dp[0] = 0;\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[a] = dp[a];\n      } else {\n        // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1);\n      }\n    }\n  }\n  return dp[amt] != MAX ? dp[amt] : -1;\n}\n
coin_change.rs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn coin_change_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    let max = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![0; amt + 1];\n    dp.fill(max);\n    dp[0] = 0;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = std::cmp::min(dp[a], dp[a - coins[i - 1] as usize] + 1);\n            }\n        }\n    }\n    if dp[amt] != max {\n        return dp[amt] as i32;\n    } else {\n        -1\n    }\n}\n
coin_change.c
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    int MAX = amt + 1;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int *dp = malloc((amt + 1) * sizeof(int));\n    for (int j = 1; j <= amt; j++) {\n        dp[j] = MAX;\n    } \n    dp[0] = 0;\n\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = myMin(dp[a], dp[a - coins[i - 1]] + 1);\n            }\n        }\n    }\n    int res = dp[amt] != MAX ? dp[amt] : -1;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
coin_change.kt
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun coinChangeDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    val MAX = amt + 1\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = IntArray(amt + 1)\n    dp.fill(MAX)\n    dp[0] = 0\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = min(dp[a], dp[a - coins[i - 1]] + 1)\n            }\n        }\n    }\n    return if (dp[amt] != MAX) dp[amt] else -1\n}\n
coin_change.rb
=begin\nFile: coin_change.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef coin_change_dp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n  (1...(amt + 1)).each { |a| dp[0][a] = _MAX }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = [dp[i - 1][a], dp[i][a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[n][amt] != _MAX ? dp[n][amt] : -1\nend\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef coin_change_dp_comp(coins, amt)\n  n = coins.length\n  _MAX = amt + 1\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(amt + 1, _MAX)\n  dp[0] = 0\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[a] = dp[a]\n      else\n        # \u041c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[a] = [dp[a], dp[a - coins[i - 1]] + 1].min\n      end\n    end\n  end\n  dp[amt] != _MAX ? dp[amt] : -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1453-ii","title":"14.5.3 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 II","text":"

Question

\u0414\u0430\u043d\u044b \\(n\\) \u0432\u0438\u0434\u043e\u0432 \u043c\u043e\u043d\u0435\u0442, \u043d\u043e\u043c\u0438\u043d\u0430\u043b \u043c\u043e\u043d\u0435\u0442\u044b \\(i\\) \u0440\u0430\u0432\u0435\u043d \\(coins[i - 1]\\) , \u0430 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0440\u0430\u0432\u043d\u0430 \\(amt\\) . \u041c\u043e\u043d\u0435\u0442\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0432\u0438\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u043c\u043e\u043d\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0443\u043c\u043c\u0443. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 14-26.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 14-26 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 II

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#1_2","title":"1. \u00a0 \u0418\u0434\u0435\u044f \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u0442\u0435\u043f\u0435\u0440\u044c \u0446\u0435\u043b\u044c\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0434\u0437\u0430\u0434\u0430\u0447\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e: \u0447\u0438\u0441\u043b\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u0438\u0437 \u043f\u0435\u0440\u0432\u044b\u0445 \\(i\\) \u0432\u0438\u0434\u043e\u0432 \u043c\u043e\u043d\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0441\u0443\u043c\u043c\u0443 \\(a\\). \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \\(dp\\) \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\((n+1) \\times (amt + 1)\\) .

\u0427\u0438\u0441\u043b\u043e \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u043e \u0441\u0443\u043c\u043c\u0435 \u0447\u0438\u0441\u043b\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043c\u043e\u043d\u0435\u0442\u0443 \u0438 \u0431\u0440\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u043c\u043e\u043d\u0435\u0442\u0443. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0443\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0438\u0434:

\\[ dp[i, a] = dp[i-1, a] + dp[i, a - coins[i-1]] \\]

\u041a\u043e\u0433\u0434\u0430 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c, \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u043d\u0438 \u043e\u0434\u043d\u043e\u0439 \u043c\u043e\u043d\u0435\u0442\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0435\u0441\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \\(dp[i, 0]\\) \u043d\u0443\u0436\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0434\u0438\u043d\u0438\u0446\u0430\u043c\u0438. \u041a\u043e\u0433\u0434\u0430 \u043c\u043e\u043d\u0435\u0442 \u043d\u0435\u0442, \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u043d\u0438\u043a\u0430\u043a\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \\(>0\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u044f \u043f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \\(dp[0, a]\\) \u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043d\u0443\u043b\u044f\u043c\u0438.

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#2_2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430","text":"PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
def coin_change_ii_dp(coins: list[int], amt: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    n = len(coins)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [[0] * (amt + 1) for _ in range(n + 1)]\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for i in range(n + 1):\n        dp[i][0] = 1\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            else:\n                # \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n    return dp[n][amt]\n
coin_change_ii.cpp
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeIIDP(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<vector<int>> dp(n + 1, vector<int>(amt + 1, 0));\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
coin_change_ii.java
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeIIDP(int[] coins, int amt) {\n    int n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[][] dp = new int[n + 1][amt + 1];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
coin_change_ii.cs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint CoinChangeIIDP(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[,] dp = new int[n + 1, amt + 1];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (int i = 0; i <= n; i++) {\n        dp[i, 0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i, a] = dp[i - 1, a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i, a] = dp[i - 1, a] + dp[i, a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n, amt];\n}\n
coin_change_ii.go
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc coinChangeIIDP(coins []int, amt int) int {\n    n := len(coins)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([][]int, n+1)\n    for i := 0; i <= n; i++ {\n        dp[i] = make([]int, amt+1)\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for i := 0; i <= n; i++ {\n        dp[i][0] = 1\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439: \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u044b\n    for i := 1; i <= n; i++ {\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i-1][a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i-1][a] + dp[i][a-coins[i-1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
coin_change_ii.swift
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc coinChangeIIDP(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: Array(repeating: 0, count: amt + 1), count: n + 1)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for i in 0 ... n {\n        dp[i][0] = 1\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
coin_change_ii.js
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction coinChangeIIDP(coins, amt) {\n    const n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
coin_change_ii.ts
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction coinChangeIIDP(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: n + 1 }, () =>\n        Array.from({ length: amt + 1 }, () => 0)\n    );\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (let i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[n][amt];\n}\n
coin_change_ii.dart
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeIIDP(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<List<int>> dp = List.generate(n + 1, (index) => List.filled(amt + 1, 0));\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n  for (int i = 0; i <= n; i++) {\n    dp[i][0] = 1;\n  }\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a];\n      } else {\n        // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[n][amt];\n}\n
coin_change_ii.rs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn coin_change_ii_dp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![vec![0; amt + 1]; n + 1];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for i in 0..=n {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[n][amt]\n}\n
coin_change_ii.c
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint coinChangeIIDP(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int **dp = malloc((n + 1) * sizeof(int *));\n    for (int i = 0; i <= n; i++) {\n        dp[i] = calloc(amt + 1, sizeof(int));\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (int i = 0; i <= n; i++) {\n        dp[i][0] = 1;\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[n][amt];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    for (int i = 0; i <= n; i++) {\n        free(dp[i]);\n    }\n    free(dp);\n    return res;\n}\n
coin_change_ii.kt
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun coinChangeIIDP(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = Array(n + 1) { IntArray(amt + 1) }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n    for (i in 0..n) {\n        dp[i][0] = 1\n    }\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[i][a] = dp[i - 1][a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[n][amt]\n}\n
coin_change_ii.rb
=begin\nFile: coin_change_ii.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_dynamic_programming/unbounded_knapsack_problem/#3_2","title":"3. \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430","text":"

\u041f\u0440\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u0435\u043c \u0436\u0435 \u0441\u0430\u043c\u044b\u043c: \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435, \u043e\u0442\u0432\u0435\u0447\u0430\u044e\u0449\u0435\u0435 \u0437\u0430 \u0432\u0438\u0434\u044b \u043c\u043e\u043d\u0435\u0442:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_ii.py
def coin_change_ii_dp_comp(coins: list[int], amt: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438\"\"\"\n    n = len(coins)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp = [0] * (amt + 1)\n    dp[0] = 1\n    # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in range(1, n + 1):\n        # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n        for a in range(1, amt + 1):\n            if coins[i - 1] > a:\n                # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            else:\n                # \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n    return dp[amt]\n
coin_change_ii.cpp
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeIIDPComp(vector<int> &coins, int amt) {\n    int n = coins.size();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    vector<int> dp(amt + 1, 0);\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
coin_change_ii.java
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
coin_change_ii.cs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint CoinChangeIIDPComp(int[] coins, int amt) {\n    int n = coins.Length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int[] dp = new int[amt + 1];\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
coin_change_ii.go
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc coinChangeIIDPComp(coins []int, amt int) int {\n    n := len(coins)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    dp := make([]int, amt+1)\n    dp[0] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i := 1; i <= n; i++ {\n        // \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n        for a := 1; a <= amt; a++ {\n            if coins[i-1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a-coins[i-1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
coin_change_ii.swift
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunc coinChangeIIDPComp(coins: [Int], amt: Int) -> Int {\n    let n = coins.count\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    var dp = Array(repeating: 0, count: amt + 1)\n    dp[0] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1 ... n {\n        for a in 1 ... amt {\n            if coins[i - 1] > a {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
coin_change_ii.js
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction coinChangeIIDPComp(coins, amt) {\n    const n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
coin_change_ii.ts
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfunction coinChangeIIDPComp(coins: Array<number>, amt: number): number {\n    const n = coins.length;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    const dp = Array.from({ length: amt + 1 }, () => 0);\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (let i = 1; i <= n; i++) {\n        for (let a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    return dp[amt];\n}\n
coin_change_ii.dart
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeIIDPComp(List<int> coins, int amt) {\n  int n = coins.length;\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  List<int> dp = List.filled(amt + 1, 0);\n  dp[0] = 1;\n  // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for (int i = 1; i <= n; i++) {\n    for (int a = 1; a <= amt; a++) {\n      if (coins[i - 1] > a) {\n        // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[a] = dp[a];\n      } else {\n        // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[a] = dp[a] + dp[a - coins[i - 1]];\n      }\n    }\n  }\n  return dp[amt];\n}\n
coin_change_ii.rs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfn coin_change_ii_dp_comp(coins: &[i32], amt: usize) -> i32 {\n    let n = coins.len();\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    let mut dp = vec![0; amt + 1];\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for i in 1..=n {\n        for a in 1..=amt {\n            if coins[i - 1] > a as i32 {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1] as usize];\n            }\n        }\n    }\n    dp[amt]\n}\n
coin_change_ii.c
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nint coinChangeIIDPComp(int coins[], int amt, int coinsSize) {\n    int n = coinsSize;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    int *dp = calloc(amt + 1, sizeof(int));\n    dp[0] = 1;\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (int i = 1; i <= n; i++) {\n        for (int a = 1; a <= amt; a++) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a];\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]];\n            }\n        }\n    }\n    int res = dp[amt];\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(dp);\n    return res;\n}\n
coin_change_ii.kt
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 */\nfun coinChangeIIDPComp(coins: IntArray, amt: Int): Int {\n    val n = coins.size\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n    val dp = IntArray(amt + 1)\n    dp[0] = 1\n    // \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n    for (i in 1..n) {\n        for (a in 1..amt) {\n            if (coins[i - 1] > a) {\n                // \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n                dp[a] = dp[a]\n            } else {\n                // \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n                dp[a] = dp[a] + dp[a - coins[i - 1]]\n            }\n        }\n    }\n    return dp[amt]\n}\n
coin_change_ii.rb
=begin\nFile: coin_change_ii.rb\nCreated Time: 2024-05-29\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef coin_change_ii_dp(coins, amt)\n  n = coins.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(n + 1) { Array.new(amt + 1, 0) }\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430\n  (0...(n + 1)).each { |i| dp[i][0] = 1 }\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[i][a] = dp[i - 1][a]\n      else\n        # \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[i][a] = dp[i - 1][a] + dp[i][a - coins[i - 1]]\n      end\n    end\n  end\n  dp[n][amt]\nend\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442 II: \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 ###\ndef coin_change_ii_dp_comp(coins, amt)\n  n = coins.length\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b dp\n  dp = Array.new(amt + 1, 0)\n  dp[0] = 1\n  # \u041f\u0435\u0440\u0435\u0445\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439\n  for i in 1...(n + 1)\n    # \u041f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434\n    for a in 1...(amt + 1)\n      if coins[i - 1] > a\n        # \u0415\u0441\u043b\u0438 \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u043c\u043e\u043d\u0435\u0442\u0443 i \u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\n        dp[a] = dp[a]\n      else\n        # \u0421\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439: \u043d\u0435 \u0431\u0440\u0430\u0442\u044c \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u043c\u043e\u043d\u0435\u0442\u0443 i\n        dp[a] = dp[a] + dp[a - coins[i - 1]]\n      end\n    end\n  end\n  dp[amt]\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_graph/","title":"\u0413\u043b\u0430\u0432\u0430 9. \u00a0 \u0413\u0440\u0430\u0444\u044b","text":"

Abstract

\u041d\u0430 \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u043c \u043f\u0443\u0442\u0438 \u043c\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b \u0443\u0437\u043b\u0430\u043c, \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u043c \u0431\u0435\u0441\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043d\u0435\u0432\u0438\u0434\u0438\u043c\u044b\u043c\u0438 \u0440\u0435\u0431\u0440\u0430\u043c\u0438.

\u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0441\u0442\u0440\u0435\u0447\u0430 \u0438 \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u0430\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0432 \u044d\u0442\u043e\u0439 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u0441\u0432\u043e\u0439 \u043e\u0441\u043e\u0431\u044b\u0439 \u0441\u043b\u0435\u0434.

"},{"location":"chapter_graph/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_graph/graph/","title":"9.1 \u00a0 \u0413\u0440\u0430\u0444","text":"

\u0413\u0440\u0430\u0444 (graph) - \u044d\u0442\u043e \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d (vertex) \u0438 \u0440\u0435\u0431\u0435\u0440 (edge). \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0433\u0440\u0430\u0444 \\(G\\) \u043a\u0430\u043a \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \\(V\\) \u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440 \\(E\\) . \u0412 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0433\u0440\u0430\u0444, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 5 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 7 \u0440\u0435\u0431\u0435\u0440.

\\[ \\begin{aligned} V & = \\{ 1, 2, 3, 4, 5 \\} \\newline E & = \\{ (1,2), (1,3), (1,5), (2,3), (2,4), (2,5), (4,5) \\} \\newline G & = \\{ V, E \\} \\newline \\end{aligned} \\]

\u0415\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0430\u043a \u0443\u0437\u043b\u044b, \u0430 \u0440\u0435\u0431\u0440\u0430 \u043a\u0430\u043a \u0441\u0441\u044b\u043b\u043a\u0438 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438), \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u0435 \u044d\u0442\u0438 \u0443\u0437\u043b\u044b, \u0442\u043e \u0433\u0440\u0430\u0444 \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0432\u044b\u0440\u043e\u0441\u0448\u0435\u0439 \u0438\u0437 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-1, \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c\u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f\u043c\u0438 (\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a) \u0438 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f\u043c\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f (\u0434\u0435\u0440\u0435\u0432\u043e), \u0441\u0435\u0442\u0435\u0432\u044b\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f (\u0433\u0440\u0430\u0444) \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0432\u043e\u0431\u043e\u0434\u043e\u0439 , \u0430 \u043f\u043e\u0442\u043e\u043c\u0443 \u0438 \u0441\u043b\u043e\u0436\u043d\u0435\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-1 \u00a0 \u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u0432\u044f\u0437\u043d\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u043e\u043c, \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u0438 \u0433\u0440\u0430\u0444\u043e\u043c

"},{"location":"chapter_graph/graph/#911","title":"9.1.1 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u043e\u0432","text":"

\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0438\u043c\u0435\u044e\u0442 \u043b\u0438 \u0440\u0435\u0431\u0440\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0433\u0440\u0430\u0444\u044b \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b (undirected graph) \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b (directed graph) , \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-2.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-2 \u00a0 \u041e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444\u044b

\u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b \u043b\u0438 \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0440\u0443\u0433 \u0438\u0437 \u0434\u0440\u0443\u0433\u0430, \u0433\u0440\u0430\u0444 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 (connected graph) \u0438 \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 (disconnected graph) , \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-3.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-3 \u00a0 \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0438 \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444\u044b

\u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a \u0440\u0435\u0431\u0440\u0430\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \"\u0432\u0435\u0441\" \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 (weighted graph) , \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-4. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u0438\u0433\u0440\u0430\u0445 \u0432\u0440\u043e\u0434\u0435 Honor of Kings \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \"\u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438\" \u043c\u0435\u0436\u0434\u0443 \u0438\u0433\u0440\u043e\u043a\u0430\u043c\u0438 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c\u0443 \u0432 \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u044b\u0445 \u0438\u0433\u0440\u0430\u0445; \u0442\u0430\u043a\u0443\u044e \u0441\u0435\u0442\u044c \u0431\u043b\u0438\u0437\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u043c \u0433\u0440\u0430\u0444\u043e\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-4 \u00a0 \u0412\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0438 \u043d\u0435\u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444\u044b

\u0414\u043b\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \"\u0433\u0440\u0430\u0444\" \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b.

"},{"location":"chapter_graph/graph/#912","title":"9.1.2 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430","text":"

\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \"\u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\" \u0438 \"\u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\". \u041d\u0438\u0436\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444.

"},{"location":"chapter_graph/graph/#1","title":"1. \u00a0 \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 \u0440\u0430\u0432\u043d\u043e \\(n\\) ; \u0442\u043e\u0433\u0434\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (adjacency matrix) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n \\times n\\) \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430: \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u0435, \u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0440\u0435\u0431\u0440\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442, \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0441\u0432\u044f\u0437\u044c \u0438\u043b\u0438 \u043d\u0435\u0442.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-5, \u043f\u0443\u0441\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\(M\\) , \u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d - \u043a\u0430\u043a \\(V\\) ; \u0442\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \\(M[i, j] = 1\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \\(V[i]\\) \u0438 \\(V[j]\\) \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0431\u0440\u043e, \u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \\(M[i, j] = 0\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0440\u0435\u0431\u0440\u0430 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043d\u0435\u0442.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-5 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438

\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438.

\u041f\u0440\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0433\u0440\u0430\u0444\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u044b, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0440\u0435\u0431\u0440\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c\u044e, \u0440\u0430\u0432\u043d\u043e\u0439 \\(O(1)\\) . \u041e\u0434\u043d\u0430\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438.

"},{"location":"chapter_graph/graph/#2","title":"2. \u00a0 \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (adjacency list) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \\(n\\) \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0433\u0434\u0435 \u0443\u0437\u043b\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \\(i\\)-\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \\(i\\) \u0438 \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0441 \u043d\u0435\u0439, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0435 \u0441 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-6 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0433\u0440\u0430\u0444\u0430, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-6 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438

\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0440\u0435\u0431\u0440\u0430, \u0430 \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u0431\u0435\u0440 \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435 \\(n^2\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0435\u0431\u0440\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u0441\u043f\u0438\u0441\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u043d \u0443\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438.

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043e\u043a 9-6, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \"\u043c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a\" \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043b\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0437\u0434\u0435\u0441\u044c \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0438\u0434\u0435\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043a\u043e\u0433\u0434\u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0434\u043b\u0438\u043d\u043d\u044b\u043c, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u0438\u043b\u0438 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u0447\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \\(O(n)\\) \u0434\u043e \\(O(\\log n)\\) ; \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438 \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e \\(O(1)\\) .

"},{"location":"chapter_graph/graph/#913","title":"9.1.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u043e\u0432","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 9-1, \u043c\u043d\u043e\u0433\u0438\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043c\u043e\u0436\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0430\u043c\u0438, \u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0437\u0430\u0442\u0435\u043c \u0441\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u0437\u0430\u0434\u0430\u0447\u0430\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u0433\u0440\u0430\u0444\u0430\u0445.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 9-1 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438

\u0412\u0435\u0440\u0448\u0438\u043d\u0430 \u0420\u0435\u0431\u0440\u043e \u0417\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u043d\u0430 \u0433\u0440\u0430\u0444\u0435 \u0421\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0441\u0435\u0442\u0438 \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0414\u0440\u0443\u0436\u0435\u0441\u043a\u0430\u044f \u0441\u0432\u044f\u0437\u044c \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u0440\u0443\u0437\u0435\u0439 \u041b\u0438\u043d\u0438\u0438 \u043c\u0435\u0442\u0440\u043e \u0421\u0442\u0430\u043d\u0446\u0438\u044f \u0421\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0430\u043d\u0446\u0438\u044f\u043c\u0438 \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u044f \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0421\u043e\u043b\u043d\u0435\u0447\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u041d\u0435\u0431\u0435\u0441\u043d\u043e\u0435 \u0442\u0435\u043b\u043e \u0413\u0440\u0430\u0432\u0438\u0442\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043b\u0430\u043c\u0438 \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u0440\u0431\u0438\u0442 \u043f\u043b\u0430\u043d\u0435\u0442"},{"location":"chapter_graph/graph_operations/","title":"9.2 \u00a0 \u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0433\u0440\u0430\u0444\u0430","text":"

\u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0433\u0440\u0430\u0444\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \"\u0440\u0435\u0431\u0440\u0430\u043c\u0438\" \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \"\u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438\". \u0412 \u0434\u0432\u0443\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f - \"\u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\" \u0438 \"\u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\" - \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f.

"},{"location":"chapter_graph/graph_operations/#921","title":"9.2.1 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u041f\u0443\u0441\u0442\u044c \u0434\u0430\u043d \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0441 \u0447\u0438\u0441\u043b\u043e\u043c \u0432\u0435\u0440\u0448\u0438\u043d \\(n\\) . \u0422\u043e\u0433\u0434\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435.

\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-7 \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0435\u0440 \u0438 \u0432\u0435\u0440\u0448\u0438\u043d

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_matrix.py
class GraphAdjMat:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\"\"\"\n\n    def __init__(self, vertices: list[int], edges: list[list[int]]):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        # \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n        self.vertices: list[int] = []\n        # \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n        self.adj_mat: list[list[int]] = []\n        # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for val in vertices:\n            self.add_vertex(val)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        # \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for e in edges:\n            self.add_edge(e[0], e[1])\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d\"\"\"\n        return len(self.vertices)\n\n    def add_vertex(self, val: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"\"\"\n        n = self.size()\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        self.vertices.append(val)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        new_row = [0] * n\n        self.adj_mat.append(new_row)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for row in self.adj_mat:\n            row.append(0)\n\n    def remove_vertex(self, index: int):\n        \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"\"\"\n        if index >= self.size():\n            raise IndexError()\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        self.vertices.pop(index)\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        self.adj_mat.pop(index)\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for row in self.adj_mat:\n            row.pop(index)\n\n    def add_edge(self, i: int, j: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\"\"\"\n        # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        # \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        self.adj_mat[i][j] = 1\n        self.adj_mat[j][i] = 1\n\n    def remove_edge(self, i: int, j: int):\n        \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\"\"\"\n        # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i < 0 or j < 0 or i >= self.size() or j >= self.size() or i == j:\n            raise IndexError()\n        self.adj_mat[i][j] = 0\n        self.adj_mat[j][i] = 0\n\n    def print(self):\n        \"\"\"\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\"\"\"\n        print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d =\", self.vertices)\n        print(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        print_matrix(self.adj_mat)\n
graph_adjacency_matrix.cpp
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    vector<int> vertices;       // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    vector<vector<int>> adjMat; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    GraphAdjMat(const vector<int> &vertices, const vector<vector<int>> &edges) {\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for (const vector<int> &edge : edges) {\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    int size() const {\n        return vertices.size();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    void addVertex(int val) {\n        int n = size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.push_back(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.emplace_back(vector<int>(n, 0));\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (vector<int> &row : adjMat) {\n            row.push_back(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    void removeVertex(int index) {\n        if (index >= size()) {\n            throw out_of_range(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.erase(vertices.begin() + index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.erase(adjMat.begin() + index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (vector<int> &row : adjMat) {\n            row.erase(row.begin() + index);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    void addEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        }\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    void removeEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n            throw out_of_range(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        }\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    void print() {\n        cout << \"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \";\n        printVector(vertices);\n        cout << \"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\" << endl;\n        printVectorMatrix(adjMat);\n    }\n};\n
graph_adjacency_matrix.java
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    List<Integer> vertices; // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    List<List<Integer>> adjMat; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = new ArrayList<>();\n        this.adjMat = new ArrayList<>();\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (int val : vertices) {\n            addVertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for (int[] e : edges) {\n            addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    public int size() {\n        return vertices.size();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void addVertex(int val) {\n        int n = size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.add(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        List<Integer> newRow = new ArrayList<>(n);\n        for (int j = 0; j < n; j++) {\n            newRow.add(0);\n        }\n        adjMat.add(newRow);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (List<Integer> row : adjMat) {\n            row.add(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void removeVertex(int index) {\n        if (index >= size())\n            throw new IndexOutOfBoundsException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.remove(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.remove(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (List<Integer> row : adjMat) {\n            row.remove(index);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    public void addEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        adjMat.get(i).set(j, 1);\n        adjMat.get(j).set(i, 1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    public void removeEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw new IndexOutOfBoundsException();\n        adjMat.get(i).set(j, 0);\n        adjMat.get(j).set(i, 0);\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    public void print() {\n        System.out.print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \");\n        System.out.println(vertices);\n        System.out.println(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        PrintUtil.printMatrix(adjMat);\n    }\n}\n
graph_adjacency_matrix.cs
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    List<int> vertices;     // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    List<List<int>> adjMat; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public GraphAdjMat(int[] vertices, int[][] edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        foreach (int val in vertices) {\n            AddVertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        foreach (int[] e in edges) {\n            AddEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    int Size() {\n        return vertices.Count;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void AddVertex(int val) {\n        int n = Size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.Add(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        List<int> newRow = new(n);\n        for (int j = 0; j < n; j++) {\n            newRow.Add(0);\n        }\n        adjMat.Add(newRow);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        foreach (List<int> row in adjMat) {\n            row.Add(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void RemoveVertex(int index) {\n        if (index >= Size())\n            throw new IndexOutOfRangeException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.RemoveAt(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.RemoveAt(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        foreach (List<int> row in adjMat) {\n            row.RemoveAt(index);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    public void AddEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        adjMat[i][j] = 1;\n        adjMat[j][i] = 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    public void RemoveEdge(int i, int j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= Size() || j >= Size() || i == j)\n            throw new IndexOutOfRangeException();\n        adjMat[i][j] = 0;\n        adjMat[j][i] = 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    public void Print() {\n        Console.Write(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \");\n        PrintUtil.PrintList(vertices);\n        Console.WriteLine(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        PrintUtil.PrintMatrix(adjMat);\n    }\n}\n
graph_adjacency_matrix.go
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\ntype graphAdjMat struct {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    vertices []int\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    adjMat [][]int\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newGraphAdjMat(vertices []int, edges [][]int) *graphAdjMat {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    n := len(vertices)\n    adjMat := make([][]int, n)\n    for i := range adjMat {\n        adjMat[i] = make([]int, n)\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0430\u0444\n    g := &graphAdjMat{\n        vertices: vertices,\n        adjMat:   adjMat,\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n    // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    for i := range edges {\n        g.addEdge(edges[i][0], edges[i][1])\n    }\n    return g\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\nfunc (g *graphAdjMat) size() int {\n    return len(g.vertices)\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nfunc (g *graphAdjMat) addVertex(val int) {\n    n := g.size()\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n    g.vertices = append(g.vertices, val)\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    newRow := make([]int, n)\n    g.adjMat = append(g.adjMat, newRow)\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i], 0)\n    }\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nfunc (g *graphAdjMat) removeVertex(index int) {\n    if index >= g.size() {\n        return\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    g.vertices = append(g.vertices[:index], g.vertices[index+1:]...)\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    g.adjMat = append(g.adjMat[:index], g.adjMat[index+1:]...)\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for i := range g.adjMat {\n        g.adjMat[i] = append(g.adjMat[i][:index], g.adjMat[i][index+1:]...)\n    }\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n// \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\nfunc (g *graphAdjMat) addEdge(i, j int) {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n    g.adjMat[i][j] = 1\n    g.adjMat[j][i] = 1\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n// \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\nfunc (g *graphAdjMat) removeEdge(i, j int) {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if i < 0 || j < 0 || i >= g.size() || j >= g.size() || i == j {\n        fmt.Errorf(\"%s\", \"Index Out Of Bounds Exception\")\n    }\n    g.adjMat[i][j] = 0\n    g.adjMat[j][i] = 0\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nfunc (g *graphAdjMat) print() {\n    fmt.Printf(\"\\t\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = %v\\n\", g.vertices)\n    fmt.Printf(\"\\t\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 = \\n\")\n    for i := range g.adjMat {\n        fmt.Printf(\"\\t\\t\\t%v\\n\", g.adjMat[i])\n    }\n}\n
graph_adjacency_matrix.swift
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    private var vertices: [Int] // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    private var adjMat: [[Int]] // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init(vertices: [Int], edges: [[Int]]) {\n        self.vertices = []\n        adjMat = []\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for val in vertices {\n            addVertex(val: val)\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for e in edges {\n            addEdge(i: e[0], j: e[1])\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    func size() -> Int {\n        vertices.count\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    func addVertex(val: Int) {\n        let n = size()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.append(val)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        let newRow = Array(repeating: 0, count: n)\n        adjMat.append(newRow)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for i in adjMat.indices {\n            adjMat[i].append(0)\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    func removeVertex(index: Int) {\n        if index >= size() {\n            fatalError(\"\u0412\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\")\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.remove(at: index)\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.remove(at: index)\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for i in adjMat.indices {\n            adjMat[i].remove(at: index)\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    func addEdge(i: Int, j: Int) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u0412\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\")\n        }\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    func removeEdge(i: Int, j: Int) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i < 0 || j < 0 || i >= size() || j >= size() || i == j {\n            fatalError(\"\u0412\u044b\u0445\u043e\u0434 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\")\n        }\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    func print() {\n        Swift.print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \", terminator: \"\")\n        Swift.print(vertices)\n        Swift.print(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        PrintUtil.printMatrix(matrix: adjMat)\n    }\n}\n
graph_adjacency_matrix.js
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    vertices; // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    adjMat; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(vertices, edges) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    size() {\n        return this.vertices.length;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    addVertex(val) {\n        const n = this.size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        this.vertices.push(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        const newRow = [];\n        for (let j = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    removeVertex(index) {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        this.vertices.splice(index, 1);\n\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        this.adjMat.splice(index, 1);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    addEdge(i, j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    removeEdge(i, j) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    print() {\n        console.log('\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = ', this.vertices);\n        console.log('\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =', this.adjMat);\n    }\n}\n
graph_adjacency_matrix.ts
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n    vertices: number[]; // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    adjMat: number[][]; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(vertices: number[], edges: number[][]) {\n        this.vertices = [];\n        this.adjMat = [];\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (const val of vertices) {\n            this.addVertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for (const e of edges) {\n            this.addEdge(e[0], e[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    size(): number {\n        return this.vertices.length;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    addVertex(val: number): void {\n        const n: number = this.size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        this.vertices.push(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        const newRow: number[] = [];\n        for (let j: number = 0; j < n; j++) {\n            newRow.push(0);\n        }\n        this.adjMat.push(newRow);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (const row of this.adjMat) {\n            row.push(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    removeVertex(index: number): void {\n        if (index >= this.size()) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        this.vertices.splice(index, 1);\n\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        this.adjMat.splice(index, 1);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (const row of this.adjMat) {\n            row.splice(index, 1);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    addEdge(i: number, j: number): void {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) === (j, i)\n        this.adjMat[i][j] = 1;\n        this.adjMat[j][i] = 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    removeEdge(i: number, j: number): void {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) {\n            throw new RangeError('Index Out Of Bounds Exception');\n        }\n        this.adjMat[i][j] = 0;\n        this.adjMat[j][i] = 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    print(): void {\n        console.log('\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = ', this.vertices);\n        console.log('\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =', this.adjMat);\n    }\n}\n
graph_adjacency_matrix.dart
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat {\n  List<int> vertices = []; // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b: \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\u00bb, \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\u00bb\n  List<List<int>> adjMat = []; // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  GraphAdjMat(List<int> vertices, List<List<int>> edges) {\n    this.vertices = [];\n    this.adjMat = [];\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (int val in vertices) {\n      addVertex(val);\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n    // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    for (List<int> e in edges) {\n      addEdge(e[0], e[1]);\n    }\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n  int size() {\n    return vertices.length;\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n  void addVertex(int val) {\n    int n = size();\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n    vertices.add(val);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    List<int> newRow = List.filled(n, 0, growable: true);\n    adjMat.add(newRow);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for (List<int> row in adjMat) {\n      row.add(0);\n    }\n  }\n\n  /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n  void removeVertex(int index) {\n    if (index >= size()) {\n      throw IndexError;\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    vertices.removeAt(index);\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    adjMat.removeAt(index);\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for (List<int> row in adjMat) {\n      row.removeAt(index);\n    }\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n  // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n  void addEdge(int i, int j) {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n    adjMat[i][j] = 1;\n    adjMat[j][i] = 1;\n  }\n\n  /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n  // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n  void removeEdge(int i, int j) {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) {\n      throw IndexError;\n    }\n    adjMat[i][j] = 0;\n    adjMat[j][i] = 0;\n  }\n\n  /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n  void printAdjMat() {\n    print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = $vertices\");\n    print(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 = \");\n    printMatrix(adjMat);\n  }\n}\n
graph_adjacency_matrix.rs
/* \u0422\u0438\u043f \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\npub struct GraphAdjMat {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    pub vertices: Vec<i32>,\n    // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    pub adj_mat: Vec<Vec<i32>>,\n}\n\nimpl GraphAdjMat {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    pub fn new(vertices: Vec<i32>, edges: Vec<[usize; 2]>) -> Self {\n        let mut graph = GraphAdjMat {\n            vertices: vec![],\n            adj_mat: vec![],\n        };\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for val in vertices {\n            graph.add_vertex(val);\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for edge in edges {\n            graph.add_edge(edge[0], edge[1])\n        }\n\n        graph\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    pub fn size(&self) -> usize {\n        self.vertices.len()\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    pub fn add_vertex(&mut self, val: i32) {\n        let n = self.size();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        self.vertices.push(val);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        self.adj_mat.push(vec![0; n]);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for row in self.adj_mat.iter_mut() {\n            row.push(0);\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    pub fn remove_vertex(&mut self, index: usize) {\n        if index >= self.size() {\n            panic!(\"index error\")\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        self.vertices.remove(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        self.adj_mat.remove(index);\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for row in self.adj_mat.iter_mut() {\n            row.remove(index);\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    pub fn add_edge(&mut self, i: usize, j: usize) {\n        // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        self.adj_mat[i][j] = 1;\n        self.adj_mat[j][i] = 1;\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    pub fn remove_edge(&mut self, i: usize, j: usize) {\n        // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if i >= self.size() || j >= self.size() || i == j {\n            panic!(\"index error\")\n        }\n        self.adj_mat[i][j] = 0;\n        self.adj_mat[j][i] = 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    pub fn print(&self) {\n        println!(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = {:?}\", self.vertices);\n        println!(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        println!(\"[\");\n        for row in &self.adj_mat {\n            println!(\"  {:?},\", row);\n        }\n        println!(\"]\")\n    }\n}\n
graph_adjacency_matrix.c
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\ntypedef struct {\n    int vertices[MAX_SIZE];\n    int adjMat[MAX_SIZE][MAX_SIZE];\n    int size;\n} GraphAdjMat;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nGraphAdjMat *newGraphAdjMat() {\n    GraphAdjMat *graph = (GraphAdjMat *)malloc(sizeof(GraphAdjMat));\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        for (int j = 0; j < MAX_SIZE; j++) {\n            graph->adjMat[i][j] = 0;\n        }\n    }\n    return graph;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delGraphAdjMat(GraphAdjMat *graph) {\n    free(graph);\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nvoid addVertex(GraphAdjMat *graph, int val) {\n    if (graph->size == MAX_SIZE) {\n        fprintf(stderr, \"\u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u043b\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0430\\n\");\n        return;\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c n-\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438 \u043e\u0431\u043d\u0443\u043b\u0438\u0442\u044c n-\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0441\u0442\u043e\u043b\u0431\u0435\u0446\n    int n = graph->size;\n    graph->vertices[n] = val;\n    for (int i = 0; i <= n; i++) {\n        graph->adjMat[n][i] = graph->adjMat[i][n] = 0;\n    }\n    graph->size++;\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nvoid removeVertex(GraphAdjMat *graph, int index) {\n    if (index < 0 || index >= graph->size) {\n        fprintf(stderr, \"\u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b\\n\");\n        return;\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    for (int i = index; i < graph->size - 1; i++) {\n        graph->vertices[i] = graph->vertices[i + 1];\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for (int i = index; i < graph->size - 1; i++) {\n        for (int j = 0; j < graph->size; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i + 1][j];\n        }\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    for (int i = 0; i < graph->size; i++) {\n        for (int j = index; j < graph->size - 1; j++) {\n            graph->adjMat[i][j] = graph->adjMat[i][j + 1];\n        }\n    }\n    graph->size--;\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n// \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\nvoid addEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0440\u0435\u0431\u0440\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043b\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 1;\n    graph->adjMat[j][i] = 1;\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n// \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\nvoid removeEdge(GraphAdjMat *graph, int i, int j) {\n    if (i < 0 || j < 0 || i >= graph->size || j >= graph->size || i == j) {\n        fprintf(stderr, \"\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0440\u0435\u0431\u0440\u0430 \u0432\u044b\u0445\u043e\u0434\u044f\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043b\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442\\n\");\n        return;\n    }\n    graph->adjMat[i][j] = 0;\n    graph->adjMat[j][i] = 0;\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nvoid printGraphAdjMat(GraphAdjMat *graph) {\n    printf(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \");\n    printArray(graph->vertices, graph->size);\n    printf(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\\n\");\n    for (int i = 0; i < graph->size; i++) {\n        printArray(graph->adjMat[i], graph->size);\n    }\n}\n
graph_adjacency_matrix.kt
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjMat(vertices: IntArray, edges: Array<IntArray>) {\n    val vertices = mutableListOf<Int>() // \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    val adjMat = mutableListOf<MutableList<Int>>() // \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init {\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (vertex in vertices) {\n            addVertex(vertex)\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n        // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n        for (edge in edges) {\n            addEdge(edge[0], edge[1])\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    fun size(): Int {\n        return vertices.size\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    fun addVertex(_val: Int) {\n        val n = size()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.add(_val)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        val newRow = mutableListOf<Int>()\n        for (j in 0..<n) {\n            newRow.add(0)\n        }\n        adjMat.add(newRow)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (row in adjMat) {\n            row.add(0)\n        }\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    fun removeVertex(index: Int) {\n        if (index >= size())\n            throw IndexOutOfBoundsException()\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n        vertices.removeAt(index)\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjMat.removeAt(index)\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        for (row in adjMat) {\n            row.removeAt(index)\n        }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    fun addEdge(i: Int, j: Int) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        // \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n        adjMat[i][j] = 1\n        adjMat[j][i] = 1\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    fun removeEdge(i: Int, j: Int) {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n        if (i < 0 || j < 0 || i >= size() || j >= size() || i == j)\n            throw IndexOutOfBoundsException()\n        adjMat[i][j] = 0\n        adjMat[j][i] = 0\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    fun print() {\n        print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = \")\n        println(vertices)\n        println(\"\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        printMatrix(adjMat)\n    }\n}\n
graph_adjacency_matrix.rb
=begin\nFile: graph_adjacency_matrix.rb\nCreated Time: 2024-04-25\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/print_util'\n\n# ## \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ###\nclass GraphAdjMat\n  def initialize(vertices, edges)\n    # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n    # \u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u00ab\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u00bb, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u2014 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    @vertices = []\n    # \u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u00bb\n    @adj_mat = []\n    # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    vertices.each { |val| add_vertex(val) }\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u0430\n    # \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b edges \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0441\u043e\u0431\u043e\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    edges.each { |e| add_edge(e[0], e[1]) }\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432\u0435\u0440\u0448\u0438\u043d ###\n  def size\n    @vertices.length\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b ###\n  def add_vertex(val)\n    n = size\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d\n    @vertices << val\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    new_row = Array.new(n, 0)\n    @adj_mat << new_row\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    @adj_mat.each { |row| row << 0 }\n  end\n\n  # ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b ###\n  def remove_vertex(index)\n    raise IndexError if index >= size\n\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    @vertices.delete_at(index)\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    @adj_mat.delete_at(index)\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c index \u0438\u0437 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    @adj_mat.each { |row| row.delete_at(index) }\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 ###\n  def add_edge(i, j)\n    # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    # \u0412 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0433\u043b\u0430\u0432\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u043e\u043d\u0430\u043b\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f (i, j) == (j, i)\n    @adj_mat[i][j] = 1\n    @adj_mat[j][i] = 1\n  end\n\n  # ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 ###\n  def remove_edge(i, j)\n    # \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b i \u0438 j \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 vertices\n    # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438 \u0441\u043b\u0443\u0447\u0430\u044f \u0440\u0430\u0432\u0435\u043d\u0441\u0442\u0432\u0430\n    if i < 0 || j < 0 || i >= size || j >= size || i == j\n      raise IndexError\n    end\n    @adj_mat[i][j] = 0\n    @adj_mat[j][i] = 0\n  end\n\n  # ## \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ###\n  def __print__\n    puts \"\u0421\u043f\u0438\u0441\u043e\u043a \u0432\u0435\u0440\u0448\u0438\u043d = #{@vertices}\"\n    puts '\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ='\n    print_matrix(@adj_mat)\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_graph/graph_operations/#922","title":"9.2.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u041f\u0443\u0441\u0442\u044c \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0432 \u0441\u0443\u043c\u043c\u0435 \\(n\\) \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \\(m\\) \u0440\u0435\u0431\u0435\u0440. \u0422\u043e\u0433\u0434\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435.

\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-8 \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0435\u0440 \u0438 \u0432\u0435\u0440\u0448\u0438\u043d

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043a\u043e\u0434 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438. \u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u043c\u0438 \u0432\u044b\u0448\u0435, \u0440\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0442\u043b\u0438\u0447\u0438\u044f.

\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043a\u043b\u0430\u0441\u0441 Vertex \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c: \u0435\u0441\u043b\u0438, \u043a\u0430\u043a \u0438 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u043e \u043f\u0440\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \\(i\\) \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0442\u044c \u043d\u0430 \\(1\\) \u0432\u0441\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b, \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \\(i\\) , \u0447\u0442\u043e \u043a\u0440\u0430\u0439\u043d\u0435 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u0415\u0441\u043b\u0438 \u0436\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u043e\u043c Vertex , \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u0435\u043d\u044f\u0442\u044c \u0443\u0436\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_adjacency_list.py
class GraphAdjList:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\"\"\"\n\n    def __init__(self, edges: list[list[Vertex]]):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        # \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        self.adj_list = dict[Vertex, list[Vertex]]()\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for edge in edges:\n            self.add_vertex(edge[0])\n            self.add_vertex(edge[1])\n            self.add_edge(edge[0], edge[1])\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d\"\"\"\n        return len(self.adj_list)\n\n    def add_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        self.adj_list[vet1].append(vet2)\n        self.adj_list[vet2].append(vet1)\n\n    def remove_edge(self, vet1: Vertex, vet2: Vertex):\n        \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430\"\"\"\n        if vet1 not in self.adj_list or vet2 not in self.adj_list or vet1 == vet2:\n            raise ValueError()\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        self.adj_list[vet1].remove(vet2)\n        self.adj_list[vet2].remove(vet1)\n\n    def add_vertex(self, vet: Vertex):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"\"\"\n        if vet in self.adj_list:\n            return\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        self.adj_list[vet] = []\n\n    def remove_vertex(self, vet: Vertex):\n        \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\"\"\"\n        if vet not in self.adj_list:\n            raise ValueError()\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        self.adj_list.pop(vet)\n        # \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for vertex in self.adj_list:\n            if vet in self.adj_list[vertex]:\n                self.adj_list[vertex].remove(vet)\n\n    def print(self):\n        \"\"\"\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\"\"\"\n        print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        for vertex in self.adj_list:\n            tmp = [v.val for v in self.adj_list[vertex]]\n            print(f\"{vertex.val}: {tmp},\")\n
graph_adjacency_list.cpp
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n  public:\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    unordered_map<Vertex *, vector<Vertex *>> adjList;\n\n    /* \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u0438\u0437 vector */\n    void remove(vector<Vertex *> &vec, Vertex *vet) {\n        for (int i = 0; i < vec.size(); i++) {\n            if (vec[i] == vet) {\n                vec.erase(vec.begin() + i);\n                break;\n            }\n        }\n    }\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    GraphAdjList(const vector<vector<Vertex *>> &edges) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for (const vector<Vertex *> &edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    int size() {\n        return adjList.size();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    void addEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1].push_back(vet2);\n        adjList[vet2].push_back(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    void removeEdge(Vertex *vet1, Vertex *vet2) {\n        if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2)\n            throw invalid_argument(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        remove(adjList[vet1], vet2);\n        remove(adjList[vet2], vet1);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    void addVertex(Vertex *vet) {\n        if (adjList.count(vet))\n            return;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjList[vet] = vector<Vertex *>();\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    void removeVertex(Vertex *vet) {\n        if (!adjList.count(vet))\n            throw invalid_argument(\"\u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\");\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        adjList.erase(vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for (auto &adj : adjList) {\n            remove(adj.second, vet);\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    void print() {\n        cout << \"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\" << endl;\n        for (auto &adj : adjList) {\n            const auto &key = adj.first;\n            const auto &vec = adj.second;\n            cout << key->val << \": \";\n            printVector(vetsToVals(vec));\n        }\n    }\n};\n
graph_adjacency_list.java
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    Map<Vertex, List<Vertex>> adjList;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public GraphAdjList(Vertex[][] edges) {\n        this.adjList = new HashMap<>();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for (Vertex[] edge : edges) {\n            addVertex(edge[0]);\n            addVertex(edge[1]);\n            addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    public int size() {\n        return adjList.size();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public void addEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList.get(vet1).add(vet2);\n        adjList.get(vet2).add(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public void removeEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw new IllegalArgumentException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList.get(vet1).remove(vet2);\n        adjList.get(vet2).remove(vet1);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void addVertex(Vertex vet) {\n        if (adjList.containsKey(vet))\n            return;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjList.put(vet, new ArrayList<>());\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void removeVertex(Vertex vet) {\n        if (!adjList.containsKey(vet))\n            throw new IllegalArgumentException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        adjList.remove(vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for (List<Vertex> list : adjList.values()) {\n            list.remove(vet);\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    public void print() {\n        System.out.println(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        for (Map.Entry<Vertex, List<Vertex>> pair : adjList.entrySet()) {\n            List<Integer> tmp = new ArrayList<>();\n            for (Vertex vertex : pair.getValue())\n                tmp.add(vertex.val);\n            System.out.println(pair.getKey().val + \": \" + tmp + \",\");\n        }\n    }\n}\n
graph_adjacency_list.cs
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    public Dictionary<Vertex, List<Vertex>> adjList;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public GraphAdjList(Vertex[][] edges) {\n        adjList = [];\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        foreach (Vertex[] edge in edges) {\n            AddVertex(edge[0]);\n            AddVertex(edge[1]);\n            AddEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    int Size() {\n        return adjList.Count;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public void AddEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1].Add(vet2);\n        adjList[vet2].Add(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public void RemoveEdge(Vertex vet1, Vertex vet2) {\n        if (!adjList.ContainsKey(vet1) || !adjList.ContainsKey(vet2) || vet1 == vet2)\n            throw new InvalidOperationException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1].Remove(vet2);\n        adjList[vet2].Remove(vet1);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void AddVertex(Vertex vet) {\n        if (adjList.ContainsKey(vet))\n            return;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjList.Add(vet, []);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public void RemoveVertex(Vertex vet) {\n        if (!adjList.ContainsKey(vet))\n            throw new InvalidOperationException();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        adjList.Remove(vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        foreach (List<Vertex> list in adjList.Values) {\n            list.Remove(vet);\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    public void Print() {\n        Console.WriteLine(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        foreach (KeyValuePair<Vertex, List<Vertex>> pair in adjList) {\n            List<int> tmp = [];\n            foreach (Vertex vertex in pair.Value)\n                tmp.Add(vertex.val);\n            Console.WriteLine(pair.Key.val + \": [\" + string.Join(\", \", tmp) + \"],\");\n        }\n    }\n}\n
graph_adjacency_list.go
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\ntype graphAdjList struct {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    adjList map[Vertex][]Vertex\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newGraphAdjList(edges [][]Vertex) *graphAdjList {\n    g := &graphAdjList{\n        adjList: make(map[Vertex][]Vertex),\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n    for _, edge := range edges {\n        g.addVertex(edge[0])\n        g.addVertex(edge[1])\n        g.addEdge(edge[0], edge[1])\n    }\n    return g\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\nfunc (g *graphAdjList) size() int {\n    return len(g.adjList)\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\nfunc (g *graphAdjList) addEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0443\u044e struct{}\n    g.adjList[vet1] = append(g.adjList[vet1], vet2)\n    g.adjList[vet2] = append(g.adjList[vet2], vet1)\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\nfunc (g *graphAdjList) removeEdge(vet1 Vertex, vet2 Vertex) {\n    _, ok1 := g.adjList[vet1]\n    _, ok2 := g.adjList[vet2]\n    if !ok1 || !ok2 || vet1 == vet2 {\n        panic(\"error\")\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    g.adjList[vet1] = DeleteSliceElms(g.adjList[vet1], vet2)\n    g.adjList[vet2] = DeleteSliceElms(g.adjList[vet2], vet1)\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nfunc (g *graphAdjList) addVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if ok {\n        return\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    g.adjList[vet] = make([]Vertex, 0)\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nfunc (g *graphAdjList) removeVertex(vet Vertex) {\n    _, ok := g.adjList[vet]\n    if !ok {\n        panic(\"error\")\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n    delete(g.adjList, vet)\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n    for v, list := range g.adjList {\n        g.adjList[v] = DeleteSliceElms(list, vet)\n    }\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nfunc (g *graphAdjList) print() {\n    var builder strings.Builder\n    fmt.Printf(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 = \\n\")\n    for k, v := range g.adjList {\n        builder.WriteString(\"\\t\\t\" + strconv.Itoa(k.Val) + \": \")\n        for _, vet := range v {\n            builder.WriteString(strconv.Itoa(vet.Val) + \" \")\n        }\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
graph_adjacency_list.swift
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    public private(set) var adjList: [Vertex: [Vertex]]\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public init(edges: [[Vertex]]) {\n        adjList = [:]\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for edge in edges {\n            addVertex(vet: edge[0])\n            addVertex(vet: edge[1])\n            addEdge(vet1: edge[0], vet2: edge[1])\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    public func size() -> Int {\n        adjList.count\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public func addEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\")\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1]?.append(vet2)\n        adjList[vet2]?.append(vet1)\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    public func removeEdge(vet1: Vertex, vet2: Vertex) {\n        if adjList[vet1] == nil || adjList[vet2] == nil || vet1 == vet2 {\n            fatalError(\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\")\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1]?.removeAll { $0 == vet2 }\n        adjList[vet2]?.removeAll { $0 == vet1 }\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public func addVertex(vet: Vertex) {\n        if adjList[vet] != nil {\n            return\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjList[vet] = []\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    public func removeVertex(vet: Vertex) {\n        if adjList[vet] == nil {\n            fatalError(\"\u041d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\")\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        adjList.removeValue(forKey: vet)\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for key in adjList.keys {\n            adjList[key]?.removeAll { $0 == vet }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    public func print() {\n        Swift.print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        for (vertex, list) in adjList {\n            let list = list.map { $0.val }\n            Swift.print(\"\\(vertex.val): \\(list),\")\n        }\n    }\n}\n
graph_adjacency_list.js
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    adjList;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(edges) {\n        this.adjList = new Map();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    size() {\n        return this.adjList.size;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    addEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    removeEdge(vet1, vet2) {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    addVertex(vet) {\n        if (this.adjList.has(vet)) return;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        this.adjList.set(vet, []);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    removeVertex(vet) {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        this.adjList.delete(vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for (const set of this.adjList.values()) {\n            const index = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    print() {\n        console.log('\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =');\n        for (const [key, value] of this.adjList) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
graph_adjacency_list.ts
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    adjList: Map<Vertex, Vertex[]>;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(edges: Vertex[][]) {\n        this.adjList = new Map();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for (const edge of edges) {\n            this.addVertex(edge[0]);\n            this.addVertex(edge[1]);\n            this.addEdge(edge[0], edge[1]);\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    size(): number {\n        return this.adjList.size;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    addEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        this.adjList.get(vet1).push(vet2);\n        this.adjList.get(vet2).push(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    removeEdge(vet1: Vertex, vet2: Vertex): void {\n        if (\n            !this.adjList.has(vet1) ||\n            !this.adjList.has(vet2) ||\n            vet1 === vet2 ||\n            this.adjList.get(vet1).indexOf(vet2) === -1\n        ) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        this.adjList.get(vet1).splice(this.adjList.get(vet1).indexOf(vet2), 1);\n        this.adjList.get(vet2).splice(this.adjList.get(vet2).indexOf(vet1), 1);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    addVertex(vet: Vertex): void {\n        if (this.adjList.has(vet)) return;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        this.adjList.set(vet, []);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    removeVertex(vet: Vertex): void {\n        if (!this.adjList.has(vet)) {\n            throw new Error('Illegal Argument Exception');\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        this.adjList.delete(vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for (const set of this.adjList.values()) {\n            const index: number = set.indexOf(vet);\n            if (index > -1) {\n                set.splice(index, 1);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    print(): void {\n        console.log('\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =');\n        for (const [key, value] of this.adjList.entries()) {\n            const tmp = [];\n            for (const vertex of value) {\n                tmp.push(vertex.val);\n            }\n            console.log(key.val + ': ' + tmp.join());\n        }\n    }\n}\n
graph_adjacency_list.dart
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList {\n  // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  Map<Vertex, List<Vertex>> adjList = {};\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  GraphAdjList(List<List<Vertex>> edges) {\n    for (List<Vertex> edge in edges) {\n      addVertex(edge[0]);\n      addVertex(edge[1]);\n      addEdge(edge[0], edge[1]);\n    }\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n  int size() {\n    return adjList.length;\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n  void addEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    adjList[vet1]!.add(vet2);\n    adjList[vet2]!.add(vet1);\n  }\n\n  /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n  void removeEdge(Vertex vet1, Vertex vet2) {\n    if (!adjList.containsKey(vet1) ||\n        !adjList.containsKey(vet2) ||\n        vet1 == vet2) {\n      throw ArgumentError;\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    adjList[vet1]!.remove(vet2);\n    adjList[vet2]!.remove(vet1);\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n  void addVertex(Vertex vet) {\n    if (adjList.containsKey(vet)) return;\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    adjList[vet] = [];\n  }\n\n  /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n  void removeVertex(Vertex vet) {\n    if (!adjList.containsKey(vet)) {\n      throw ArgumentError;\n    }\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n    adjList.remove(vet);\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n    adjList.forEach((key, value) {\n      value.remove(vet);\n    });\n  }\n\n  /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n  void printAdjList() {\n    print(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n    adjList.forEach((key, value) {\n      List<int> tmp = [];\n      for (Vertex vertex in value) {\n        tmp.add(vertex.val);\n      }\n      print(\"${key.val}: $tmp,\");\n    });\n  }\n}\n
graph_adjacency_list.rs
/* \u0422\u0438\u043f \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\npub struct GraphAdjList {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    pub adj_list: HashMap<Vertex, Vec<Vertex>>, // maybe HashSet<Vertex> for value part is better?\n}\n\nimpl GraphAdjList {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    pub fn new(edges: Vec<[Vertex; 2]>) -> Self {\n        let mut graph = GraphAdjList {\n            adj_list: HashMap::new(),\n        };\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for edge in edges {\n            graph.add_vertex(edge[0]);\n            graph.add_vertex(edge[1]);\n            graph.add_edge(edge[0], edge[1]);\n        }\n\n        graph\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    #[allow(unused)]\n    pub fn size(&self) -> usize {\n        self.adj_list.len()\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    pub fn add_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        self.adj_list.entry(vet1).or_default().push(vet2);\n        self.adj_list.entry(vet2).or_default().push(vet1);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    #[allow(unused)]\n    pub fn remove_edge(&mut self, vet1: Vertex, vet2: Vertex) {\n        if vet1 == vet2 {\n            panic!(\"value error\");\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        self.adj_list\n            .entry(vet1)\n            .and_modify(|v| v.retain(|&e| e != vet2));\n        self.adj_list\n            .entry(vet2)\n            .and_modify(|v| v.retain(|&e| e != vet1));\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    pub fn add_vertex(&mut self, vet: Vertex) {\n        if self.adj_list.contains_key(&vet) {\n            return;\n        }\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        self.adj_list.insert(vet, vec![]);\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    #[allow(unused)]\n    pub fn remove_vertex(&mut self, vet: Vertex) {\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        self.adj_list.remove(&vet);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for list in self.adj_list.values_mut() {\n            list.retain(|&v| v != vet);\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    pub fn print(&self) {\n        println!(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\");\n        for (vertex, list) in &self.adj_list {\n            let list = list.iter().map(|vertex| vertex.val).collect::<Vec<i32>>();\n            println!(\"{}: {:?},\", vertex.val, list);\n        }\n    }\n}\n
graph_adjacency_list.c
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 */\ntypedef struct AdjListNode {\n    Vertex *vertex;           // \u0412\u0435\u0440\u0448\u0438\u043d\u0430\n    struct AdjListNode *next; // \u0423\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n} AdjListNode;\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0431\u0440\u0430 */\nvoid addEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *node = (AdjListNode *)malloc(sizeof(AdjListNode));\n    node->vertex = vet;\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\n    node->next = head->next;\n    head->next = node;\n}\n\n/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0431\u0440\u0430 */\nvoid removeEdgeHelper(AdjListNode *head, Vertex *vet) {\n    AdjListNode *pre = head;\n    AdjListNode *cur = head->next;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0443\u0437\u0435\u043b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 vet\n    while (cur != NULL && cur->vertex != vet) {\n        pre = cur;\n        cur = cur->next;\n    }\n    if (cur == NULL)\n        return;\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0443\u0437\u0435\u043b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 vet\n    pre->next = cur->next;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(cur);\n}\n\n/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\ntypedef struct {\n    AdjListNode *heads[MAX_SIZE]; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0443\u0437\u043b\u043e\u0432\n    int size;                     // \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u0437\u043b\u043e\u0432\n} GraphAdjList;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nGraphAdjList *newGraphAdjList() {\n    GraphAdjList *graph = (GraphAdjList *)malloc(sizeof(GraphAdjList));\n    if (!graph) {\n        return NULL;\n    }\n    graph->size = 0;\n    for (int i = 0; i < MAX_SIZE; i++) {\n        graph->heads[i] = NULL;\n    }\n    return graph;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delGraphAdjList(GraphAdjList *graph) {\n    for (int i = 0; i < graph->size; i++) {\n        AdjListNode *cur = graph->heads[i];\n        while (cur != NULL) {\n            AdjListNode *next = cur->next;\n            if (cur != graph->heads[i]) {\n                free(cur);\n            }\n            cur = next;\n        }\n        free(graph->heads[i]->vertex);\n        free(graph->heads[i]);\n    }\n    free(graph);\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 */\nAdjListNode *findNode(GraphAdjList *graph, Vertex *vet) {\n    for (int i = 0; i < graph->size; i++) {\n        if (graph->heads[i]->vertex == vet) {\n            return graph->heads[i];\n        }\n    }\n    return NULL;\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\nvoid addEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL && head1 != head2);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    addEdgeHelper(head1, vet2);\n    addEdgeHelper(head2, vet1);\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\nvoid removeEdge(GraphAdjList *graph, Vertex *vet1, Vertex *vet2) {\n    AdjListNode *head1 = findNode(graph, vet1);\n    AdjListNode *head2 = findNode(graph, vet2);\n    assert(head1 != NULL && head2 != NULL);\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    removeEdgeHelper(head1, head2->vertex);\n    removeEdgeHelper(head2, head1->vertex);\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nvoid addVertex(GraphAdjList *graph, Vertex *vet) {\n    assert(graph != NULL && graph->size < MAX_SIZE);\n    AdjListNode *head = (AdjListNode *)malloc(sizeof(AdjListNode));\n    head->vertex = vet;\n    head->next = NULL;\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    graph->heads[graph->size++] = head;\n}\n\n/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\nvoid removeVertex(GraphAdjList *graph, Vertex *vet) {\n    AdjListNode *node = findNode(graph, vet);\n    assert(node != NULL);\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n    AdjListNode *cur = node, *pre = NULL;\n    while (cur) {\n        pre = cur;\n        cur = cur->next;\n        free(pre);\n    }\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n    for (int i = 0; i < graph->size; i++) {\n        cur = graph->heads[i];\n        pre = NULL;\n        while (cur) {\n            pre = cur;\n            cur = cur->next;\n            if (cur && cur->vertex == vet) {\n                pre->next = cur->next;\n                free(cur);\n                break;\n            }\n        }\n    }\n    // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u043f\u0435\u0440\u0435\u0434, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043f\u0440\u043e\u0431\u0435\u043b\n    int i;\n    for (i = 0; i < graph->size; i++) {\n        if (graph->heads[i] == node)\n            break;\n    }\n    for (int j = i; j < graph->size - 1; j++) {\n        graph->heads[j] = graph->heads[j + 1];\n    }\n    graph->size--;\n    free(vet);\n}\n
graph_adjacency_list.kt
/* \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\nclass GraphAdjList(edges: Array<Array<Vertex?>>) {\n    // \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    val adjList = HashMap<Vertex, MutableList<Vertex>>()\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n        for (edge in edges) {\n            addVertex(edge[0]!!)\n            addVertex(edge[1]!!)\n            addEdge(edge[0]!!, edge[1]!!)\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d */\n    fun size(): Int {\n        return adjList.size\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    fun addEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1]?.add(vet2)\n        adjList[vet2]?.add(vet1)\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 */\n    fun removeEdge(vet1: Vertex, vet2: Vertex) {\n        if (!adjList.containsKey(vet1) || !adjList.containsKey(vet2) || vet1 == vet2)\n            throw IllegalArgumentException()\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n        adjList[vet1]?.remove(vet2)\n        adjList[vet2]?.remove(vet1)\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    fun addVertex(vet: Vertex) {\n        if (adjList.containsKey(vet))\n            return\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n        adjList[vet] = mutableListOf()\n    }\n\n    /* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b */\n    fun removeVertex(vet: Vertex) {\n        if (!adjList.containsKey(vet))\n            throw IllegalArgumentException()\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n        adjList.remove(vet)\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n        for (list in adjList.values) {\n            list.remove(vet)\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 */\n    fun print() {\n        println(\"\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 =\")\n        for (pair in adjList.entries) {\n            val tmp = mutableListOf<Int>()\n            for (vertex in pair.value) {\n                tmp.add(vertex._val)\n            }\n            println(\"${pair.key._val}: $tmp,\")\n        }\n    }\n}\n
graph_adjacency_list.rb
=begin\nFile: graph_adjacency_list.rb\nCreated Time: 2024-04-25\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/vertex'\n\n# ## \u041a\u043b\u0430\u0441\u0441 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ###\nclass GraphAdjList\n  attr_reader :adj_list\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(edges)\n    # \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0433\u0434\u0435 key \u2014 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 value \u2014 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    @adj_list = {}\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0431\u0440\u0430\n    for edge in edges\n      add_vertex(edge[0])\n      add_vertex(edge[1])\n      add_edge(edge[0], edge[1])\n    end\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u0432\u0435\u0440\u0448\u0438\u043d ###\n  def size\n    @adj_list.length\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 ###\n  def add_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    @adj_list[vet1] << vet2\n    @adj_list[vet2] << vet1\n  end\n\n  # ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 ###\n  def remove_edge(vet1, vet2)\n    raise ArgumentError if !@adj_list.include?(vet1) || !@adj_list.include?(vet2)\n\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0440\u0435\u0431\u0440\u043e vet1 - vet2\n    @adj_list[vet1].delete(vet2)\n    @adj_list[vet2].delete(vet1)\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b ###\n  def add_vertex(vet)\n    return if @adj_list.include?(vet)\n\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438\n    @adj_list[vet] = []\n  end\n\n  # ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b ###\n  def remove_vertex(vet)\n    raise ArgumentError unless @adj_list.include?(vet)\n\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 vet\n    @adj_list.delete(vet)\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 vet\n    for vertex in @adj_list\n      @adj_list[vertex.first].delete(vet) if @adj_list[vertex.first].include?(vet)\n    end\n  end\n\n  # ## \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ###\n  def __print__\n    puts '\u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 ='\n    for vertex in @adj_list\n      tmp = @adj_list[vertex.first].map { |v| v.val }\n      puts \"#{vertex.first.val}: #{tmp},\"\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_graph/graph_operations/#923","title":"9.2.3 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438","text":"

\u041f\u0443\u0441\u0442\u044c \u0432 \u0433\u0440\u0430\u0444\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \\(n\\) \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \\(m\\) \u0440\u0435\u0431\u0435\u0440. \u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 9-2 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0438 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438. \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a) \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0437 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (\u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u0433\u0434\u0435 \u0432\u0441\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u044b \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 9-2 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438

\u041c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a) \u0421\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 (\u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430) \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0440\u0435\u0431\u0440\u0430 \\(O(1)\\) \\(O(n)\\) \\(O(1)\\) \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \\(O(n)\\) \\(O(1)\\) \\(O(1)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n)\\) \u0417\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \\(O(n^2)\\) \\(O(n + m)\\) \\(O(n + m)\\)

\u0415\u0441\u043b\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0438\u043c \u0438 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0438 \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u0440\u0435\u0431\u0440\u0430\u043c\u0438 \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0447\u0430\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438\u043b\u0438 \u043e\u0434\u043d\u043e \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u0435. \u0412 \u0446\u0435\u043b\u043e\u043c \u043c\u0430\u0442\u0440\u0438\u0446\u0430 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0432\u043e\u043f\u043b\u043e\u0449\u0430\u0435\u0442 \u043f\u0440\u0438\u043d\u0446\u0438\u043f \"\u043e\u0431\u043c\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f\", \u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 - \u043f\u0440\u0438\u043d\u0446\u0438\u043f \"\u043e\u0431\u043c\u0435\u043d \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\".

"},{"location":"chapter_graph/graph_traversal/","title":"9.3 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0433\u0440\u0430\u0444\u0430","text":"

\u0414\u0435\u0440\u0435\u0432\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \"\u043e\u0434\u0438\u043d \u043a\u043e \u043c\u043d\u043e\u0433\u0438\u043c\", \u0430 \u0433\u0440\u0430\u0444 \u0438\u043c\u0435\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u044b \u0438 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0440\u0430\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \"\u043c\u043d\u043e\u0433\u0438\u0435 \u043a\u043e \u043c\u043d\u043e\u0433\u0438\u043c\". \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e \u043a\u0430\u043a \u0447\u0430\u0441\u0442\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u0433\u0440\u0430\u0444\u0430. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u043d\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u0431\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u0444\u0430.

\u0418 \u0433\u0440\u0430\u0444\u044b, \u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u0445\u043e\u0434\u0430. \u0421\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u0444\u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u0442\u0438\u043f\u0430: \u043e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0438 \u043e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443.

"},{"location":"chapter_graph/graph_traversal/#931","title":"9.3.1 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443","text":"

\u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 - \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0445\u043e\u0434\u0430 \"\u043e\u0442 \u0431\u043b\u0438\u0437\u043a\u043e\u0433\u043e \u043a \u0434\u0430\u043b\u0435\u043a\u043e\u043c\u0443\": \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0441\u0435\u0449\u0430\u0435\u043c \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0441\u043b\u043e\u0439 \u0437\u0430 \u0441\u043b\u043e\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u043c\u0441\u044f \u043d\u0430\u0440\u0443\u0436\u0443. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-9, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043b\u0435\u0432\u043e\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0443\u0433\u043b\u0443, \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0437\u0430\u0442\u0435\u043c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-9 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0433\u0440\u0430\u0444\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443

"},{"location":"chapter_graph/graph_traversal/#1","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

BFS \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435. \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \"\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\", \u0447\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0438\u0434\u0435\u0435 BFS \"\u043e\u0442 \u0431\u043b\u0438\u0437\u043a\u043e\u0433\u043e \u043a \u0434\u0430\u043b\u0435\u043a\u043e\u043c\u0443\".

  1. \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043e\u0431\u0445\u043e\u0434\u0430 startVet \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0446\u0438\u043a\u043b.
  2. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0446\u0438\u043a\u043b\u0430 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0444\u0430\u043a\u0442 \u0435\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.
  3. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433 2. \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.

\u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u043e\u0442\u0432\u0440\u0430\u0442\u0438\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 \u0432\u0435\u0440\u0448\u0438\u043d, \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 visited , \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c\u0441\u044f, \u043a\u0430\u043a\u0438\u0435 \u0443\u0437\u043b\u044b \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b.

Tip

\u0425\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e key \u0438 \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 value . \u041e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u0438\u0441\u043a \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 key \u0437\u0430 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 key \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0443\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_bfs.py
def graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443\"\"\"\n    # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    res = []\n    # \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    visited = set[Vertex]([start_vet])\n    # \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    que = deque[Vertex]([start_vet])\n    # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while len(que) > 0:\n        vet = que.popleft()  # \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.append(vet)  # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for adj_vet in graph.adj_list[vet]:\n            if adj_vet in visited:\n                continue  # \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            que.append(adj_vet)  # \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.add(adj_vet)  # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res\n
graph_bfs.cpp
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nvector<Vertex *> graphBFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    vector<Vertex *> res;\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    unordered_set<Vertex *> visited = {startVet};\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    queue<Vertex *> que;\n    que.push(startVet);\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (!que.empty()) {\n        Vertex *vet = que.front();\n        que.pop();          // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.push_back(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (auto adjVet : graph.adjList[vet]) {\n            if (visited.count(adjVet))\n                continue;            // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            que.push(adjVet);        // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.emplace(adjVet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res;\n}\n
graph_bfs.java
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    List<Vertex> res = new ArrayList<>();\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    Set<Vertex> visited = new HashSet<>();\n    visited.add(startVet);\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    Queue<Vertex> que = new LinkedList<>();\n    que.offer(startVet);\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (!que.isEmpty()) {\n        Vertex vet = que.poll(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.add(vet);            // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (Vertex adjVet : graph.adjList.get(vet)) {\n            if (visited.contains(adjVet))\n                continue;        // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            que.offer(adjVet);   // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.add(adjVet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res;\n}\n
graph_bfs.cs
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nList<Vertex> GraphBFS(GraphAdjList graph, Vertex startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    List<Vertex> res = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    HashSet<Vertex> visited = [startVet];\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    Queue<Vertex> que = new();\n    que.Enqueue(startVet);\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (que.Count > 0) {\n        Vertex vet = que.Dequeue(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.Add(vet);               // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        foreach (Vertex adjVet in graph.adjList[vet]) {\n            if (visited.Contains(adjVet)) {\n                continue;          // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            }\n            que.Enqueue(adjVet);   // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.Add(adjVet);   // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res;\n}\n
graph_bfs.go
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunc graphBFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    res := make([]Vertex, 0)\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    visited := make(map[Vertex]struct{})\n    visited[startVet] = struct{}{}\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS, \u0441\u0440\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0438\u043c\u0438\u0442\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queue := make([]Vertex, 0)\n    queue = append(queue, startVet)\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for len(queue) > 0 {\n        // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        vet := queue[0]\n        queue = queue[1:]\n        // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        res = append(res, vet)\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for _, adjVet := range g.adjList[vet] {\n            _, isExist := visited[adjVet]\n            // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            if !isExist {\n                queue = append(queue, adjVet)\n                visited[adjVet] = struct{}{}\n            }\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res\n}\n
graph_bfs.swift
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunc graphBFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    var res: [Vertex] = []\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    var visited: Set<Vertex> = [startVet]\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    var que: [Vertex] = [startVet]\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while !que.isEmpty {\n        let vet = que.removeFirst() // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.append(vet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for adjVet in graph.adjList[vet] ?? [] {\n            if visited.contains(adjVet) {\n                continue // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            }\n            que.append(adjVet) // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.insert(adjVet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res\n}\n
graph_bfs.js
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunction graphBFS(graph, startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    const res = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    const visited = new Set();\n    visited.add(startVet);\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    const que = [startVet];\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (que.length) {\n        const vet = que.shift(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            }\n            que.push(adjVet); // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.add(adjVet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res;\n}\n
graph_bfs.ts
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunction graphBFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    const res: Vertex[] = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    const visited: Set<Vertex> = new Set();\n    visited.add(startVet);\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    const que = [startVet];\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (que.length) {\n        const vet = que.shift(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (const adjVet of graph.adjList.get(vet) ?? []) {\n            if (visited.has(adjVet)) {\n                continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            }\n            que.push(adjVet); // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.add(adjVet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res;\n}\n
graph_bfs.dart
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nList<Vertex> graphBFS(GraphAdjList graph, Vertex startVet) {\n  // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  List<Vertex> res = [];\n  // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n  Set<Vertex> visited = {};\n  visited.add(startVet);\n  // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n  Queue<Vertex> que = Queue();\n  que.add(startVet);\n  // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  while (que.isNotEmpty) {\n    Vertex vet = que.removeFirst(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    res.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (Vertex adjVet in graph.adjList[vet]!) {\n      if (visited.contains(adjVet)) {\n        continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n      }\n      que.add(adjVet); // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n      visited.add(adjVet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    }\n  }\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  return res;\n}\n
graph_bfs.rs
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfn graph_bfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    let mut res = vec![];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    let mut visited = HashSet::new();\n    visited.insert(start_vet);\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    let mut que = VecDeque::new();\n    que.push_back(start_vet);\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while let Some(vet) = que.pop_front() {\n        res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        if let Some(adj_vets) = graph.adj_list.get(&vet) {\n            for &adj_vet in adj_vets {\n                if visited.contains(&adj_vet) {\n                    continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n                }\n                que.push_back(adj_vet); // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n                visited.insert(adj_vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n            }\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    res\n}\n
graph_bfs.c
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0443\u0437\u043b\u043e\u0432 */\ntypedef struct {\n    Vertex *vertices[MAX_SIZE];\n    int front, rear, size;\n} Queue;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nQueue *newQueue() {\n    Queue *q = (Queue *)malloc(sizeof(Queue));\n    q->front = q->rear = q->size = 0;\n    return q;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nint isEmpty(Queue *q) {\n    return q->size == 0;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid enqueue(Queue *q, Vertex *vet) {\n    q->vertices[q->rear] = vet;\n    q->rear = (q->rear + 1) % MAX_SIZE;\n    q->size++;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nVertex *dequeue(Queue *q) {\n    Vertex *vet = q->vertices[q->front];\n    q->front = (q->front + 1) % MAX_SIZE;\n    q->size--;\n    return vet;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0431\u044b\u043b\u0430 \u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430 */\nint isVisited(Vertex **visited, int size, Vertex *vet) {\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0443\u0437\u0435\u043b \u043e\u0431\u0445\u043e\u0434\u043e\u043c \u0437\u0430 O(n) \u0432\u0440\u0435\u043c\u0435\u043d\u0438\n    for (int i = 0; i < size; i++) {\n        if (visited[i] == vet)\n            return 1;\n    }\n    return 0;\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nvoid graphBFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize, Vertex **visited, int *visitedSize) {\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    Queue *queue = newQueue();\n    enqueue(queue, startVet);\n    visited[(*visitedSize)++] = startVet;\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (!isEmpty(queue)) {\n        Vertex *vet = dequeue(queue); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res[(*resSize)++] = vet;      // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        AdjListNode *node = findNode(graph, vet);\n        while (node != NULL) {\n            // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            if (!isVisited(visited, *visitedSize, node->vertex)) {\n                enqueue(queue, node->vertex);             // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n                visited[(*visitedSize)++] = node->vertex; // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n            }\n            node = node->next;\n        }\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(queue);\n}\n
graph_bfs.kt
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfun graphBFS(graph: GraphAdjList, startVet: Vertex): MutableList<Vertex?> {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    val res = mutableListOf<Vertex?>()\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    val visited = HashSet<Vertex>()\n    visited.add(startVet)\n    // \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n    val que = LinkedList<Vertex>()\n    que.offer(startVet)\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    while (!que.isEmpty()) {\n        val vet = que.poll() // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.add(vet)         // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        for (adjVet in graph.adjList[vet]!!) {\n            if (visited.contains(adjVet))\n                continue        // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            que.offer(adjVet)   // \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            visited.add(adjVet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res\n}\n
graph_bfs.rb
=begin\nFile: graph_bfs.rb\nCreated Time: 2024-04-25\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire 'set'\nrequire_relative './graph_adjacency_list'\nrequire_relative '../utils/vertex'\n\n# ## \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 ###\ndef graph_bfs(graph, start_vet)\n  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  res = []\n  # \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n  visited = Set.new([start_vet])\n  # \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 BFS\n  que = [start_vet]\n  # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b vet, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0446\u0438\u043a\u043b, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  while que.length > 0\n    vet = que.shift # \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    res << vet # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for adj_vet in graph.adj_list[vet]\n      next if visited.include?(adj_vet) # \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n      que << adj_vet # \u041f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n      visited.add(adj_vet) # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    end\n  end\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u043e\u0434 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u0435\u043d, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u044f\u0442\u044c\u0441\u044f \u0441 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u043c\u0438 \u043d\u0438\u0436\u0435 \u0434\u043b\u044f \u043b\u0443\u0447\u0448\u0435\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f.

<1><2><3><4><5><6><7><8><9><10><11>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-10 \u00a0 \u0428\u0430\u0433\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u0444\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443

\u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439?

\u041d\u0435\u0442. \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043e\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \"\u043e\u0442 \u0431\u043b\u0438\u0437\u043a\u043e\u0433\u043e \u043a \u0434\u0430\u043b\u0435\u043a\u043e\u043c\u0443\", \u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u043d\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-10 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d \\(1\\) \u0438 \\(3\\) , \u0430 \u0442\u0430\u043a\u0436\u0435 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \\(2\\), \\(4\\), \\(6\\) .

"},{"location":"chapter_graph/graph_traversal/#2","title":"2. \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c: \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443 \u043f\u043e\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0438\u0437 \u043d\u0435\u0435, \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(|V|)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438; \u043f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0433\u0440\u0430\u0444 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439, \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u043f\u043e \\(2\\) \u0440\u0430\u0437\u0430, \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(2|E|)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438; \u0432 \u0441\u0443\u043c\u043c\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \\(O(|V| + |E|)\\) .

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c: \u0441\u043f\u0438\u0441\u043e\u043a res , \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 visited \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c que \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u043e \\(|V|\\) \u0432\u0435\u0440\u0448\u0438\u043d, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \\(O(|V|)\\) \u043f\u0430\u043c\u044f\u0442\u0438.

"},{"location":"chapter_graph/graph_traversal/#932","title":"9.3.2 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443","text":"

\u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 - \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0445\u043e\u0434\u0430, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0443\u0442 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0430, \u0430 \u043a\u043e\u0433\u0434\u0430 \u0434\u0430\u043b\u044c\u0448\u0435 \u0438\u0434\u0442\u0438 \u043d\u0435\u043b\u044c\u0437\u044f, \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430\u0437\u0430\u0434. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 9-11, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043b\u0435\u0432\u043e\u043c \u0432\u0435\u0440\u0445\u043d\u0435\u043c \u0443\u0433\u043b\u0443, \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0441\u043c\u0435\u0436\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438\u0434\u0435\u043c \u0434\u043e \u0443\u043f\u043e\u0440\u0430, \u0437\u0430\u0442\u0435\u043c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u043d\u0430\u0437\u0430\u0434, \u0441\u043d\u043e\u0432\u0430 \u0438\u0434\u0435\u043c \u0434\u043e \u0443\u043f\u043e\u0440\u0430 \u0438 \u0442\u0430\u043a \u0434\u0430\u043b\u0435\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-11 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0433\u0440\u0430\u0444\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443

"},{"location":"chapter_graph/graph_traversal/#1_1","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0422\u0430\u043a\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0448\u0430\u0431\u043b\u043e\u043d \"\u0434\u043e\u0439\u0442\u0438 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f\" \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e. \u041f\u043e\u0434\u043e\u0431\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0443 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443, \u0432 \u043e\u0431\u0445\u043e\u0434\u0435 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440 visited \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0438\u0437\u0431\u0435\u0433\u0430\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby graph_dfs.py
def dfs(graph: GraphAdjList, visited: set[Vertex], res: list[Vertex], vet: Vertex):\n    \"\"\"\u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443\"\"\"\n    res.append(vet)  # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.add(vet)  # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for adjVet in graph.adj_list[vet]:\n        if adjVet in visited:\n            continue  # \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet)\n\ndef graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> list[Vertex]:\n    \"\"\"\u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443\"\"\"\n    # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    res = []\n    # \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    visited = set[Vertex]()\n    dfs(graph, visited, res, start_vet)\n    return res\n
graph_dfs.cpp
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid dfs(GraphAdjList &graph, unordered_set<Vertex *> &visited, vector<Vertex *> &res, Vertex *vet) {\n    res.push_back(vet);   // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.emplace(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (Vertex *adjVet : graph.adjList[vet]) {\n        if (visited.count(adjVet))\n            continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nvector<Vertex *> graphDFS(GraphAdjList &graph, Vertex *startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    vector<Vertex *> res;\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    unordered_set<Vertex *> visited;\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
graph_dfs.java
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid dfs(GraphAdjList graph, Set<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.add(vet);     // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (Vertex adjVet : graph.adjList.get(vet)) {\n        if (visited.contains(adjVet))\n            continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    List<Vertex> res = new ArrayList<>();\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    Set<Vertex> visited = new HashSet<>();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
graph_dfs.cs
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid DFS(GraphAdjList graph, HashSet<Vertex> visited, List<Vertex> res, Vertex vet) {\n    res.Add(vet);     // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.Add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    foreach (Vertex adjVet in graph.adjList[vet]) {\n        if (visited.Contains(adjVet)) {\n            continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        DFS(graph, visited, res, adjVet);\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nList<Vertex> GraphDFS(GraphAdjList graph, Vertex startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    List<Vertex> res = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    HashSet<Vertex> visited = [];\n    DFS(graph, visited, res, startVet);\n    return res;\n}\n
graph_dfs.go
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfunc dfs(g *graphAdjList, visited map[Vertex]struct{}, res *[]Vertex, vet Vertex) {\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f append \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043d\u043e\u0432\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u043d\u043e\u0432\u043e\u043c\u0443 \u0441\u0440\u0435\u0437\u0443\n    *res = append(*res, vet)\n    visited[vet] = struct{}{}\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for _, adjVet := range g.adjList[vet] {\n        _, isExist := visited[adjVet]\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        if !isExist {\n            dfs(g, visited, res, adjVet)\n        }\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunc graphDFS(g *graphAdjList, startVet Vertex) []Vertex {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    res := make([]Vertex, 0)\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    visited := make(map[Vertex]struct{})\n    dfs(g, visited, &res, startVet)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    return res\n}\n
graph_dfs.swift
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfunc dfs(graph: GraphAdjList, visited: inout Set<Vertex>, res: inout [Vertex], vet: Vertex) {\n    res.append(vet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.insert(vet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for adjVet in graph.adjList[vet] ?? [] {\n        if visited.contains(adjVet) {\n            continue // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph: graph, visited: &visited, res: &res, vet: adjVet)\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunc graphDFS(graph: GraphAdjList, startVet: Vertex) -> [Vertex] {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    var res: [Vertex] = []\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    var visited: Set<Vertex> = []\n    dfs(graph: graph, visited: &visited, res: &res, vet: startVet)\n    return res\n}\n
graph_dfs.js
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunction dfs(graph, visited, res, vet) {\n    res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunction graphDFS(graph, startVet) {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    const res = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    const visited = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
graph_dfs.ts
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfunction dfs(\n    graph: GraphAdjList,\n    visited: Set<Vertex>,\n    res: Vertex[],\n    vet: Vertex\n): void {\n    res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (const adjVet of graph.adjList.get(vet)) {\n        if (visited.has(adjVet)) {\n            continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet);\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfunction graphDFS(graph: GraphAdjList, startVet: Vertex): Vertex[] {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    const res: Vertex[] = [];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    const visited: Set<Vertex> = new Set();\n    dfs(graph, visited, res, startVet);\n    return res;\n}\n
graph_dfs.dart
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid dfs(\n  GraphAdjList graph,\n  Set<Vertex> visited,\n  List<Vertex> res,\n  Vertex vet,\n) {\n  res.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n  visited.add(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n  // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  for (Vertex adjVet in graph.adjList[vet]!) {\n    if (visited.contains(adjVet)) {\n      continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    dfs(graph, visited, res, adjVet);\n  }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nList<Vertex> graphDFS(GraphAdjList graph, Vertex startVet) {\n  // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  List<Vertex> res = [];\n  // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n  Set<Vertex> visited = {};\n  dfs(graph, visited, res, startVet);\n  return res;\n}\n
graph_dfs.rs
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfn dfs(graph: &GraphAdjList, visited: &mut HashSet<Vertex>, res: &mut Vec<Vertex>, vet: Vertex) {\n    res.push(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.insert(vet); // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n                         // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    if let Some(adj_vets) = graph.adj_list.get(&vet) {\n        for &adj_vet in adj_vets {\n            if visited.contains(&adj_vet) {\n                continue; // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n            }\n            // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            dfs(graph, visited, res, adj_vet);\n        }\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfn graph_dfs(graph: GraphAdjList, start_vet: Vertex) -> Vec<Vertex> {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    let mut res = vec![];\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    let mut visited = HashSet::new();\n    dfs(&graph, &mut visited, &mut res, start_vet);\n\n    res\n}\n
graph_dfs.c
/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0431\u044b\u043b\u0430 \u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430 */\nint isVisited(Vertex **res, int size, Vertex *vet) {\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0443\u0437\u0435\u043b \u043e\u0431\u0445\u043e\u0434\u043e\u043c \u0437\u0430 O(n) \u0432\u0440\u0435\u043c\u0435\u043d\u0438\n    for (int i = 0; i < size; i++) {\n        if (res[i] == vet) {\n            return 1;\n        }\n    }\n    return 0;\n}\n\n/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid dfs(GraphAdjList *graph, Vertex **res, int *resSize, Vertex *vet) {\n    // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    res[(*resSize)++] = vet;\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    AdjListNode *node = findNode(graph, vet);\n    while (node != NULL) {\n        // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        if (!isVisited(res, *resSize, node->vertex)) {\n            // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n            dfs(graph, res, resSize, node->vertex);\n        }\n        node = node->next;\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nvoid graphDFS(GraphAdjList *graph, Vertex *startVet, Vertex **res, int *resSize) {\n    dfs(graph, res, resSize, startVet);\n}\n
graph_dfs.kt
/* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfun dfs(\n    graph: GraphAdjList,\n    visited: MutableSet<Vertex?>,\n    res: MutableList<Vertex?>,\n    vet: Vertex?\n) {\n    res.add(vet)     // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    visited.add(vet) // \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    for (adjVet in graph.adjList[vet]!!) {\n        if (visited.contains(adjVet))\n            continue  // \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n        dfs(graph, visited, res, adjVet)\n    }\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\nfun graphDFS(graph: GraphAdjList, startVet: Vertex?): MutableList<Vertex?> {\n    // \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n    val res = mutableListOf<Vertex?>()\n    // \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n    val visited = HashSet<Vertex?>()\n    dfs(graph, visited, res, startVet)\n    return res\n}\n
graph_dfs.rb
=begin\nFile: graph_dfs.rb\nCreated Time: 2024-04-25\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire 'set'\nrequire_relative './graph_adjacency_list'\nrequire_relative '../utils/vertex'\n\n# ## \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 ###\ndef dfs(graph, visited, res, vet)\n  res << vet # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n  visited.add(vet) # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n=begin\nFile: graph_dfs.rb\nCreated Time: 2024-04-25\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire 'set'\nrequire_relative './graph_adjacency_list'\nrequire_relative '../utils/vertex'\n\n# ## \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 ###\ndef dfs(graph, visited, res, vet)\n  res << vet # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n  visited.add(vet) # \u041e\u0442\u043c\u0435\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0430\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  for adj_vet in graph.adj_list[vet]\n    next if visited.include?(adj_vet) # \u041f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n    dfs(graph, visited, res, adj_vet)\n  end\nend\n\n# ## \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 ###\ndef graph_dfs(graph, start_vet)\n  # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043b\u044f \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b\n  # \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\n  res = []\n  # \u0425\u0435\u0448-\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d\n  visited = Set.new\n  dfs(graph, visited, res, start_vet)\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435.

\u0427\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0440\u0438\u0441\u0443\u043d\u043a\u0438 \u043d\u0438\u0436\u0435 \u0441 \u043a\u043e\u0434\u043e\u043c \u0438 \u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0432\u0435\u0441\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 DFS, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043c\u043e\u043c\u0435\u043d\u0442\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430.

<1><2><3><4><5><6><7><8><9><10><11>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 9-12 \u00a0 \u0428\u0430\u0433\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u0433\u0440\u0430\u0444\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443

\u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439?

\u041a\u0430\u043a \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443, \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c DFS \u0442\u043e\u0436\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u0433\u043b\u0443\u0431\u0438\u0442\u044c\u0441\u044f \u0432 \u043b\u044e\u0431\u043e\u0435 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c, \u0438 \u0432\u0441\u0435 \u0442\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\u043c\u0438 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443.

\u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043e\u0431\u0445\u043e\u0434 \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \"\u043a\u043e\u0440\u0435\u043d\u044c \\(\\rightarrow\\) \u043b\u0435\u0432\u043e \\(\\rightarrow\\) \u043f\u0440\u0430\u0432\u043e\", \"\u043b\u0435\u0432\u043e \\(\\rightarrow\\) \u043a\u043e\u0440\u0435\u043d\u044c \\(\\rightarrow\\) \u043f\u0440\u0430\u0432\u043e\" \u0438 \"\u043b\u0435\u0432\u043e \\(\\rightarrow\\) \u043f\u0440\u0430\u0432\u043e \\(\\rightarrow\\) \u043a\u043e\u0440\u0435\u043d\u044c\" \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043f\u0440\u044f\u043c\u043e\u043c\u0443, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0430\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u041e\u043d\u0438 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0442\u0440\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430 \u043e\u0431\u0445\u043e\u0434\u0430, \u043d\u043e \u0432\u0441\u0435 \u043e\u043d\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043e\u0431\u0445\u043e\u0434\u0443 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443.

"},{"location":"chapter_graph/graph_traversal/#2_1","title":"2. \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c: \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u043f\u043e \\(1\\) \u0440\u0430\u0437\u0443, \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(|V|)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438; \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u044b \u043f\u043e \\(2\\) \u0440\u0430\u0437\u0430, \u0447\u0442\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(2|E|)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438; \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \\(O(|V| + |E|)\\) .

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c: \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 res \u0438 \u0445\u0435\u0448-\u043d\u0430\u0431\u043e\u0440\u0435 visited \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \\(|V|\\) , \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0442\u043e\u0436\u0435 \u0440\u0430\u0432\u043d\u0430 \\(|V|\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \\(O(|V|)\\) \u043f\u0430\u043c\u044f\u0442\u0438.

"},{"location":"chapter_graph/summary/","title":"9.4 \u00a0 \u041a\u0440\u0430\u0442\u043a\u0438\u0435 \u0438\u0442\u043e\u0433\u0438","text":""},{"location":"chapter_graph/summary/#1","title":"1. \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_graph/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u0427\u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0443\u0442\u0435\u043c: \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u0440?

\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u043e\u0432\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u0412\u0438\u043a\u0438\u043f\u0435\u0434\u0438\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f: \u0432 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u043f\u0443\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \"\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u0440\", \u0430 \u0432 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 - \u043a\u0430\u043a \"\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\". \u0412 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u0430\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a: In graph theory, a path in a graph is a finite or infinite sequence of edges which joins a sequence of vertices.

\u0412 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u043f\u0443\u0442\u044c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0431\u0435\u0440, \u0430 \u043d\u0435 \u043a\u0430\u043a \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0431\u0435\u0440, \u0438 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0440\u0435\u0431\u0440\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0432\u043e\u0439 \u043f\u0443\u0442\u044c.

Q: \u0415\u0441\u0442\u044c \u043b\u0438 \u0432 \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0434\u043e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u043b\u044c\u0437\u044f \u0434\u043e\u0439\u0442\u0438?

\u0412 \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0438\u0437 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u043f\u043e \u043a\u0440\u0430\u0439\u043d\u0435\u0439 \u043c\u0435\u0440\u0435 \u043e\u0434\u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u043e\u0439. \u0427\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438 \u0432\u0435\u0441\u044c \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0433\u0440\u0430\u0444, \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u044b\u0445 \u0442\u043e\u0447\u0435\u043a \u0438 \u043e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0441\u0432\u044f\u0437\u043d\u044b\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0433\u0440\u0430\u0444\u0430.

Q: \u0415\u0441\u0442\u044c \u043b\u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \"\u0432\u0441\u0435\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441 \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439\" \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438?

\u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c. \u041d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d \u0438\u043b\u0438 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432\u0435\u0440\u0448\u0438\u043d; \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u044d\u043a\u0441\u0442\u0440\u0435\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c.

"},{"location":"chapter_greedy/","title":"\u0413\u043b\u0430\u0432\u0430 15. \u00a0 \u0416\u0430\u0434\u043d\u043e\u0441\u0442\u044c","text":"

Abstract

\u041f\u043e\u0434\u0441\u043e\u043b\u043d\u0443\u0445 \u043f\u043e\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043a \u0441\u043e\u043b\u043d\u0446\u0443, \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0441\u0442\u0440\u0435\u043c\u044f\u0441\u044c \u043a \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c \u0434\u043b\u044f \u0440\u043e\u0441\u0442\u0430.

\u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0446\u0435\u043f\u043e\u0447\u043a\u0443 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0432\u044b\u0431\u043e\u0440\u043e\u0432 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0435\u043c\u0443 \u043e\u0442\u0432\u0435\u0442\u0443.

"},{"location":"chapter_greedy/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_greedy/fractional_knapsack_problem/","title":"15.2 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435","text":"

Question

\u0414\u0430\u043d\u043e \\(n\\) \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432. \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(i\\) \u0440\u0430\u0432\u0435\u043d \\(wgt[i-1]\\), \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(val[i-1]\\), \u0442\u0430\u043a\u0436\u0435 \u0434\u0430\u043d \u0440\u044e\u043a\u0437\u0430\u043a \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\u044e \\(cap\\). \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0440\u0430\u0437, \u043d\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0437\u044f\u0442\u044c \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430, \u0430 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0432\u0437\u044f\u0442\u043e\u043c\u0443 \u0432\u0435\u0441\u0443. \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043f\u0440\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-3.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435

\u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u0432 \u0446\u0435\u043b\u043e\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0440\u044e\u043a\u0437\u0430\u043a\u0435 0-1: \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(i\\) \u0438 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \\(c\\), \u0430 \u0446\u0435\u043b\u044c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u044e\u043a\u0437\u0430\u043a\u0430.

\u041e\u0442\u043b\u0438\u0447\u0438\u0435 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-4, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u043f\u043e\u0440\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0432\u0435\u0441\u0443.

  1. \u0414\u043b\u044f \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(i\\) \u0435\u0433\u043e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432\u0435\u0441\u0430 \u0440\u0430\u0432\u043d\u0430 \\(val[i-1] / wgt[i-1]\\), \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u043d\u043e - \u0443\u0434\u0435\u043b\u044c\u043d\u0430\u044f \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c.
  2. \u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \\(i\\) \u0432\u0435\u0441\u043e\u043c \\(w\\), \u0442\u043e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u0441\u044f \u043d\u0430 \\(w \\times val[i-1] / wgt[i-1]\\).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-4 \u00a0 \u0426\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432\u0435\u0441\u0430

"},{"location":"chapter_greedy/fractional_knapsack_problem/#1","title":"1. \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438","text":"

\u041c\u0430\u043a\u0441\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0431\u0449\u0435\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0432 \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043f\u043e \u0441\u0443\u0442\u0438 \u0440\u0430\u0432\u043d\u043e\u0441\u0438\u043b\u044c\u043d\u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443 \u0432\u0435\u0441\u0430. \u041e\u0442\u0441\u044e\u0434\u0430 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0436\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f.

  1. \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438.
  2. \u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0436\u0430\u0434\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e.
  3. \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0432\u0437\u044f\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430, \u0447\u0442\u043e\u0431\u044b \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u044e\u043a\u0437\u0430\u043a.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-5 \u00a0 \u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435

"},{"location":"chapter_greedy/fractional_knapsack_problem/#2","title":"2. \u00a0 \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u041c\u044b \u0432\u0432\u043e\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 Item, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u0438. \u0414\u0430\u043b\u0435\u0435 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0438, \u043a\u043e\u0433\u0434\u0430 \u0440\u044e\u043a\u0437\u0430\u043a \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d, \u0432\u044b\u0445\u043e\u0434\u0438\u043c \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043e\u0442\u0432\u0435\u0442:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby fractional_knapsack.py
class Item:\n    \"\"\"\u041f\u0440\u0435\u0434\u043c\u0435\u0442\"\"\"\n\n    def __init__(self, w: int, v: int):\n        self.w = w  # \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n        self.v = v  # \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\ndef fractional_knapsack(wgt: list[int], val: list[int], cap: int) -> int:\n    \"\"\"\u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\"\"\"\n    # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    items = [Item(w, v) for w, v in zip(wgt, val)]\n    # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sort(key=lambda item: item.v / item.w, reverse=True)\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    res = 0\n    for item in items:\n        if item.w <= cap:\n            # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v\n            cap -= item.w\n        else:\n            # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (item.v / item.w) * cap\n            # \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break\n    return res\n
fractional_knapsack.cpp
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n  public:\n    int w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    int v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n    Item(int w, int v) : w(w), v(v) {\n    }\n};\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\ndouble fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    vector<Item> items;\n    for (int i = 0; i < wgt.size(); i++) {\n        items.push_back(Item(wgt[i], val[i]));\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    double res = 0;\n    for (auto &item : items) {\n        if (item.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (double)item.v / item.w * cap;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    return res;\n}\n
fractional_knapsack.java
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n    int w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    int v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n    public Item(int w, int v) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\ndouble fractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    Item[] items = new Item[wgt.length];\n    for (int i = 0; i < wgt.length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    double res = 0;\n    for (Item item : items) {\n        if (item.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (double) item.v / item.w * cap;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    return res;\n}\n
fractional_knapsack.cs
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item(int w, int v) {\n    public int w = w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    public int v = v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\ndouble FractionalKnapsack(int[] wgt, int[] val, int cap) {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    Item[] items = new Item[wgt.Length];\n    for (int i = 0; i < wgt.Length; i++) {\n        items[i] = new Item(wgt[i], val[i]);\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    double res = 0;\n    foreach (Item item in items) {\n        if (item.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (double)item.v / item.w * cap;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    return res;\n}\n
fractional_knapsack.go
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\ntype Item struct {\n    w int // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    v int // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc fractionalKnapsack(wgt []int, val []int, cap int) float64 {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    items := make([]Item, len(wgt))\n    for i := 0; i < len(wgt); i++ {\n        items[i] = Item{wgt[i], val[i]}\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    sort.Slice(items, func(i, j int) bool {\n        return float64(items[i].v)/float64(items[i].w) > float64(items[j].v)/float64(items[j].w)\n    })\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    res := 0.0\n    for _, item := range items {\n        if item.w <= cap {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += float64(item.v)\n            cap -= item.w\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += float64(item.v) / float64(item.w) * float64(cap)\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break\n        }\n    }\n    return res\n}\n
fractional_knapsack.swift
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n    var w: Int // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    var v: Int // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n    init(w: Int, v: Int) {\n        self.w = w\n        self.v = v\n    }\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc fractionalKnapsack(wgt: [Int], val: [Int], cap: Int) -> Double {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    var items = zip(wgt, val).map { Item(w: $0, v: $1) }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sort { -(Double($0.v) / Double($0.w)) < -(Double($1.v) / Double($1.w)) }\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    var res = 0.0\n    var cap = cap\n    for item in items {\n        if item.w <= cap {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += Double(item.v)\n            cap -= item.w\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += Double(item.v) / Double(item.w) * Double(cap)\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break\n        }\n    }\n    return res\n}\n
fractional_knapsack.js
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n    constructor(w, v) {\n        this.w = w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n        this.v = v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    }\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction fractionalKnapsack(wgt, val, cap) {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    const items = wgt.map((w, i) => new Item(w, val[i]));\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (item.v / item.w) * cap;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    return res;\n}\n
fractional_knapsack.ts
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n    w: number; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    v: number; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n    constructor(w: number, v: number) {\n        this.w = w;\n        this.v = v;\n    }\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction fractionalKnapsack(wgt: number[], val: number[], cap: number): number {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    const items: Item[] = wgt.map((w, i) => new Item(w, val[i]));\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sort((a, b) => b.v / b.w - a.v / a.w);\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    let res = 0;\n    for (const item of items) {\n        if (item.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (item.v / item.w) * cap;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    return res;\n}\n
fractional_knapsack.dart
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item {\n  int w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n  int v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n  Item(this.w, this.v);\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\ndouble fractionalKnapsack(List<int> wgt, List<int> val, int cap) {\n  // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n  List<Item> items = List.generate(wgt.length, (i) => Item(wgt[i], val[i]));\n  // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n  items.sort((a, b) => (b.v / b.w).compareTo(a.v / a.w));\n  // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n  double res = 0;\n  for (Item item in items) {\n    if (item.w <= cap) {\n      // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n      res += item.v;\n      cap -= item.w;\n    } else {\n      // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n      res += item.v / item.w * cap;\n      // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n      break;\n    }\n  }\n  return res;\n}\n
fractional_knapsack.rs
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nstruct Item {\n    w: i32, // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    v: i32, // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n}\n\nimpl Item {\n    fn new(w: i32, v: i32) -> Self {\n        Self { w, v }\n    }\n}\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    let mut items = wgt\n        .iter()\n        .zip(val.iter())\n        .map(|(&w, &v)| Item::new(w, v))\n        .collect::<Vec<Item>>();\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sort_by(|a, b| {\n        (b.v as f64 / b.w as f64)\n            .partial_cmp(&(a.v as f64 / a.w as f64))\n            .unwrap()\n    });\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    let mut res = 0.0;\n    for item in &items {\n        if item.w <= cap {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v as f64;\n            cap -= item.w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += item.v as f64 / item.w as f64 * cap as f64;\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    res\n}\n
fractional_knapsack.c
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\ntypedef struct {\n    int w; // \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n    int v; // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n} Item;\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfloat fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    Item *items = malloc(sizeof(Item) * itemCount);\n    for (int i = 0; i < itemCount; i++) {\n        items[i] = (Item){.w = wgt[i], .v = val[i]};\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    float res = 0.0;\n    for (int i = 0; i < itemCount; i++) {\n        if (items[i].w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += items[i].v;\n            cap -= items[i].w;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += (float)cap / items[i].w * items[i].v;\n            cap = 0;\n            break;\n        }\n    }\n    free(items);\n    return res;\n}\n
fractional_knapsack.kt
/* \u041f\u0440\u0435\u0434\u043c\u0435\u0442 */\nclass Item(\n    val w: Int, // \u041f\u0440\u0435\u0434\u043c\u0435\u0442\n    val v: Int  // \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n)\n\n/* \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfun fractionalKnapsack(wgt: IntArray, _val: IntArray, c: Int): Double {\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n    var cap = c\n    val items = arrayOfNulls<Item>(wgt.size)\n    for (i in wgt.indices) {\n        items[i] = Item(wgt[i], _val[i])\n    }\n    // \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n    items.sortBy { item: Item? -> -(item!!.v.toDouble() / item.w) }\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n    var res = 0.0\n    for (item in items) {\n        if (item!!.w <= cap) {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n            res += item.v\n            cap -= item.w\n        } else {\n            // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n            res += item.v.toDouble() / item.w * cap\n            // \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break\n        }\n    }\n    return res\n}\n
fractional_knapsack.rb
=begin\nFile: fractional_knapsack.rb\nCreated Time: 2024-05-07\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0440\u0435\u0434\u043c\u0435\u0442 ###\nclass Item\n  attr_accessor :w # \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n  attr_accessor :v # \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n=begin\nFile: fractional_knapsack.rb\nCreated Time: 2024-05-07\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0440\u0435\u0434\u043c\u0435\u0442 ###\nclass Item\n  attr_accessor :w # \u0412\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n  attr_accessor :v # \u0421\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n\n  def initialize(w, v)\n    @w = w\n    @v = v\n  end\nend\n\n# ## \u0414\u0440\u043e\u0431\u043d\u044b\u0439 \u0440\u044e\u043a\u0437\u0430\u043a: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c ###\ndef fractional_knapsack(wgt, val, cap)\n  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0441 \u0434\u0432\u0443\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438: \u0432\u0435\u0441 \u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c\n  items = wgt.each_with_index.map { |w, i| Item.new(w, val[i]) }\n  # \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u0438 item.v / item.w \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f\n  items.sort! { |a, b| (b.v.to_f / b.w) <=> (a.v.to_f / a.w) }\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440\n  res = 0\n  for item in items\n    if item.w <= cap\n      # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0446\u0435\u043b\u0438\u043a\u043e\u043c\n      res += item.v\n      cap -= item.w\n    else\n      # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0432\u0448\u0435\u0439\u0441\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0440\u044e\u043a\u0437\u0430\u043a \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u0430\n      res += (item.v.to_f / item.w) * cap\n      # \u0421\u0432\u043e\u0431\u043e\u0434\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n      break\n    end\n  end\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u043c\u0435\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(\\log n)\\), \u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u0430 \\(O(\\log n)\\) \u0438\u043b\u0438 \\(O(n)\\), \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u041f\u043e\u043c\u0438\u043c\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0439\u0442\u0438 \u0432\u0435\u0441\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\), \u0433\u0434\u0435 \\(n\\) - \u0447\u0438\u0441\u043b\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043f\u0438\u0441\u043e\u043a \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 Item, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\).

"},{"location":"chapter_greedy/fractional_knapsack_problem/#3","title":"3. \u00a0 \u0414\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438","text":"

\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u0433\u043e. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(x\\) \u0438\u043c\u0435\u0435\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0443\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c res, \u043d\u043e \u0432 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(x\\) \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442.

\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b\u043d\u0435\u043c \u0438\u0437 \u0440\u044e\u043a\u0437\u0430\u043a\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \u0435\u0434\u0438\u043d\u0438\u0447\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430 \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0435\u0433\u043e \u043d\u0430 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(x\\) \u0442\u043e\u0433\u043e \u0436\u0435 \u0432\u0435\u0441\u0430. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(x\\) \u0438\u043c\u0435\u0435\u0442 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0443\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c, \u043e\u0431\u0449\u0430\u044f \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043c\u0435\u043d\u044b \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0442\u0430\u043d\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 res. \u042d\u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0442 \u0442\u043e\u043c\u0443, \u0447\u0442\u043e res \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043f\u0440\u0435\u0434\u043c\u0435\u0442 \\(x\\).

\u0414\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0432 \u044d\u0442\u043e\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0435. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u044b \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0443\u0434\u0435\u043b\u044c\u043d\u043e\u0439 \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u0441\u0435\u0433\u0434\u0430 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0433\u043e\u0434\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u0436\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u0430.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-6, \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0432\u0435\u0441 \u043f\u0440\u0435\u0434\u043c\u0435\u0442\u043e\u0432 \u0438 \u0438\u0445 \u0443\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u0443\u044e \u0438 \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u043e\u0441\u0438 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0439 \u0434\u0438\u0430\u0433\u0440\u0430\u043c\u043c\u044b, \u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0443 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u00ab\u043f\u043e\u0438\u0441\u043a \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043b\u043e\u0449\u0430\u0434\u0438, \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u043c \u043e\u0442\u0440\u0435\u0437\u043a\u043e\u043c \u043f\u043e \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u0438\u00bb. \u042d\u0442\u0430 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0441 \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-6 \u00a0 \u0413\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0446\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0434\u0440\u043e\u0431\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435

"},{"location":"chapter_greedy/greedy_algorithm/","title":"15.1 \u00a0 \u0416\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u0416\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c (greedy algorithm) - \u044d\u0442\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0437\u0430\u0434\u0430\u0447 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0415\u0433\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u0438\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u043c \u043f\u0440\u044f\u043c\u043e \u0441\u0435\u0439\u0447\u0430\u0441, \u0442\u043e \u0435\u0441\u0442\u044c \u0436\u0430\u0434\u043d\u043e \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043d\u0430\u0434\u0435\u0436\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0416\u0430\u0434\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0448\u0438\u0440\u043e\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445.

\u0416\u0430\u0434\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438. \u0423 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0441\u0445\u043e\u0434\u0441\u0442\u0432\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043e\u0431\u0430 \u043e\u043f\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b, \u043d\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f.

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u00ab\u0440\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442\u00bb. \u042d\u0442\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0443\u0436\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0430\u0441\u044c \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u00ab\u0437\u0430\u0434\u0430\u0447\u0430 \u043e \u043f\u043e\u043b\u043d\u043e\u043c \u0440\u044e\u043a\u0437\u0430\u043a\u0435\u00bb, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d\u0430 \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0430\u043c \u0437\u043d\u0430\u043a\u043e\u043c\u0430.

Question

\u0414\u0430\u043d\u043e \\(n\\) \u0432\u0438\u0434\u043e\u0432 \u043c\u043e\u043d\u0435\u0442. \u041d\u043e\u043c\u0438\u043d\u0430\u043b \u043c\u043e\u043d\u0435\u0442\u044b \\(i\\) \u0440\u0430\u0432\u0435\u043d \\(coins[i - 1]\\), \u0446\u0435\u043b\u0435\u0432\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0440\u0430\u0432\u043d\u0430 \\(amt\\), \u043f\u0440\u0438\u0447\u0435\u043c \u043a\u0430\u0436\u0434\u0443\u044e \u043c\u043e\u043d\u0435\u0442\u0443 \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u043d\u0435\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437. \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043c\u043e\u043d\u0435\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0441\u0443\u043c\u043c\u0443. \u0415\u0441\u043b\u0438 \u043d\u0430\u0431\u0440\u0430\u0442\u044c \u0441\u0443\u043c\u043c\u0443 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \\(-1\\).

\u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-1. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u043c\u044b \u0436\u0430\u0434\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u0435\u0435 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u043d\u0435\u0439 \u0431\u043b\u0438\u0436\u0435 \u0432\u0441\u0435\u0433\u043e, \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u0448\u0430\u0433, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043d\u0443\u0436\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-1 \u00a0 \u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby coin_change_greedy.py
def coin_change_greedy(coins: list[int], amt: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\"\"\"\n    # \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    i = len(coins) - 1\n    count = 0\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while amt > 0:\n        # \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while i > 0 and coins[i] > amt:\n            i -= 1\n        # \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i]\n        count += 1\n    # \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return count if amt == 0 else -1\n
coin_change_greedy.cpp
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint coinChangeGreedy(vector<int> &coins, int amt) {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    int i = coins.size() - 1;\n    int count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt == 0 ? count : -1;\n}\n
coin_change_greedy.java
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint coinChangeGreedy(int[] coins, int amt) {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    int i = coins.length - 1;\n    int count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt == 0 ? count : -1;\n}\n
coin_change_greedy.cs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint CoinChangeGreedy(int[] coins, int amt) {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    int i = coins.Length - 1;\n    int count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt == 0 ? count : -1;\n}\n
coin_change_greedy.go
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc coinChangeGreedy(coins []int, amt int) int {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    i := len(coins) - 1\n    count := 0\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    for amt > 0 {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        for i > 0 && coins[i] > amt {\n            i--\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i]\n        count++\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if amt != 0 {\n        return -1\n    }\n    return count\n}\n
coin_change_greedy.swift
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc coinChangeGreedy(coins: [Int], amt: Int) -> Int {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    var i = coins.count - 1\n    var count = 0\n    var amt = amt\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while amt > 0 {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while i > 0 && coins[i] > amt {\n            i -= 1\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i]\n        count += 1\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt == 0 ? count : -1\n}\n
coin_change_greedy.js
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction coinChangeGreedy(coins, amt) {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    let i = coins.length - 1;\n    let count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt === 0 ? count : -1;\n}\n
coin_change_greedy.ts
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction coinChangeGreedy(coins: number[], amt: number): number {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    let i = coins.length - 1;\n    let count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt === 0 ? count : -1;\n}\n
coin_change_greedy.dart
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint coinChangeGreedy(List<int> coins, int amt) {\n  // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n  int i = coins.length - 1;\n  int count = 0;\n  // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n  while (amt > 0) {\n    // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n    while (i > 0 && coins[i] > amt) {\n      i--;\n    }\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n    amt -= coins[i];\n    count++;\n  }\n  // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return amt == 0 ? count : -1;\n}\n
coin_change_greedy.rs
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    let mut i = coins.len() - 1;\n    let mut count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while amt > 0 {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while i > 0 && coins[i] > amt {\n            i -= 1;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count += 1;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if amt == 0 {\n        count\n    } else {\n        -1\n    }\n}\n
coin_change_greedy.c
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint coinChangeGreedy(int *coins, int size, int amt) {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    int i = size - 1;\n    int count = 0;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (amt > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > amt) {\n            i--;\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        amt -= coins[i];\n        count++;\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return amt == 0 ? count : -1;\n}\n
coin_change_greedy.kt
/* \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfun coinChangeGreedy(coins: IntArray, amt: Int): Int {\n    // \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n    var am = amt\n    var i = coins.size - 1\n    var count = 0\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n    while (am > 0) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n        while (i > 0 && coins[i] > am) {\n            i--\n        }\n        // \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n        am -= coins[i]\n        count++\n    }\n    // \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return if (am == 0) count else -1\n}\n
coin_change_greedy.rb
=begin\nFile: coin_change_greedy.rb\nCreated Time: 2024-05-07\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0420\u0430\u0437\u043c\u0435\u043d \u043c\u043e\u043d\u0435\u0442: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c ###\ndef coin_change_greedy(coins, amt)\n  # \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a coins \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\n  i = coins.length - 1\n  count = 0\n  # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0441\u0443\u043c\u043c\u044b\n  while amt > 0\n    # \u041d\u0430\u0439\u0442\u0438 \u043c\u043e\u043d\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u0443\u043c\u043c\u044b \u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u043a \u043d\u0435\u043c\u0443 \u0431\u043b\u0438\u0437\u043a\u0430\n    while i > 0 && coins[i] > amt\n      i -= 1\n    end\n    # \u0412\u044b\u0431\u0440\u0430\u0442\u044c coins[i]\n    amt -= coins[i]\n    count += 1\n  end\n  # \u0415\u0441\u043b\u0438 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043e, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  amt == 0 ? count : -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0423 \u0432\u0430\u0441 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0440\u0432\u0430\u0442\u044c\u0441\u044f: So clean! \u0416\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0435\u0448\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0440\u0430\u0437\u043c\u0435\u043d\u0430 \u043c\u043e\u043d\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0434\u0435\u0441\u044f\u0442\u044c\u044e \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u043a\u043e\u0434\u0430.

"},{"location":"chapter_greedy/greedy_algorithm/#1511","title":"15.1.1 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0416\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442 \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f\u0445 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d. \u0412 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043a\u043e\u0434\u0435 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043d\u043e\u043c\u0438\u043d\u0430\u043b \u043c\u043e\u043d\u0435\u0442\u044b \u0447\u0435\u0440\u0435\u0437 \\(\\min(coins)\\), \u0442\u043e\u0433\u0434\u0430 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u043c \\(amt / \\min(coins)\\) \u0440\u0430\u0437, \u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(amt / \\min(coins))\\). \u042d\u0442\u043e \u043d\u0430 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \\(O(n \\times amt)\\).

\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u043d\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0432 \u043c\u043e\u043d\u0435\u0442 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442. \u041d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0434\u0432\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-2 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440\u044b, \u0433\u0434\u0435 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f

\u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043e\u0447\u0435\u043d\u044c \u043f\u043b\u043e\u0445\u043e\u043c\u0443 \u043e\u0442\u0432\u0435\u0442\u0443. \u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.

\u0412 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u0432 \u0434\u0432\u0443\u0445 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445.

  1. \u041c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f: \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0447\u0430\u0441\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0438\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043d \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0447\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c \u0438 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.
  2. \u041c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0435\u043d\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435: \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0442\u043e\u0436\u0435 \u043f\u043e\u043b\u0435\u0437\u0435\u043d. \u0414\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0438\u0441\u043a \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043f\u0442\u0438\u043c\u0443\u043c\u0430 \u043e\u0447\u0435\u043d\u044c \u0442\u0440\u0443\u0434\u0435\u043d, \u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0439\u0442\u0438 \u0441\u0443\u0431\u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442 \u0443\u0436\u0435 \u0432\u0435\u0441\u044c\u043c\u0430 \u0446\u0435\u043d\u043d\u0430.
"},{"location":"chapter_greedy/greedy_algorithm/#1512","title":"15.1.2 \u00a0 \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0422\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0436\u0430\u0434\u043d\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c? \u0418\u043b\u0438, \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0432 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u043e\u0436\u0435\u0442 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442?

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u0442\u0440\u043e\u0436\u0435. \u0412 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043d\u0430\u0441 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0442 \u0434\u0432\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0437\u0430\u0434\u0430\u0447\u0438.

\u041e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0436\u0435 \u043e\u0431\u0441\u0443\u0436\u0434\u0430\u043b\u0430\u0441\u044c \u0432 \u0433\u043b\u0430\u0432\u0435 \u00ab\u0414\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u00bb, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c\u0441\u044f. \u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043d\u0435 \u0441\u0442\u043e\u043b\u044c \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430, \u043d\u043e \u0438\u0445 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0430\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c.

\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043c\u044b \u0443\u0434\u0435\u043b\u044f\u0435\u043c \u0442\u043e\u043c\u0443, \u043a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430. \u0425\u043e\u0442\u044f \u0444\u043e\u0440\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439, \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043b\u0435\u0433\u043a\u043e.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0437\u0430\u0434\u0430\u0447\u0435 \u043e \u0440\u0430\u0437\u043c\u0435\u043d\u0435 \u043c\u043e\u043d\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a\u043e\u043d\u0442\u0440\u043f\u0440\u0438\u043c\u0435\u0440 \u0438 \u043e\u043f\u0440\u043e\u0432\u0435\u0440\u0433\u043d\u0443\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430, \u043d\u043e \u0432\u043e\u0442 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0435\u0433\u043e \u0438\u0441\u0442\u0438\u043d\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0415\u0441\u043b\u0438 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c: \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445 \u043d\u0430\u0431\u043e\u0440\u043e\u0432 \u043c\u043e\u043d\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c? - \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u0430\u0442\u044c \u043b\u0438\u0448\u044c \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u0438\u043b\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442, \u0430 \u043d\u0435 \u0441\u0442\u0440\u043e\u0433\u043e\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e.

Quote

\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0441\u0442\u0430\u0442\u044c\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \\(O(n^3)\\) \u0434\u043b\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0442\u043e\u0433\u043e, \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u043d \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0439 \u0441\u0443\u043c\u043c\u044b \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043c\u043e\u043d\u0435\u0442.

Pearson, D. A polynomial-time algorithm for the change-making problem[J]. Operations Research Letters, 2005, 33(3): 231-234.

"},{"location":"chapter_greedy/greedy_algorithm/#1513","title":"15.1.3 \u00a0 \u042d\u0442\u0430\u043f\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0436\u0430\u0434\u043d\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c","text":"

\u0412 \u043e\u0431\u0449\u0435\u043c \u0432\u0438\u0434\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0436\u0430\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u043d\u0430 \u0442\u0440\u0438 \u0448\u0430\u0433\u0430.

  1. \u0410\u043d\u0430\u043b\u0438\u0437 \u0437\u0430\u0434\u0430\u0447\u0438: \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u0445 \u0437\u0430\u0434\u0430\u0447\u0438, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439. \u042d\u0442\u043e\u0442 \u044d\u0442\u0430\u043f \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0438 \u0432 \u043f\u043e\u0438\u0441\u043a\u0435 \u0441 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u043e\u043c, \u0438 \u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438.
  2. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u043a\u0430\u043a\u043e\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435. \u042d\u0442\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u0432\u0441\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.
  3. \u0414\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438: \u043e\u0431\u044b\u0447\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439. \u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u043d\u0434\u0443\u043a\u0446\u0438\u044f \u0438\u043b\u0438 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u0433\u043e.

\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 - \u044d\u0442\u043e \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0439 \u044d\u0442\u0430\u043f \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u043d\u043e \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u043d \u0447\u0430\u0441\u0442\u043e \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u044b\u043c \u043f\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c.

\u0427\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c, \u043d\u0443\u0436\u043d\u043e \u0434\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u0433\u043e\u0435 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438, \u043e\u0431\u044b\u0447\u043d\u043e \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u043e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0438\u043d\u0434\u0443\u043a\u0446\u0438\u0438.

\u041e\u0434\u043d\u0430\u043a\u043e \u0438 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439. \u0415\u0441\u043b\u0438 \u0438\u0434\u0435\u0439 \u043d\u0435\u0442, \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445, \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u043c\u0435\u043d\u044f\u044f \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044f \u0436\u0430\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e.

"},{"location":"chapter_greedy/greedy_algorithm/#1514","title":"15.1.4 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0434\u043b\u044f \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0416\u0430\u0434\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0447\u0430\u0441\u0442\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u0436\u0430\u0434\u043d\u043e\u0433\u043e \u0432\u044b\u0431\u043e\u0440\u0430 \u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u0440\u0435\u0448\u0430\u0435\u043c\u044b\u0435 \u0436\u0430\u0434\u043d\u044b\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c.

"},{"location":"chapter_greedy/max_capacity_problem/","title":"15.3 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438","text":"

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \\(ht\\), \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0432\u044b\u0441\u043e\u0442\u0443 \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438. \u041b\u044e\u0431\u044b\u0435 \u0434\u0432\u0435 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440.

\u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u0440\u0430\u0432\u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0432\u044b\u0441\u043e\u0442\u044b \u0438 \u0448\u0438\u0440\u0438\u043d\u044b (\u043f\u043b\u043e\u0449\u0430\u0434\u0438), \u0433\u0434\u0435 \u0432\u044b\u0441\u043e\u0442\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u043e\u0439, \u0430 \u0448\u0438\u0440\u0438\u043d\u0430 - \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043e\u043a \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435.

\u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0434\u0432\u0435 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438\u043c\u0438 \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u0438\u043c\u0435\u043b \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-7.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-7 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438

\u041a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440 \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0430\u043c\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043b\u0443\u0436\u0438\u0442 \u043f\u0430\u0440\u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u044d\u0442\u0438\u0445 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043e\u043a, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0435\u0435 \u043a\u0430\u043a \\([i, j]\\).

\u0421\u043e\u0433\u043b\u0430\u0441\u043d\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e, \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044e \u0432\u044b\u0441\u043e\u0442\u044b \u043d\u0430 \u0448\u0438\u0440\u0438\u043d\u0443, \u0433\u0434\u0435 \u0432\u044b\u0441\u043e\u0442\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u043e\u0439, \u0430 \u0448\u0438\u0440\u0438\u043d\u0430 - \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043e\u043a. \u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \\(cap[i, j]\\), \u0442\u043e\u0433\u0434\u0430 \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0438\u0434:

\\[ cap[i, j] = \\min(ht[i], ht[j]) \\times (j - i) \\]

\u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(n\\). \u0422\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043e\u043a, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439, \u0440\u0430\u0432\u043d\u043e \\(C_n^2 = \\frac{n(n - 1)}{2}\\). \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 - \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u0415\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\).

"},{"location":"chapter_greedy/max_capacity_problem/#1","title":"1. \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438","text":"

\u0423 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0441\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-8, \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \\([i, j]\\), \u0433\u0434\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \\(i < j\\), \u0430 \u0432\u044b\u0441\u043e\u0442\u044b - \u0443\u0441\u043b\u043e\u0432\u0438\u044e \\(ht[i] < ht[j]\\), \u0442\u043e \u0435\u0441\u0442\u044c \\(i\\) - \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0430, \u0430 \\(j\\) - \u0434\u043b\u0438\u043d\u043d\u0430\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-8 \u00a0 \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-9, \u0435\u0441\u043b\u0438 \u0432 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0434\u043b\u0438\u043d\u043d\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0443 \\(j\\) \u0431\u043b\u0438\u0436\u0435 \u043a \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0435 \\(i\\), \u0442\u043e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u0441\u044f.

\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \\(j\\) \u0448\u0438\u0440\u0438\u043d\u0430 \\(j-i\\) \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0442\u0430\u043d\u0435\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u0430 \u0432\u044b\u0441\u043e\u0442\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0431\u043e \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0435\u0439 (\u0435\u0441\u043b\u0438 \\(i\\) \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u043e\u0439), \u043b\u0438\u0431\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u0441\u044f (\u0435\u0441\u043b\u0438 \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u0430\u044f \\(j\\) \u0441\u0442\u0430\u043d\u0435\u0442 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u043e\u0439).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-9 \u00a0 \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u044c

\u0420\u0430\u0441\u0441\u0443\u0436\u0434\u0430\u044f \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443, \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u0434\u0432\u0438\u0433\u0430\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0443 \\(i\\) \u0432\u043d\u0443\u0442\u0440\u044c, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0448\u0430\u043d\u0441 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u0425\u043e\u0442\u044f \u0448\u0438\u0440\u0438\u043d\u0430 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u0441\u044f, \u0432\u044b\u0441\u043e\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0438 (\u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0430 \\(i\\) \u0441\u0442\u0430\u043d\u0435\u0442 \u0432\u044b\u0448\u0435). \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-10 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u043f\u043b\u043e\u0449\u0430\u0434\u044c \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-10 \u00a0 \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u044c

\u041e\u0442\u0441\u044e\u0434\u0430 \u0438 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u0436\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438: \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u043a\u0440\u0430\u044f\u043c \u043a\u043e\u043d\u0442\u0435\u0439\u043d\u0435\u0440\u0430 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0441\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0435, \u043f\u043e\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438.

  1. \u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \\(i\\) \u0438 \\(j\\) \u0441\u0442\u043e\u044f\u0442 \u043d\u0430 \u0434\u0432\u0443\u0445 \u043a\u043e\u043d\u0446\u0430\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.
  2. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \\(cap[i, j]\\) \u0438 \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c.
  3. \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u044b \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043e\u043a \\(i\\) \u0438 \\(j\\), \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0443 \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u044c.
  4. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433\u0438 2. \u0438 3. \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \\(i\\) \u0438 \\(j\\) \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f.
<1><2><3><4><5><6><7><8><9>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-11 \u00a0 \u0416\u0430\u0434\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u0438

"},{"location":"chapter_greedy/max_capacity_problem/#2","title":"2. \u00a0 \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u0426\u0438\u043a\u043b \u0432 \u043a\u043e\u0434\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \\(n\\) \u0440\u0430\u0437, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\).

\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \\(i\\), \\(j\\), \\(res\\) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\).

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_capacity.py
def max_capacity(ht: list[int]) -> int:\n    \"\"\"\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    i, j = 0, len(ht) - 1\n    # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    res = 0\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while i < j:\n        # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        # \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if ht[i] < ht[j]:\n            i += 1\n        else:\n            j -= 1\n    return res\n
max_capacity.cpp
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxCapacity(vector<int> &ht) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int i = 0, j = ht.size() - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    int res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        int cap = min(ht[i], ht[j]) * (j - i);\n        res = max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
max_capacity.java
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxCapacity(int[] ht) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int i = 0, j = ht.length - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    int res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        int cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
max_capacity.cs
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint MaxCapacity(int[] ht) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int i = 0, j = ht.Length - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    int res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        int cap = Math.Min(ht[i], ht[j]) * (j - i);\n        res = Math.Max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
max_capacity.go
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc maxCapacity(ht []int) int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    i, j := 0, len(ht)-1\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    res := 0\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    for i < j {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        capacity := int(math.Min(float64(ht[i]), float64(ht[j]))) * (j - i)\n        res = int(math.Max(float64(res), float64(capacity)))\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if ht[i] < ht[j] {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
max_capacity.swift
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc maxCapacity(ht: [Int]) -> Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    var i = ht.startIndex, j = ht.endIndex - 1\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    var res = 0\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while i < j {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        let cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if ht[i] < ht[j] {\n            i += 1\n        } else {\n            j -= 1\n        }\n    }\n    return res\n}\n
max_capacity.js
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction maxCapacity(ht) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    let i = 0,\n        j = ht.length - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    let res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        const cap = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
max_capacity.ts
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction maxCapacity(ht: number[]): number {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    let i = 0,\n        j = ht.length - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    let res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        const cap: number = Math.min(ht[i], ht[j]) * (j - i);\n        res = Math.max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    return res;\n}\n
max_capacity.dart
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxCapacity(List<int> ht) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  int i = 0, j = ht.length - 1;\n  // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n  int res = 0;\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n  while (i < j) {\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n    int cap = min(ht[i], ht[j]) * (j - i);\n    res = max(res, cap);\n    // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n    if (ht[i] < ht[j]) {\n      i++;\n    } else {\n      j--;\n    }\n  }\n  return res;\n}\n
max_capacity.rs
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfn max_capacity(ht: &[i32]) -> i32 {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    let mut i = 0;\n    let mut j = ht.len() - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    let mut res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while i < j {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;\n        res = std::cmp::max(res, cap);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if ht[i] < ht[j] {\n            i += 1;\n        } else {\n            j -= 1;\n        }\n    }\n    res\n}\n
max_capacity.c
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxCapacity(int ht[], int htLength) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int i = 0;\n    int j = htLength - 1;\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    int res = 0;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        int capacity = myMin(ht[i], ht[j]) * (j - i);\n        res = myMax(res, capacity);\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i++;\n        } else {\n            j--;\n        }\n    }\n    return res;\n}\n
max_capacity.kt
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfun maxCapacity(ht: IntArray): Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    var i = 0\n    var j = ht.size - 1\n    // \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n    var res = 0\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n    while (i < j) {\n        // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n        val cap = min(ht[i], ht[j]) * (j - i)\n        res = max(res, cap)\n        // \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n        if (ht[i] < ht[j]) {\n            i++\n        } else {\n            j--\n        }\n    }\n    return res\n}\n
max_capacity.rb
=begin\nFile: max_capacity.rb\nCreated Time: 2024-05-07\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c ###\ndef max_capacity(ht)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c i \u0438 j \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u043b\u0438\u0441\u044c \u043f\u043e \u0434\u0432\u0443\u043c \u043a\u043e\u043d\u0446\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  i, j = 0, ht.length - 1\n  # \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 0\n  res = 0\n\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0436\u0430\u0434\u043d\u044b\u0439 \u0432\u044b\u0431\u043e\u0440 \u0432 \u0446\u0438\u043a\u043b\u0435, \u043f\u043e\u043a\u0430 \u0434\u0432\u0435 \u0434\u043e\u0441\u043a\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f\n  while i < j\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c\n    cap = [ht[i], ht[j]].min * (j - i)\n    res = [res, cap].max\n    # \u0421\u0434\u0432\u0438\u0433\u0430\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u044c \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443\n    if ht[i] < ht[j]\n      i += 1\n    else\n      j -= 1\n    end\n  end\n\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_greedy/max_capacity_problem/#3","title":"3. \u00a0 \u0414\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438","text":"

\u0416\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0436\u0430\u0434\u043d\u044b\u0439 \u0448\u0430\u0433 \u00ab\u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u00bb \u0447\u0430\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \\(cap[i, j]\\) \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0430 \\(i\\) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439, \u0430 \\(j\\) - \u0434\u043b\u0438\u043d\u043d\u043e\u0439. \u0415\u0441\u043b\u0438 \u0436\u0430\u0434\u043d\u043e \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0443\u044e \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0443 \\(i\\) \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u044c, \u0442\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-12, \u0431\u0443\u0434\u0443\u0442 \u00ab\u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u044b\u00bb. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0437\u0436\u0435 \u043c\u044b \u0443\u0436\u0435 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u044d\u0442\u0438\u0445 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439.

\\[ cap[i, i+1], cap[i, i+2], \\dots, cap[i, j-2], cap[i, j-1] \\]

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-12 \u00a0 \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u0438\u0437-\u0437\u0430 \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u0438 \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0438 \u0435\u0441\u0442\u044c \u0432\u0441\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0434\u043b\u0438\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0430 \\(j\\) \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u044c. \u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0443\u0436\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u044c \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043f\u0440\u043e\u043f\u0443\u0449\u0435\u043d\u043d\u044b\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043d\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043f\u0440\u043e\u043f\u0443\u0441\u043a \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043f\u043e\u0442\u0435\u0440\u0435 \u043e\u043f\u0442\u0438\u043c\u0443\u043c\u0430.

\u041f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0439 \u0430\u043d\u0430\u043b\u0438\u0437 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0439 \u043f\u0435\u0440\u0435\u0433\u043e\u0440\u043e\u0434\u043a\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u00ab\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439\u00bb, \u0430 \u0436\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430.

"},{"location":"chapter_greedy/max_product_cutting_problem/","title":"15.4 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 \u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f","text":"

Question

\u0414\u0430\u043d \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0446\u0435\u043b\u044b\u0439 \\(n\\). \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0441\u0443\u043c\u043c\u0443 \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0434\u0432\u0443\u0445 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0438 \u043d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-13.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-13 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0438 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f

\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0437\u0431\u0438\u043b\u0438 \\(n\\) \u043d\u0430 \\(m\\) \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0435\u0439, \u0433\u0434\u0435 \\(i\\)-\u0439 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0447\u0435\u0440\u0435\u0437 \\(n_i\\), \u0442\u043e \u0435\u0441\u0442\u044c

\\[ n = \\sum_{i=1}^{m}n_i \\]

\u0426\u0435\u043b\u044c \u0437\u0430\u0434\u0430\u0447\u0438 - \u043d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0445 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0435\u0439, \u0442\u043e \u0435\u0441\u0442\u044c

\\[ \\max(\\prod_{i=1}^{m}n_i) \\]

\u041d\u0443\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c: \u043a\u0430\u043a\u0438\u043c \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0447\u0430\u0441\u0442\u0435\u0439 \\(m\\) \u0438 \u043a\u0430\u043a\u0438\u043c\u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \\(n_i\\)?

"},{"location":"chapter_greedy/max_product_cutting_problem/#1","title":"1. \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0436\u0430\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438","text":"

\u0418\u0437 \u043e\u043f\u044b\u0442\u0430 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0447\u0430\u0441\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0445 \u0441\u0443\u043c\u043c\u044b. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u043c \u0438\u0437 \\(n\\) \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(2\\), \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e \\(2(n-2)\\). \u0421\u0440\u0430\u0432\u043d\u0438\u043c \u044d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \\(n\\):

\\[ \\begin{aligned} 2(n-2) & \\geq n \\newline 2n - n - 4 & \\geq 0 \\newline n & \\geq 4 \\end{aligned} \\]

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-14, \u043a\u043e\u0433\u0434\u0430 \\(n \\geq 4\\), \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044f \\(2\\) \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u0441\u0435 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430, \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u043d\u044b\u0435 \\(4\\), \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c.

\u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f 1: \u0435\u0441\u043b\u0438 \u0432 \u0441\u0445\u0435\u043c\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(\\geq 4\\), \u0442\u043e \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0442\u044c. \u0412 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 \\(1\\), \\(2\\), \\(3\\).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-14 \u00a0 \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435

\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c, \u043a\u0430\u043a\u043e\u0439 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u043c. \u0421\u0440\u0435\u0434\u0438 \\(1\\), \\(2\\), \\(3\\) \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0445\u0443\u0434\u0448\u0438\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \\(1\\), \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(1 \\times (n-1) < n\\), \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \\(1\\) \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-15, \u043f\u0440\u0438 \\(n = 6\\) \u0438\u043c\u0435\u0435\u043c \\(3 \\times 3 > 2 \\times 2 \\times 2\\). \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \\(3\\) \u0432\u044b\u0433\u043e\u0434\u043d\u0435\u0435, \u0447\u0435\u043c \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \\(2\\).

\u0416\u0430\u0434\u043d\u0430\u044f \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f 2: \u0432 \u0441\u0445\u0435\u043c\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u0434\u0432\u0443\u0445 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0435\u0439 \\(2\\). \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0440\u0438 \u0434\u0432\u043e\u0439\u043a\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-15 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f

\u0418\u0442\u0430\u043a, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0436\u0430\u0434\u043d\u0443\u044e \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e.

  1. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0446\u0435\u043b\u043e\u0433\u043e \\(n\\) \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u044f\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(3\\), \u043f\u043e\u043a\u0430 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u044b\u043c \\(0\\), \\(1\\) \u0438\u043b\u0438 \\(2\\).
  2. \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d \\(0\\), \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \\(n\\) \u043a\u0440\u0430\u0442\u043d\u043e \\(3\\), \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e.
  3. \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d \\(2\\), \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c.
  4. \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d \\(1\\), \u0442\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \\(2 \\times 2 > 1 \\times 3\\), \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(3\\) \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \\(2\\).
"},{"location":"chapter_greedy/max_product_cutting_problem/#2","title":"2. \u00a0 \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 15-16, \u043d\u0430\u043c \u043d\u0435 \u043d\u0443\u0436\u0435\u043d \u0446\u0438\u043a\u043b, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430. \u041c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a \\(a\\), \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0441\u0442\u0430\u0442\u043e\u043a \\(b\\). \u0422\u043e\u0433\u0434\u0430 \u0438\u043c\u0435\u0435\u043c:

\\[ n = 3 a + b \\]

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \\(n \\leq 3\\) \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(1\\), \u0438 \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e \\(1 \\times (n - 1)\\).

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby max_product_cutting.py
def max_product_cutting(n: int) -> int:\n    \"\"\"\u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\"\"\"\n    # \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if n <= 3:\n        return 1 * (n - 1)\n    # \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    a, b = n // 3, n % 3\n    if b == 1:\n        # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return int(math.pow(3, a - 1)) * 2 * 2\n    if b == 2:\n        # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return int(math.pow(3, a)) * 2\n    # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return int(math.pow(3, a))\n
max_product_cutting.cpp
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxProductCutting(int n) {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return (int)pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return (int)pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return (int)pow(3, a);\n}\n
max_product_cutting.java
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxProductCutting(int n) {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return (int) Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return (int) Math.pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return (int) Math.pow(3, a);\n}\n
max_product_cutting.cs
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint MaxProductCutting(int n) {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return (int)Math.Pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return (int)Math.Pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return (int)Math.Pow(3, a);\n}\n
max_product_cutting.go
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc maxProductCutting(n int) int {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    a := n / 3\n    b := n % 3\n    if b == 1 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return int(math.Pow(3, float64(a-1))) * 2 * 2\n    }\n    if b == 2 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return int(math.Pow(3, float64(a))) * 2\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return int(math.Pow(3, float64(a)))\n}\n
max_product_cutting.swift
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunc maxProductCutting(n: Int) -> Int {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if n <= 3 {\n        return 1 * (n - 1)\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    let a = n / 3\n    let b = n % 3\n    if b == 1 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return pow(3, a - 1) * 2 * 2\n    }\n    if b == 2 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return pow(3, a) * 2\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return pow(3, a)\n}\n
max_product_cutting.js
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction maxProductCutting(n) {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    let a = Math.floor(n / 3);\n    let b = n % 3;\n    if (b === 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return Math.pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return Math.pow(3, a);\n}\n
max_product_cutting.ts
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfunction maxProductCutting(n: number): number {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    let a: number = Math.floor(n / 3);\n    let b: number = n % 3;\n    if (b === 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return Math.pow(3, a - 1) * 2 * 2;\n    }\n    if (b === 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return Math.pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return Math.pow(3, a);\n}\n
max_product_cutting.dart
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxProductCutting(int n) {\n  // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n  if (n <= 3) {\n    return 1 * (n - 1);\n  }\n  // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n  int a = n ~/ 3;\n  int b = n % 3;\n  if (b == 1) {\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n    return (pow(3, a - 1) * 2 * 2).toInt();\n  }\n  if (b == 2) {\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return (pow(3, a) * 2).toInt();\n  }\n  // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n  return pow(3, a).toInt();\n}\n
max_product_cutting.rs
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfn max_product_cutting(n: i32) -> i32 {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if n <= 3 {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    let a = n / 3;\n    let b = n % 3;\n    if b == 1 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        3_i32.pow(a as u32 - 1) * 2 * 2\n    } else if b == 2 {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        3_i32.pow(a as u32) * 2\n    } else {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        3_i32.pow(a as u32)\n    }\n}\n
max_product_cutting.c
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nint maxProductCutting(int n) {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1);\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    int a = n / 3;\n    int b = n % 3;\n    if (b == 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return pow(3, a - 1) * 2 * 2;\n    }\n    if (b == 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return pow(3, a) * 2;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return pow(3, a);\n}\n
max_product_cutting.kt
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c */\nfun maxProductCutting(n: Int): Int {\n    // \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n    if (n <= 3) {\n        return 1 * (n - 1)\n    }\n    // \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n    val a = n / 3\n    val b = n % 3\n    if (b == 1) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n        return 3.0.pow((a - 1)).toInt() * 2 * 2\n    }\n    if (b == 2) {\n        // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n        return 3.0.pow(a).toInt() * 2 * 2\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n    return 3.0.pow(a).toInt()\n}\n
max_product_cutting.rb
=begin\nFile: max_product_cutting.rb\nCreated Time: 2024-05-07\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0440\u0435\u0437\u0430\u043d\u0438\u044f: \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c ###\ndef max_product_cutting(n)\n  # \u041a\u043e\u0433\u0434\u0430 n <= 3, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0434\u043d\u0443 1\n  return 1 * (n - 1) if n <= 3\n  # \u0416\u0430\u0434\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 3, \u0433\u0434\u0435 a \u2014 \u0447\u0438\u0441\u043b\u043e \u0442\u0440\u043e\u0435\u043a, \u0430 b \u2014 \u043e\u0441\u0442\u0430\u0442\u043e\u043a\n  a, b = n / 3, n % 3\n  # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 1, \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 1 * 3 \u0432 2 * 2\n  return (3.pow(a - 1) * 2 * 2).to_i if b == 1\n  # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 2, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n  return (3.pow(a) * 2).to_i if b == 2\n  # \u0415\u0441\u043b\u0438 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0440\u0430\u0432\u0435\u043d 0, \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0442\u044c\n  3.pow(a).to_i\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 15-16 \u00a0 \u041c\u0435\u0442\u043e\u0434 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u0432\u043e\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c. \u0415\u0441\u043b\u0438 \u0432\u0437\u044f\u0442\u044c Python, \u0442\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0442\u0440\u0438 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438.

\u041f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \\(a\\) \u0438 \\(b\\) \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\).

"},{"location":"chapter_greedy/max_product_cutting_problem/#3","title":"3. \u00a0 \u0414\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u0438","text":"

\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e \u043e\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043d\u043e\u0433\u043e \u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u0443\u0447\u0430\u0439 \\(n \\geq 4\\).

  1. \u0412\u0441\u0435 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0438 \\(\\leq 3\\): \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(x \\geq 4\\). \u0422\u043e\u0433\u0434\u0430 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \\(2(x-2)\\) \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0438\u043b\u0438 \u0440\u0430\u0432\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0442 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044e.
  2. \u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \\(1\\): \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u044c \\(1\\). \u0422\u043e\u0433\u0434\u0430 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u043c\u043d\u043e\u0436\u0438\u0442\u0435\u043b\u0435\u043c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0442 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044e.
  3. \u0421\u0445\u0435\u043c\u0430 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u0434\u0432\u0443\u0445 \\(2\\): \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0432 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0445\u0435\u043c\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0442\u0440\u0438 \u0434\u0432\u043e\u0439\u043a\u0438. \u0422\u043e\u0433\u0434\u0430 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0432\u0443\u043c\u044f \u0442\u0440\u043e\u0439\u043a\u0430\u043c\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u0440\u0435\u0447\u0438\u0442 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044e.
"},{"location":"chapter_greedy/summary/","title":"15.5 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_greedy/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_hashing/","title":"\u0413\u043b\u0430\u0432\u0430 6. \u00a0 \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b","text":"

Abstract

\u0412 \u043c\u0438\u0440\u0435 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0441\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0440\u044f.

\u041e\u043d \u0443\u043c\u0435\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0448\u0438\u0444\u0440 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u0442\u043e\u043c\u0443 \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442 \u043d\u0443\u0436\u043d\u0443\u044e \u043a\u043d\u0438\u0433\u0443.

"},{"location":"chapter_hashing/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_hashing/hash_algorithm/","title":"6.3 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0412 \u0434\u0432\u0443\u0445 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0440\u0430\u0437\u0434\u0435\u043b\u0430\u0445 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043b\u0438 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0438 \u043e\u0442\u043a\u0440\u044b\u0442\u0430\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f, \u0438 \u043c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a \u043b\u0438\u0448\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439, \u043d\u043e \u043d\u0435 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u044e\u0442 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u0438\u0445 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439.

\u0415\u0441\u043b\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0447\u0430\u0441\u0442\u043e, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0440\u0435\u0437\u043a\u043e \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-8, \u0434\u043b\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0446\u0435\u043f\u043e\u0447\u0435\u043a \u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u043f\u043e \u0432\u0441\u0435\u043c \u0431\u0430\u043a\u0435\u0442\u0430\u043c, \u0438 \u044d\u0442\u043e \u0434\u0430\u0435\u0442 \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0443\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430; \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0436\u0435 \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0431\u0430\u043a\u0435\u0442\u0435, \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u043e \\(O(n)\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-8 \u00a0 \u041b\u0443\u0447\u0448\u0438\u0439 \u0438 \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439

\u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439. \u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u044d\u0442\u0430\u043f\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043e\u043d\u043e \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430:

index = hash(key) % capacity\n

\u0418\u0437 \u044d\u0442\u043e\u0439 \u0444\u043e\u0440\u043c\u0443\u043b\u044b \u0432\u0438\u0434\u043d\u043e: \u043f\u0440\u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0435\u043c\u043a\u043e\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b capacity **\u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c hash() **, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435.

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0434\u043b\u044f \u0443\u043c\u0435\u043d\u044c\u0448\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 hash() .

"},{"location":"chapter_hashing/hash_algorithm/#631-","title":"6.3.1 \u00a0 \u0426\u0435\u043b\u0438 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \"\u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0438 \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0439\", \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446, \u043d\u043e \u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u0434\u0440\u0443\u0433\u0438\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445.

\u0414\u043b\u044f \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0441 \u043a\u0440\u0438\u043f\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0435\u0439, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u043e\u043b\u044f \u043f\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u0438 \u0438\u043d\u044b\u0445 \u0444\u043e\u0440\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430, \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0434\u043e\u043b\u0436\u0435\u043d \u043e\u0431\u043b\u0430\u0434\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u0442\u0440\u043e\u0433\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438.

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \"\u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\" \u0438 \"\u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u043a \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u044f\u043c\" - \u044d\u0442\u043e \u0434\u0432\u0430 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c\u044b\u0445 \u043f\u043e\u043d\u044f\u0442\u0438\u044f , \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u043d\u0435 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0432\u0442\u043e\u0440\u043e\u0433\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 key \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f key % 100 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e\u0442 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043f\u0440\u043e\u0441\u0442: \u0432\u0441\u0435 key \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u043c\u0435\u0442\u044c \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043f\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u0434\u043e\u0431\u0440\u0430\u0442\u044c \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0435 key \u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0437\u043b\u043e\u043c\u0430\u0442\u044c \u043f\u0430\u0440\u043e\u043b\u044c.

"},{"location":"chapter_hashing/hash_algorithm/#632-","title":"6.3.2 \u00a0 \u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0420\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 - \u044d\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0444\u0430\u043a\u0442\u043e\u0440\u043e\u0432. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby simple_hash.py
def add_hash(key: str) -> int:\n    \"\"\"\u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash += ord(c)\n    return hash % modulus\n\ndef mul_hash(key: str) -> int:\n    \"\"\"\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = 31 * hash + ord(c)\n    return hash % modulus\n\ndef xor_hash(key: str) -> int:\n    \"\"\"XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash ^= ord(c)\n    return hash % modulus\n\ndef rot_hash(key: str) -> int:\n    \"\"\"\u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c\"\"\"\n    hash = 0\n    modulus = 1000000007\n    for c in key:\n        hash = (hash << 4) ^ (hash >> 28) ^ ord(c)\n    return hash % modulus\n
simple_hash.cpp
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint addHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint mulHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = (31 * hash + (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint xorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash ^= (int)c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nint rotHash(string key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (unsigned char c : key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int)c) % MODULUS;\n    }\n    return (int)hash;\n}\n
simple_hash.java
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint addHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint mulHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = (31 * hash + (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint xorHash(String key) {\n    int hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash ^= (int) c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nint rotHash(String key) {\n    long hash = 0;\n    final int MODULUS = 1000000007;\n    for (char c : key.toCharArray()) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (int) c) % MODULUS;\n    }\n    return (int) hash;\n}\n
simple_hash.cs
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint AddHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint MulHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = (31 * hash + c) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint XorHash(string key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash ^= c;\n    }\n    return hash & MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nint RotHash(string key) {\n    long hash = 0;\n    const int MODULUS = 1000000007;\n    foreach (char c in key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c) % MODULUS;\n    }\n    return (int)hash;\n}\n
simple_hash.go
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc addHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc mulHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = (31*hash + int64(b)) % modulus\n    }\n    return int(hash)\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc xorHash(key string) int {\n    hash := 0\n    modulus := 1000000007\n    for _, b := range []byte(key) {\n        fmt.Println(int(b))\n        hash ^= int(b)\n        hash = (31*hash + int(b)) % modulus\n    }\n    return hash & modulus\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfunc rotHash(key string) int {\n    var hash int64\n    var modulus int64\n\n    modulus = 1000000007\n    for _, b := range []byte(key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ int64(b)) % modulus\n    }\n    return int(hash)\n}\n
simple_hash.swift
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc addHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc mulHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = (31 * hash + Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunc xorHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash ^= Int(scalar.value)\n        }\n    }\n    return hash & MODULUS\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfunc rotHash(key: String) -> Int {\n    var hash = 0\n    let MODULUS = 1_000_000_007\n    for c in key {\n        for scalar in c.unicodeScalars {\n            hash = ((hash << 4) ^ (hash >> 28) ^ Int(scalar.value)) % MODULUS\n        }\n    }\n    return hash\n}\n
simple_hash.js
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction addHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction mulHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction xorHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfunction rotHash(key) {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
simple_hash.ts
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction addHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction mulHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = (31 * hash + c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfunction xorHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash ^= c.charCodeAt(0);\n    }\n    return hash % MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfunction rotHash(key: string): number {\n    let hash = 0;\n    const MODULUS = 1000000007;\n    for (const c of key) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c.charCodeAt(0)) % MODULUS;\n    }\n    return hash;\n}\n
simple_hash.dart
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint addHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint mulHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = (31 * hash + key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint xorHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash ^= key.codeUnitAt(i);\n  }\n  return hash & MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nint rotHash(String key) {\n  int hash = 0;\n  final int MODULUS = 1000000007;\n  for (int i = 0; i < key.length; i++) {\n    hash = ((hash << 4) ^ (hash >> 28) ^ key.codeUnitAt(i)) % MODULUS;\n  }\n  return hash;\n}\n
simple_hash.rs
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn add_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn mul_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = (31 * hash + c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfn xor_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash ^= c as i64;\n    }\n\n    (hash & MODULUS) as i32\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfn rot_hash(key: &str) -> i32 {\n    let mut hash = 0_i64;\n    const MODULUS: i64 = 1000000007;\n\n    for c in key.chars() {\n        hash = ((hash << 4) ^ (hash >> 28) ^ c as i64) % MODULUS;\n    }\n\n    hash as i32\n}\n
simple_hash.c
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint addHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint mulHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = (31 * hash + (unsigned char)key[i]) % MODULUS;\n    }\n    return (int)hash;\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nint xorHash(char *key) {\n    int hash = 0;\n    const int MODULUS = 1000000007;\n\n    for (int i = 0; i < strlen(key); i++) {\n        hash ^= (unsigned char)key[i];\n    }\n    return hash & MODULUS;\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nint rotHash(char *key) {\n    long long hash = 0;\n    const int MODULUS = 1000000007;\n    for (int i = 0; i < strlen(key); i++) {\n        hash = ((hash << 4) ^ (hash >> 28) ^ (unsigned char)key[i]) % MODULUS;\n    }\n\n    return (int)hash;\n}\n
simple_hash.kt
/* \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun addHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun mulHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = (31 * hash + c.code) % MODULUS\n    }\n    return hash.toInt()\n}\n\n/* XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 */\nfun xorHash(key: String): Int {\n    var hash = 0\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = hash xor c.code\n    }\n    return hash and MODULUS\n}\n\n/* \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c */\nfun rotHash(key: String): Int {\n    var hash = 0L\n    val MODULUS = 1000000007\n    for (c in key.toCharArray()) {\n        hash = ((hash shl 4) xor (hash shr 28) xor c.code.toLong()) % MODULUS\n    }\n    return hash.toInt()\n}\n
simple_hash.rb
=begin\nFile: simple_hash.rb\nCreated Time: 2024-04-14\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n=begin\nFile: simple_hash.rb\nCreated Time: 2024-04-14\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n# ## \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n=begin\nFile: simple_hash.rb\nCreated Time: 2024-04-14\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n# ## \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n# ## XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n=begin\nFile: simple_hash.rb\nCreated Time: 2024-04-14\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0410\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef add_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash += c.ord }\n\n  hash % modulus\nend\n\n# ## \u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0438\u043a\u0430\u0442\u0438\u0432\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef mul_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = 31 * hash + c.ord }\n\n  hash % modulus\nend\n\n# ## XOR-\u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 ###\ndef xor_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash ^= c.ord }\n\n  hash % modulus\nend\n\n# ## \u0425\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0441\u0434\u0432\u0438\u0433\u043e\u043c ###\ndef rot_hash(key)\n  hash = 0\n  modulus = 1_000_000_007\n\n  key.each_char { |c| hash = (hash << 4) ^ (hash >> 28) ^ c.ord }\n\n  hash % modulus\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u044d\u0442\u0438\u0445 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 - \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \\(1000000007\\) , \u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u0443\u043c\u043d\u044b\u0445 \u0433\u0440\u0430\u043d\u0438\u0446\u0430\u0445. \u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u0434\u0443\u043c\u0430\u0442\u044c\u0441\u044f: \u043f\u043e\u0447\u0435\u043c\u0443 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430, \u0438 \u043a\u0430\u043a\u0438\u0435 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0433\u043e \u043c\u043e\u0434\u0443\u043b\u044f? \u042d\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441.

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0434\u0438\u043c \u0432\u044b\u0432\u043e\u0434: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0443\u043b\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043e\u0431\u0449\u0438\u0445 \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435\u0439 \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438, \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0449\u0438\u0435 \u0438\u0437-\u0437\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430, \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0441\u043d\u0438\u0437\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439.

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0432\u044b\u0431\u0440\u0430\u043b\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(9\\) \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0443\u043b\u044f. \u041e\u043d\u043e \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \\(3\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0441\u0435 key , \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \\(3\\) , \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u0440\u0438 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f: \\(0\\) , \\(3\\) , \\(6\\) .

\\[ \\begin{aligned} \\text{modulus} & = 9 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 0, 3, 6, 0, 3, 6, 0, 3, 6,\\dots \\} \\end{aligned} \\]

\u0415\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 key \u043a\u0430\u043a \u0440\u0430\u0437 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0442\u0430\u043a\u043e\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e \u0432 \u0432\u0438\u0434\u0435 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u0438, \u0442\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u043d\u0443\u0442 \u0441\u043a\u0443\u0447\u0438\u0432\u0430\u0442\u044c\u0441\u044f, \u0430 \u044d\u0442\u043e \u0443\u0441\u0443\u0433\u0443\u0431\u0438\u0442 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438. \u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043b\u0438 modulus \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \\(13\\) ; \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043c\u0435\u0436\u0434\u0443 key \u0438 modulus \u043d\u0435\u0442 \u043e\u0431\u0449\u0438\u0445 \u0434\u0435\u043b\u0438\u0442\u0435\u043b\u0435\u0439, \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u0441\u044f.

\\[ \\begin{aligned} \\text{modulus} & = 13 \\newline \\text{key} & = \\{ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, \\dots \\} \\newline \\text{hash} & = \\{ 0, 3, 6, 9, 12, 2, 5, 8, 11, 1, 4, 7, \\dots \\} \\end{aligned} \\]

\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u0435\u0441\u043b\u0438 \u043c\u043e\u0436\u043d\u043e \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e key \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0438 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e, \u0442\u043e \u0432\u044b\u0431\u043e\u0440 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0438\u043b\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0443\u043b\u044f \u043d\u0435 \u0442\u0430\u043a \u0432\u0430\u0436\u0435\u043d - \u043e\u0431\u0430 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u0434\u0430\u0442\u044c \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041d\u043e \u0435\u0441\u043b\u0438 \u0432 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 key \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u043d\u043e\u0441\u0442\u044c, \u0442\u043e \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u043b\u0435\u0433\u0447\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438.

\u0418\u0442\u0430\u043a, \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043c\u043e\u0434\u0443\u043b\u044f, \u043f\u0440\u0438\u0447\u0435\u043c \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0436\u0435\u043b\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0443\u0431\u0440\u0430\u0442\u044c \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u0438 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

"},{"location":"chapter_hashing/hash_algorithm/#633-","title":"6.3.3 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b","text":"

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \"\u0445\u0440\u0443\u043f\u043a\u0438\u0435\" \u0438 \u0434\u0430\u043b\u0435\u043a\u0438 \u043e\u0442 \u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0446\u0435\u043b\u0435\u0439. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0438 XOR \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043a\u043e\u043c\u043c\u0443\u0442\u0430\u0442\u0438\u0432\u043d\u043e\u043c\u0443 \u0437\u0430\u043a\u043e\u043d\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u0434\u0434\u0438\u0442\u0438\u0432\u043d\u044b\u0439 \u0445\u0435\u0448 \u0438 XOR-\u0445\u0435\u0448 \u043d\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442 \u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0435 \u0438\u0437 \u043e\u0434\u043d\u0438\u0445 \u0438 \u0442\u0435\u0445 \u0436\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043d\u043e \u0432 \u0440\u0430\u0437\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u042d\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0443\u0441\u0438\u043b\u0438\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0438 \u0434\u0430\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a MD5, SHA-1, SHA-2 \u0438 SHA-3. \u041e\u043d\u0438 \u043c\u043e\u0433\u0443\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0432 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.

\u041d\u0430 \u043f\u0440\u043e\u0442\u044f\u0436\u0435\u043d\u0438\u0438 \u043f\u043e\u0447\u0442\u0438 \u0441\u0442\u0430 \u043b\u0435\u0442 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043b\u0438\u0441\u044c. \u041e\u0434\u043d\u0438 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0441\u0442\u0430\u0440\u0430\u043b\u0438\u0441\u044c \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u0438\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0430 \u0434\u0440\u0443\u0433\u0438\u0435 \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u0438 \u0438 \u0445\u0430\u043a\u0435\u0440\u044b \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u0443\u044f\u0437\u0432\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0432 \u0438\u0445 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438. \u0412 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 6-2 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 6-2 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b

MD5 SHA-1 SHA-2 SHA-3 \u0413\u043e\u0434 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f 1992 1995 2002 2008 \u0414\u043b\u0438\u043d\u0430 \u0432\u044b\u0432\u043e\u0434\u0430 128 bit 160 bit 256/512 bit 224/256/384/512 bit \u0425\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0427\u0430\u0441\u0442\u044b\u0435 \u0427\u0430\u0441\u0442\u044b\u0435 \u0420\u0435\u0434\u043a\u0438\u0435 \u0420\u0435\u0434\u043a\u0438\u0435 \u0423\u0440\u043e\u0432\u0435\u043d\u044c \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u0438 \u041d\u0438\u0437\u043a\u0438\u0439, \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0430\u0442\u0430\u043a\u043e\u0432\u0430\u043d \u041d\u0438\u0437\u043a\u0438\u0439, \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u0430\u0442\u0430\u043a\u043e\u0432\u0430\u043d \u0412\u044b\u0441\u043e\u043a\u0438\u0439 \u0412\u044b\u0441\u043e\u043a\u0438\u0439 \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0423\u0441\u0442\u0430\u0440\u0435\u043b, \u043d\u043e \u0435\u0449\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u043e\u0441\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0423\u0441\u0442\u0430\u0440\u0435\u043b \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043a\u0440\u0438\u043f\u0442\u043e\u0432\u0430\u043b\u044e\u0442\u043d\u044b\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0439, \u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 \u0438 \u0442. \u0434. \u041c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u0437\u0430\u043c\u0435\u043d\u0430 SHA-2"},{"location":"chapter_hashing/hash_algorithm/#634-","title":"6.3.4 \u00a0 \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e key \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u0441\u0442\u0440\u043e\u043a\u0430\u043c\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u042f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0442\u0438\u043f\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0431\u0430\u043a\u0435\u0442\u043e\u0432 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u0412\u043e\u0437\u044c\u043c\u0435\u043c Python: \u0432 \u043d\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e hash() , \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby built_in_hash.py
num = 3\nhash_num = hash(num)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 3\n\nbol = True\nhash_bol = hash(bol)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f True \u0440\u0430\u0432\u043d\u043e 1\n\ndec = 3.14159\nhash_dec = hash(dec)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e 326484311674566659\n\nstr = \"Hello Algo\"\nhash_str = hash(str)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e 4617003410720528961\n\ntup = (12836, \"\u0421\u044f\u043e \u0425\u0430\")\nhash_tup = hash(tup)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 (12836, \"\u0421\u044f\u043e \u0425\u0430\") \u0440\u0430\u0432\u043d\u043e 1029005403108185979\n\nobj = ListNode(0)\nhash_obj = hash(obj)\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0437\u043b\u0430 <ListNode object at 0x1058fd810> \u0440\u0430\u0432\u043d\u043e 274267521\n
built_in_hash.cpp
int num = 3;\nsize_t hashNum = hash<int>()(num);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 3\n\nbool bol = true;\nsize_t hashBol = hash<bool>()(bol);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f 1 \u0440\u0430\u0432\u043d\u043e 1\n\ndouble dec = 3.14159;\nsize_t hashDec = hash<double>()(dec);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e 4614256650576692846\n\nstring str = \"Hello Algo\";\nsize_t hashStr = hash<string>()(str);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e 15466937326284535026\n\n// \u0412 C++ \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 std::hash() \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445\n// \u0414\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e\n
built_in_hash.java
int num = 3;\nint hashNum = Integer.hashCode(num);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 3\n\nboolean bol = true;\nint hashBol = Boolean.hashCode(bol);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e 1231\n\ndouble dec = 3.14159;\nint hashDec = Double.hashCode(dec);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e -1340954729\n\nString str = \"Hello Algo\";\nint hashStr = str.hashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e -727081396\n\nObject[] arr = { 12836, \"\u0421\u044f\u043e \u0425\u0430\" };\nint hashTup = Arrays.hashCode(arr);\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [12836, \u0421\u044f\u043e \u0425\u0430] \u0440\u0430\u0432\u043d\u043e 1151158\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0437\u043b\u0430 utils.ListNode@7dc5e7b4 \u0440\u0430\u0432\u043d\u043e 2110121908\n
built_in_hash.cs
int num = 3;\nint hashNum = num.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 3;\n\nbool bol = true;\nint hashBol = bol.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e 1;\n\ndouble dec = 3.14159;\nint hashDec = dec.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e -1340954729;\n\nstring str = \"Hello Algo\";\nint hashStr = str.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e -586107568;\n\nobject[] arr = [12836, \"\u0421\u044f\u043e \u0425\u0430\"];\nint hashTup = arr.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [12836, \u0421\u044f\u043e \u0425\u0430] \u0440\u0430\u0432\u043d\u043e 42931033;\n\nListNode obj = new(0);\nint hashObj = obj.GetHashCode();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0437\u043b\u0430 0 \u0440\u0430\u0432\u043d\u043e 39053774;\n
built_in_hash.go
// \u0412 Go \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hash code\n
built_in_hash.swift
let num = 3\nlet hashNum = num.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 9047044699613009734\n\nlet bol = true\nlet hashBol = bol.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e -4431640247352757451\n\nlet dec = 3.14159\nlet hashDec = dec.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e -2465384235396674631\n\nlet str = \"Hello Algo\"\nlet hashStr = str.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e -7850626797806988787\n\nlet arr = [AnyHashable(12836), AnyHashable(\"\u0421\u044f\u043e \u0425\u0430\")]\nlet hashTup = arr.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [AnyHashable(12836), AnyHashable(\"\u0421\u044f\u043e \u0425\u0430\")] \u0440\u0430\u0432\u043d\u043e -2308633508154532996\n\nlet obj = ListNode(x: 0)\nlet hashObj = obj.hashValue\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0437\u043b\u0430 utils.ListNode \u0440\u0430\u0432\u043d\u043e -2434780518035996159\n
built_in_hash.js
// \u0412 JavaScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hash code\n
built_in_hash.ts
// \u0412 TypeScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hash code\n
built_in_hash.dart
int num = 3;\nint hashNum = num.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 34803\n\nbool bol = true;\nint hashBol = bol.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e 1231\n\ndouble dec = 3.14159;\nint hashDec = dec.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e 2570631074981783\n\nString str = \"Hello Algo\";\nint hashStr = str.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e 468167534\n\nList arr = [12836, \"\u0421\u044f\u043e \u0425\u0430\"];\nint hashArr = arr.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [12836, \u0421\u044f\u043e \u0425\u0430] \u0440\u0430\u0432\u043d\u043e 976512528\n\nListNode obj = new ListNode(0);\nint hashObj = obj.hashCode;\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Instance of 'ListNode' \u0440\u0430\u0432\u043d\u043e 1033450432\n
built_in_hash.rs
use std::collections::hash_map::DefaultHasher;\nuse std::hash::{Hash, Hasher};\n\nlet num = 3;\nlet mut num_hasher = DefaultHasher::new();\nnum.hash(&mut num_hasher);\nlet hash_num = num_hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 568126464209439262\n\nlet bol = true;\nlet mut bol_hasher = DefaultHasher::new();\nbol.hash(&mut bol_hasher);\nlet hash_bol = bol_hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e 4952851536318644461\n\nlet dec: f32 = 3.14159;\nlet mut dec_hasher = DefaultHasher::new();\ndec.to_bits().hash(&mut dec_hasher);\nlet hash_dec = dec_hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e 2566941990314602357\n\nlet str = \"Hello Algo\";\nlet mut str_hasher = DefaultHasher::new();\nstr.hash(&mut str_hasher);\nlet hash_str = str_hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e 16092673739211250988\n\nlet arr = (&12836, &\"\u0421\u044f\u043e \u0425\u0430\");\nlet mut tup_hasher = DefaultHasher::new();\narr.hash(&mut tup_hasher);\nlet hash_tup = tup_hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 (12836, \"\u0421\u044f\u043e \u0425\u0430\") \u0440\u0430\u0432\u043d\u043e 1885128010422702749\n\nlet node = ListNode::new(42);\nlet mut hasher = DefaultHasher::new();\nnode.borrow().val.hash(&mut hasher);\nlet hash = hasher.finish();\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 RefCell { value: ListNode { val: 42, next: None } } \u0440\u0430\u0432\u043d\u043e 15387811073369036852\n
built_in_hash.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 hash code\n
built_in_hash.kt
val num = 3\nval hashNum = num.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e 3\n\nval bol = true\nval hashBol = bol.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e 1231\n\nval dec = 3.14159\nval hashDec = dec.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e -1340954729\n\nval str = \"Hello Algo\"\nval hashStr = str.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e -727081396\n\nval arr = arrayOf<Any>(12836, \"\u0421\u044f\u043e \u0425\u0430\")\nval hashTup = arr.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [12836, \u0421\u044f\u043e \u0425\u0430] \u0440\u0430\u0432\u043d\u043e 189568618\n\nval obj = ListNode(0)\nval hashObj = obj.hashCode()\n// \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0443\u0437\u043b\u0430 utils.ListNode@1d81eb93 \u0440\u0430\u0432\u043d\u043e 495053715\n
built_in_hash.rb
num = 3\nhash_num = num.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0446\u0435\u043b\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 3 \u0440\u0430\u0432\u043d\u043e -4385856518450339636\n\nbol = true\nhash_bol = bol.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f true \u0440\u0430\u0432\u043d\u043e -1617938112149317027\n\ndec = 3.14159\nhash_dec = dec.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 3.14159 \u0440\u0430\u0432\u043d\u043e -1479186995943067893\n\nstr = \"Hello Algo\"\nhash_str = str.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \"Hello Algo\" \u0440\u0430\u0432\u043d\u043e -4075943250025831763\n\ntup = [12836, '\u0421\u044f\u043e \u0425\u0430']\nhash_tup = tup.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u0442\u0435\u0436\u0430 (12836, '\u0421\u044f\u043e \u0425\u0430') \u0440\u0430\u0432\u043d\u043e 1999544809202288822\n\nobj = ListNode.new(0)\nhash_obj = obj.hash\n# \u0425\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 #<ListNode:0x000078133140ab70> \u0440\u0430\u0432\u043d\u043e 4302940560806366381\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=class%20ListNode%3A%0A%20%20%20%20%22%22%22%D1%81%D0%B2%D1%8F%D0%B7%D0%BD%D1%8B%D0%B9%20%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%D1%83%D0%B7%D0%B5%D0%BB%D0%BA%D0%BB%D0%B0%D1%81%D1%81%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%23%20%D0%97%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%B0%0A%20%20%20%20%20%20%20%20self.next%3A%20ListNode%20%7C%20None%20%3D%20None%20%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D1%81%D0%BB%D0%B5%D0%B4%D1%83%D1%8E%D1%89%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20num%20%3D%203%0A%20%20%20%20hash_num%20%3D%20hash%28num%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%86%D0%B5%D0%BB%D0%BE%D0%B3%D0%BE%20%D1%87%D0%B8%D1%81%D0%BB%D0%B0%203%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%203%0A%0A%20%20%20%20bol%20%3D%20True%0A%20%20%20%20hash_bol%20%3D%20hash%28bol%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%B1%D1%83%D0%BB%D0%B5%D0%B2%D0%B0%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20True%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%201%0A%0A%20%20%20%20dec%20%3D%203.14159%0A%20%20%20%20hash_dec%20%3D%20hash%28dec%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%87%D0%B8%D1%81%D0%BB%D0%B0%203.14159%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%20326484311674566659%0A%0A%20%20%20%20str%20%3D%20%22Hello%20Algo%22%0A%20%20%20%20hash_str%20%3D%20hash%28str%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B8%20%22Hello%20Algo%22%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%204617003410720528961%0A%0A%20%20%20%20tup%20%3D%20%2812836%2C%20%22%D0%A1%D1%8F%D0%BE%20%D0%A5%D0%B0%22%29%0A%20%20%20%20hash_tup%20%3D%20hash%28tup%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%BA%D0%BE%D1%80%D1%82%D0%B5%D0%B6%D0%B0%20%2812836%2C%20%27%D0%A1%D1%8F%D0%BE%20%D0%A5%D0%B0%27%29%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%201029005403108185979%0A%0A%20%20%20%20obj%20%3D%20ListNode%280%29%0A%20%20%20%20hash_obj%20%3D%20hash%28obj%29%0A%20%20%20%20%23%20%D0%A5%D0%B5%D1%88-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%BE%D0%B1%D1%8A%D0%B5%D0%BA%D1%82%D0%B0%20%D1%83%D0%B7%D0%BB%D0%B0%20%3CListNode%20object%20at%200x1058fd810%3E%20%D1%80%D0%B0%D0%B2%D0%BD%D0%BE%20274267521&cumulative=false&curInstr=19&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

\u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 key \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b . \u0415\u0441\u043b\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a (\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432) \u043a\u0430\u043a key , \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f \u0438 \u0435\u0433\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043c\u044b \u0443\u0436\u0435 \u043d\u0435 \u0441\u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u043f\u0440\u0435\u0436\u043d\u0435\u0435 value \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435.

\u0425\u043e\u0442\u044f \u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0445 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0443\u0437\u043b\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430) \u043f\u043e\u043b\u044f \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u043c\u0438, \u0441\u0430\u043c \u043e\u0431\u044a\u0435\u043a\u0442 \u0432\u0441\u0435 \u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0445\u0435\u0448\u0438\u0440\u0443\u0435\u043c\u044b\u043c. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 : \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u0435\u0433\u043e \u0430\u0434\u0440\u0435\u0441 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0438 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f.

\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0433 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u043a\u043e\u043d\u0441\u043e\u043b\u044f\u0445 \u0432\u044b\u0432\u043e\u0434\u0438\u043c\u044b\u0435 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440 Python \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u0442\u0440\u043e\u043a \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u0443\u044e \u0441\u043e\u043b\u044c (salt). \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0437\u0430\u0449\u0438\u0449\u0430\u0435\u0442 \u043e\u0442 \u0430\u0442\u0430\u043a \u0442\u0438\u043f\u0430 HashDoS \u0438 \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

"},{"location":"chapter_hashing/hash_collision/","title":"6.2 \u00a0 \u0425\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438","text":"

\u041a\u0430\u043a \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435, \u0432 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0432\u0441\u0435\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b, \u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u043e \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c\u0441\u044f \u0432 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0438\u043d\u0434\u0435\u043a\u0441 \u0431\u0430\u043a\u0435\u0442\u0430.

\u0425\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u043f\u0440\u0438\u0432\u043e\u0434\u044f\u0442 \u043a \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u044b\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e \u0432\u043b\u0438\u044f\u044e\u0442 \u043d\u0430 \u043f\u0440\u0438\u0433\u043e\u0434\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044e. \u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u043f\u043e\u043a\u0430 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442 \u043d\u0435 \u0438\u0441\u0447\u0435\u0437\u043d\u0435\u0442. \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u043e\u0441\u0442 \u0438 \u0433\u0440\u0443\u0431, \u043d\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043c\u0430 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438.

  1. \u0423\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0447\u0442\u043e\u0431\u044b \u043e\u043d\u0430 \u043c\u043e\u0433\u043b\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439.
  2. \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u043c\u0438.

\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \"\u043c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a\" \u0438 \"\u043e\u0442\u043a\u0440\u044b\u0442\u0443\u044e \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044e\".

"},{"location":"chapter_hashing/hash_collision/#621","title":"6.2.1 \u00a0 \u041c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a","text":"

\u0412 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0430\u043a\u0435\u0442 \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a (separate chaining) \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a: \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0443\u0437\u043b\u0430\u043c\u0438 \u0441\u043f\u0438\u0441\u043a\u0430, \u0438 \u0432\u0441\u0435 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0443\u044e\u0449\u0438\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u043e\u043c \u0438 \u0442\u043e\u043c \u0436\u0435 \u0441\u043f\u0438\u0441\u043a\u0435. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-5 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0446\u0435\u043f\u043e\u0447\u0435\u043a.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-5 \u00a0 \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0446\u0435\u043f\u043e\u0447\u0435\u043a

\u041c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u0446\u0435\u043f\u043e\u0447\u0435\u043a, \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.

\u041c\u0435\u0442\u043e\u0434 \u0446\u0435\u043f\u043e\u0447\u0435\u043a \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u0435\u0442\u043e\u0434\u043e\u043c \u0446\u0435\u043f\u043e\u0447\u0435\u043a. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_chaining.py
class HashMapChaining:\n    \"\"\"\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self.size = 0  # \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        self.capacity = 4  # \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n        self.load_thres = 2.0 / 3.0  # \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.extend_ratio = 2  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.buckets = [[] for _ in range(self.capacity)]  # \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438\"\"\"\n        return self.size / self.capacity\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for pair in bucket:\n            if pair.key == key:\n                return pair.val\n        # \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\"\"\"\n        # \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for pair in bucket:\n            if pair.key == key:\n                pair.val = val\n                return\n        # \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        pair = Pair(key, val)\n        bucket.append(pair)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\"\"\"\n        index = self.hash_func(key)\n        bucket = self.buckets[index]\n        # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for pair in bucket:\n            if pair.key == key:\n                bucket.remove(pair)\n                self.size -= 1\n                break\n\n    def extend(self):\n        \"\"\"\u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\"\"\"\n        # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        buckets = self.buckets\n        # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.capacity *= self.extend_ratio\n        self.buckets = [[] for _ in range(self.capacity)]\n        self.size = 0\n        # \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for bucket in buckets:\n            for pair in bucket:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\"\"\"\n        for bucket in self.buckets:\n            res = []\n            for pair in bucket:\n                res.append(str(pair.key) + \" -> \" + pair.val)\n            print(res)\n
hash_map_chaining.cpp
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n  private:\n    int size;                       // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity;                   // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres;               // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio;                // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    vector<vector<Pair *>> buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    HashMapChaining() : size(0), capacity(4), loadThres(2.0 / 3.0), extendRatio(2) {\n        buckets.resize(capacity);\n    }\n\n    /* \u041c\u0435\u0442\u043e\u0434-\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ~HashMapChaining() {\n        for (auto &bucket : buckets) {\n            for (Pair *pair : bucket) {\n                // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n                delete pair;\n            }\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    double loadFactor() {\n        return (double)size / (double)capacity;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    string get(int key) {\n        int index = hashFunc(key);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                return pair->val;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443\n        return \"\";\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    void put(int key, string val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for (Pair *pair : buckets[index]) {\n            if (pair->key == key) {\n                pair->val = val;\n                return;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        buckets[index].push_back(new Pair(key, val));\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    void remove(int key) {\n        int index = hashFunc(key);\n        auto &bucket = buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (int i = 0; i < bucket.size(); i++) {\n            if (bucket[i]->key == key) {\n                Pair *tmp = bucket[i];\n                bucket.erase(bucket.begin() + i); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n                delete tmp;                       // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n                size--;\n                return;\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        vector<vector<Pair *>> bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets.clear();\n        buckets.resize(capacity);\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (auto &bucket : bucketsTmp) {\n            for (Pair *pair : bucket) {\n                put(pair->key, pair->val);\n                // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n                delete pair;\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void print() {\n        for (auto &bucket : buckets) {\n            cout << \"[\";\n            for (Pair *pair : bucket) {\n                cout << pair->key << \" -> \" << pair->val << \", \";\n            }\n            cout << \"]\\n\";\n        }\n    }\n};\n
hash_map_chaining.java
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    int size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    List<List<Pair>> buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    String get(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    void put(int key, String val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        Pair pair = new Pair(key, val);\n        bucket.add(pair);\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    void remove(int key) {\n        int index = hashFunc(key);\n        List<Pair> bucket = buckets.get(index);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (Pair pair : bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets = new ArrayList<>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.add(new ArrayList<>());\n        }\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (List<Pair> bucket : bucketsTmp) {\n            for (Pair pair : bucket) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void print() {\n        for (List<Pair> bucket : buckets) {\n            List<String> res = new ArrayList<>();\n            for (Pair pair : bucket) {\n                res.add(pair.key + \" -> \" + pair.val);\n            }\n            System.out.println(res);\n        }\n    }\n}\n
hash_map_chaining.cs
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    int size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    List<List<Pair>> buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public HashMapChaining() {\n        size = 0;\n        capacity = 4;\n        loadThres = 2.0 / 3.0;\n        extendRatio = 2;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                return pair.val;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public void Put(int key, string val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        int index = HashFunc(key);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        foreach (Pair pair in buckets[index]) {\n            if (pair.key == key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        buckets[index].Add(new Pair(key, val));\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        foreach (Pair pair in buckets[index].ToList()) {\n            if (pair.key == key) {\n                buckets[index].Remove(pair);\n                size--;\n                break;\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void Extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        List<List<Pair>> bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets = new List<List<Pair>>(capacity);\n        for (int i = 0; i < capacity; i++) {\n            buckets.Add([]);\n        }\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        foreach (List<Pair> bucket in bucketsTmp) {\n            foreach (Pair pair in bucket) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public void Print() {\n        foreach (List<Pair> bucket in buckets) {\n            List<string> res = [];\n            foreach (Pair pair in bucket) {\n                res.Add(pair.key + \" -> \" + pair.val);\n            }\n            foreach (string kv in res) {\n                Console.WriteLine(kv);\n            }\n        }\n    }\n}\n
hash_map_chaining.go
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\ntype hashMapChaining struct {\n    size        int      // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    capacity    int      // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    loadThres   float64  // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    extendRatio int      // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    buckets     [][]pair // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newHashMapChaining() *hashMapChaining {\n    buckets := make([][]pair, 4)\n    for i := 0; i < 4; i++ {\n        buckets[i] = make([]pair, 0)\n    }\n    return &hashMapChaining{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     buckets,\n    }\n}\n\n/* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc (m *hashMapChaining) hashFunc(key int) int {\n    return key % m.capacity\n}\n\n/* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\nfunc (m *hashMapChaining) loadFactor() float64 {\n    return float64(m.size) / float64(m.capacity)\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\nfunc (m *hashMapChaining) get(key int) string {\n    idx := m.hashFunc(key)\n    bucket := m.buckets[idx]\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    for _, p := range bucket {\n        if p.key == key {\n            return p.val\n        }\n    }\n    // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443\n    return \"\"\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nfunc (m *hashMapChaining) put(key int, val string) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if m.loadFactor() > m.loadThres {\n        m.extend()\n    }\n    idx := m.hashFunc(key)\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    for i := range m.buckets[idx] {\n        if m.buckets[idx][i].key == key {\n            m.buckets[idx][i].val = val\n            return\n        }\n    }\n    // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n    p := pair{\n        key: key,\n        val: val,\n    }\n    m.buckets[idx] = append(m.buckets[idx], p)\n    m.size += 1\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nfunc (m *hashMapChaining) remove(key int) {\n    idx := m.hashFunc(key)\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    for i, p := range m.buckets[idx] {\n        if p.key == key {\n            // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u0440\u0435\u0437\u0430\n            m.buckets[idx] = append(m.buckets[idx][:i], m.buckets[idx][i+1:]...)\n            m.size -= 1\n            break\n        }\n    }\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nfunc (m *hashMapChaining) extend() {\n    // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    tmpBuckets := make([][]pair, len(m.buckets))\n    for i := 0; i < len(m.buckets); i++ {\n        tmpBuckets[i] = make([]pair, len(m.buckets[i]))\n        copy(tmpBuckets[i], m.buckets[i])\n    }\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    m.capacity *= m.extendRatio\n    m.buckets = make([][]pair, m.capacity)\n    for i := 0; i < m.capacity; i++ {\n        m.buckets[i] = make([]pair, 0)\n    }\n    m.size = 0\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for _, bucket := range tmpBuckets {\n        for _, p := range bucket {\n            m.put(p.key, p.val)\n        }\n    }\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nfunc (m *hashMapChaining) print() {\n    var builder strings.Builder\n\n    for _, bucket := range m.buckets {\n        builder.WriteString(\"[\")\n        for _, p := range bucket {\n            builder.WriteString(strconv.Itoa(p.key) + \" -> \" + p.val + \" \")\n        }\n        builder.WriteString(\"]\")\n        fmt.Println(builder.String())\n        builder.Reset()\n    }\n}\n
hash_map_chaining.swift
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    var size: Int // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    var capacity: Int // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    var loadThres: Double // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    var extendRatio: Int // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    var buckets: [[Pair]] // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: [], count: capacity)\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for pair in bucket {\n            if pair.key == key {\n                return pair.val\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c nil\n        return nil\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    func put(key: Int, val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if loadFactor() > loadThres {\n            extend()\n        }\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for pair in bucket {\n            if pair.key == key {\n                pair.val = val\n                return\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        let pair = Pair(key: key, val: val)\n        buckets[index].append(pair)\n        size += 1\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        let bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (pairIndex, pair) in bucket.enumerated() {\n            if pair.key == key {\n                buckets[index].remove(at: pairIndex)\n                size -= 1\n                break\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    func extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        let bucketsTmp = buckets\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio\n        buckets = Array(repeating: [], count: capacity)\n        size = 0\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for bucket in bucketsTmp {\n            for pair in bucket {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    func print() {\n        for bucket in buckets {\n            let res = bucket.map { \"\\($0.key) -> \\($0.val)\" }\n            Swift.print(res)\n        }\n    }\n}\n
hash_map_chaining.js
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    #size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    #capacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    #loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    get(key) {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    put(key, val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    remove(key) {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    #extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        const bucketsTmp = this.#buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    print() {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
hash_map_chaining.ts
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    #size: number; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    #capacity: number; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    #loadThres: number; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #extendRatio: number; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #buckets: Pair[][]; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.#size = 0;\n        this.#capacity = 4;\n        this.#loadThres = 2.0 / 3.0;\n        this.#extendRatio = 2;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    #hashFunc(key: number): number {\n        return key % this.#capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    #loadFactor(): number {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    get(key: number): string | null {\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                return pair.val;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    put(key: number, val: string): void {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        const index = this.#hashFunc(key);\n        const bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for (const pair of bucket) {\n            if (pair.key === key) {\n                pair.val = val;\n                return;\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        const pair = new Pair(key, val);\n        bucket.push(pair);\n        this.#size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    remove(key: number): void {\n        const index = this.#hashFunc(key);\n        let bucket = this.#buckets[index];\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (let i = 0; i < bucket.length; i++) {\n            if (bucket[i].key === key) {\n                bucket.splice(i, 1);\n                this.#size--;\n                break;\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    #extend(): void {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        const bucketsTmp = this.#buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = new Array(this.#capacity).fill(null).map((x) => []);\n        this.#size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (const bucket of bucketsTmp) {\n            for (const pair of bucket) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    print(): void {\n        for (const bucket of this.#buckets) {\n            let res = [];\n            for (const pair of bucket) {\n                res.push(pair.key + ' -> ' + pair.val);\n            }\n            console.log(res);\n        }\n    }\n}\n
hash_map_chaining.dart
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n  late int size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n  late int capacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n  late double loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  late int extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  late List<List<Pair>> buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  HashMapChaining() {\n    size = 0;\n    capacity = 4;\n    loadThres = 2.0 / 3.0;\n    extendRatio = 2;\n    buckets = List.generate(capacity, (_) => []);\n  }\n\n  /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n  int hashFunc(int key) {\n    return key % capacity;\n  }\n\n  /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n  double loadFactor() {\n    return size / capacity;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n  String? get(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        return pair.val;\n      }\n    }\n    // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n    return null;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n  void put(int key, String val) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (loadFactor() > loadThres) {\n      extend();\n    }\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        pair.val = val;\n        return;\n      }\n    }\n    // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n    Pair pair = Pair(key, val);\n    bucket.add(pair);\n    size++;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n  void remove(int key) {\n    int index = hashFunc(key);\n    List<Pair> bucket = buckets[index];\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    for (Pair pair in bucket) {\n      if (pair.key == key) {\n        bucket.remove(pair);\n        size--;\n        break;\n      }\n    }\n  }\n\n  /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n  void extend() {\n    // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    List<List<Pair>> bucketsTmp = buckets;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    capacity *= extendRatio;\n    buckets = List.generate(capacity, (_) => []);\n    size = 0;\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for (List<Pair> bucket in bucketsTmp) {\n      for (Pair pair in bucket) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n  void printHashMap() {\n    for (List<Pair> bucket in buckets) {\n      List<String> res = [];\n      for (Pair pair in bucket) {\n        res.add(\"${pair.key} -> ${pair.val}\");\n      }\n      print(res);\n    }\n  }\n}\n
hash_map_chaining.rs
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nstruct HashMapChaining {\n    size: usize,\n    capacity: usize,\n    load_thres: f32,\n    extend_ratio: usize,\n    buckets: Vec<Vec<Pair>>,\n}\n\nimpl HashMapChaining {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![vec![]; 4],\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % self.capacity\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    fn load_factor(&self) -> f32 {\n        self.size as f32 / self.capacity as f32\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    fn remove(&mut self, key: i32) -> Option<String> {\n        let index = self.hash_func(key);\n\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (i, p) in self.buckets[index].iter_mut().enumerate() {\n            if p.key == key {\n                let pair = self.buckets[index].remove(i);\n                self.size -= 1;\n                return Some(pair.val);\n            }\n        }\n\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None\n        None\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fn extend(&mut self) {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        let buckets_tmp = std::mem::take(&mut self.buckets);\n\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![Vec::new(); self.capacity as usize];\n        self.size = 0;\n\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for bucket in buckets_tmp {\n            for pair in bucket {\n                self.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fn print(&self) {\n        for bucket in &self.buckets {\n            let mut res = Vec::new();\n            for pair in bucket {\n                res.push(format!(\"{} -> {}\", pair.key, pair.val));\n            }\n            println!(\"{:?}\", res);\n        }\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    fn put(&mut self, key: i32, val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n\n        let index = self.hash_func(key);\n\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for pair in self.buckets[index].iter_mut() {\n            if pair.key == key {\n                pair.val = val;\n                return;\n            }\n        }\n\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        let pair = Pair { key, val };\n        self.buckets[index].push(pair);\n        self.size += 1;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    fn get(&self, key: i32) -> Option<&str> {\n        let index = self.hash_func(key);\n\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for pair in self.buckets[index].iter() {\n            if pair.key == key {\n                return Some(&pair.val);\n            }\n        }\n\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None\n        None\n    }\n}\n
hash_map_chaining.c
/* \u0423\u0437\u0435\u043b \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct Node {\n    Pair *pair;\n    struct Node *next;\n} Node;\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\ntypedef struct {\n    int size;         // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity;     // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio;  // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    Node **buckets;   // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n} HashMapChaining;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nHashMapChaining *newHashMapChaining() {\n    HashMapChaining *hashMap = (HashMapChaining *)malloc(sizeof(HashMapChaining));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    return hashMap;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delHashMapChaining(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        while (cur) {\n            Node *tmp = cur;\n            cur = cur->next;\n            free(tmp->pair);\n            free(tmp);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap);\n}\n\n/* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\nint hashFunc(HashMapChaining *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\ndouble loadFactor(HashMapChaining *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\nchar *get(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            return cur->pair->val;\n        }\n        cur = cur->next;\n    }\n    return \"\"; // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nvoid put(HashMapChaining *hashMap, int key, const char *val) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    int index = hashFunc(hashMap, key);\n    // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    Node *cur = hashMap->buckets[index];\n    while (cur) {\n        if (cur->pair->key == key) {\n            strcpy(cur->pair->val, val); // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            return;\n        }\n        cur = cur->next;\n    }\n    // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\n    Pair *newPair = (Pair *)malloc(sizeof(Pair));\n    newPair->key = key;\n    strcpy(newPair->val, val);\n    Node *newNode = (Node *)malloc(sizeof(Node));\n    newNode->pair = newPair;\n    newNode->next = hashMap->buckets[index];\n    hashMap->buckets[index] = newNode;\n    hashMap->size++;\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid extend(HashMapChaining *hashMap) {\n    // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    int oldCapacity = hashMap->capacity;\n    Node **oldBuckets = hashMap->buckets;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Node **)malloc(hashMap->capacity * sizeof(Node *));\n    for (int i = 0; i < hashMap->capacity; i++) {\n        hashMap->buckets[i] = NULL;\n    }\n    hashMap->size = 0;\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for (int i = 0; i < oldCapacity; i++) {\n        Node *cur = oldBuckets[i];\n        while (cur) {\n            put(hashMap, cur->pair->key, cur->pair->val);\n            Node *temp = cur;\n            cur = cur->next;\n            // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n            free(temp->pair);\n            free(temp);\n        }\n    }\n\n    free(oldBuckets);\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nvoid removeItem(HashMapChaining *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    Node *cur = hashMap->buckets[index];\n    Node *pre = NULL;\n    while (cur) {\n        if (cur->pair->key == key) {\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n            if (pre) {\n                pre->next = cur->next;\n            } else {\n                hashMap->buckets[index] = cur->next;\n            }\n            // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n            free(cur->pair);\n            free(cur);\n            hashMap->size--;\n            return;\n        }\n        pre = cur;\n        cur = cur->next;\n    }\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid print(HashMapChaining *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Node *cur = hashMap->buckets[i];\n        printf(\"[\");\n        while (cur) {\n            printf(\"%d -> %s, \", cur->pair->key, cur->pair->val);\n            cur = cur->next;\n        }\n        printf(\"]\\n\");\n    }\n}\n
hash_map_chaining.kt
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 */\nclass HashMapChaining {\n    var size: Int // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    var capacity: Int // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    val loadThres: Double // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    val extendRatio: Int // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    var buckets: MutableList<MutableList<Pair>> // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        for (pair in bucket) {\n            if (pair.key == key) return pair._val\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    fun put(key: Int, _val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        for (pair in bucket) {\n            if (pair.key == key) {\n                pair._val = _val\n                return\n            }\n        }\n        // \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n        val pair = Pair(key, _val)\n        bucket.add(pair)\n        size++\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        val bucket = buckets[index]\n        // \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        for (pair in bucket) {\n            if (pair.key == key) {\n                bucket.remove(pair)\n                size--\n                break\n            }\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fun extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        val bucketsTmp = buckets\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio\n        // mutablelist \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430\n        buckets = mutableListOf()\n        for (i in 0..<capacity) {\n            buckets.add(mutableListOf())\n        }\n        size = 0\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (bucket in bucketsTmp) {\n            for (pair in bucket) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fun print() {\n        for (bucket in buckets) {\n            val res = mutableListOf<String>()\n            for (pair in bucket) {\n                val k = pair.key\n                val v = pair._val\n                res.add(\"$k -> $v\")\n            }\n            println(res)\n        }\n    }\n}\n
hash_map_chaining.rb
=begin\nFile: hash_map_chaining.rb\nCreated Time: 2024-04-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative './array_hash_map'\n\n# ## \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u0446\u0435\u043f\u043e\u0447\u043a\u0430\u043c\u0438 ###\nclass HashMapChaining\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @size = 0 # \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    @capacity = 4 # \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    @load_thres = 2.0 / 3.0 # \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @extend_ratio = 2 # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @buckets = Array.new(@capacity) { [] } # \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n  end\n\n  # ## \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  # ## \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 ###\n  def get(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u043d\u0430\u0439\u0434\u0435\u043d key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    for pair in bucket\n      return pair.val if pair.key == key\n    end\n    # \u0415\u0441\u043b\u0438 key \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c nil\n    nil\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f ###\n  def put(key, val)\n    # \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    extend if load_factor > @load_thres\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 key, \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    for pair in bucket\n      if pair.key == key\n        pair.val = val\n        return\n      end\n    end\n    # \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0433\u043e key \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446\n    pair = Pair.new(key, val)\n    bucket << pair\n    @size += 1\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f ###\n  def remove(key)\n    index = hash_func(key)\n    bucket = @buckets[index]\n    # \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0435 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    for pair in bucket\n      if pair.key == key\n        bucket.delete(pair)\n        @size -= 1\n        break\n      end\n    end\n  end\n\n  # ## \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b ###\n  def extend\n    # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    buckets = @buckets\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity) { [] }\n    @size = 0\n    # \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for bucket in buckets\n      for pair in bucket\n        put(pair.key, pair.val)\n      end\n    end\n  end\n\n  # ## \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 ###\n  def print\n    for bucket in @buckets\n      res = []\n      for pair in bucket\n        res << \"#{pair.key} -> #{pair.val}\"\n      end\n      pp res\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0434\u043b\u0438\u043d\u043d\u044b\u043c, \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \\(O(n)\\) \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0438\u0437\u043a\u043e\u0439. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \"AVL-\u0434\u0435\u0440\u0435\u0432\u043e\" \u0438\u043b\u0438 \"\u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\" , \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0434\u043e \\(O(\\log n)\\) .

"},{"location":"chapter_hashing/hash_collision/#622","title":"6.2.2 \u00a0 \u041e\u0442\u043a\u0440\u044b\u0442\u0430\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f","text":"

\u041e\u0442\u043a\u0440\u044b\u0442\u0430\u044f \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u044f (open addressing) \u043d\u0435 \u0432\u0432\u043e\u0434\u0438\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \"\u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\"; \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435.

\u041d\u0438\u0436\u0435 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439.

"},{"location":"chapter_hashing/hash_collision/#1","title":"1. \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0448\u0430\u0433\u043e\u043c. \u0415\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-6 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 (\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435). \u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0432\u0441\u0435 key \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c\u0438 \u0446\u0438\u0444\u0440\u0430\u043c\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0431\u0430\u043a\u0435\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044e \u043e\u043d\u0438 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u044d\u0442\u043e\u043c \u0431\u0430\u043a\u0435\u0442\u0435 \u0438 \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0437\u0430 \u043d\u0438\u043c \u0431\u0430\u043a\u0435\u0442\u0430\u0445.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-6 \u00a0 \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 (\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435)

\u041e\u0434\u043d\u0430\u043a\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043b\u0435\u0433\u043a\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \"\u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438\". \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0447\u0435\u043c \u0434\u043b\u0438\u043d\u043d\u0435\u0435 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u0430\u044f \u0437\u0430\u043d\u044f\u0442\u0430\u044f \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0442\u0435\u043c \u0432\u044b\u0448\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u043d\u043e\u0432\u044b\u0445 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439 \u0432 \u044d\u0442\u043e\u0439 \u043e\u0431\u043b\u0430\u0441\u0442\u0438, \u0447\u0442\u043e \u0435\u0449\u0435 \u0441\u0438\u043b\u044c\u043d\u0435\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u043e\u0441\u0442\u0443 \u044d\u0442\u043e\u0439 \u0433\u0440\u0443\u043f\u043f\u044b \u0438 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f.

\u0421\u0442\u043e\u0438\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0432\u043d\u0443\u0442\u0440\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0443\u0441\u0442\u043e\u0439 \u0431\u0430\u043a\u0435\u0442 None , \u0430 \u043f\u0440\u0438 \u043f\u043e\u0438\u0441\u043a\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u0443\u0441\u0442\u043e\u043c \u0431\u0430\u043a\u0435\u0442\u0435 \u0438 \u0432\u0435\u0440\u043d\u0435\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0438\u0436\u0435 \u044d\u0442\u043e\u0433\u043e \u0431\u0430\u043a\u0435\u0442\u0430 \u0443\u0436\u0435 \u043d\u0435 \u0441\u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u044b, \u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0448\u0438\u0431\u043e\u0447\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u0445 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-7.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-7 \u00a0 \u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043b\u0435\u043d\u0438\u0432\u043e\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f (lazy deletion): \u043e\u043d \u043d\u0435 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, **\u0430 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \u0431\u0430\u043a\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043e\u0439 TOMBSTONE **. \u0412 \u044d\u0442\u043e\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u0435 \u0438 None , \u0438 TOMBSTONE \u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0431\u0430\u043a\u0435\u0442, \u0438 \u043e\u0431\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041d\u043e \u0435\u0441\u0442\u044c \u0432\u0430\u0436\u043d\u043e\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u0435: \u043f\u0440\u0438 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u043c \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438, \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432 TOMBSTONE , \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043e\u0431\u0445\u043e\u0434, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0438\u0436\u0435 \u043d\u0435\u0433\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u043c\u043e\u0433\u0443\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.

\u041e\u0434\u043d\u0430\u043a\u043e \u043b\u0435\u043d\u0438\u0432\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0443\u0441\u043a\u043e\u0440\u044f\u0442\u044c \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f; \u043f\u043e \u043c\u0435\u0440\u0435 \u0440\u043e\u0441\u0442\u0430 \u0447\u0438\u0441\u043b\u0430 TOMBSTONE \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u043e\u0436\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u043e \u043f\u0435\u0440\u0435\u0441\u043a\u0430\u043a\u0438\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e TOMBSTONE , \u043f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043d\u0430\u0439\u0434\u0435\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b \u043f\u0440\u0438 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u043c \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0435\u043d\u043d\u043e\u0433\u043e TOMBSTONE \u0438 \u0437\u0430\u0442\u0435\u043c \u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u044d\u0442\u0438\u043c TOMBSTONE . \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435 \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u0432 \u0431\u0430\u043a\u0435\u0442, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0439 \u0431\u043b\u0438\u0436\u0435 \u043a \u0435\u0433\u043e \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 (\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f), \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u0441\u044f.

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 (\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435), \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0430\u044f \u043b\u0435\u043d\u0438\u0432\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u043b\u043d\u043e, \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0435\u0435 \u043a\u0430\u043a \"\u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\": \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0445\u043e\u0434 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u0442\u0441\u044f.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map_open_addressing.py
class HashMapOpenAddressing:\n    \"\"\"\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self.size = 0  # \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        self.capacity = 4  # \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n        self.load_thres = 2.0 / 3.0  # \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.extend_ratio = 2  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.buckets: list[Pair | None] = [None] * self.capacity  # \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n        self.TOMBSTONE = Pair(-1, \"-1\")  # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f\"\"\"\n        return key % self.capacity\n\n    def load_factor(self) -> float:\n        \"\"\"\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438\"\"\"\n        return self.size / self.capacity\n\n    def find_bucket(self, key: int) -> int:\n        \"\"\"\u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\"\"\"\n        index = self.hash_func(key)\n        first_tombstone = -1\n        # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while self.buckets[index] is not None:\n            # \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if self.buckets[index].key == key:\n                # \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if first_tombstone != -1:\n                    self.buckets[first_tombstone] = self.buckets[index]\n                    self.buckets[index] = self.TOMBSTONE\n                    return first_tombstone  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                return index  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if first_tombstone == -1 and self.buckets[index] is self.TOMBSTONE:\n                first_tombstone = index\n            # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % self.capacity\n        # \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return index if first_tombstone == -1 else first_tombstone\n\n    def get(self, key: int) -> str:\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430\"\"\"\n        # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        index = self.find_bucket(key)\n        # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            return self.buckets[index].val\n        # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None\n        return None\n\n    def put(self, key: int, val: str):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\"\"\"\n        # \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.load_factor() > self.load_thres:\n            self.extend()\n        # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        index = self.find_bucket(key)\n        # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index].val = val\n            return\n        # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        self.buckets[index] = Pair(key, val)\n        self.size += 1\n\n    def remove(self, key: int):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\"\"\"\n        # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        index = self.find_bucket(key)\n        # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if self.buckets[index] not in [None, self.TOMBSTONE]:\n            self.buckets[index] = self.TOMBSTONE\n            self.size -= 1\n\n    def extend(self):\n        \"\"\"\u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\"\"\"\n        # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        buckets_tmp = self.buckets\n        # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.capacity *= self.extend_ratio\n        self.buckets = [None] * self.capacity\n        self.size = 0\n        # \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for pair in buckets_tmp:\n            if pair not in [None, self.TOMBSTONE]:\n                self.put(pair.key, pair.val)\n\n    def print(self):\n        \"\"\"\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\"\"\"\n        for pair in self.buckets:\n            if pair is None:\n                print(\"None\")\n            elif pair is self.TOMBSTONE:\n                print(\"TOMBSTONE\")\n            else:\n                print(pair.key, \"->\", pair.val)\n
hash_map_open_addressing.cpp
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n  private:\n    int size;                             // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity = 4;                     // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    const double loadThres = 2.0 / 3.0;     // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    const int extendRatio = 2;            // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    vector<Pair *> buckets;               // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    Pair *TOMBSTONE = new Pair(-1, \"-1\"); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    HashMapOpenAddressing() : size(0), buckets(capacity, nullptr) {\n    }\n\n    /* \u041c\u0435\u0442\u043e\u0434-\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ~HashMapOpenAddressing() {\n        for (Pair *pair : buckets) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                delete pair;\n            }\n        }\n        delete TOMBSTONE;\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    double loadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (buckets[index] != nullptr) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (buckets[index]->key == key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    string get(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            return buckets[index]->val;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443\n        return \"\";\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    void put(int key, string val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            buckets[index]->val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    void remove(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (buckets[index] != nullptr && buckets[index] != TOMBSTONE) {\n            delete buckets[index];\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        vector<Pair *> bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets = vector<Pair *>(capacity, nullptr);\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (Pair *pair : bucketsTmp) {\n            if (pair != nullptr && pair != TOMBSTONE) {\n                put(pair->key, pair->val);\n                delete pair;\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void print() {\n        for (Pair *pair : buckets) {\n            if (pair == nullptr) {\n                cout << \"nullptr\" << endl;\n            } else if (pair == TOMBSTONE) {\n                cout << \"TOMBSTONE\" << endl;\n            } else {\n                cout << pair->key << \" -> \" << pair->val << endl;\n            }\n        }\n    }\n};\n
hash_map_open_addressing.java
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    private int size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    private int capacity = 4; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    private final double loadThres = 2.0 / 3.0; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private final int extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private Pair[] buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    private final Pair TOMBSTONE = new Pair(-1, \"-1\"); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    private int hashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    private double loadFactor() {\n        return (double) size / capacity;\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    private int findBucket(int key) {\n        int index = hashFunc(key);\n        int firstTombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (buckets[index] != null) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (buckets[index].key == key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public String get(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public void put(int key, String val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public void remove(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    private void extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        Pair[] bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (Pair pair : bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public void print() {\n        for (Pair pair : buckets) {\n            if (pair == null) {\n                System.out.println(\"null\");\n            } else if (pair == TOMBSTONE) {\n                System.out.println(\"TOMBSTONE\");\n            } else {\n                System.out.println(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
hash_map_open_addressing.cs
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    int size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity = 4; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres = 2.0 / 3.0; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    Pair[] buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    Pair TOMBSTONE = new(-1, \"-1\"); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public HashMapOpenAddressing() {\n        size = 0;\n        buckets = new Pair[capacity];\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int HashFunc(int key) {\n        return key % capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    double LoadFactor() {\n        return (double)size / capacity;\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    int FindBucket(int key) {\n        int index = HashFunc(key);\n        int firstTombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (buckets[index] != null) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (buckets[index].key == key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index];\n                    buckets[index] = TOMBSTONE;\n                    return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone == -1 ? index : firstTombstone;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public string? Get(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = FindBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index].val;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public void Put(int key, string val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (LoadFactor() > loadThres) {\n            Extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = FindBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index].val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        buckets[index] = new Pair(key, val);\n        size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public void Remove(int key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        int index = FindBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE;\n            size--;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void Extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        Pair[] bucketsTmp = buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio;\n        buckets = new Pair[capacity];\n        size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        foreach (Pair pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                Put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public void Print() {\n        foreach (Pair pair in buckets) {\n            if (pair == null) {\n                Console.WriteLine(\"null\");\n            } else if (pair == TOMBSTONE) {\n                Console.WriteLine(\"TOMBSTONE\");\n            } else {\n                Console.WriteLine(pair.key + \" -> \" + pair.val);\n            }\n        }\n    }\n}\n
hash_map_open_addressing.go
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\ntype hashMapOpenAddressing struct {\n    size        int     // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    capacity    int     // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    loadThres   float64 // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    extendRatio int     // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    buckets     []*pair // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    TOMBSTONE   *pair   // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newHashMapOpenAddressing() *hashMapOpenAddressing {\n    return &hashMapOpenAddressing{\n        size:        0,\n        capacity:    4,\n        loadThres:   2.0 / 3.0,\n        extendRatio: 2,\n        buckets:     make([]*pair, 4),\n        TOMBSTONE:   &pair{-1, \"-1\"},\n    }\n}\n\n/* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc (h *hashMapOpenAddressing) hashFunc(key int) int {\n    return key % h.capacity // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e \u043a\u043b\u044e\u0447\u0443\n}\n\n/* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\nfunc (h *hashMapOpenAddressing) loadFactor() float64 {\n    return float64(h.size) / float64(h.capacity) // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\nfunc (h *hashMapOpenAddressing) findBucket(key int) int {\n    index := h.hashFunc(key) // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\n    firstTombstone := -1     // \u0417\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e TOMBSTONE\n    for h.buckets[index] != nil {\n        if h.buckets[index].key == key {\n            if firstTombstone != -1 {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                h.buckets[firstTombstone] = h.buckets[index]\n                h.buckets[index] = h.TOMBSTONE\n                return firstTombstone // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n            }\n            return index // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\n        }\n        if firstTombstone == -1 && h.buckets[index] == h.TOMBSTONE {\n            firstTombstone = index // \u0417\u0430\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0439 \u043c\u0435\u0442\u043a\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        }\n        index = (index + 1) % h.capacity // \u041b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u0445\u0432\u043e\u0441\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n    }\n    // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n    if firstTombstone != -1 {\n        return firstTombstone\n    }\n    return index\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\nfunc (h *hashMapOpenAddressing) get(key int) string {\n    index := h.findBucket(key) // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        return h.buckets[index].val // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    }\n    return \"\" // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \"\"\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nfunc (h *hashMapOpenAddressing) put(key int, val string) {\n    if h.loadFactor() > h.loadThres {\n        h.extend() // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    }\n    index := h.findBucket(key) // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    if h.buckets[index] == nil || h.buckets[index] == h.TOMBSTONE {\n        h.buckets[index] = &pair{key, val} // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        h.size++\n    } else {\n        h.buckets[index].val = val // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val\n    }\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nfunc (h *hashMapOpenAddressing) remove(key int) {\n    index := h.findBucket(key) // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    if h.buckets[index] != nil && h.buckets[index] != h.TOMBSTONE {\n        h.buckets[index] = h.TOMBSTONE // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        h.size--\n    }\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nfunc (h *hashMapOpenAddressing) extend() {\n    oldBuckets := h.buckets               // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    h.capacity *= h.extendRatio           // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0435\u043c\u043a\u043e\u0441\u0442\u044c\n    h.buckets = make([]*pair, h.capacity) // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    h.size = 0                            // \u0421\u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for _, pair := range oldBuckets {\n        if pair != nil && pair != h.TOMBSTONE {\n            h.put(pair.key, pair.val)\n        }\n    }\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nfunc (h *hashMapOpenAddressing) print() {\n    for _, pair := range h.buckets {\n        if pair == nil {\n            fmt.Println(\"nil\")\n        } else if pair == h.TOMBSTONE {\n            fmt.Println(\"TOMBSTONE\")\n        } else {\n            fmt.Printf(\"%d -> %s\\n\", pair.key, pair.val)\n        }\n    }\n}\n
hash_map_open_addressing.swift
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    var size: Int // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    var capacity: Int // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    var loadThres: Double // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    var extendRatio: Int // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    var buckets: [Pair?] // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    var TOMBSTONE: Pair // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init() {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = Array(repeating: nil, count: capacity)\n        TOMBSTONE = Pair(key: -1, val: \"-1\")\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    func hashFunc(key: Int) -> Int {\n        key % capacity\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    func loadFactor() -> Double {\n        Double(size) / Double(capacity)\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    func findBucket(key: Int) -> Int {\n        var index = hashFunc(key: key)\n        var firstTombstone = -1\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while buckets[index] != nil {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if buckets[index]!.key == key {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if firstTombstone != -1 {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if firstTombstone == -1 && buckets[index] == TOMBSTONE {\n                firstTombstone = index\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % capacity\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone == -1 ? index : firstTombstone\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    func get(key: Int) -> String? {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = findBucket(key: key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            return buckets[index]!.val\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return nil\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    func put(key: Int, val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if loadFactor() > loadThres {\n            extend()\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = findBucket(key: key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index]!.val = val\n            return\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        buckets[index] = Pair(key: key, val: val)\n        size += 1\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    func remove(key: Int) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = findBucket(key: key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if buckets[index] != nil, buckets[index] != TOMBSTONE {\n            buckets[index] = TOMBSTONE\n            size -= 1\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    func extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        let bucketsTmp = buckets\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio\n        buckets = Array(repeating: nil, count: capacity)\n        size = 0\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for pair in bucketsTmp {\n            if let pair, pair != TOMBSTONE {\n                put(key: pair.key, val: pair.val)\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    func print() {\n        for pair in buckets {\n            if pair == nil {\n                Swift.print(\"null\")\n            } else if pair == TOMBSTONE {\n                Swift.print(\"TOMBSTONE\")\n            } else {\n                Swift.print(\"\\(pair!.key) -> \\(pair!.val)\")\n            }\n        }\n    }\n}\n
hash_map_open_addressing.js
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    #size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    #capacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    #loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #extendRatio; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    #buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    #TOMBSTONE; // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.#size = 0; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        this.#capacity = 4; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n        this.#loadThres = 2.0 / 3.0; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.#extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.#buckets = Array(this.#capacity).fill(null); // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n        this.#TOMBSTONE = new Pair(-1, '-1'); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    #hashFunc(key) {\n        return key % this.#capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    #loadFactor() {\n        return this.#size / this.#capacity;\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    #findBucket(key) {\n        let index = this.#hashFunc(key);\n        let firstTombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (this.#buckets[index] !== null) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (this.#buckets[index].key === key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone !== -1) {\n                    this.#buckets[firstTombstone] = this.#buckets[index];\n                    this.#buckets[index] = this.#TOMBSTONE;\n                    return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (\n                firstTombstone === -1 &&\n                this.#buckets[index] === this.#TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % this.#capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    get(key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.#findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            return this.#buckets[index].val;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    put(key, val) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.#loadFactor() > this.#loadThres) {\n            this.#extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.#findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index].val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        this.#buckets[index] = new Pair(key, val);\n        this.#size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    remove(key) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.#findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (\n            this.#buckets[index] !== null &&\n            this.#buckets[index] !== this.#TOMBSTONE\n        ) {\n            this.#buckets[index] = this.#TOMBSTONE;\n            this.#size--;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    #extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        const bucketsTmp = this.#buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.#capacity *= this.#extendRatio;\n        this.#buckets = Array(this.#capacity).fill(null);\n        this.#size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.#TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    print() {\n        for (const pair of this.#buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.#TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
hash_map_open_addressing.ts
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    private size: number; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    private capacity: number; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    private loadThres: number; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private extendRatio: number; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private buckets: Array<Pair | null>; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    private TOMBSTONE: Pair; // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor() {\n        this.size = 0; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        this.capacity = 4; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n        this.loadThres = 2.0 / 3.0; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.buckets = Array(this.capacity).fill(null); // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n        this.TOMBSTONE = new Pair(-1, '-1'); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    private hashFunc(key: number): number {\n        return key % this.capacity;\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    private loadFactor(): number {\n        return this.size / this.capacity;\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    private findBucket(key: number): number {\n        let index = this.hashFunc(key);\n        let firstTombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (this.buckets[index] !== null) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (this.buckets[index]!.key === key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone !== -1) {\n                    this.buckets[firstTombstone] = this.buckets[index];\n                    this.buckets[index] = this.TOMBSTONE;\n                    return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (\n                firstTombstone === -1 &&\n                this.buckets[index] === this.TOMBSTONE\n            ) {\n                firstTombstone = index;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % this.capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return firstTombstone === -1 ? index : firstTombstone;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    get(key: number): string | null {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            return this.buckets[index]!.val;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    put(key: number, val: string): void {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (this.loadFactor() > this.loadThres) {\n            this.extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index]!.val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        this.buckets[index] = new Pair(key, val);\n        this.size++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    remove(key: number): void {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        const index = this.findBucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (\n            this.buckets[index] !== null &&\n            this.buckets[index] !== this.TOMBSTONE\n        ) {\n            this.buckets[index] = this.TOMBSTONE;\n            this.size--;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    private extend(): void {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        const bucketsTmp = this.buckets;\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        this.capacity *= this.extendRatio;\n        this.buckets = Array(this.capacity).fill(null);\n        this.size = 0;\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (const pair of bucketsTmp) {\n            if (pair !== null && pair !== this.TOMBSTONE) {\n                this.put(pair.key, pair.val);\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    print(): void {\n        for (const pair of this.buckets) {\n            if (pair === null) {\n                console.log('null');\n            } else if (pair === this.TOMBSTONE) {\n                console.log('TOMBSTONE');\n            } else {\n                console.log(pair.key + ' -> ' + pair.val);\n            }\n        }\n    }\n}\n
hash_map_open_addressing.dart
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n  late int _size; // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n  int _capacity = 4; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n  double _loadThres = 2.0 / 3.0; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  int _extendRatio = 2; // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n  late List<Pair?> _buckets; // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n  Pair _TOMBSTONE = Pair(-1, \"-1\"); // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  HashMapOpenAddressing() {\n    _size = 0;\n    _buckets = List.generate(_capacity, (index) => null);\n  }\n\n  /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n  int hashFunc(int key) {\n    return key % _capacity;\n  }\n\n  /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n  double loadFactor() {\n    return _size / _capacity;\n  }\n\n  /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n  int findBucket(int key) {\n    int index = hashFunc(key);\n    int firstTombstone = -1;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n    while (_buckets[index] != null) {\n      // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n      if (_buckets[index]!.key == key) {\n        // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n        if (firstTombstone != -1) {\n          _buckets[firstTombstone] = _buckets[index];\n          _buckets[index] = _TOMBSTONE;\n          return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n        }\n        return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n      }\n      // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n      if (firstTombstone == -1 && _buckets[index] == _TOMBSTONE) {\n        firstTombstone = index;\n      }\n      // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n      index = (index + 1) % _capacity;\n    }\n    // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n    return firstTombstone == -1 ? index : firstTombstone;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n  String? get(int key) {\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      return _buckets[index]!.val;\n    }\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n    return null;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n  void put(int key, String val) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (loadFactor() > _loadThres) {\n      extend();\n    }\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index]!.val = val;\n      return;\n    }\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n    _buckets[index] = new Pair(key, val);\n    _size++;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n  void remove(int key) {\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n    if (_buckets[index] != null && _buckets[index] != _TOMBSTONE) {\n      _buckets[index] = _TOMBSTONE;\n      _size--;\n    }\n  }\n\n  /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n  void extend() {\n    // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    List<Pair?> bucketsTmp = _buckets;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    _capacity *= _extendRatio;\n    _buckets = List.generate(_capacity, (index) => null);\n    _size = 0;\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for (Pair? pair in bucketsTmp) {\n      if (pair != null && pair != _TOMBSTONE) {\n        put(pair.key, pair.val);\n      }\n    }\n  }\n\n  /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n  void printHashMap() {\n    for (Pair? pair in _buckets) {\n      if (pair == null) {\n        print(\"null\");\n      } else if (pair == _TOMBSTONE) {\n        print(\"TOMBSTONE\");\n      } else {\n        print(\"${pair.key} -> ${pair.val}\");\n      }\n    }\n  }\n}\n
hash_map_open_addressing.rs
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nstruct HashMapOpenAddressing {\n    size: usize,                // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    capacity: usize,            // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    load_thres: f64,            // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    extend_ratio: usize,        // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    buckets: Vec<Option<Pair>>, // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    TOMBSTONE: Option<Pair>,    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n}\n\nimpl HashMapOpenAddressing {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new() -> Self {\n        Self {\n            size: 0,\n            capacity: 4,\n            load_thres: 2.0 / 3.0,\n            extend_ratio: 2,\n            buckets: vec![None; 4],\n            TOMBSTONE: Some(Pair {\n                key: -1,\n                val: \"-1\".to_string(),\n            }),\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fn hash_func(&self, key: i32) -> usize {\n        (key % self.capacity as i32) as usize\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    fn load_factor(&self) -> f64 {\n        self.size as f64 / self.capacity as f64\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    fn find_bucket(&mut self, key: i32) -> usize {\n        let mut index = self.hash_func(key);\n        let mut first_tombstone = -1;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while self.buckets[index].is_some() {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if self.buckets[index].as_ref().unwrap().key == key {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if first_tombstone != -1 {\n                    self.buckets[first_tombstone as usize] = self.buckets[index].take();\n                    self.buckets[index] = self.TOMBSTONE.clone();\n                    return first_tombstone as usize; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if first_tombstone == -1 && self.buckets[index] == self.TOMBSTONE {\n                first_tombstone = index as i32;\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % self.capacity;\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        if first_tombstone == -1 {\n            index\n        } else {\n            first_tombstone as usize\n        }\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    fn get(&mut self, key: i32) -> Option<&str> {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = self.find_bucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            return self.buckets[index].as_ref().map(|pair| &pair.val as &str);\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        None\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    fn put(&mut self, key: i32, val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if self.load_factor() > self.load_thres {\n            self.extend();\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = self.find_bucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index].as_mut().unwrap().val = val;\n            return;\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        self.buckets[index] = Some(Pair { key, val });\n        self.size += 1;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    fn remove(&mut self, key: i32) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        let index = self.find_bucket(key);\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if self.buckets[index].is_some() && self.buckets[index] != self.TOMBSTONE {\n            self.buckets[index] = self.TOMBSTONE.clone();\n            self.size -= 1;\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fn extend(&mut self) {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        let buckets_tmp = self.buckets.clone();\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        self.capacity *= self.extend_ratio;\n        self.buckets = vec![None; self.capacity];\n        self.size = 0;\n\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for pair in buckets_tmp {\n            if pair.is_none() || pair == self.TOMBSTONE {\n                continue;\n            }\n            let pair = pair.unwrap();\n\n            self.put(pair.key, pair.val);\n        }\n    }\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fn print(&self) {\n        for pair in &self.buckets {\n            if pair.is_none() {\n                println!(\"null\");\n            } else if pair == &self.TOMBSTONE {\n                println!(\"TOMBSTONE\");\n            } else {\n                let pair = pair.as_ref().unwrap();\n                println!(\"{} -> {}\", pair.key, pair.val);\n            }\n        }\n    }\n}\n
hash_map_open_addressing.c
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\ntypedef struct {\n    int size;         // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    int capacity;     // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    double loadThres; // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    int extendRatio;  // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    Pair **buckets;   // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    Pair *TOMBSTONE;  // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n} HashMapOpenAddressing;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nHashMapOpenAddressing *newHashMapOpenAddressing() {\n    HashMapOpenAddressing *hashMap = (HashMapOpenAddressing *)malloc(sizeof(HashMapOpenAddressing));\n    hashMap->size = 0;\n    hashMap->capacity = 4;\n    hashMap->loadThres = 2.0 / 3.0;\n    hashMap->extendRatio = 2;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->TOMBSTONE = (Pair *)malloc(sizeof(Pair));\n    hashMap->TOMBSTONE->key = -1;\n    hashMap->TOMBSTONE->val = \"-1\";\n\n    return hashMap;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delHashMapOpenAddressing(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(hashMap->buckets);\n    free(hashMap->TOMBSTONE);\n    free(hashMap);\n}\n\n/* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\nint hashFunc(HashMapOpenAddressing *hashMap, int key) {\n    return key % hashMap->capacity;\n}\n\n/* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\ndouble loadFactor(HashMapOpenAddressing *hashMap) {\n    return (double)hashMap->size / (double)hashMap->capacity;\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\nint findBucket(HashMapOpenAddressing *hashMap, int key) {\n    int index = hashFunc(hashMap, key);\n    int firstTombstone = -1;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n    while (hashMap->buckets[index] != NULL) {\n        // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n        if (hashMap->buckets[index]->key == key) {\n            // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n            if (firstTombstone != -1) {\n                hashMap->buckets[firstTombstone] = hashMap->buckets[index];\n                hashMap->buckets[index] = hashMap->TOMBSTONE;\n                return firstTombstone; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n            }\n            return index; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n        }\n        // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (firstTombstone == -1 && hashMap->buckets[index] == hashMap->TOMBSTONE) {\n            firstTombstone = index;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n        index = (index + 1) % hashMap->capacity;\n    }\n    // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n    return firstTombstone == -1 ? index : firstTombstone;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\nchar *get(HashMapOpenAddressing *hashMap, int key) {\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(hashMap, key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        return hashMap->buckets[index]->val;\n    }\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u0443\u0441\u0442\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443\n    return \"\";\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nvoid put(HashMapOpenAddressing *hashMap, int key, char *val) {\n    // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    if (loadFactor(hashMap) > hashMap->loadThres) {\n        extend(hashMap);\n    }\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(hashMap, key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        free(hashMap->buckets[index]->val);\n        hashMap->buckets[index]->val = (char *)malloc(sizeof(strlen(val) + 1));\n        strcpy(hashMap->buckets[index]->val, val);\n        hashMap->buckets[index]->val[strlen(val)] = '\\0';\n        return;\n    }\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n    Pair *pair = (Pair *)malloc(sizeof(Pair));\n    pair->key = key;\n    pair->val = (char *)malloc(sizeof(strlen(val) + 1));\n    strcpy(pair->val, val);\n    pair->val[strlen(val)] = '\\0';\n\n    hashMap->buckets[index] = pair;\n    hashMap->size++;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nvoid removeItem(HashMapOpenAddressing *hashMap, int key) {\n    // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    int index = findBucket(hashMap, key);\n    // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n    if (hashMap->buckets[index] != NULL && hashMap->buckets[index] != hashMap->TOMBSTONE) {\n        Pair *pair = hashMap->buckets[index];\n        free(pair->val);\n        free(pair);\n        hashMap->buckets[index] = hashMap->TOMBSTONE;\n        hashMap->size--;\n    }\n}\n\n/* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid extend(HashMapOpenAddressing *hashMap) {\n    // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    Pair **bucketsTmp = hashMap->buckets;\n    int oldCapacity = hashMap->capacity;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    hashMap->capacity *= hashMap->extendRatio;\n    hashMap->buckets = (Pair **)calloc(hashMap->capacity, sizeof(Pair *));\n    hashMap->size = 0;\n    // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for (int i = 0; i < oldCapacity; i++) {\n        Pair *pair = bucketsTmp[i];\n        if (pair != NULL && pair != hashMap->TOMBSTONE) {\n            put(hashMap, pair->key, pair->val);\n            free(pair->val);\n            free(pair);\n        }\n    }\n    free(bucketsTmp);\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid print(HashMapOpenAddressing *hashMap) {\n    for (int i = 0; i < hashMap->capacity; i++) {\n        Pair *pair = hashMap->buckets[i];\n        if (pair == NULL) {\n            printf(\"NULL\\n\");\n        } else if (pair == hashMap->TOMBSTONE) {\n            printf(\"TOMBSTONE\\n\");\n        } else {\n            printf(\"%d -> %s\\n\", pair->key, pair->val);\n        }\n    }\n}\n
hash_map_open_addressing.kt
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 */\nclass HashMapOpenAddressing {\n    private var size: Int               // \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    private var capacity: Int           // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    private val loadThres: Double       // \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private val extendRatio: Int        // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private var buckets: Array<Pair?>   // \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n    private val TOMBSTONE: Pair         // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init {\n        size = 0\n        capacity = 4\n        loadThres = 2.0 / 3.0\n        extendRatio = 2\n        buckets = arrayOfNulls(capacity)\n        TOMBSTONE = Pair(-1, \"-1\")\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fun hashFunc(key: Int): Int {\n        return key % capacity\n    }\n\n    /* \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 */\n    fun loadFactor(): Double {\n        return (size / capacity).toDouble()\n    }\n\n    /* \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key */\n    fun findBucket(key: Int): Int {\n        var index = hashFunc(key)\n        var firstTombstone = -1\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n        while (buckets[index] != null) {\n            // \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            if (buckets[index]?.key == key) {\n                // \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n                if (firstTombstone != -1) {\n                    buckets[firstTombstone] = buckets[index]\n                    buckets[index] = TOMBSTONE\n                    return firstTombstone // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n                }\n                return index // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n            }\n            // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n            if (firstTombstone == -1 && buckets[index] == TOMBSTONE) {\n                firstTombstone = index\n            }\n            // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n            index = (index + 1) % capacity\n        }\n        // \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n        return if (firstTombstone == -1) index else firstTombstone\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    fun get(key: Int): String? {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        val index = findBucket(key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            return buckets[index]?._val\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n        return null\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    fun put(key: Int, _val: String) {\n        // \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n        if (loadFactor() > loadThres) {\n            extend()\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        val index = findBucket(key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index]!!._val = _val\n            return\n        }\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n        buckets[index] = Pair(key, _val)\n        size++\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    fun remove(key: Int) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n        val index = findBucket(key)\n        // \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n        if (buckets[index] != null && buckets[index] != TOMBSTONE) {\n            buckets[index] = TOMBSTONE\n            size--\n        }\n    }\n\n    /* \u0420\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fun extend() {\n        // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n        val bucketsTmp = buckets\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n        capacity *= extendRatio\n        buckets = arrayOfNulls(capacity)\n        size = 0\n        // \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n        for (pair in bucketsTmp) {\n            if (pair != null && pair != TOMBSTONE) {\n                put(pair.key, pair._val)\n            }\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fun print() {\n        for (pair in buckets) {\n            if (pair == null) {\n                println(\"null\")\n            } else if (pair == TOMBSTONE) {\n                println(\"TOMESTOME\")\n            } else {\n                println(\"${pair.key} -> ${pair._val}\")\n            }\n        }\n    }\n}\n
hash_map_open_addressing.rb
=begin\nFile: hash_map_open_addressing.rb\nCreated Time: 2024-04-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative './array_hash_map'\n\n# ## \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 ###\nclass HashMapOpenAddressing\n  TOMBSTONE = Pair.new(-1, '-1') # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043c\u0435\u0442\u043a\u0443\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @size = 0 # \u0427\u0438\u0441\u043b\u043e \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n    @capacity = 4 # \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n    @load_thres = 2.0 / 3.0 # \u041f\u043e\u0440\u043e\u0433 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0434\u043b\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @extend_ratio = 2 # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @buckets = Array.new(@capacity) # \u041c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d\n  end\n\n  # ## \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f ###\n  def hash_func(key)\n    key % @capacity\n  end\n\n  # ## \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 ###\n  def load_factor\n    @size / @capacity\n  end\n\n  # ## \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key ###\n  def find_bucket(key)\n    index = hash_func(key)\n    first_tombstone = -1\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u0441\u0442\u0440\u0435\u0447\u0435 \u0441 \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u043e\u0439\n    while !@buckets[index].nil?\n      # \u0415\u0441\u043b\u0438 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0441\u044f key, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n      if @buckets[index].key == key\n        # \u0415\u0441\u043b\u0438 \u0440\u0430\u043d\u0435\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043b\u0430\u0441\u044c \u043c\u0435\u0442\u043a\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0438\u043d\u0434\u0435\u043a\u0441\n        if first_tombstone != -1\n          @buckets[first_tombstone] = @buckets[index]\n          @buckets[index] = TOMBSTONE\n          return first_tombstone # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u044f\n        end\n        return index # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n      end\n      # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0432\u0448\u0443\u044e\u0441\u044f \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n      first_tombstone = index if first_tombstone == -1 && @buckets[index] == TOMBSTONE\n      # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b; \u043f\u0440\u0438 \u0432\u044b\u0445\u043e\u0434\u0435 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n      index = (index + 1) % @capacity\n    end\n    # \u0415\u0441\u043b\u0438 key \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u0447\u043a\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n    first_tombstone == -1 ? index : first_tombstone\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 ###\n  def get(key)\n    # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    index = find_bucket(key)\n    # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 val\n    return @buckets[index].val unless [nil, TOMBSTONE].include?(@buckets[index])\n    # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0432\u0435\u0440\u043d\u0443\u0442\u044c nil\n    nil\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f ###\n  def put(key, val)\n    # \u041a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u0440\u043e\u0433, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\n    extend if load_factor > @load_thres\n    # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    index = find_bucket(key)\n    # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c val \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index].val = val\n      return\n    end\n    # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0435\u0442, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0435\n    @buckets[index] = Pair.new(key, val)\n    @size += 1\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f ###\n  def remove(key)\n    # \u041d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u043e\u0440\u0437\u0438\u043d\u044b, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key\n    index = find_bucket(key)\n    # \u0415\u0441\u043b\u0438 \u043f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0435\u0435 \u043c\u0435\u0442\u043a\u043e\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n    unless [nil, TOMBSTONE].include?(@buckets[index])\n      @buckets[index] = TOMBSTONE\n      @size -= 1\n    end\n  end\n\n  # ## \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b ###\n  def extend\n    # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    buckets_tmp = @buckets\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u043e\u0432\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    @capacity *= @extend_ratio\n    @buckets = Array.new(@capacity)\n    @size = 0\n    # \u041f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e\n    for pair in buckets_tmp\n      put(pair.key, pair.val) unless [nil, TOMBSTONE].include?(pair)\n    end\n  end\n\n  # ## \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 ###\n  def print\n    for pair in @buckets\n      if pair.nil?\n        puts \"Nil\"\n      elsif pair == TOMBSTONE\n        puts \"TOMBSTONE\"\n      else\n        puts \"#{pair.key} -> #{pair.val}\"\n      end\n    end\n  end\nend\n
"},{"location":"chapter_hashing/hash_collision/#2","title":"2. \u00a0 \u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0442\u043e\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0439 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438. \u041f\u0440\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0438 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0430 \u043e\u043d\u043e \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432, \u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0440\u0430\u0432\u043d\u043e\u0435 \"\u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0443 \u0447\u0438\u0441\u043b\u0430 \u043f\u043e\u043f\u044b\u0442\u043e\u043a\", \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430 \\(1, 4, 9, \\dots\\) \u0448\u0430\u0433\u043e\u0432.

\u041a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430.

\u041e\u0434\u043d\u0430\u043a\u043e \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u043c.

"},{"location":"chapter_hashing/hash_collision/#3","title":"3. \u00a0 \u041f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435","text":"

\u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 \\(f_1(x)\\), \\(f_2(x)\\), \\(f_3(x)\\), \\(\\dots\\) .

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u043c \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u043d\u044c\u0448\u0435 \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d \u043a\u043b\u0430\u0441\u0442\u0435\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u043f\u0440\u0438\u043d\u043e\u0441\u044f\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0442\u0440\u0430\u0442\u044b.

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0443 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446 \u0441 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0435\u0439 (\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u0438\u0447\u043d\u043e\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435) \u0435\u0441\u0442\u044c \u043e\u0431\u0449\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430: \u0432 \u043d\u0438\u0445 \u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b.

"},{"location":"chapter_hashing/hash_collision/#623","title":"6.2.3 \u00a0 \u0412\u044b\u0431\u043e\u0440 \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f","text":"

\u0420\u0430\u0437\u043d\u044b\u0435 \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432.

"},{"location":"chapter_hashing/hash_map/","title":"6.1 \u00a0 \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430","text":"

\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 (hash table), \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u0440\u0430\u0441\u0441\u0435\u044f\u043d\u0438\u044f, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043a\u043b\u044e\u0447\u043e\u043c key \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c value . \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key , \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430 \\(O(1)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value .

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-1, \u043f\u0443\u0441\u0442\u044c \u0435\u0441\u0442\u044c \\(n\\) \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432, \u0438 \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u043f\u043e\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445: \"\u0438\u043c\u044f\" \u0438 \"\u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0431\u0438\u043b\u0435\u0442\u0430\". \u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u0438\u0434\u0430 \"\u0432\u0432\u0435\u0441\u0442\u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0431\u0438\u043b\u0435\u0442\u0430 \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0435 \u0438\u043c\u044f\", \u0442\u043e \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u043d\u0438\u0436\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-1 \u00a0 \u0410\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b

\u041f\u043e\u043c\u0438\u043c\u043e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0441\u0441\u0438\u0432, \u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0438\u0445 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 6-1.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 6-1 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432

\u041c\u0430\u0441\u0441\u0438\u0432 \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u041f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\) \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(1)\\) \\(O(1)\\) \\(O(1)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(n)\\) \\(O(1)\\)

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0447\u0442\u0435\u043d\u0438\u044f, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438\u043c\u0435\u044e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

"},{"location":"chapter_hashing/hash_map/#611-","title":"6.1.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439","text":"

\u041a \u0431\u0430\u0437\u043e\u0432\u044b\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u043c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0438\u0441\u043a, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nhmap: dict = {}\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n# \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nhmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\"\nhmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\"\nhmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\"\nhmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\"\nhmap[10583] = \"\u0421\u044f\u043e \u042f\"\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430\n# \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nname: str = hmap[15937]\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nhmap.pop(10583)\n
hash_map.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nunordered_map<int, string> map;\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\";\nmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\";\nmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\";\nmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\";\nmap[10583] = \"\u0421\u044f\u043e \u042f\";\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nstring name = map[15937];\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.erase(10583);\n
hash_map.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nMap<Integer, String> map = new HashMap<>();\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap.put(12836, \"\u0421\u044f\u043e \u0425\u0430\");\nmap.put(15937, \"\u0421\u044f\u043e \u041b\u043e\");\nmap.put(16750, \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\");\nmap.put(13276, \"\u0421\u044f\u043e \u0424\u0430\");\nmap.put(10583, \"\u0421\u044f\u043e \u042f\");\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nString name = map.get(15937);\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.remove(10583);\n
hash_map.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nDictionary<int, string> map = new() {\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\n    { 12836, \"\u0421\u044f\u043e \u0425\u0430\" },\n    { 15937, \"\u0421\u044f\u043e \u041b\u043e\" },\n    { 16750, \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\" },\n    { 13276, \"\u0421\u044f\u043e \u0424\u0430\" },\n    { 10583, \"\u0421\u044f\u043e \u042f\" }\n};\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nstring name = map[15937];\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.Remove(10583);\n
hash_map_test.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nhmap := make(map[int]string)\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nhmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\"\nhmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\"\nhmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\"\nhmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\"\nhmap[10583] = \"\u0421\u044f\u043e \u042f\"\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nname := hmap[15937]\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\ndelete(hmap, 10583)\n
hash_map.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nvar map: [Int: String] = [:]\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\"\nmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\"\nmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\"\nmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\"\nmap[10583] = \"\u0421\u044f\u043e \u042f\"\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nlet name = map[15937]!\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.removeValue(forKey: 10583)\n
hash_map.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nconst map = new Map();\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap.set(12836, '\u0421\u044f\u043e \u0425\u0430');\nmap.set(15937, '\u0421\u044f\u043e \u041b\u043e');\nmap.set(16750, '\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c');\nmap.set(13276, '\u0421\u044f\u043e \u0424\u0430');\nmap.set(10583, '\u0421\u044f\u043e \u042f');\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nlet name = map.get(15937);\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.delete(10583);\n
hash_map.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nconst map = new Map<number, string>();\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap.set(12836, '\u0421\u044f\u043e \u0425\u0430');\nmap.set(15937, '\u0421\u044f\u043e \u041b\u043e');\nmap.set(16750, '\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c');\nmap.set(13276, '\u0421\u044f\u043e \u0424\u0430');\nmap.set(10583, '\u0421\u044f\u043e \u042f');\nconsole.info('\\n\u041f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434\\nKey -> Value');\nconsole.info(map);\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nlet name = map.get(15937);\nconsole.info('\\n\u041f\u043e \u043d\u043e\u043c\u0435\u0440\u0443 15937 \u043d\u0430\u0439\u0434\u0435\u043d\u043e \u0438\u043c\u044f ' + name);\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.delete(10583);\nconsole.info('\\n\u041f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f 10583 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434\\nKey -> Value');\nconsole.info(map);\n
hash_map.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nMap<int, String> map = {};\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\";\nmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\";\nmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\";\nmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\";\nmap[10583] = \"\u0421\u044f\u043e \u042f\";\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nString name = map[15937];\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.remove(10583);\n
hash_map.rs
use std::collections::HashMap;\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nlet mut map: HashMap<i32, String> = HashMap::new();\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap.insert(12836, \"\u0421\u044f\u043e \u0425\u0430\".to_string());\nmap.insert(15937, \"\u0421\u044f\u043e \u041b\u043e\".to_string());\nmap.insert(16750, \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\".to_string());\nmap.insert(13279, \"\u0421\u044f\u043e \u0424\u0430\".to_string());\nmap.insert(10583, \"\u0421\u044f\u043e \u042f\".to_string());\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nlet _name: Option<&String> = map.get(&15937);\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nlet _removed_value: Option<String> = map.remove(&10583);\n
hash_map.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n
hash_map.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nval map = HashMap<Int,String>()\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n// \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\"\nmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\"\nmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\"\nmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\"\nmap[10583] = \"\u0421\u044f\u043e \u042f\"\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n// \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nval name = map[15937]\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nmap.remove(10583)\n
hash_map.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nhmap = {}\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\n# \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\nhmap[12836] = \"\u0421\u044f\u043e \u0425\u0430\"\nhmap[15937] = \"\u0421\u044f\u043e \u041b\u043e\"\nhmap[16750] = \"\u0421\u044f\u043e \u0421\u0443\u0430\u043d\u044c\"\nhmap[13276] = \"\u0421\u044f\u043e \u0424\u0430\"\nhmap[10583] = \"\u0421\u044f\u043e \u042f\"\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430\n# \u041f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043a\u043b\u044e\u0447 key \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 value\nname = hmap[15937]\n\n# \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (key, value) \u0438\u0437 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\nhmap.delete(10583)\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D0%B4%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F%0A%20%20%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D0%B2%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%20%D0%BF%D0%B0%D1%80%D1%83%20%D0%BA%D0%BB%D1%8E%D1%87-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%28key%2C%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A5%D0%B0%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%9B%D0%BE%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A1%D1%83%D0%B0%D0%BD%D1%8C%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A4%D0%B0%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%D0%A3%D1%82%D0%B5%D0%BD%D0%BE%D0%BA%22%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D0%BF%D0%BE%D0%B8%D1%81%D0%BA%D0%B0%0A%20%20%20%20%23%20%D0%9F%D0%B5%D1%80%D0%B5%D0%B4%D0%B0%D1%82%D1%8C%20%D0%BA%D0%BB%D1%8E%D1%87%20key%20%D0%B2%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%20%D0%B8%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20value%0A%20%20%20%20name%20%3D%20hmap%5B15937%5D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D1%83%D0%B4%D0%B0%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F%0A%20%20%20%20%23%20%D0%A3%D0%B4%D0%B0%D0%BB%D0%B8%D1%82%D1%8C%20%D0%B8%D0%B7%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%8B%20%D0%BF%D0%B0%D1%80%D1%83%20%D0%BA%D0%BB%D1%8E%D1%87-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%28key%2C%20value%29%0A%20%20%20%20hmap.pop%2810583%29&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

\u0423 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043e\u0431\u0445\u043e\u0434\u0430: \u043e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043e\u0431\u0445\u043e\u0434 \u043a\u043b\u044e\u0447\u0435\u0439 \u0438 \u043e\u0431\u0445\u043e\u0434 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439. \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby hash_map.py
# \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n# \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nfor key, value in hmap.items():\n    print(key, \"->\", value)\n# \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nfor key in hmap.keys():\n    print(key)\n# \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nfor value in hmap.values():\n    print(value)\n
hash_map.cpp
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nfor (auto kv: map) {\n    cout << kv.first << \" -> \" << kv.second << endl;\n}\n// \u041e\u0431\u0445\u043e\u0434 key->value \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0442\u0435\u0440\u0430\u0442\u043e\u0440\u0430\nfor (auto iter = map.begin(); iter != map.end(); iter++) {\n    cout << iter->first << \"->\" << iter->second << endl;\n}\n
hash_map.java
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nfor (Map.Entry <Integer, String> kv: map.entrySet()) {\n    System.out.println(kv.getKey() + \" -> \" + kv.getValue());\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nfor (int key: map.keySet()) {\n    System.out.println(key);\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nfor (String val: map.values()) {\n    System.out.println(val);\n}\n
hash_map.cs
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value\nforeach (var kv in map) {\n    Console.WriteLine(kv.Key + \" -> \" + kv.Value);\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nforeach (int key in map.Keys) {\n    Console.WriteLine(key);\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nforeach (string val in map.Values) {\n    Console.WriteLine(val);\n}\n
hash_map_test.go
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nfor key, value := range hmap {\n    fmt.Println(key, \"->\", value)\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nfor key := range hmap {\n    fmt.Println(key)\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nfor _, value := range hmap {\n    fmt.Println(value)\n}\n
hash_map.swift
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value\nfor (key, value) in map {\n    print(\"\\(key) -> \\(value)\")\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 Key\nfor key in map.keys {\n    print(key)\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Value\nfor value in map.values {\n    print(value)\n}\n
hash_map.js
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
hash_map.ts
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value');\nfor (const [k, v] of map.entries()) {\n    console.info(k + ' -> ' + v);\n}\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 Key');\nfor (const k of map.keys()) {\n    console.info(k);\n}\nconsole.info('\\n\u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Value');\nfor (const v of map.values()) {\n    console.info(v);\n}\n
hash_map.dart
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value\nmap.forEach((key, value) {\n  print('$key -> $value');\n});\n\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 Key\nmap.keys.forEach((key) {\n  print(key);\n});\n\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Value\nmap.values.forEach((value) {\n  print(value);\n});\n
hash_map.rs
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Key->Value\nfor (key, value) in &map {\n    println!(\"{key} -> {value}\");\n}\n\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 Key\nfor key in map.keys() {\n    println!(\"{key}\");\n}\n\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 Value\nfor value in map.values() {\n    println!(\"{value}\");\n}\n
hash_map.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n
hash_map.kt
/* \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\n// \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nfor ((key, value) in map) {\n    println(\"$key -> $value\")\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nfor (key in map.keys) {\n    println(key)\n}\n// \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nfor (_val in map.values) {\n    println(_val)\n}\n
hash_map.rb
# \u041e\u0431\u0445\u043e\u0434 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b\n# \u041e\u0431\u0445\u043e\u0434 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 key->value\nhmap.entries.each { |key, value| puts \"#{key} -> #{value}\" }\n\n# \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043b\u044e\u0447\u0435\u0439 key\nhmap.keys.each { |key| puts key }\n\n# \u041e\u0431\u0445\u043e\u0434 \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 value\nhmap.values.each { |val| puts val }\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%0A%20%20%20%20hmap%20%3D%20%7B%7D%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9E%D0%BF%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D1%8F%20%D0%B4%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F%0A%20%20%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D0%B2%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%20%D0%BF%D0%B0%D1%80%D1%83%20%D0%BA%D0%BB%D1%8E%D1%87-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%28key%2C%20value%29%0A%20%20%20%20hmap%5B12836%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A5%D0%B0%22%0A%20%20%20%20hmap%5B15937%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%9B%D0%BE%22%0A%20%20%20%20hmap%5B16750%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A1%D1%83%D0%B0%D0%BD%D1%8C%22%0A%20%20%20%20hmap%5B13276%5D%20%3D%20%22%D0%A1%D1%8F%D0%BE%20%D0%A4%D0%B0%22%0A%20%20%20%20hmap%5B10583%5D%20%3D%20%22%D0%A3%D1%82%D0%B5%D0%BD%D0%BE%D0%BA%22%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9F%D0%B5%D1%80%D0%B5%D0%B1%D1%80%D0%B0%D1%82%D1%8C%20%D1%85%D0%B5%D1%88-%D1%82%D0%B0%D0%B1%D0%BB%D0%B8%D1%86%D1%83%0A%20%20%20%20%23%20%D0%9E%D0%B1%D0%BE%D0%B9%D1%82%D0%B8%D0%BF%D0%B0%D1%80%D0%B0%20%D0%BA%D0%BB%D1%8E%D1%87-%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20key-%3Evalue%0A%20%20%20%20for%20key%2C%20value%20in%20hmap.items%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key%2C%20%22-%3E%22%2C%20value%29%0A%20%20%20%20%23%20%D0%BE%D1%82%D0%B4%D0%B5%D0%BB%D1%8C%D0%BD%D0%BE%D0%9E%D0%B1%D0%BE%D0%B9%D1%82%D0%B8%D0%BA%D0%BB%D1%8E%D1%87%20key%0A%20%20%20%20for%20key%20in%20hmap.keys%28%29%3A%0A%20%20%20%20%20%20%20%20print%28key%29%0A%20%20%20%20%23%20%D0%BE%D1%82%D0%B4%D0%B5%D0%BB%D1%8C%D0%BD%D0%BE%D0%9E%D0%B1%D0%BE%D0%B9%D1%82%D0%B8%D0%B7%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20value%0A%20%20%20%20for%20value%20in%20hmap.values%28%29%3A%0A%20%20%20%20%20%20%20%20print%28value%29&cumulative=false&curInstr=8&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_hashing/hash_map/#612-","title":"6.1.2 \u00a0 \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b","text":"

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439: \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0412 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043a\u0430\u0436\u0434\u0443\u044e \u043f\u0443\u0441\u0442\u0443\u044e \u044f\u0447\u0435\u0439\u043a\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0431\u0430\u043a\u0435\u0442\u043e\u043c (bucket), \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u0430\u043a\u0435\u0442 \u043c\u043e\u0436\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043e\u0434\u043d\u0443 \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0431\u0430\u043a\u0435\u0442, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 key , \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e value .

\u041d\u043e \u043a\u0430\u043a \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0431\u0430\u043a\u0435\u0442, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 key ? \u042d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 (hash function). \u041d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 - \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0435 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0432 \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e. \u0412 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0432\u0445\u043e\u0434\u043d\u044b\u043c \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e\u043c \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0435 key , \u0430 \u0432\u044b\u0445\u043e\u0434\u043d\u044b\u043c - \u0432\u0441\u0435 \u0431\u0430\u043a\u0435\u0442\u044b (\u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430). \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043f\u0435\u0440\u0435\u0434\u0430\u0432 key \u043d\u0430 \u0432\u0445\u043e\u0434, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435.

\u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e key \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0434\u0432\u0430 \u0448\u0430\u0433\u0430.

  1. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 hash() \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.
  2. \u0417\u0430\u0442\u0435\u043c \u0445\u0435\u0448-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0447\u0438\u0441\u043b\u0430 \u0431\u0430\u043a\u0435\u0442\u043e\u0432 (\u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430) capacity , \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u0430\u043a\u0435\u0442 (\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430) index , \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u044d\u0442\u043e\u043c\u0443 key .
index = hash(key) % capacity\n

\u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c index \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0431\u0430\u043a\u0435\u0442\u0443 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f value .

\u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 capacity = 100 , \u0430 \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c hash(key) = key . \u0422\u043e\u0433\u0434\u0430 \u043b\u0435\u0433\u043a\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044e key % 100 . \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-2 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 key \"\u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0431\u0438\u043b\u0435\u0442\u0430\" \u0438 value \"\u0438\u043c\u044f\" \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-2 \u00a0 \u041f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043a\u043e\u0434 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u0412 \u043d\u0435\u043c \u043c\u044b \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u043c key \u0438 value \u0432 \u043a\u043b\u0430\u0441\u0441 Pair , \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_hash_map.py
class Pair:\n    \"\"\"\u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\"\"\"\n\n    def __init__(self, key: int, val: str):\n        self.key = key\n        self.val = val\n\nclass ArrayHashMap:\n    \"\"\"\u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        self.buckets: list[Pair | None] = [None] * 100\n\n    def hash_func(self, key: int) -> int:\n        \"\"\"\u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f\"\"\"\n        index = key % 100\n        return index\n\n    def get(self, key: int) -> str | None:\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430\"\"\"\n        index: int = self.hash_func(key)\n        pair: Pair = self.buckets[index]\n        if pair is None:\n            return None\n        return pair.val\n\n    def put(self, key: int, val: str):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f\"\"\"\n        pair = Pair(key, val)\n        index: int = self.hash_func(key)\n        self.buckets[index] = pair\n\n    def remove(self, key: int):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\"\"\"\n        index: int = self.hash_func(key)\n        # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c None, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        self.buckets[index] = None\n\n    def entry_set(self) -> list[Pair]:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\"\"\"\n        result: list[Pair] = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair)\n        return result\n\n    def key_set(self) -> list[int]:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.key)\n        return result\n\n    def value_set(self) -> list[str]:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\"\"\"\n        result = []\n        for pair in self.buckets:\n            if pair is not None:\n                result.append(pair.val)\n        return result\n\n    def print(self):\n        \"\"\"\u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\"\"\"\n        for pair in self.buckets:\n            if pair is not None:\n                print(pair.key, \"->\", pair.val)\n
array_hash_map.cpp
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nstruct Pair {\n  public:\n    int key;\n    string val;\n    Pair(int key, string val) {\n        this->key = key;\n        this->val = val;\n    }\n};\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n  private:\n    vector<Pair *> buckets;\n\n  public:\n    ArrayHashMap() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        buckets = vector<Pair *>(100);\n    }\n\n    ~ArrayHashMap() {\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        for (const auto &bucket : buckets) {\n            delete bucket;\n        }\n        buckets.clear();\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    string get(int key) {\n        int index = hashFunc(key);\n        Pair *pair = buckets[index];\n        if (pair == nullptr)\n            return \"\";\n        return pair->val;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    void put(int key, string val) {\n        Pair *pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    void remove(int key) {\n        int index = hashFunc(key);\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0438 \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c nullptr\n        delete buckets[index];\n        buckets[index] = nullptr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    vector<Pair *> pairSet() {\n        vector<Pair *> pairSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                pairSet.push_back(pair);\n            }\n        }\n        return pairSet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    vector<int> keySet() {\n        vector<int> keySet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                keySet.push_back(pair->key);\n            }\n        }\n        return keySet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    vector<string> valueSet() {\n        vector<string> valueSet;\n        for (Pair *pair : buckets) {\n            if (pair != nullptr) {\n                valueSet.push_back(pair->val);\n            }\n        }\n        return valueSet;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    void print() {\n        for (Pair *kv : pairSet()) {\n            cout << kv->key << \" -> \" << kv->val << endl;\n        }\n    }\n};\n
array_hash_map.java
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nclass Pair {\n    public int key;\n    public String val;\n\n    public Pair(int key, String val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    private List<Pair> buckets;\n\n    public ArrayHashMap() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        buckets = new ArrayList<>();\n        for (int i = 0; i < 100; i++) {\n            buckets.add(null);\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    private int hashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public String get(int key) {\n        int index = hashFunc(key);\n        Pair pair = buckets.get(index);\n        if (pair == null)\n            return null;\n        return pair.val;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public void put(int key, String val) {\n        Pair pair = new Pair(key, val);\n        int index = hashFunc(key);\n        buckets.set(index, pair);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public void remove(int key) {\n        int index = hashFunc(key);\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c null, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        buckets.set(index, null);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    public List<Pair> pairSet() {\n        List<Pair> pairSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                pairSet.add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    public List<Integer> keySet() {\n        List<Integer> keySet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                keySet.add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    public List<String> valueSet() {\n        List<String> valueSet = new ArrayList<>();\n        for (Pair pair : buckets) {\n            if (pair != null)\n                valueSet.add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public void print() {\n        for (Pair kv : pairSet()) {\n            System.out.println(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
array_hash_map.cs
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 int->string */\nclass Pair(int key, string val) {\n    public int key = key;\n    public string val = val;\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    List<Pair?> buckets;\n    public ArrayHashMap() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        buckets = [];\n        for (int i = 0; i < 100; i++) {\n            buckets.Add(null);\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    int HashFunc(int key) {\n        int index = key % 100;\n        return index;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public string? Get(int key) {\n        int index = HashFunc(key);\n        Pair? pair = buckets[index];\n        if (pair == null) return null;\n        return pair.val;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public void Put(int key, string val) {\n        Pair pair = new(key, val);\n        int index = HashFunc(key);\n        buckets[index] = pair;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public void Remove(int key) {\n        int index = HashFunc(key);\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c null, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        buckets[index] = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    public List<Pair> PairSet() {\n        List<Pair> pairSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                pairSet.Add(pair);\n        }\n        return pairSet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    public List<int> KeySet() {\n        List<int> keySet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                keySet.Add(pair.key);\n        }\n        return keySet;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    public List<string> ValueSet() {\n        List<string> valueSet = [];\n        foreach (Pair? pair in buckets) {\n            if (pair != null)\n                valueSet.Add(pair.val);\n        }\n        return valueSet;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public void Print() {\n        foreach (Pair kv in PairSet()) {\n            Console.WriteLine(kv.key + \" -> \" + kv.val);\n        }\n    }\n}\n
array_hash_map.go
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\ntype pair struct {\n    key int\n    val string\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntype arrayHashMap struct {\n    buckets []*pair\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b */\nfunc newArrayHashMap() *arrayHashMap {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n    buckets := make([]*pair, 100)\n    return &arrayHashMap{buckets: buckets}\n}\n\n/* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\nfunc (a *arrayHashMap) hashFunc(key int) int {\n    index := key % 100\n    return index\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\nfunc (a *arrayHashMap) get(key int) string {\n    index := a.hashFunc(key)\n    pair := a.buckets[index]\n    if pair == nil {\n        return \"Not Found\"\n    }\n    return pair.val\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nfunc (a *arrayHashMap) put(key int, val string) {\n    pair := &pair{key: key, val: val}\n    index := a.hashFunc(key)\n    a.buckets[index] = pair\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nfunc (a *arrayHashMap) remove(key int) {\n    index := a.hashFunc(key)\n    // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c nil, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n    a.buckets[index] = nil\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\nfunc (a *arrayHashMap) pairSet() []*pair {\n    var pairs []*pair\n    for _, pair := range a.buckets {\n        if pair != nil {\n            pairs = append(pairs, pair)\n        }\n    }\n    return pairs\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\nfunc (a *arrayHashMap) keySet() []int {\n    var keys []int\n    for _, pair := range a.buckets {\n        if pair != nil {\n            keys = append(keys, pair.key)\n        }\n    }\n    return keys\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\nfunc (a *arrayHashMap) valueSet() []string {\n    var values []string\n    for _, pair := range a.buckets {\n        if pair != nil {\n            values = append(values, pair.val)\n        }\n    }\n    return values\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nfunc (a *arrayHashMap) print() {\n    for _, pair := range a.buckets {\n        if pair != nil {\n            fmt.Println(pair.key, \"->\", pair.val)\n        }\n    }\n}\n
array_hash_map.swift
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nclass Pair: Equatable {\n    public var key: Int\n    public var val: String\n\n    public init(key: Int, val: String) {\n        self.key = key\n        self.val = val\n    }\n\n    public static func == (lhs: Pair, rhs: Pair) -> Bool {\n        lhs.key == rhs.key && lhs.val == rhs.val\n    }\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    private var buckets: [Pair?]\n\n    init() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        buckets = Array(repeating: nil, count: 100)\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    private func hashFunc(key: Int) -> Int {\n        let index = key % 100\n        return index\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    func get(key: Int) -> String? {\n        let index = hashFunc(key: key)\n        let pair = buckets[index]\n        return pair?.val\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    func put(key: Int, val: String) {\n        let pair = Pair(key: key, val: val)\n        let index = hashFunc(key: key)\n        buckets[index] = pair\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    func remove(key: Int) {\n        let index = hashFunc(key: key)\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c nil, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        buckets[index] = nil\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    func pairSet() -> [Pair] {\n        buckets.compactMap { $0 }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    func keySet() -> [Int] {\n        buckets.compactMap { $0?.key }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    func valueSet() -> [String] {\n        buckets.compactMap { $0?.val }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    func print() {\n        for pair in pairSet() {\n            Swift.print(\"\\(pair.key) -> \\(pair.val)\")\n        }\n    }\n}\n
array_hash_map.js
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Number -> String */\nclass Pair {\n    constructor(key, val) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    #buckets;\n    constructor() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        this.#buckets = new Array(100).fill(null);\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    #hashFunc(key) {\n        return key % 100;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    get(key) {\n        let index = this.#hashFunc(key);\n        let pair = this.#buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    set(key, val) {\n        let index = this.#hashFunc(key);\n        this.#buckets[index] = new Pair(key, val);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    delete(key) {\n        let index = this.#hashFunc(key);\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c null, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        this.#buckets[index] = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    entries() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    keys() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    values() {\n        let arr = [];\n        for (let i = 0; i < this.#buckets.length; i++) {\n            if (this.#buckets[i]) {\n                arr.push(this.#buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
array_hash_map.ts
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Number -> String */\nclass Pair {\n    public key: number;\n    public val: string;\n\n    constructor(key: number, val: string) {\n        this.key = key;\n        this.val = val;\n    }\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    private readonly buckets: (Pair | null)[];\n\n    constructor() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        this.buckets = new Array(100).fill(null);\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    private hashFunc(key: number): number {\n        return key % 100;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    public get(key: number): string | null {\n        let index = this.hashFunc(key);\n        let pair = this.buckets[index];\n        if (pair === null) return null;\n        return pair.val;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    public set(key: number, val: string) {\n        let index = this.hashFunc(key);\n        this.buckets[index] = new Pair(key, val);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    public delete(key: number) {\n        let index = this.hashFunc(key);\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c null, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        this.buckets[index] = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    public entries(): (Pair | null)[] {\n        let arr: (Pair | null)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i]);\n            }\n        }\n        return arr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    public keys(): (number | undefined)[] {\n        let arr: (number | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].key);\n            }\n        }\n        return arr;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    public values(): (string | undefined)[] {\n        let arr: (string | undefined)[] = [];\n        for (let i = 0; i < this.buckets.length; i++) {\n            if (this.buckets[i]) {\n                arr.push(this.buckets[i].val);\n            }\n        }\n        return arr;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    public print() {\n        let pairSet = this.entries();\n        for (const pair of pairSet) {\n            console.info(`${pair.key} -> ${pair.val}`);\n        }\n    }\n}\n
array_hash_map.dart
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nclass Pair {\n  int key;\n  String val;\n  Pair(this.key, this.val);\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n  late List<Pair?> _buckets;\n\n  ArrayHashMap() {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n    _buckets = List.filled(100, null);\n  }\n\n  /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n  int _hashFunc(int key) {\n    final int index = key % 100;\n    return index;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n  String? get(int key) {\n    final int index = _hashFunc(key);\n    final Pair? pair = _buckets[index];\n    if (pair == null) {\n      return null;\n    }\n    return pair.val;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n  void put(int key, String val) {\n    final Pair pair = Pair(key, val);\n    final int index = _hashFunc(key);\n    _buckets[index] = pair;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n  void remove(int key) {\n    final int index = _hashFunc(key);\n    _buckets[index] = null;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n  List<Pair> pairSet() {\n    List<Pair> pairSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        pairSet.add(pair);\n      }\n    }\n    return pairSet;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n  List<int> keySet() {\n    List<int> keySet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        keySet.add(pair.key);\n      }\n    }\n    return keySet;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n  List<String> values() {\n    List<String> valueSet = [];\n    for (final Pair? pair in _buckets) {\n      if (pair != null) {\n        valueSet.add(pair.val);\n      }\n    }\n    return valueSet;\n  }\n\n  /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n  void printHashMap() {\n    for (final Pair kv in pairSet()) {\n      print(\"${kv.key} -> ${kv.val}\");\n    }\n  }\n}\n
array_hash_map.rs
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n#[derive(Debug, Clone, PartialEq)]\npub struct Pair {\n    pub key: i32,\n    pub val: String,\n}\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\npub struct ArrayHashMap {\n    buckets: Vec<Option<Pair>>,\n}\n\nimpl ArrayHashMap {\n    pub fn new() -> ArrayHashMap {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n        Self {\n            buckets: vec![None; 100],\n        }\n    }\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fn hash_func(&self, key: i32) -> usize {\n        key as usize % 100\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    pub fn get(&self, key: i32) -> Option<&String> {\n        let index = self.hash_func(key);\n        self.buckets[index].as_ref().map(|pair| &pair.val)\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    pub fn put(&mut self, key: i32, val: &str) {\n        let index = self.hash_func(key);\n        self.buckets[index] = Some(Pair {\n            key,\n            val: val.to_string(),\n        });\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    pub fn remove(&mut self, key: i32) {\n        let index = self.hash_func(key);\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c None, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        self.buckets[index] = None;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    pub fn entry_set(&self) -> Vec<&Pair> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref())\n            .collect()\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    pub fn key_set(&self) -> Vec<&i32> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.key))\n            .collect()\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    pub fn value_set(&self) -> Vec<&String> {\n        self.buckets\n            .iter()\n            .filter_map(|pair| pair.as_ref().map(|pair| &pair.val))\n            .collect()\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    pub fn print(&self) {\n        for pair in self.entry_set() {\n            println!(\"{} -> {}\", pair.key, pair.val);\n        }\n    }\n}\n
array_hash_map.c
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 int->string */\ntypedef struct {\n    int key;\n    char *val;\n} Pair;\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntypedef struct {\n    Pair *buckets[MAX_SIZE];\n} ArrayHashMap;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nArrayHashMap *newArrayHashMap() {\n    ArrayHashMap *hmap = malloc(sizeof(ArrayHashMap));\n    for (int i=0; i < MAX_SIZE; i++) {\n        hmap->buckets[i] = NULL;\n    }\n    return hmap;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delArrayHashMap(ArrayHashMap *hmap) {\n    for (int i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            free(hmap->buckets[i]->val);\n            free(hmap->buckets[i]);\n        }\n    }\n    free(hmap);\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\nvoid put(ArrayHashMap *hmap, const int key, const char *val) {\n    Pair *Pair = malloc(sizeof(Pair));\n    Pair->key = key;\n    Pair->val = malloc(strlen(val) + 1);\n    strcpy(Pair->val, val);\n\n    int index = hashFunc(key);\n    hmap->buckets[index] = Pair;\n}\n\n/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\nvoid removeItem(ArrayHashMap *hmap, const int key) {\n    int index = hashFunc(key);\n    free(hmap->buckets[index]->val);\n    free(hmap->buckets[index]);\n    hmap->buckets[index] = NULL;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nvoid pairSet(ArrayHashMap *hmap, MapSet *set) {\n    Pair *entries;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    entries = malloc(sizeof(Pair) * total);\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            entries[index].key = hmap->buckets[i]->key;\n            entries[index].val = malloc(strlen(hmap->buckets[i]->val) + 1);\n            strcpy(entries[index].val, hmap->buckets[i]->val);\n            index++;\n        }\n    }\n    set->set = entries;\n    set->len = total;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\nvoid keySet(ArrayHashMap *hmap, MapSet *set) {\n    int *keys;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    keys = malloc(total * sizeof(int));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            keys[index] = hmap->buckets[i]->key;\n            index++;\n        }\n    }\n    set->set = keys;\n    set->len = total;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\nvoid valueSet(ArrayHashMap *hmap, MapSet *set) {\n    char **vals;\n    int i = 0, index = 0;\n    int total = 0;\n    /* \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            total++;\n        }\n    }\n    vals = malloc(total * sizeof(char *));\n    for (i = 0; i < MAX_SIZE; i++) {\n        if (hmap->buckets[i] != NULL) {\n            vals[index] = hmap->buckets[i]->val;\n            index++;\n        }\n    }\n    set->set = vals;\n    set->len = total;\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid print(ArrayHashMap *hmap) {\n    int i;\n    MapSet set;\n    pairSet(hmap, &set);\n    Pair *entries = (Pair *)set.set;\n    for (i = 0; i < set.len; i++) {\n        printf(\"%d -> %s\\n\", entries[i].key, entries[i].val);\n    }\n    free(set.set);\n}\n
array_hash_map.kt
/* \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\nclass Pair(\n    var key: Int,\n    var _val: String\n)\n\n/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayHashMap {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n    private val buckets = arrayOfNulls<Pair>(100)\n\n    /* \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f */\n    fun hashFunc(key: Int): Int {\n        val index = key % 100\n        return index\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 */\n    fun get(key: Int): String? {\n        val index = hashFunc(key)\n        val pair = buckets[index] ?: return null\n        return pair._val\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f */\n    fun put(key: Int, _val: String) {\n        val pair = Pair(key, _val)\n        val index = hashFunc(key)\n        buckets[index] = pair\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f */\n    fun remove(key: Int) {\n        val index = hashFunc(key)\n        // \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c null, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n        buckets[index] = null\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 */\n    fun pairSet(): MutableList<Pair> {\n        val pairSet = mutableListOf<Pair>()\n        for (pair in buckets) {\n            if (pair != null)\n                pairSet.add(pair)\n        }\n        return pairSet\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 */\n    fun keySet(): MutableList<Int> {\n        val keySet = mutableListOf<Int>()\n        for (pair in buckets) {\n            if (pair != null)\n                keySet.add(pair.key)\n        }\n        return keySet\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f */\n    fun valueSet(): MutableList<String> {\n        val valueSet = mutableListOf<String>()\n        for (pair in buckets) {\n            if (pair != null)\n                valueSet.add(pair._val)\n        }\n        return valueSet\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\n    fun print() {\n        for (kv in pairSet()) {\n            val key = kv.key\n            val _val = kv._val\n            println(\"$key -> $_val\")\n        }\n    }\n}\n
array_hash_map.rb
=begin\nFile: array_hash_map.rb\nCreated Time: 2024-04-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n=begin\nFile: array_hash_map.rb\nCreated Time: 2024-04-13\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0430\u0440\u0430 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 ###\nclass Pair\n  attr_accessor :key, :val\n\n  def initialize(key, val)\n    @key = key\n    @val = val\n  end\nend\n\n# ## \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\nclass ArrayHashMap\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 100 \u043a\u043e\u0440\u0437\u0438\u043d\n    @buckets = Array.new(100)\n  end\n\n  # ## \u0425\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f ###\n  def hash_func(key)\n    index = key % 100\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 ###\n  def get(key)\n    index = hash_func(key)\n    pair = @buckets[index]\n\n    return if pair.nil?\n    pair.val\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f ###\n  def put(key, val)\n    pair = Pair.new(key, val)\n    index = hash_func(key)\n    @buckets[index] = pair\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f ###\n  def remove(key)\n    index = hash_func(key)\n    # \u041f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c nil, \u0447\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\n    @buckets[index] = nil\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 ###\n  def entry_set\n    result = []\n    @buckets.each { |pair| result << pair unless pair.nil? }\n    result\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u043b\u044e\u0447\u0438 ###\n  def key_set\n    result = []\n    @buckets.each { |pair| result << pair.key unless pair.nil? }\n    result\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f ###\n  def value_set\n    result = []\n    @buckets.each { |pair| result << pair.val unless pair.nil? }\n    result\n  end\n\n  # ## \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 ###\n  def print\n    @buckets.each { |pair| puts \"#{pair.key} -> #{pair.val}\" unless pair.nil? }\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_hashing/hash_map/#613-","title":"6.1.3 \u00a0 \u0425\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435","text":"

\u041f\u043e \u0441\u0443\u0442\u0438, \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0430\u0435\u0442 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 \u0432\u0441\u0435\u0445 key , \u0432 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0435 \u0438\u0437 \u0432\u0441\u0435\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \"\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0445\u043e\u0434\u043e\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u043e\u0434\u043d\u043e\u043c\u0443 \u0432\u044b\u0445\u043e\u0434\u0443\".

\u0414\u043b\u044f \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u0437 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0435 \u0446\u0438\u0444\u0440\u044b key \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442, \u0442\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 12836 \u0438 20336, \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043c:

12836 % 100 = 36\n20336 % 100 = 36\n

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-3, \u0434\u0432\u0430 \u043d\u043e\u043c\u0435\u0440\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435 \u0438\u043c\u044f, \u0447\u0442\u043e, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u043d\u0435\u0432\u0435\u0440\u043d\u043e. \u0422\u0430\u043a\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0432\u0445\u043e\u0434\u0430\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0432\u044b\u0445\u043e\u0434, \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0435\u0439 (hash collision).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438

\u041b\u0435\u0433\u043a\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u0447\u0442\u043e \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(n\\) , \u0442\u0435\u043c \u043d\u0438\u0436\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e key \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u0432 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0431\u0430\u043a\u0435\u0442, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0442\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439 \u043f\u0443\u0442\u0435\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 6-4, \u0434\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (136, A) \u0438 (236, D) \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432\u0430\u043b\u0438, \u0430 \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u044f \u0438\u0441\u0447\u0435\u0437\u043b\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 6-4 \u00a0 \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b

\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u0442\u0430\u0440\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 \u043d\u043e\u0432\u0443\u044e, \u0430 \u044d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u043e \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438; \u043a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b capacity \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0430\u0441\u044c, \u043d\u0430\u043c \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0437\u0430\u043d\u043e\u0432\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0435\u0445 \u043f\u0430\u0440 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0430\u0441\u0445\u043e\u0434\u044b \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0440\u0435\u0437\u0435\u0440\u0432\u0438\u0440\u0443\u044e\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u044b\u0445 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0439.

\u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 (load factor) - \u0432\u0430\u0436\u043d\u043e\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u041e\u043d \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043a \u0447\u0438\u0441\u043b\u0443 \u0431\u0430\u043a\u0435\u0442\u043e\u0432 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0446\u0435\u043d\u043a\u0438 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e\u0441\u0442\u0438 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0447\u0430\u0441\u0442\u043e \u0441\u043b\u0443\u0436\u0438\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u043d\u0438\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 Java, \u043a\u043e\u0433\u0434\u0430 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \\(0.75\\) , \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0434\u043e \\(2\\) \u0440\u0430\u0437 \u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0435\u043c\u043a\u043e\u0441\u0442\u0438.

"},{"location":"chapter_hashing/summary/","title":"6.4 \u00a0 \u041a\u0440\u0430\u0442\u043a\u0438\u0435 \u0438\u0442\u043e\u0433\u0438","text":""},{"location":"chapter_hashing/summary/#1","title":"1. \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_hashing/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u0412 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \\(O(n)\\) ?

\u041a\u043e\u0433\u0434\u0430 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0441\u0442\u0430\u043d\u043e\u0432\u044f\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u044b\u043c\u0438, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u0443\u0435\u0442 \u0434\u043e \\(O(n)\\) . \u0415\u0441\u043b\u0438 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0445\u043e\u0440\u043e\u0448\u043e, \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0432\u044b\u0431\u0440\u0430\u043d\u0430 \u0440\u0430\u0437\u0443\u043c\u043d\u043e, \u0430 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e, \u0442\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \\(O(1)\\) . \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u044b, \u043a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u043e, \u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u043c \u0435\u0435 \u0437\u0430 \\(O(1)\\) .

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \\(f(x) = x\\) ? \u0422\u043e\u0433\u0434\u0430 \u0432\u0435\u0434\u044c \u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0439 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442.

\u041f\u0440\u0438 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \\(f(x) = x\\) \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u0431\u0430\u043a\u0435\u0442\u0430, \u0438 \u0442\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0443. \u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u044b\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 (\u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u0432\u0437\u044f\u0442\u0438\u0435 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0446\u0435\u043b\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0432 \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \\(O(1)\\) \u043f\u043e\u0438\u0441\u043a\u0430.

Q: \u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043b\u0435\u0436\u0430\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e. \u041f\u043e\u0447\u0435\u043c\u0443 \u0436\u0435 \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043d\u0438\u0445?

\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0443 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u0432\u044b\u0448\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c, \u043d\u043e \u0441\u043d\u0438\u0436\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c. \u0417\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0435\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0439.

\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043e\u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445. \u0415\u0441\u043b\u0438 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0441 \u0442\u043e\u0439 \u0436\u0435 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u043a\u043e\u0439, \u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0442\u0430\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430. \u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u0430\u043c\u043e \u043f\u043e \u0441\u0435\u0431\u0435 \u0441\u0442\u043e\u0438\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430 \u0432 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0448\u0435.

\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u043c\u0435\u0442\u043e\u0434\u0435 \u0446\u0435\u043f\u043e\u0447\u0435\u043a \u043c\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u043e\u0438\u0441\u043a \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0438\u043b\u0438 \u043a\u0440\u0430\u0441\u043d\u043e-\u0447\u0435\u0440\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0438\u0441\u043a \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438 \u0434\u043e \\(O(n)\\) \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f.

Q: \u0415\u0441\u0442\u044c \u043b\u0438 \u0443 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \"\u043d\u0435\u043b\u044c\u0437\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\"? \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0441\u0442\u043e, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u043e\u0435 \u043a\u0430\u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0435?

\u041f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0445\u0435\u0448\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 - \u044d\u0442\u043e \u0440\u0430\u0437\u043d\u043e\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438, \u0430 \u0443 \u0432\u0441\u0435\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0439 \u0430\u0434\u0440\u0435\u0441\u0430\u0446\u0438\u0438 \u0435\u0441\u0442\u044c \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435\u043b\u044c\u0437\u044f \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043a\u0443 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. \u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u043e\u0435 \u043a\u0430\u043a \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0435, \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e. \u041a\u043e\u0433\u0434\u0430 \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438 \u0432 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 \u0442\u0430\u043a\u0443\u044e \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e, \u044d\u0442\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0437\u0430\u043d\u044f\u0442\u0430 \u043d\u043e\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u0443\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438.

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u043c \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u044f?

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u044b \u0447\u0435\u0440\u0435\u0437 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0431\u0430\u043a\u0435\u0442 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043d\u043e \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e key \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442, \u0430 \u044d\u0442\u043e \u0438 \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u0430\u0440\u0443 \u043a\u043b\u044e\u0447-\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u043b\u0438 \u043d\u0435 \u0443\u0431\u0435\u0434\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u043f\u043e\u0438\u0441\u043a \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0441\u044f \u043d\u0435\u0443\u0434\u0430\u0447\u0435\u0439.

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0441\u043c\u044f\u0433\u0447\u0430\u0442\u044c \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438?

\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433 \u0445\u0435\u0448-\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432\u043e \u0432\u0437\u044f\u0442\u0438\u0438 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\(n\\) , \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u043b \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430; \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\(n\\) \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c\u0441\u044f \u0438 \u0438\u043d\u0434\u0435\u043a\u0441, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0430\u043d\u043d\u043e\u043c\u0443 key . \u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e key , \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u043d\u044c\u0448\u0435 \u043f\u043e\u043f\u0430\u0434\u0430\u043b\u0438 \u0432 \u043e\u0434\u0438\u043d \u0431\u0430\u043a\u0435\u0442, \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043f\u043e \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u043c \u0431\u0430\u043a\u0435\u0442\u0430\u043c, \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0445\u0435\u0448-\u043a\u043e\u043b\u043b\u0438\u0437\u0438\u0438 \u0431\u0443\u0434\u0443\u0442 \u043e\u0441\u043b\u0430\u0431\u043b\u0435\u043d\u044b.

Q: \u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f, \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432?

\u041a\u043e\u0433\u0434\u0430 key \u0434\u0430\u043d\u043d\u044b\u0445 - \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0435 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0438\u0437 \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430, \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432: \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e. \u041d\u043e \u0435\u0441\u043b\u0438 key \u0438\u043c\u0435\u044e\u0442 \u0434\u0440\u0443\u0433\u043e\u0439 \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0438), \u0442\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u0435\u043d \u0445\u0435\u0448-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u0442 key \u0432 \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u043a\u0435\u0442\u043e\u0432. \u0422\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439.

"},{"location":"chapter_heap/","title":"\u0413\u043b\u0430\u0432\u0430 8. \u00a0 \u041a\u0443\u0447\u0430","text":"

Abstract

\u041a\u0443\u0447\u0430 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0433\u043e\u0440\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b: \u044f\u0440\u0443\u0441\u043d\u044b\u0435, \u0432\u043e\u043b\u043d\u0438\u0441\u0442\u044b\u0435 \u0438 \u0441\u0430\u043c\u044b\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u043f\u043e \u0444\u043e\u0440\u043c\u0435.

\u041a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0432\u044b\u0441\u043e\u0442\u0443, \u043d\u043e \u0441\u0430\u043c\u0430\u044f \u0432\u044b\u0441\u043e\u043a\u0430\u044f \u0432\u0441\u0435\u0433\u0434\u0430 \u0431\u0440\u043e\u0441\u0430\u0435\u0442\u0441\u044f \u0432 \u0433\u043b\u0430\u0437\u0430 \u043f\u0435\u0440\u0432\u043e\u0439.

"},{"location":"chapter_heap/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_heap/build_heap/","title":"8.2 \u00a0 \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438","text":"

\u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0443\u0447\u0443, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0441\u0440\u0430\u0437\u0443 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435\u043c \u043a\u0443\u0447\u0438\".

"},{"location":"chapter_heap/build_heap/#821","title":"8.2.1 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043a\u0443\u0447\u0443","text":"

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u0443\u0447\u0443, \u0437\u0430\u0442\u0435\u043c \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \"\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043a\u0443\u0447\u0443\": \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043a\u0443\u0447\u0438, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \"\u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\".

\u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043a\u0443\u0447\u0443, \u0435\u0435 \u0434\u043b\u0438\u043d\u0430 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443\u0437\u043b\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437, \u043a\u0443\u0447\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \"\u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\".

\u041f\u0443\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0440\u0430\u0432\u043d\u043e \\(n\\) ; \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u0430\u0436\u0434\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(\\log{n})\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(n \\log n)\\) .

"},{"location":"chapter_heap/build_heap/#822","title":"8.2.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u0445\u043e\u0434 \u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435","text":"

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0434\u0432\u0443\u0445 \u0448\u0430\u0433\u043e\u0432.

  1. \u0411\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443; \u0432 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043a\u0443\u0447\u0438 \u0435\u0449\u0435 \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f.
  2. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u0443\u0447\u0443 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0442\u043e \u0435\u0441\u0442\u044c \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0443 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c, \u0438 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \"\u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\" \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043d\u0435\u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430.

\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0437\u0435\u043b \u0431\u044b\u043b \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d, \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \u0441 \u044d\u0442\u0438\u043c \u0443\u0437\u043b\u043e\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u0440\u043d\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0439 \u043f\u043e\u0434\u043a\u0443\u0447\u0435\u0439. \u0410 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u0445\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043a\u0443\u0447\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \"\u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\".

\u041f\u0440\u0438\u0447\u0438\u043d\u0430 \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043e\u043d \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442: \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043d\u0438\u0436\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c\u0438 \u043f\u043e\u0434\u043a\u0443\u0447\u0430\u043c\u0438, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u043c.

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0435 \u0443\u0437\u043b\u044b \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d\u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c\u0438 \u043f\u043e\u0434\u043a\u0443\u0447\u0430\u043c\u0438 \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044f. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043a\u043e\u0434\u0435 \u043d\u0438\u0436\u0435, \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u043d\u0435\u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430, \u0438 \u0438\u043c\u0435\u043d\u043d\u043e \u0441 \u043d\u0435\u0433\u043e \u043c\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 \u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
def __init__(self, nums: list[int]):\n    \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443\"\"\"\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    self.max_heap = nums\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in range(self.parent(self.size() - 1), -1, -1):\n        self.sift_down(i)\n
my_heap.cpp
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nMaxHeap(vector<int> nums) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    maxHeap = nums;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
my_heap.java
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nMaxHeap(List<Integer> nums) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    maxHeap = new ArrayList<>(nums);\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = parent(size() - 1); i >= 0; i--) {\n        siftDown(i);\n    }\n}\n
my_heap.cs
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440: \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nMaxHeap(IEnumerable<int> nums) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    maxHeap = new List<int>(nums);\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    var size = Parent(this.Size() - 1);\n    for (int i = size; i >= 0; i--) {\n        SiftDown(i);\n    }\n}\n
my_heap.go
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0441\u0440\u0435\u0437\u0443 */\nfunc newMaxHeap(nums []any) *maxHeap {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    h := &maxHeap{data: nums}\n    for i := h.parent(len(h.data) - 1); i >= 0; i-- {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n        h.siftDown(i)\n    }\n    return h\n}\n
my_heap.swift
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\ninit(nums: [Int]) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    maxHeap = nums\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in (0 ... parent(i: size() - 1)).reversed() {\n        siftDown(i: i)\n    }\n}\n
my_heap.js
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u0443\u0447\u0443 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nconstructor(nums) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    this.#maxHeap = nums === undefined ? [] : [...nums];\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (let i = this.#parent(this.size() - 1); i >= 0; i--) {\n        this.#siftDown(i);\n    }\n}\n
my_heap.ts
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u043e\u0437\u0434\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043a\u0443\u0447\u0443 \u0438\u043b\u0438 \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nconstructor(nums?: number[]) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    this.maxHeap = nums === undefined ? [] : [...nums];\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (let i = this.parent(this.size() - 1); i >= 0; i--) {\n        this.siftDown(i);\n    }\n}\n
my_heap.dart
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nMaxHeap(List<int> nums) {\n  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  _maxHeap = nums;\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  for (int i = _parent(size() - 1); i >= 0; i--) {\n    siftDown(i);\n  }\n}\n
my_heap.rs
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\nfn new(nums: Vec<i32>) -> Self {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n    let mut heap = MaxHeap { max_heap: nums };\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in (0..=Self::parent(heap.size() - 1)).rev() {\n        heap.sift_down(i);\n    }\n    heap\n}\n
my_heap.c
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0441\u0440\u0435\u0437\u0443 */\nMaxHeap *newMaxHeap(int nums[], int size) {\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u0443\u0447\u0443\n    MaxHeap *maxHeap = (MaxHeap *)malloc(sizeof(MaxHeap));\n    maxHeap->size = size;\n    memcpy(maxHeap->data, nums, size * sizeof(int));\n    for (int i = parent(maxHeap, size - 1); i >= 0; i--) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n        siftDown(maxHeap, i);\n    }\n    return maxHeap;\n}\n
my_heap.kt
/* \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 */\nclass MaxHeap(nums: MutableList<Int>?) {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0432\u043c\u0435\u0441\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    private val maxHeap = mutableListOf<Int>()\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 */\n    init {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n        maxHeap.addAll(nums!!)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n        for (i in parent(size() - 1) downTo 0) {\n            siftDown(i)\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\n    private fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\n    private fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\n    private fun parent(i: Int): Int {\n        return (i - 1) / 2 // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n    }\n\n    /* \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u0441\u0442\u0430\u043c\u0438 */\n    private fun swap(i: Int, j: Int) {\n        val temp = maxHeap[i]\n        maxHeap[i] = maxHeap[j]\n        maxHeap[j] = temp\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\n    fun size(): Int {\n        return maxHeap.size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\n    fun isEmpty(): Boolean {\n        /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\n        return size() == 0\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\n    fun peek(): Int {\n        return maxHeap[0]\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\n    fun push(_val: Int) {\n        // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        maxHeap.add(_val)\n        // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n        siftUp(size() - 1)\n    }\n\n    /* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\n    private fun siftUp(it: Int) {\n        // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Kotlin \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\n        var i = it\n        while (true) {\n            // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n            val p = parent(i)\n            // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n            if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n            // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n            swap(i, p)\n            // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n            i = p\n        }\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\n    fun pop(): Int {\n        // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        swap(0, size() - 1)\n        // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        val _val = maxHeap.removeAt(size() - 1)\n        // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(0)\n        // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n        return _val\n    }\n\n    /* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\n    private fun siftDown(it: Int) {\n        // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Kotlin \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\n        var i = it\n        while (true) {\n            // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n            val l = left(i)\n            val r = right(i)\n            var ma = i\n            if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n            if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n            // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n            if (ma == i) break\n            // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n            swap(i, ma)\n            // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n            i = ma\n        }\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043a\u0443\u0447\u0443 (\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) */\n    fun print() {\n        val queue = PriorityQueue { a: Int, b: Int -> b - a }\n        queue.addAll(maxHeap)\n        printHeap(queue)\n    }\n}\n

```ruby title=\"my_heap.rb\"

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_heap/build_heap/#823","title":"8.2.3 \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":"

\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438.

\u041f\u0435\u0440\u0435\u043c\u043d\u043e\u0436\u0438\u0432 \u044d\u0442\u0438 \u0434\u0432\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438 \\(O(n \\log n)\\) . \u041d\u043e \u044d\u0442\u0430 \u043e\u0446\u0435\u043d\u043a\u0430 \u043d\u0435\u0442\u043e\u0447\u043d\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u0443\u0447\u043b\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430: \u043d\u0430 \u043d\u0438\u0436\u043d\u0438\u0445 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u0443\u0437\u043b\u043e\u0432 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u0430 \u0432\u0435\u0440\u0445\u043d\u0438\u0445.

\u0414\u0430\u043b\u0435\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0431\u043e\u043b\u0435\u0435 \u0442\u043e\u0447\u043d\u044b\u0439 \u0440\u0430\u0441\u0447\u0435\u0442. \u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0434\u0430\u043d\u043e \"\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\" \u0432\u044b\u0441\u043e\u0442\u044b \\(h\\) \u0441 \u0447\u0438\u0441\u043b\u043e\u043c \u0443\u0437\u043b\u043e\u0432 \\(n\\) ; \u044d\u0442\u043e \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u0435\u0442 \u043d\u0430 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-5 \u00a0 \u0427\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 8-5, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044f \"\u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\" \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044e \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043e \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0430 \u044d\u0442\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0435\u0441\u0442\u044c \"\u0432\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\". \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \"\u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \\(\\times\\) \u0432\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\" \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432.

\\[ T(h) = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{(h-1)}\\times1 \\]

\u0427\u0442\u043e\u0431\u044b \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0448\u043a\u043e\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u043d\u0438\u044f\u043c\u0438 \u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044f\u0445 \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0443\u043c\u043d\u043e\u0436\u0438\u043c \\(T(h)\\) \u043d\u0430 \\(2\\) :

\\[ \\begin{aligned} T(h) & = 2^0h + 2^1(h-1) + 2^2(h-2) + \\dots + 2^{h-1}\\times1 \\newline 2 T(h) & = 2^1h + 2^2(h-1) + 2^3(h-2) + \\dots + 2^{h}\\times1 \\newline \\end{aligned} \\]

\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044f \u0441\u043e \u0441\u0434\u0432\u0438\u0433\u043e\u043c, \u0432\u044b\u0447\u0442\u0435\u043c \u0438\u0437 \u043d\u0438\u0436\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \\(2 T(h)\\) \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0441\u0442\u0440\u043e\u043a\u0443 \\(T(h)\\) , \u0442\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u043c:

\\[ 2T(h) - T(h) = T(h) = -2^0h + 2^1 + 2^2 + \\dots + 2^{h-1} + 2^h \\]

\u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \\(T(h)\\) \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0433\u0435\u043e\u043c\u0435\u0442\u0440\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0435\u0441\u0441\u0438\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c:

\\[ \\begin{aligned} T(h) & = 2 \\frac{1 - 2^h}{1 - 2} - h \\newline & = 2^{h+1} - h - 2 \\newline & = O(2^h) \\end{aligned} \\]

\u0414\u0430\u043b\u0435\u0435, \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u044b\u0441\u043e\u0442\u044b \\(h\\) \u0440\u0430\u0432\u043d\u043e \\(n = 2^{h+1} - 1\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(2^h) = O(n)\\) . \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0432\u043e\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442, \u0447\u0442\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\) , \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

"},{"location":"chapter_heap/heap/","title":"8.1 \u00a0 \u041a\u0443\u0447\u0430","text":"

\u041a\u0443\u0447\u0430 (heap) - \u044d\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u043a\u0443\u0447\u0438 \u0434\u0432\u0430, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 8-1.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-1 \u00a0 \u041c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 \u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430

\u041a\u0443\u0447\u0430, \u044f\u0432\u043b\u044f\u044f\u0441\u044c \u0447\u0430\u0441\u0442\u043d\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438.

"},{"location":"chapter_heap/heap/#811","title":"8.1.1 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043a\u0443\u0447\u0435\u0439","text":"

\u041d\u0443\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043d\u0435 \u0441\u0430\u043c\u0443 \u043a\u0443\u0447\u0443, \u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c (priority queue) - \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c\u0443\u044e \u043a\u0430\u043a \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043a\u0443\u0447\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c, \u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e. \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c\" \u0438 \"\u043a\u0443\u0447\u0430\" \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c \u0438\u0445 \u0438 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0435\u043c \u0435\u0434\u0438\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0442\u044c \"\u043a\u0443\u0447\u0435\u0439\".

\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043a\u0443\u0447\u0435\u0439 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 8-1. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 8-1 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043a\u0443\u0447\u0435\u0439

\u0418\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c push() \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443 \\(O(\\log n)\\) pop() \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 \\(O(\\log n)\\) peek() \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 (\u0434\u043b\u044f max / min \u043a\u0443\u0447\u0438 \u044d\u0442\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c / \u043c\u0438\u043d\u0438\u043c\u0443\u043c) \\(O(1)\\) size() \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0435 \\(O(1)\\) isEmpty() \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 \\(O(1)\\)

\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u044b \u043a\u0443\u0447\u0438, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u044f\u0437\u044b\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0438\u043b\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c.

\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\u043c \"\u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e\" \u0438 \"\u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\", \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \"\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0435\u0439\" \u0438 \"\u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0435\u0439\", \u0438\u0437\u043c\u0435\u043d\u044f\u044f flag \u0438\u043b\u0438 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u044f Comparator . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nmin_heap, flag = [], 1\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nmax_heap, flag = [], -1\n\n# \u041c\u043e\u0434\u0443\u043b\u044c heapq \u0432 Python \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443\n# \u0415\u0441\u043b\u0438 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043f\u0435\u0440\u0435\u0434 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c, \u0442\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043f\u0435\u0440\u0435\u0432\u0435\u0440\u043d\u0435\u0442\u0441\u044f \u0438 \u0442\u0430\u043a \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430\n# \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 flag = 1 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0435, \u0430 flag = -1 - \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439\n\n# \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443\nheapq.heappush(max_heap, flag * 1)\nheapq.heappush(max_heap, flag * 3)\nheapq.heappush(max_heap, flag * 2)\nheapq.heappush(max_heap, flag * 5)\nheapq.heappush(max_heap, flag * 4)\n\n# \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438\npeek: int = flag * max_heap[0] # 5\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n# \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\nval = flag * heapq.heappop(max_heap) # 5\nval = flag * heapq.heappop(max_heap) # 4\nval = flag * heapq.heappop(max_heap) # 3\nval = flag * heapq.heappop(max_heap) # 2\nval = flag * heapq.heappop(max_heap) # 1\n\n# \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438\nsize: int = len(max_heap)\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430\nis_empty: bool = not max_heap\n\n# \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\nmin_heap: list[int] = [1, 3, 2, 5, 4]\nheapq.heapify(min_heap)\n
heap.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\npriority_queue<int, vector<int>, greater<int>> minHeap;\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\npriority_queue<int, vector<int>, less<int>> maxHeap;\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nmaxHeap.push(1);\nmaxHeap.push(3);\nmaxHeap.push(2);\nmaxHeap.push(5);\nmaxHeap.push(4);\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek = maxHeap.top(); // 5\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n// \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\nmaxHeap.pop(); // 5\nmaxHeap.pop(); // 4\nmaxHeap.pop(); // 3\nmaxHeap.pop(); // 2\nmaxHeap.pop(); // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nint size = maxHeap.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nbool isEmpty = maxHeap.empty();\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nvector<int> input{1, 3, 2, 5, 4};\npriority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());\n
heap.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nQueue<Integer> minHeap = new PriorityQueue<>();\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 (\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c Comparator \u0447\u0435\u0440\u0435\u0437 lambda)\nQueue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nmaxHeap.offer(1);\nmaxHeap.offer(3);\nmaxHeap.offer(2);\nmaxHeap.offer(5);\nmaxHeap.offer(4);\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek = maxHeap.peek(); // 5\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n// \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\npeek = maxHeap.poll(); // 5\npeek = maxHeap.poll(); // 4\npeek = maxHeap.poll(); // 3\npeek = maxHeap.poll(); // 2\npeek = maxHeap.poll(); // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nint size = maxHeap.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nboolean isEmpty = maxHeap.isEmpty();\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nminHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));\n
heap.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nPriorityQueue<int, int> minHeap = new();\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 (\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c Comparer \u0447\u0435\u0440\u0435\u0437 lambda)\nPriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y.CompareTo(x)));\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nmaxHeap.Enqueue(1, 1);\nmaxHeap.Enqueue(3, 3);\nmaxHeap.Enqueue(2, 2);\nmaxHeap.Enqueue(5, 5);\nmaxHeap.Enqueue(4, 4);\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek = maxHeap.Peek();//5\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n// \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\npeek = maxHeap.Dequeue();  // 5\npeek = maxHeap.Dequeue();  // 4\npeek = maxHeap.Dequeue();  // 3\npeek = maxHeap.Dequeue();  // 2\npeek = maxHeap.Dequeue();  // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nint size = maxHeap.Count;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nbool isEmpty = maxHeap.Count == 0;\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nminHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);\n
heap.go
// \u0412 Go \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u0443\u044e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443, \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0432 heap.Interface\n// \u0414\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 heap.Interface \u0442\u0430\u043a\u0436\u0435 \u043d\u0443\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c sort.Interface\ntype intHeap []any\n\n// \u041c\u0435\u0442\u043e\u0434 Push \u0438\u0437 heap.Interface, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443\nfunc (h *intHeap) Push(x any) {\n    // Push \u0438 Pop \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 pointer receiver\n    // \u041f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437\u043c\u0435\u043d\u044f\u044e\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0441\u0440\u0435\u0437\u0430, \u043d\u043e \u0438 \u0435\u0433\u043e \u0434\u043b\u0438\u043d\u0443\n    *h = append(*h, x.(int))\n}\n\n// \u041c\u0435\u0442\u043e\u0434 Pop \u0438\u0437 heap.Interface, \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\nfunc (h *intHeap) Pop() any {\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435\n    last := (*h)[len(*h)-1]\n    *h = (*h)[:len(*h)-1]\n    return last\n}\n\n// \u041c\u0435\u0442\u043e\u0434 Len \u0438\u0437 sort.Interface\nfunc (h *intHeap) Len() int {\n    return len(*h)\n}\n\n// \u041c\u0435\u0442\u043e\u0434 Less \u0438\u0437 sort.Interface\nfunc (h *intHeap) Less(i, j int) bool {\n    // \u0414\u043b\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 \u0437\u0434\u0435\u0441\u044c \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043d\u0430 <\n    return (*h)[i].(int) > (*h)[j].(int)\n}\n\n// \u041c\u0435\u0442\u043e\u0434 Swap \u0438\u0437 sort.Interface\nfunc (h *intHeap) Swap(i, j int) {\n    (*h)[i], (*h)[j] = (*h)[j], (*h)[i]\n}\n\n// Top \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438\nfunc (h *intHeap) Top() any {\n    return (*h)[0]\n}\n\n/* Driver Code */\nfunc TestHeap(t *testing.T) {\n    /* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    maxHeap := &intHeap{}\n    heap.Init(maxHeap)\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\n    // \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b heap.Interface \u0434\u043b\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    heap.Push(maxHeap, 1)\n    heap.Push(maxHeap, 3)\n    heap.Push(maxHeap, 2)\n    heap.Push(maxHeap, 4)\n    heap.Push(maxHeap, 5)\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\n    top := maxHeap.Top()\n    fmt.Printf(\"\u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438: %d\\n\", top)\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n    // \u0412\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u044b heap.Interface \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    heap.Pop(maxHeap) // 5\n    heap.Pop(maxHeap) // 4\n    heap.Pop(maxHeap) // 3\n    heap.Pop(maxHeap) // 2\n    heap.Pop(maxHeap) // 1\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\n    size := len(*maxHeap)\n    fmt.Printf(\"\u0427\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0435: %d\\n\", size)\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\n    isEmpty := len(*maxHeap) == 0\n    fmt.Printf(\"\u041f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430: %t\\n\", isEmpty)\n}\n
heap.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0422\u0438\u043f Heap \u0432 Swift \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0438 max-heap, \u0438 min-heap, \u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442 swift-collections\nvar heap = Heap<Int>()\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nheap.insert(1)\nheap.insert(3)\nheap.insert(2)\nheap.insert(5)\nheap.insert(4)\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nvar peek = heap.max()!\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\npeek = heap.removeMax() // 5\npeek = heap.removeMax() // 4\npeek = heap.removeMax() // 3\npeek = heap.removeMax() // 2\npeek = heap.removeMax() // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nlet size = heap.count\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nlet isEmpty = heap.isEmpty\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nlet heap2 = Heap([1, 3, 2, 5, 4])\n
heap.js
// \u0412 JavaScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 Heap\n
heap.ts
// \u0412 TypeScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 Heap\n
heap.dart
// \u0412 Dart \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 Heap\n
heap.rs
use std::collections::BinaryHeap;\nuse std::cmp::Reverse;\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nlet mut min_heap = BinaryHeap::<Reverse<i32>>::new();\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nlet mut max_heap = BinaryHeap::new();\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nmax_heap.push(1);\nmax_heap.push(3);\nmax_heap.push(2);\nmax_heap.push(5);\nmax_heap.push(4);\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nlet peek = max_heap.peek().unwrap();  // 5\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n// \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\nlet peek = max_heap.pop().unwrap();   // 5\nlet peek = max_heap.pop().unwrap();   // 4\nlet peek = max_heap.pop().unwrap();   // 3\nlet peek = max_heap.pop().unwrap();   // 2\nlet peek = max_heap.pop().unwrap();   // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nlet size = max_heap.len();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nlet is_empty = max_heap.is_empty();\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nlet min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);\n
heap.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 Heap\n
heap.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\nvar minHeap = PriorityQueue<Int>()\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 (\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c Comparator \u0447\u0435\u0440\u0435\u0437 lambda)\nval maxHeap = PriorityQueue { a: Int, b: Int -> b - a }\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0443\u0447\u0443 */\nmaxHeap.offer(1)\nmaxHeap.offer(3)\nmaxHeap.offer(2)\nmaxHeap.offer(5)\nmaxHeap.offer(4)\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nvar peek = maxHeap.peek() // 5\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 */\n// \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043f\u043e \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044e\npeek = maxHeap.poll() // 5\npeek = maxHeap.poll() // 4\npeek = maxHeap.poll() // 3\npeek = maxHeap.poll() // 2\npeek = maxHeap.poll() // 1\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u043a\u0443\u0447\u0438 */\nval size = maxHeap.size\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 */\nval isEmpty = maxHeap.isEmpty()\n\n/* \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nminHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))\n
heap.rb
# \u0412 Ruby \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 Heap\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=import%20heapq%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20min-%D0%BA%D1%83%D1%87%D1%83%0A%20%20%20%20min_heap%2C%20flag%20%3D%20%5B%5D%2C%201%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20max-%D0%BA%D1%83%D1%87%D1%83%0A%20%20%20%20max_heap%2C%20flag%20%3D%20%5B%5D%2C%20-1%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9C%D0%BE%D0%B4%D1%83%D0%BB%D1%8C%20heapq%20%D0%B2%20Python%20%D0%BF%D0%BE%20%D1%83%D0%BC%D0%BE%D0%BB%D1%87%D0%B0%D0%BD%D0%B8%D1%8E%20%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D1%83%D0%B5%D1%82%20min-%D0%BA%D1%83%D1%87%D1%83%0A%20%20%20%20%23%20%D0%95%D1%81%D0%BB%D0%B8%20%D0%BF%D0%B5%D1%80%D0%B5%D0%B4%20%D0%BF%D0%BE%D0%BC%D0%B5%D1%89%D0%B5%D0%BD%D0%B8%D0%B5%D0%BC%20%D0%B2%20%D0%BA%D1%83%D1%87%D1%83%20%D0%B1%D1%80%D0%B0%D1%82%D1%8C%20%D0%BE%D1%82%D1%80%D0%B8%D1%86%D0%B0%D0%BD%D0%B8%D0%B5%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%2C%20%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE%20%D0%BE%D0%B1%D1%80%D0%B0%D1%82%D0%B8%D1%82%D1%8C%20%D0%BE%D1%82%D0%BD%D0%BE%D1%88%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%BF%D0%BE%D1%80%D1%8F%D0%B4%D0%BA%D0%B0%20%D0%B8%20%D1%82%D0%B5%D0%BC%20%D1%81%D0%B0%D0%BC%D1%8B%D0%BC%20%D1%80%D0%B5%D0%B0%D0%BB%D0%B8%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20max-%D0%BA%D1%83%D1%87%D1%83%0A%20%20%20%20%23%20%D0%92%20%D1%8D%D1%82%D0%BE%D0%BC%20%D0%BF%D1%80%D0%B8%D0%BC%D0%B5%D1%80%D0%B5%20flag%20%3D%201%20%D1%81%D0%BE%D0%BE%D1%82%D0%B2%D0%B5%D1%82%D1%81%D1%82%D0%B2%D1%83%D0%B5%D1%82%20min-%D0%BA%D1%83%D1%87%D0%B5%2C%20%D0%B0%20flag%20%3D%20-1%20%D1%81%D0%BE%D0%BE%D1%82%D0%B2%D0%B5%D1%82%D1%81%D1%82%D0%B2%D1%83%D0%B5%D1%82%20max-%D0%BA%D1%83%D1%87%D0%B5%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BA%D1%83%D1%87%D1%83%0A%20%20%20%20heapq.heappush%28max_heap%2C%20flag%20%2A%201%29%0A%20%20%20%20heapq.heappush%28max_heap%2C%20flag%20%2A%203%29%0A%20%20%20%20heapq.heappush%28max_heap%2C%20flag%20%2A%202%29%0A%20%20%20%20heapq.heappush%28max_heap%2C%20flag%20%2A%205%29%0A%20%20%20%20heapq.heappush%28max_heap%2C%20flag%20%2A%204%29%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B2%D0%B5%D1%80%D1%85%D0%BD%D0%B8%D0%B9%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%BA%D1%83%D1%87%D0%B8%0A%20%20%20%20peek%20%3D%20flag%20%2A%20max_heap%5B0%5D%20%23%205%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D0%B2%D0%B5%D1%80%D1%85%D0%BD%D0%B8%D0%B9%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D0%BA%D1%83%D1%87%D0%B8%0A%20%20%20%20%23%20%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B5%20%D0%B8%D0%B7%20%D0%BA%D1%83%D1%87%D0%B8%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D1%8B%20%D0%BE%D0%B1%D1%80%D0%B0%D0%B7%D1%83%D1%8E%D1%82%20%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%D0%B4%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D0%BD%D0%BE%D1%81%D1%82%D1%8C%20%D0%BE%D1%82%20%D0%B1%D0%BE%D0%BB%D1%8C%D1%88%D0%B5%D0%B3%D0%BE%20%D0%BA%20%D0%BC%D0%B5%D0%BD%D1%8C%D1%88%D0%B5%D0%BC%D1%83%0A%20%20%20%20val%20%3D%20flag%20%2A%20heapq.heappop%28max_heap%29%20%23%205%0A%20%20%20%20val%20%3D%20flag%20%2A%20heapq.heappop%28max_heap%29%20%23%204%0A%20%20%20%20val%20%3D%20flag%20%2A%20heapq.heappop%28max_heap%29%20%23%203%0A%20%20%20%20val%20%3D%20flag%20%2A%20heapq.heappop%28max_heap%29%20%23%202%0A%20%20%20%20val%20%3D%20flag%20%2A%20heapq.heappop%28max_heap%29%20%23%201%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D1%80%D0%B0%D0%B7%D0%BC%D0%B5%D1%80%20%D0%BA%D1%83%D1%87%D0%B8%0A%20%20%20%20size%20%3D%20len%28max_heap%29%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%9F%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%B8%D1%82%D1%8C%2C%20%D0%BF%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D0%BA%D1%83%D1%87%D0%B0%0A%20%20%20%20is_empty%20%3D%20not%20max_heap%0A%20%20%20%20%0A%20%20%20%20%23%20%D0%92%D1%85%D0%BE%D0%B4%D1%81%D0%BF%D0%B8%D1%81%D0%BE%D0%BA%D0%B8%D0%BF%D0%BE%D1%81%D1%82%D1%80%D0%BE%D0%B5%D0%BD%D0%B8%D0%B5%20%D0%BA%D1%83%D1%87%D0%B8%0A%20%20%20%20min_heap%20%3D%20%5B1%2C%203%2C%202%2C%205%2C%204%5D%0A%20%20%20%20heapq.heapify%28min_heap%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_heap/heap/#812","title":"8.1.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u0443\u0447\u0438","text":"

\u041d\u0438\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430. \u0427\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0432 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439 \u043f\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \\(\\geq\\) \u043d\u0430 \\(\\leq\\) . \u0417\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.

"},{"location":"chapter_heap/heap/#1","title":"1. \u00a0 \u0425\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438","text":"

\u0412 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \"\u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\" \u043c\u044b \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438, \u0447\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u0443\u0447\u0430 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432.

\u041a\u043e\u0433\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c \u0443\u0437\u043b\u043e\u0432, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u044b - \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u043c \u044d\u0442\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435. \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \u043d\u0430 \u0443\u0437\u043b\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0444\u043e\u0440\u043c\u0443\u043b\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 8-2, \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \\(i\\) \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d \\(2i + 1\\) , \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 - \\(2i + 2\\) , \u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 - \\((i - 1) / 2\\) \u0441 \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435\u043c \u0432\u043d\u0438\u0437. \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u043c\u044b\u0435 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0443\u0437\u0435\u043b \u0438\u043b\u0438 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0438\u0435 \u0443\u0437\u043b\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-2 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438

\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0443\u043b\u044b \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u0431\u044b\u043b\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0438\u043c\u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
def left(self, i: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\"\"\"\n    return 2 * i + 1\n\ndef right(self, i: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\"\"\"\n    return 2 * i + 2\n\ndef parent(self, i: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430\"\"\"\n    return (i - 1) // 2  # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n
my_heap.cpp
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nint parent(int i) {\n    return (i - 1) / 2; // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.java
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nint parent(int i) {\n    return (i - 1) / 2; // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.cs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint Left(int i) {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint Right(int i) {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nint Parent(int i) {\n    return (i - 1) / 2; // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.go
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc (h *maxHeap) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc (h *maxHeap) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc (h *maxHeap) parent(i int) int {\n    // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n    return (i - 1) / 2\n}\n
my_heap.swift
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc left(i: Int) -> Int {\n    2 * i + 1\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc right(i: Int) -> Int {\n    2 * i + 2\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nfunc parent(i: Int) -> Int {\n    (i - 1) / 2 // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.js
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\n#left(i) {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\n#right(i) {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\n#parent(i) {\n    return Math.floor((i - 1) / 2); // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.ts
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nleft(i: number): number {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nright(i: number): number {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nparent(i: number): number {\n    return Math.floor((i - 1) / 2); // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.dart
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint _left(int i) {\n  return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint _right(int i) {\n  return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nint _parent(int i) {\n  return (i - 1) ~/ 2; // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.rs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfn left(i: usize) -> usize {\n    2 * i + 1\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfn right(i: usize) -> usize {\n    2 * i + 2\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nfn parent(i: usize) -> usize {\n    (i - 1) / 2 // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n
my_heap.c
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint left(MaxHeap *maxHeap, int i) {\n    return 2 * i + 1;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nint right(MaxHeap *maxHeap, int i) {\n    return 2 * i + 2;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nint parent(MaxHeap *maxHeap, int i) {\n    return (i - 1) / 2; // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n}\n
my_heap.kt
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfun left(i: Int): Int {\n    return 2 * i + 1\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 */\nfun right(i: Int): Int {\n    return 2 * i + 2\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 */\nfun parent(i: Int): Int {\n    return (i - 1) / 2 // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n}\n

```ruby title=\"my_heap.rb\"

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n```\n
"},{"location":"chapter_heap/heap/#2","title":"2. \u00a0 \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438","text":"

\u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 - \u044d\u0442\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043f\u0438\u0441\u043a\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
def peek(self) -> int:\n    \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438\"\"\"\n    return self.max_heap[0]\n
my_heap.cpp
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek() {\n    return maxHeap[0];\n}\n
my_heap.java
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek() {\n    return maxHeap.get(0);\n}\n
my_heap.cs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint Peek() {\n    return maxHeap[0];\n}\n
my_heap.go
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfunc (h *maxHeap) peek() any {\n    return h.data[0]\n}\n
my_heap.swift
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfunc peek() -> Int {\n    maxHeap[0]\n}\n
my_heap.js
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\npeek() {\n    return this.#maxHeap[0];\n}\n
my_heap.ts
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\npeek(): number {\n    return this.maxHeap[0];\n}\n
my_heap.dart
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek() {\n  return _maxHeap[0];\n}\n
my_heap.rs
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfn peek(&self) -> Option<i32> {\n    self.max_heap.first().copied()\n}\n
my_heap.c
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peek(MaxHeap *maxHeap) {\n    return maxHeap->data[0];\n}\n
my_heap.kt
/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfun peek(): Int {\n    return maxHeap[0]\n}\n

```ruby title=\"my_heap.rb\"

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n\n# ## \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 ###\ndef swap(i, j)\n  @max_heap[i], @max_heap[j] = @max_heap[j], @max_heap[i]\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 ###\ndef size\n  @max_heap.length\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 ###\ndef is_empty?\n  size == 0\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek\n  @max_heap[0]\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_heap/heap/#3","title":"3. \u00a0 \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443","text":"

\u041f\u0443\u0441\u0442\u044c \u0434\u0430\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 val . \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043c\u044b \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c \u0435\u0433\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0443\u0447\u0438. \u041f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u043a\u0443\u0447\u0438 \u043c\u043e\u0433\u0443\u0442 \u043d\u0430\u0440\u0443\u0448\u0438\u0442\u044c\u0441\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e val \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0434\u0440\u0443\u0433\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u0443\u0447\u0435. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043d\u0430 \u043f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043a \u043a\u043e\u0440\u043d\u044e ; \u044d\u0442\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f heapify, \u0442\u043e \u0435\u0441\u0442\u044c \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u0443\u0447\u0438.

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u043c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f; \u0435\u0441\u043b\u0438 \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u0431\u043e\u043b\u044c\u0448\u0435, \u0442\u043e \u043c\u0435\u043d\u044f\u0435\u043c \u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u043c\u0438. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0442\u0443 \u0436\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043f\u0443\u0442\u0438 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u043c \u0437\u0430 \u043a\u043e\u0440\u0435\u043d\u044c \u0438\u043b\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043c \u0443\u0437\u0435\u043b, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f.

<1><2><3><4><5><6><7><8><9>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-3 \u00a0 \u0428\u0430\u0433\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443

\u041f\u0443\u0441\u0442\u044c \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u043e \\(n\\) , \u0442\u043e\u0433\u0434\u0430 \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(O(\\log n)\\) . \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 heapify \u0442\u043e\u0436\u0435 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \\(O(\\log n)\\) . \u041e\u0442\u0441\u044e\u0434\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0440\u0430\u0432\u043d\u0430 \\(O(\\log n)\\) . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
def push(self, val: int):\n    \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443\"\"\"\n    # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    self.max_heap.append(val)\n    # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    self.sift_up(self.size() - 1)\n\ndef sift_up(self, i: int):\n    \"\"\"\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\"\"\"\n    while True:\n        # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        p = self.parent(i)\n        # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if p < 0 or self.max_heap[i] <= self.max_heap[p]:\n            break\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        self.swap(i, p)\n        # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p\n
my_heap.cpp
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid push(int val) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.push_back(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    siftUp(size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        int p = parent(i);\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(maxHeap[i], maxHeap[p]);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.java
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid push(int val) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.add(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    siftUp(size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nvoid siftUp(int i) {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        int p = parent(i);\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || maxHeap.get(i) <= maxHeap.get(p))\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.cs
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid Push(int val) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.Add(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    SiftUp(Size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nvoid SiftUp(int i) {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        int p = Parent(i);\n        // \u0415\u0441\u043b\u0438 \u00ab\u0432\u044b\u0445\u043e\u0434 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435\n        if (p < 0 || maxHeap[i] <= maxHeap[p])\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        Swap(i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.go
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfunc (h *maxHeap) push(val any) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    h.data = append(h.data, val)\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    h.siftUp(len(h.data) - 1)\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nfunc (h *maxHeap) siftUp(i int) {\n    for true {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        p := h.parent(i)\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if p < 0 || h.data[i].(int) <= h.data[p].(int) {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        h.swap(i, p)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p\n    }\n}\n
my_heap.swift
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfunc push(val: Int) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.append(val)\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    siftUp(i: size() - 1)\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nfunc siftUp(i: Int) {\n    var i = i\n    while true {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        let p = parent(i: i)\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if p < 0 || maxHeap[i] <= maxHeap[p] {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i: i, j: p)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p\n    }\n}\n
my_heap.js
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\npush(val) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    this.#maxHeap.push(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    this.#siftUp(this.size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\n#siftUp(i) {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        const p = this.#parent(i);\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || this.#maxHeap[i] <= this.#maxHeap[p]) break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        this.#swap(i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.ts
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\npush(val: number): void {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    this.maxHeap.push(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    this.siftUp(this.size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nsiftUp(i: number): void {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        const p = this.parent(i);\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || this.maxHeap[i] <= this.maxHeap[p]) break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        this.swap(i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.dart
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid push(int val) {\n  // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  _maxHeap.add(val);\n  // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n  siftUp(size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nvoid siftUp(int i) {\n  while (true) {\n    // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n    int p = _parent(i);\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n    if (p < 0 || _maxHeap[i] <= _maxHeap[p]) {\n      break;\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    _swap(i, p);\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n    i = p;\n  }\n}\n
my_heap.rs
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfn push(&mut self, val: i32) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    self.max_heap.push(val);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    self.sift_up(self.size() - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nfn sift_up(&mut self, mut i: usize) {\n    loop {\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 \u043a\u0443\u0447\u0438, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435\n        if i == 0 {\n            break;\n        }\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        let p = Self::parent(i);\n        // \u041a\u043e\u0433\u0434\u0430 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435\n        if self.max_heap[i] <= self.max_heap[p] {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        self.swap(i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.c
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid push(MaxHeap *maxHeap, int val) {\n    // \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043d\u0435 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0442\u0430\u043a \u043c\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u043e\u0432\n    if (maxHeap->size == MAX_SIZE) {\n        printf(\"heap is full!\");\n        return;\n    }\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap->data[maxHeap->size] = val;\n    maxHeap->size++;\n\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    siftUp(maxHeap, maxHeap->size - 1);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nvoid siftUp(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        int p = parent(maxHeap, i);\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || maxHeap->data[i] <= maxHeap->data[p]) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(maxHeap, i, p);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p;\n    }\n}\n
my_heap.kt
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfun push(_val: Int) {\n    // \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.add(_val)\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n    siftUp(size() - 1)\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 */\nfun siftUp(it: Int) {\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Kotlin \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\n    var i = it\n    while (true) {\n        // \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n        val p = parent(i)\n        // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n        if (p < 0 || maxHeap[i] <= maxHeap[p]) break\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i, p)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n        i = p\n    }\n}\n

```ruby title=\"my_heap.rb\"

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n\n# ## \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 ###\ndef swap(i, j)\n  @max_heap[i], @max_heap[j] = @max_heap[j], @max_heap[i]\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 ###\ndef size\n  @max_heap.length\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 ###\ndef is_empty?\n  size == 0\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek\n  @max_heap[0]\nend\n\n# ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 ###\ndef push(val)\n  # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  @max_heap << val\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n  sift_up(size - 1)\nend\n

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n\n# ## \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 ###\ndef swap(i, j)\n  @max_heap[i], @max_heap[j] = @max_heap[j], @max_heap[i]\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 ###\ndef size\n  @max_heap.length\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 ###\ndef is_empty?\n  size == 0\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek\n  @max_heap[0]\nend\n\n# ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 ###\ndef push(val)\n  # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  @max_heap << val\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n  sift_up(size - 1)\nend\n\n# ## \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 ###\ndef sift_up(i)\n  loop do\n    # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n    p = parent(i)\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    swap(i, p)\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n    i = p\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_heap/heap/#4","title":"4. \u00a0 \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438","text":"

\u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 - \u044d\u0442\u043e \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043f\u0438\u0441\u043a\u0430. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u0441\u044f, \u0438 \u044d\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u0442\u0440\u0443\u0434\u043d\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 heapify. \u0427\u0442\u043e\u0431\u044b \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439.

  1. \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0443 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0443\u0447\u0438, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u043c \u0443\u0437\u043b\u043e\u043c.
  2. \u041f\u043e\u0441\u043b\u0435 \u043e\u0431\u043c\u0435\u043d\u0430 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u0443\u0447\u0438 \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430. \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u0431\u043c\u0435\u043d \u0443\u0436\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d, \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438.
  3. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \"heapify \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\" \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \"heapify \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\". \u041c\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0434\u0432\u0443\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432, \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0438 \u043c\u0435\u043d\u044f\u0435\u043c \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c \u0443\u0437\u043b\u043e\u043c. \u0417\u0430\u0442\u0435\u043c \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0435\u043c \u0442\u0443 \u0436\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u043c \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438\u043b\u0438 \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u043c \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0436\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0431\u043c\u0435\u043d\u0430.

<1><2><3><4><5><6><7><8><9><10>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-4 \u00a0 \u0428\u0430\u0433\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438

\u041a\u0430\u043a \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043a\u0443\u0447\u0443, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438 \u0442\u0430\u043a\u0436\u0435 \u0438\u043c\u0435\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(\\log n)\\) . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby my_heap.py
def pop(self) -> int:\n    \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438\"\"\"\n    # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if self.is_empty():\n        raise IndexError(\"\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430\")\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    self.swap(0, self.size() - 1)\n    # \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val = self.max_heap.pop()\n    # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    self.sift_down(0)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val\n\ndef sift_down(self, i: int):\n    \"\"\"\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\"\"\"\n    while True:\n        # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        l, r, ma = self.left(i), self.right(i), i\n        if l < self.size() and self.max_heap[l] > self.max_heap[ma]:\n            ma = l\n        if r < self.size() and self.max_heap[r] > self.max_heap[ma]:\n            ma = r\n        # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i:\n            break\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        self.swap(i, ma)\n        # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n
my_heap.cpp
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nvoid pop() {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (isEmpty()) {\n        throw out_of_range(\"\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430\");\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    swap(maxHeap[0], maxHeap[size() - 1]);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    maxHeap.pop_back();\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(0);\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i)\n            break;\n        swap(maxHeap[i], maxHeap[ma]);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.java
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint pop() {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (isEmpty())\n        throw new IndexOutOfBoundsException();\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    swap(0, size() - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    int val = maxHeap.remove(size() - 1);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(0);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = left(i), r = right(i), ma = i;\n        if (l < size() && maxHeap.get(l) > maxHeap.get(ma))\n            ma = l;\n        if (r < size() && maxHeap.get(r) > maxHeap.get(ma))\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i)\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.cs
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint Pop() {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (IsEmpty())\n        throw new IndexOutOfRangeException();\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    Swap(0, Size() - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    int val = maxHeap.Last();\n    maxHeap.RemoveAt(Size() - 1);\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    SiftDown(0);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid SiftDown(int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = Left(i), r = Right(i), ma = i;\n        if (l < Size() && maxHeap[l] > maxHeap[ma])\n            ma = l;\n        if (r < Size() && maxHeap[r] > maxHeap[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b i \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0432\u044b\u0445\u043e\u0434 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\u00bb, \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435\n        if (ma == i) break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        Swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.go
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunc (h *maxHeap) pop() any {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if h.isEmpty() {\n        fmt.Println(\"error\")\n        return nil\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    h.swap(0, h.size()-1)\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val := h.data[len(h.data)-1]\n    h.data = h.data[:len(h.data)-1]\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    h.siftDown(0)\n\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunc (h *maxHeap) siftDown(i int) {\n    for true {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a max\n        l, r, max := h.left(i), h.right(i), i\n        if l < h.size() && h.data[l].(int) > h.data[max].(int) {\n            max = l\n        }\n        if r < h.size() && h.data[r].(int) > h.data[max].(int) {\n            max = r\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if max == i {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        h.swap(i, max)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = max\n    }\n}\n
my_heap.swift
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunc pop() -> Int {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if isEmpty() {\n        fatalError(\"\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430\")\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    swap(i: 0, j: size() - 1)\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    let val = maxHeap.remove(at: size() - 1)\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(i: 0)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunc siftDown(i: Int) {\n    var i = i\n    while true {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let l = left(i: i)\n        let r = right(i: i)\n        var ma = i\n        if l < size(), maxHeap[l] > maxHeap[ma] {\n            ma = l\n        }\n        if r < size(), maxHeap[r] > maxHeap[ma] {\n            ma = r\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i: i, j: ma)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n    }\n}\n
my_heap.js
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\npop() {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (this.isEmpty()) throw new Error('\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430');\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    this.#swap(0, this.size() - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    const val = this.#maxHeap.pop();\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    this.#siftDown(0);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\n#siftDown(i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        const l = this.#left(i),\n            r = this.#right(i);\n        let ma = i;\n        if (l < this.size() && this.#maxHeap[l] > this.#maxHeap[ma]) ma = l;\n        if (r < this.size() && this.#maxHeap[r] > this.#maxHeap[ma]) ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma === i) break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        this.#swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.ts
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\npop(): number {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (this.isEmpty()) throw new RangeError('Heap is empty.');\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    this.swap(0, this.size() - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    const val = this.maxHeap.pop();\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    this.siftDown(0);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nsiftDown(i: number): void {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        const l = this.left(i),\n            r = this.right(i);\n        let ma = i;\n        if (l < this.size() && this.maxHeap[l] > this.maxHeap[ma]) ma = l;\n        if (r < this.size() && this.maxHeap[r] > this.maxHeap[ma]) ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma === i) break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        this.swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.dart
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint pop() {\n  // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n  if (isEmpty()) throw Exception('\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430');\n  // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n  _swap(0, size() - 1);\n  // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  int val = _maxHeap.removeLast();\n  // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n  siftDown(0);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n  return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(int i) {\n  while (true) {\n    // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n    int l = _left(i);\n    int r = _right(i);\n    int ma = i;\n    if (l < size() && _maxHeap[l] > _maxHeap[ma]) ma = l;\n    if (r < size() && _maxHeap[r] > _maxHeap[ma]) ma = r;\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n    if (ma == i) break;\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    _swap(i, ma);\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n    i = ma;\n  }\n}\n
my_heap.rs
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfn pop(&mut self) -> i32 {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if self.is_empty() {\n        panic!(\"index out of bounds\");\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    self.swap(0, self.size() - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    let val = self.max_heap.pop().unwrap();\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    self.sift_down(0);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    val\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfn sift_down(&mut self, mut i: usize) {\n    loop {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let (l, r, mut ma) = (Self::left(i), Self::right(i), i);\n        if l < self.size() && self.max_heap[l] > self.max_heap[ma] {\n            ma = l;\n        }\n        if r < self.size() && self.max_heap[r] > self.max_heap[ma] {\n            ma = r;\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        self.swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n
my_heap.c
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint pop(MaxHeap *maxHeap) {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (isEmpty(maxHeap)) {\n        printf(\"heap is empty!\");\n        return INT_MAX;\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    swap(maxHeap, 0, size(maxHeap) - 1);\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    int val = maxHeap->data[maxHeap->size - 1];\n    maxHeap->size--;\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(maxHeap, 0);\n\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return val;\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(MaxHeap *maxHeap, int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a max\n        int l = left(maxHeap, i);\n        int r = right(maxHeap, i);\n        int max = i;\n        if (l < size(maxHeap) && maxHeap->data[l] > maxHeap->data[max]) {\n            max = l;\n        }\n        if (r < size(maxHeap) && maxHeap->data[r] > maxHeap->data[max]) {\n            max = r;\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (max == i) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(maxHeap, i, max);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = max;\n    }\n}\n
my_heap.kt
/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfun pop(): Int {\n    // \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n    if (isEmpty()) throw IndexOutOfBoundsException()\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    swap(0, size() - 1)\n    // \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val _val = maxHeap.removeAt(size() - 1)\n    // \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(0)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n    return _val\n}\n\n/* \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfun siftDown(it: Int) {\n    // \u041f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0432 Kotlin \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f\n    var i = it\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        val l = left(i)\n        val r = right(i)\n        var ma = i\n        if (l < size() && maxHeap[l] > maxHeap[ma]) ma = l\n        if (r < size() && maxHeap[r] > maxHeap[ma]) ma = r\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i) break\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(i, ma)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n    }\n}\n

```ruby title=\"my_heap.rb\"

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n\n# ## \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 ###\ndef swap(i, j)\n  @max_heap[i], @max_heap[j] = @max_heap[j], @max_heap[i]\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 ###\ndef size\n  @max_heap.length\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 ###\ndef is_empty?\n  size == 0\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek\n  @max_heap[0]\nend\n\n# ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 ###\ndef push(val)\n  # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  @max_heap << val\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n  sift_up(size - 1)\nend\n\n# ## \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 ###\ndef sift_up(i)\n  loop do\n    # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n    p = parent(i)\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    swap(i, p)\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n    i = p\n  end\nend\n\n# ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 ###\ndef pop\n  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n  raise IndexError, \"\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430\" if is_empty?\n  # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n  swap(0, size - 1)\n  # \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  val = @max_heap.pop\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n  sift_down(0)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n  val\nend\n

=begin File: my_heap.rb Created Time: 2024-04-19 Author: Blue Bean (lonnnnnnner@gmail.com) =end

require_relative '../utils/print_util'

# ## \u041c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430 ### class MaxHeap attr_reader :max_heap

# ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0439 \u043a\u0443\u0447\u0443 \u043f\u043e \u0432\u0445\u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443 ###\ndef initialize(nums)\n  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043a\u0443\u0447\u0443 \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439\n  @max_heap = nums\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  parent(size - 1).downto(0) do |i|\n    sift_down(i)\n  end\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef left(i)\n  2 * i + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef right(i)\n  2 * i + 2\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 ###\ndef parent(i)\n  (i - 1) / 2     # \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\nend\n\n# ## \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 ###\ndef swap(i, j)\n  @max_heap[i], @max_heap[j] = @max_heap[j], @max_heap[i]\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0443\u0447\u0438 ###\ndef size\n  @max_heap.length\nend\n\n# ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043a\u0443\u0447\u0430 ###\ndef is_empty?\n  size == 0\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek\n  @max_heap[0]\nend\n\n# ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 ###\ndef push(val)\n  # \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  @max_heap << val\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445\n  sift_up(size - 1)\nend\n\n# ## \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 ###\ndef sift_up(i)\n  loop do\n    # \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043b\u044f \u0443\u0437\u043b\u0430 i\n    p = parent(i)\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c heapify, \u043a\u043e\u0433\u0434\u0430 \u00ab\u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u00bb \u0438\u043b\u0438 \u00ab\u0443\u0437\u0435\u043b \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u00bb\n    break if p < 0 || @max_heap[i] <= @max_heap[p]\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    swap(i, p)\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u0432\u0435\u0440\u0445\n    i = p\n  end\nend\n\n# ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 ###\ndef pop\n  # \u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f\n  raise IndexError, \"\u043a\u0443\u0447\u0430 \u043f\u0443\u0441\u0442\u0430\" if is_empty?\n  # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n  swap(0, size - 1)\n  # \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  val = @max_heap.pop\n  # \u041f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n  sift_down(0)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u0443\u0447\u0438\n  val\nend\n\n# ## \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 ###\ndef sift_down(i)\n  loop do\n    # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n    l, r, ma = left(i), right(i), i\n    ma = l if l < size && @max_heap[l] > @max_heap[ma]\n    ma = r if r < size && @max_heap[r] > @max_heap[ma]\n\n    # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n    break if ma == i\n\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    swap(i, ma)\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n    i = ma\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_heap/heap/#813","title":"8.1.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043a\u0443\u0447\u0438","text":""},{"location":"chapter_heap/summary/","title":"8.4 \u00a0 \u041a\u0440\u0430\u0442\u043a\u0438\u0435 \u0438\u0442\u043e\u0433\u0438","text":""},{"location":"chapter_heap/summary/#1","title":"1. \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_heap/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u042f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \"\u043a\u0443\u0447\u0430\" \u043a\u0430\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0435\u043c \u0436\u0435 \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043d\u044f\u0442\u0438\u0435\u043c, \u0447\u0442\u043e \u0438 \"\u043a\u0443\u0447\u0430\" \u0432 \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u044c\u044e?

\u042d\u0442\u043e \u043d\u0435 \u043e\u0434\u043d\u043e \u0438 \u0442\u043e \u0436\u0435, \u043f\u0440\u043e\u0441\u0442\u043e \u0443 \u043d\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u0441\u043e\u0432\u043f\u0430\u043b\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435. \u041a\u0443\u0447\u0430 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438: \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 \u043a\u0443\u0447\u0435 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u044b. \u041a\u043e\u0433\u0434\u0430 \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b, \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u0435\u043a. \u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441\u043e \u0441\u0442\u0435\u043a\u043e\u0432\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0443\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432 \u043a\u0443\u0447\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u043e\u0441\u0442\u043e\u0440\u043e\u0436\u043d\u043e\u0441\u0442\u0438, \u0430 \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0443\u0442\u0435\u0447\u043a\u0430\u043c \u043f\u0430\u043c\u044f\u0442\u0438, \u0432\u0438\u0441\u044f\u0447\u0438\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c \u0438 \u0434\u0440\u0443\u0433\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c.

"},{"location":"chapter_heap/top_k/","title":"8.3 \u00a0 \u0417\u0430\u0434\u0430\u0447\u0430 Top-k","text":"

Question

\u0414\u0430\u043d \u043d\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\) . \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.

\u0414\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043a\u0430\u0436\u0435\u043c \u0434\u0432\u0430 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u0443\u0447\u0438.

"},{"location":"chapter_heap/top_k/#831-1","title":"8.3.1 \u00a0 \u041c\u0435\u0442\u043e\u0434 1: \u0432\u044b\u0431\u043e\u0440 \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u0445\u043e\u0434","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 8-6, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \\(k\\) \u043f\u0440\u043e\u0445\u043e\u0434\u043e\u0432 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0435 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \\(1\\)-\u0439, \\(2\\)-\u0439, \\(\\dots\\) , \\(k\\)-\u0439 \u043f\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442; \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u0440\u0430\u0432\u043d\u0430 \\(O(nk)\\) .

\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f \\(k \\ll n\\) , \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043a\u043e\u0433\u0434\u0430 \\(k\\) \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0430\u0435\u0442\u0441\u044f \u043a \\(n\\) , \u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u043a \\(O(n^2)\\) , \u0430 \u044d\u0442\u043e \u0443\u0436\u0435 \u043e\u0447\u0435\u043d\u044c \u0437\u0430\u0442\u0440\u0430\u0442\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-6 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u0445\u043e\u0434

Tip

\u041a\u043e\u0433\u0434\u0430 \\(k = n\\) , \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u043b\u043d\u0443\u044e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0438 \u0432 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c\".

"},{"location":"chapter_heap/top_k/#832-2","title":"8.3.2 \u00a0 \u041c\u0435\u0442\u043e\u0434 2: \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 8-7, \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 nums , \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u043a\u0440\u0430\u0439\u043d\u0438\u0435 \u043f\u0440\u0430\u0432\u044b\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432; \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) .

\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u044d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \"\u0434\u0435\u043b\u0430\u0435\u0442 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e\", \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-7 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0447\u0435\u0440\u0435\u0437 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443

"},{"location":"chapter_heap/top_k/#833-3","title":"8.3.3 \u00a0 \u041c\u0435\u0442\u043e\u0434 3: \u043a\u0443\u0447\u0430","text":"

\u0417\u0430\u0434\u0430\u0447\u0443 Top-k \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435.

  1. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.
  2. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043a\u0443\u0447\u0443 \u043f\u0435\u0440\u0432\u044b\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.
  3. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u043e\u043c\u0435\u0440 \\(k + 1\\) , \u0435\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0442\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043a\u0443\u0447\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.
  4. \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043e\u0431\u0445\u043e\u0434\u0430 \u0432 \u043a\u0443\u0447\u0435 \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u0440\u0430\u0437 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.
<1><2><3><4><5><6><7><8><9>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 8-8 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438

\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby top_k.py
def top_k_heap(nums: list[int], k: int) -> list[int]:\n    \"\"\"\u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    heap = []\n    # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for i in range(k):\n        heapq.heappush(heap, nums[i])\n    # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for i in range(k, len(nums)):\n        # \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if nums[i] > heap[0]:\n            heapq.heappop(heap)\n            heapq.heappush(heap, nums[i])\n    return heap\n
top_k.cpp
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\npriority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    priority_queue<int, vector<int>, greater<int>> heap;\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (int i = 0; i < k; i++) {\n        heap.push(nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (int i = k; i < nums.size(); i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > heap.top()) {\n            heap.pop();\n            heap.push(nums[i]);\n        }\n    }\n    return heap;\n}\n
top_k.java
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nQueue<Integer> topKHeap(int[] nums, int k) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    Queue<Integer> heap = new PriorityQueue<Integer>();\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (int i = 0; i < k; i++) {\n        heap.offer(nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (int i = k; i < nums.length; i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > heap.peek()) {\n            heap.poll();\n            heap.offer(nums[i]);\n        }\n    }\n    return heap;\n}\n
top_k.cs
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nPriorityQueue<int, int> TopKHeap(int[] nums, int k) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    PriorityQueue<int, int> heap = new();\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (int i = 0; i < k; i++) {\n        heap.Enqueue(nums[i], nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (int i = k; i < nums.Length; i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > heap.Peek()) {\n            heap.Dequeue();\n            heap.Enqueue(nums[i], nums[i]);\n        }\n    }\n    return heap;\n}\n
top_k.go
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfunc topKHeap(nums []int, k int) *minHeap {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    h := &minHeap{}\n    heap.Init(h)\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for i := 0; i < k; i++ {\n        heap.Push(h, nums[i])\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for i := k; i < len(nums); i++ {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if nums[i] > h.Top().(int) {\n            heap.Pop(h)\n            heap.Push(h, nums[i])\n        }\n    }\n    return h\n}\n
top_k.swift
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfunc topKHeap(nums: [Int], k: Int) -> [Int] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0435\u0435 \u043f\u043e \u043f\u0435\u0440\u0432\u044b\u043c k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\n    var heap = Heap(nums.prefix(k))\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for i in nums.indices.dropFirst(k) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if nums[i] > heap.min()! {\n            _ = heap.removeMin()\n            heap.insert(nums[i])\n        }\n    }\n    return heap.unordered\n}\n
top_k.js
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfunction pushMinHeap(maxHeap, val) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    maxHeap.push(-val);\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunction popMinHeap(maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -maxHeap.pop();\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfunction peekMinHeap(maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -maxHeap.peek();\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunction getMinHeap(maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return maxHeap.getMaxHeap().map((num) => -num);\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfunction topKHeap(nums, k) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438, \u0447\u0442\u043e\u0431\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 \u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e\n    const maxHeap = new MaxHeap([]);\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (let i = k; i < nums.length; i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438\n    return getMinHeap(maxHeap);\n}\n
top_k.ts
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nfunction pushMinHeap(maxHeap: MaxHeap, val: number): void {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    maxHeap.push(-val);\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunction popMinHeap(maxHeap: MaxHeap): number {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -maxHeap.pop();\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nfunction peekMinHeap(maxHeap: MaxHeap): number {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -maxHeap.peek();\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043a\u0443\u0447\u0438 */\nfunction getMinHeap(maxHeap: MaxHeap): number[] {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return maxHeap.getMaxHeap().map((num: number) => -num);\n}\n\n/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfunction topKHeap(nums: number[], k: number): number[] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438, \u0447\u0442\u043e\u0431\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 \u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e\n    const maxHeap = new MaxHeap([]);\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (let i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (let i = k; i < nums.length; i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438\n    return getMinHeap(maxHeap);\n}\n
top_k.dart
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nMinHeap topKHeap(List<int> nums, int k) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443, \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0432 \u0432 \u043d\u0435\u0435 \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  MinHeap heap = MinHeap(nums.sublist(0, k));\n  // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n  for (int i = k; i < nums.length; i++) {\n    // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n    if (nums[i] > heap.peek()) {\n      heap.pop();\n      heap.push(nums[i]);\n    }\n  }\n  return heap;\n}\n
top_k.rs
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {\n    // BinaryHeap \u2014 \u044d\u0442\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u043a\u0443\u0447\u0430; \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Reverse \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043a\u0443\u0447\u0443\n    let mut heap = BinaryHeap::<Reverse<i32>>::new();\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for &num in nums.iter().take(k) {\n        heap.push(Reverse(num));\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for &num in nums.iter().skip(k) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if num > heap.peek().unwrap().0 {\n            heap.pop();\n            heap.push(Reverse(num));\n        }\n    }\n    heap\n}\n
top_k.c
/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 */\nvoid pushMinHeap(MaxHeap *maxHeap, int val) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    push(maxHeap, -val);\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint popMinHeap(MaxHeap *maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -pop(maxHeap);\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 */\nint peekMinHeap(MaxHeap *maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return -peek(maxHeap);\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043a\u0443\u0447\u0438 */\nint *getMinHeap(MaxHeap *maxHeap) {\n    // \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0445 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    int *res = (int *)malloc(maxHeap->size * sizeof(int));\n    for (int i = 0; i < maxHeap->size; i++) {\n        res[i] = -maxHeap->data[i];\n    }\n    return res;\n}\n\n// \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u0443\u0447\u0438\nint *topKHeap(int *nums, int sizeNums, int k) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    // \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438, \u0447\u0442\u043e\u0431\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 \u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e\n    int *empty = (int *)malloc(0);\n    MaxHeap *maxHeap = newMaxHeap(empty, 0);\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (int i = 0; i < k; i++) {\n        pushMinHeap(maxHeap, nums[i]);\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (int i = k; i < sizeNums; i++) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > peekMinHeap(maxHeap)) {\n            popMinHeap(maxHeap);\n            pushMinHeap(maxHeap, nums[i]);\n        }\n    }\n    int *res = getMinHeap(maxHeap);\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    delMaxHeap(maxHeap);\n    return res;\n}\n
top_k.kt
/* \u041d\u0430\u0439\u0442\u0438 k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 */\nfun topKHeap(nums: IntArray, k: Int): Queue<Int> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n    val heap = PriorityQueue<Int>()\n    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n    for (i in 0..<k) {\n        heap.offer(nums[i])\n    }\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n    for (i in k..<nums.size) {\n        // \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n        if (nums[i] > heap.peek()) {\n            heap.poll()\n            heap.offer(nums[i])\n        }\n    }\n    return heap\n}\n
top_k.rb
=begin\nFile: top_k.rb\nCreated Time: 2024-04-19\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\nrequire_relative \"./my_heap\"\n\n# ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u0443\u0447\u0443 ###\ndef push_min_heap(heap, val)\n  # \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  heap.push(-val)\nend\n\n# ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u0443\u0447\u0438 ###\ndef pop_min_heap(heap)\n  # \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  -heap.pop\nend\n\n# ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 ###\ndef peek_min_heap(heap)\n  # \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  -heap.peek\nend\n\n# ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043a\u0443\u0447\u0438 ###\ndef get_min_heap(heap)\n  # \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438\n  heap.max_heap.map { |x| -x }\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a k \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u0443\u0447\u0438 ###\ndef top_k_heap(nums, k)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438\n  # \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u044b \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u0443\u0447\u0438, \u0447\u0442\u043e\u0431\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u043a\u0443\u0447\u0438 \u0438\u043c\u0438\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e\n  max_heap = MaxHeap.new([])\n\n  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0435 k \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0443\u0447\u0443\n  for i in 0...k\n    push_min_heap(max_heap, nums[i])\n  end\n\n  # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 k+1, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u043e\u0439 k\n  for i in k...nums.length\n    # \u0415\u0441\u043b\u0438 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438, \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043a\u0443\u0447\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043a\u0443\u0447\u0443\n    if nums[i] > peek_min_heap(max_heap)\n      pop_min_heap(max_heap)\n      push_min_heap(max_heap, nums[i])\n    end\n  end\n\n  get_min_heap(max_heap)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412\u0441\u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(n\\) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043a\u0443\u0447\u0438, \u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 \\(k\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log k)\\) . \u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d: \u043a\u043e\u0433\u0434\u0430 \\(k\\) \u043c\u0430\u043b\u043e, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u043a \\(O(n)\\) ; \u043a\u043e\u0433\u0434\u0430 \\(k\\) \u0432\u0435\u043b\u0438\u043a\u043e, \u043e\u043d\u0430 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \\(O(n \\log n)\\) .

\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0438 \u0434\u043b\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0435\u0432 \u0441 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u043f\u043e\u0442\u043e\u043a\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0445. \u041f\u0440\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u043c \u043f\u043e\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0438 \u043d\u043e\u0432\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u043a\u0443\u0447\u0438, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044f \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0438\u0435 \\(k\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.

"},{"location":"chapter_hello_algo/","title":"\u041f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c","text":"

\u041d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043b\u0435\u0442 \u043d\u0430\u0437\u0430\u0434 \u044f \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b \u043d\u0430 LeetCode \u0440\u0430\u0437\u0431\u043e\u0440\u044b \u0441\u0435\u0440\u0438\u0438 \u0437\u0430\u0434\u0430\u0447 \"Sword for Offer\" \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0443 \u0438 \u043e\u0431\u043e\u0434\u0440\u0435\u043d\u0438\u0435 \u043e\u0442 \u043c\u043d\u043e\u0433\u0438\u0445 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0441 \u043d\u0438\u043c\u0438 \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043c\u043d\u0435 \u0437\u0430\u0434\u0430\u0432\u0430\u043b\u0438 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0432\u043e\u043f\u0440\u043e\u0441: \"\u043a\u0430\u043a \u043d\u0430\u0447\u0430\u0442\u044c \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b\". \u041f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441 \u043d\u0430\u0447\u0430\u043b \u043c\u0435\u043d\u044f \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c.

\u0421\u043b\u0435\u043f\u043e \u0431\u0440\u043e\u0441\u0430\u0442\u044c\u0441\u044f \u0432 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c: \u043e\u043d \u043f\u0440\u043e\u0441\u0442, \u043f\u0440\u044f\u043c\u043e\u043b\u0438\u043d\u0435\u0435\u043d \u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u041d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447 \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u0438\u0433\u0440\u0443 \u0432 \"\u0421\u0430\u043f\u0435\u0440\u0430\": \u043b\u044e\u0434\u0438 \u0441 \u0441\u0438\u043b\u044c\u043d\u044b\u043c\u0438 \u043d\u0430\u0432\u044b\u043a\u0430\u043c\u0438 \u0441\u0430\u043c\u043e\u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u043e\u0431\u0435\u0437\u0432\u0440\u0435\u0434\u0438\u0442\u044c \u043c\u0438\u043d\u044b \u043e\u0434\u043d\u0443 \u0437\u0430 \u0434\u0440\u0443\u0433\u043e\u0439, \u0430 \u0442\u0435\u043c, \u0443 \u043a\u043e\u0433\u043e \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0431\u0430\u0437\u044b, \u043b\u0435\u0433\u043a\u043e \u043d\u0430\u0431\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u0448\u0438\u0448\u043a\u0438 \u0438 \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u043e\u0442\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043f\u043e\u0434 \u0434\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c \u043d\u0435\u0443\u0434\u0430\u0447. \u041f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0438 \u0442\u043e\u0436\u0435 \u043f\u0440\u0438\u043d\u044f\u0442\u043e \u0447\u0430\u0441\u0442\u043e, \u043d\u043e \u0434\u043b\u044f \u0442\u0435\u0445, \u043a\u0442\u043e \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u0441\u044f \u043a \u043f\u043e\u0438\u0441\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b, \u0434\u0438\u043f\u043b\u043e\u043c, \u0440\u0435\u0437\u044e\u043c\u0435, \u043f\u0438\u0441\u044c\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0441\u0442\u044b \u0438 \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0443\u0436\u0435 \u043e\u0442\u043d\u0438\u043c\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0447\u0430\u0441\u0442\u044c \u0441\u0438\u043b, \u0438 \u043f\u043e\u0442\u043e\u043c\u0443 \u0442\u043e\u043b\u0441\u0442\u044b\u0435 \u043a\u043d\u0438\u0433\u0438 \u043d\u0435\u0440\u0435\u0434\u043a\u043e \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u044f\u0436\u0435\u043b\u043e\u0435 \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435.

\u0415\u0441\u043b\u0438 \u0442\u044b \u0442\u043e\u0436\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0448\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u044f\u043c\u0438, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u0441\u0430\u043c\u0430 \"\u043d\u0430\u0448\u043b\u0430\" \u0442\u0435\u0431\u044f. \u041e\u043d\u0430 \u0441\u0442\u0430\u043b\u0430 \u043c\u043e\u0438\u043c \u043e\u0442\u0432\u0435\u0442\u043e\u043c \u043d\u0430 \u044d\u0442\u043e\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u043f\u0443\u0441\u0442\u044c \u043d\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u043c, \u043d\u043e \u043a\u0430\u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c \u0447\u0435\u0441\u0442\u043d\u043e\u0439 \u0438 \u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u043e\u0439. \u042d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u0441\u0430\u043c\u0430 \u043f\u043e \u0441\u0435\u0431\u0435 \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0444\u0444\u0435\u0440, \u043d\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0442\u0435\u0431\u0435 \u0443\u0432\u0438\u0434\u0435\u0442\u044c \"\u043a\u0430\u0440\u0442\u0443 \u0437\u043d\u0430\u043d\u0438\u0439\" \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c, \u043f\u043e\u043d\u044f\u0442\u044c \u0444\u043e\u0440\u043c\u0443, \u0440\u0430\u0437\u043c\u0435\u0440 \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043d\u044b\u0445 \"\u043c\u0438\u043d\" \u0438 \u043e\u0441\u0432\u043e\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \"\u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0440\u0430\u0437\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f\". \u041e\u0441\u0432\u043e\u0438\u0432 \u044d\u0442\u043e, \u0442\u044b \u0441\u043c\u043e\u0436\u0435\u0448\u044c \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u0435\u0435 \u0440\u0435\u0448\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0447\u0438\u0442\u0430\u0442\u044c \u0442\u0435\u0445\u043d\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b, \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u0432\u044b\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u044f \u0446\u0435\u043b\u043e\u0441\u0442\u043d\u0443\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0437\u043d\u0430\u043d\u0438\u0439.

\u042f \u0433\u043b\u0443\u0431\u043e\u043a\u043e \u0441\u043e\u0433\u043b\u0430\u0441\u0435\u043d \u0441\u043e \u0441\u043b\u043e\u0432\u0430\u043c\u0438 \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u043e\u0440\u0430 \u0424\u0435\u0439\u043d\u043c\u0430\u043d\u0430: \"Knowledge isn't free. You have to pay attention.\" \u0412 \u044d\u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435 \u043a\u043d\u0438\u0433\u0430 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \"\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\". \u0427\u0442\u043e\u0431\u044b \u043d\u0435 \u043f\u043e\u0434\u0432\u0435\u0441\u0442\u0438 \u0442\u043e \u0434\u0440\u0430\u0433\u043e\u0446\u0435\u043d\u043d\u043e\u0435 \"\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\", \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0442\u044b \u0435\u0439 \u0443\u0434\u0435\u043b\u0438\u0448\u044c, \u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u0432\u043b\u043e\u0436\u0438\u0442\u044c \u0432 \u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \"\u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044f\".

\u042f \u0445\u043e\u0440\u043e\u0448\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u044e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u043d\u0438\u0439. \u0425\u043e\u0442\u044f \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u0443\u0436\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e \u0448\u043b\u0438\u0444\u043e\u0432\u0430\u043b\u0441\u044f, \u0432 \u043d\u0435\u043c \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u0430\u043b\u043e \u043e\u0448\u0438\u0431\u043e\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0438\u0441\u043a\u0440\u0435\u043d\u043d\u0435 \u043f\u0440\u043e\u0448\u0443 \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430 \u043d\u0435\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0438 \u043d\u0435\u0434\u043e\u0440\u0430\u0431\u043e\u0442\u043a\u0438.

Hello, \u0410\u043b\u0433\u043e!

\u041f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432 \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e \u043c\u0438\u0440. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0438 \u043e\u0442\u043b\u0438\u0447\u043d\u043e\u0439 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u0443\u0435\u043c\u043e\u0441\u0442\u0438 \u043e\u043d\u0438 \u0441\u0442\u0430\u043b\u0438 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0440\u0435\u0434\u043e\u0439 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0420\u0435\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u0430\u044f \u0433\u0440\u0430\u0444\u0438\u043a\u0430 \u0432 \u0438\u0433\u0440\u0430\u0445, \u0438\u043d\u0442\u0435\u043b\u043b\u0435\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u0430\u0432\u0442\u043e\u043d\u043e\u043c\u043d\u043e\u043c \u0432\u043e\u0436\u0434\u0435\u043d\u0438\u0438, \u0432\u043f\u0435\u0447\u0430\u0442\u043b\u044f\u044e\u0449\u0438\u0435 \u043f\u0430\u0440\u0442\u0438\u0438 AlphaGo \u0438 \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 ChatGPT: \u0432\u0441\u0435 \u044d\u0442\u043e \u0438\u0437\u044f\u0449\u043d\u044b\u0435 \u043f\u0440\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u0435.

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0435\u0449\u0435 \u0434\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0438 \u0432\u043e \u0432\u0441\u0435\u0445 \u0443\u0433\u043e\u043b\u043a\u0430\u0445 \u043c\u0438\u0440\u0430. \u0420\u0430\u043d\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0431\u044b\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u044b\u043c\u0438: \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0440\u0435\u0432\u043d\u0438\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u044b \u0441\u0447\u0435\u0442\u0430 \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u043f\u0440\u0438 \u0438\u0437\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432. \u041f\u043e \u043c\u0435\u0440\u0435 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044f \u0446\u0438\u0432\u0438\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043b\u0438\u0441\u044c \u0442\u043e\u043d\u044c\u0448\u0435 \u0438 \u0441\u043b\u043e\u0436\u043d\u0435\u0435. \u0417\u0430 \u043c\u0430\u0441\u0442\u0435\u0440\u0441\u0442\u0432\u043e\u043c \u0440\u0435\u043c\u0435\u0441\u043b\u0435\u043d\u043d\u0438\u043a\u043e\u0432, \u043f\u0440\u043e\u043c\u044b\u0448\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u043f\u0440\u043e\u0434\u0443\u043a\u0442\u0430\u043c\u0438, \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u044e\u0449\u0438\u043c\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0441\u0438\u043b\u044b, \u0438 \u0434\u0430\u0436\u0435 \u0437\u0430 \u043d\u0430\u0443\u0447\u043d\u044b\u043c\u0438 \u0437\u0430\u043a\u043e\u043d\u0430\u043c\u0438 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0412\u0441\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0442\u043e\u0438\u0442 \u0438\u0437\u043e\u0431\u0440\u0435\u0442\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u043c\u044b\u0441\u043b\u044c.

\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0432\u0441\u044e\u0434\u0443: \u043e\u0442 \u0441\u043e\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u0435\u0442\u0435\u0439 \u0434\u043e \u0441\u0445\u0435\u043c \u043c\u0435\u0442\u0440\u043e \u043c\u043d\u043e\u0433\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043c\u043e\u0436\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \"\u0433\u0440\u0430\u0444\"; \u043e\u0442 \u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0430 \u0434\u043e \u0441\u0435\u043c\u044c\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u044b \u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438 \"\u0434\u0435\u0440\u0435\u0432\u0430\"; \u0437\u0438\u043c\u043d\u044f\u044f \u043e\u0434\u0435\u0436\u0434\u0430 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \"\u0441\u0442\u0435\u043a\", \u0433\u0434\u0435 \u0442\u043e, \u0447\u0442\u043e \u043d\u0430\u0434\u0435\u0432\u0430\u044e\u0442 \u043f\u0435\u0440\u0432\u044b\u043c, \u0441\u043d\u0438\u043c\u0430\u044e\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c; \u0442\u0443\u0431\u0443\u0441 \u0434\u043b\u044f \u0431\u0430\u0434\u043c\u0438\u043d\u0442\u043e\u043d\u043d\u044b\u0445 \u0432\u043e\u043b\u0430\u043d\u043e\u0432 \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \"\u043e\u0447\u0435\u0440\u0435\u0434\u044c\", \u0433\u0434\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0430 \u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u044e\u0442\u0441\u044f \u0441 \u0434\u0440\u0443\u0433\u043e\u0433\u043e; \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \"\u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443\", \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0443\u044e \u0431\u044b\u0441\u0442\u0440\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0443\u0436\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e.

\u042d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u0445 \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0439 \u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u043a\u043e\u0434\u0430 \u043f\u043e\u043c\u043e\u0447\u044c \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044e \u043f\u043e\u043d\u044f\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0438\u0434\u0435\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e. \u041d\u0430 \u044d\u0442\u043e\u0439 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043d\u0438\u0433\u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0436\u0438\u0432\u044b\u0435 \u043f\u0440\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0432 \u0441\u043b\u043e\u0436\u043d\u043e\u043c \u043c\u0438\u0440\u0435 \u0438 \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044c \u0438\u0445 \u043a\u0440\u0430\u0441\u043e\u0442\u0443. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043e\u043d\u0430 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0442\u0435\u0431\u044f \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439.

"},{"location":"chapter_introduction/","title":"\u0413\u043b\u0430\u0432\u0430 1. \u00a0 \u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b","text":"

Abstract

\u042e\u043d\u0430\u044f \u0434\u0435\u0432\u0443\u0448\u043a\u0430 \u043b\u0435\u0433\u043a\u043e \u043a\u0440\u0443\u0436\u0438\u0442\u0441\u044f \u0432 \u0442\u0430\u043d\u0446\u0435 \u0441\u0440\u0435\u0434\u0438 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u043f\u043e\u0434\u043e\u043b \u0435\u0435 \u043f\u043b\u0430\u0442\u044c\u044f \u0441\u0442\u0440\u0443\u0438\u0442\u0441\u044f \u043c\u0435\u043b\u043e\u0434\u0438\u0435\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.

\u041e\u043d\u0430 \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u0442 \u0442\u0435\u0431\u044f \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c\u0441\u044f \u043a \u0442\u0430\u043d\u0446\u0443: \u0441\u043b\u0435\u0434\u0443\u0439 \u0437\u0430 \u0435\u0435 \u0448\u0430\u0433\u0430\u043c\u0438 \u0438 \u0432\u043e\u0439\u0434\u0438 \u0432 \u043c\u0438\u0440 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432, \u043f\u043e\u043b\u043d\u044b\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0438 \u043a\u0440\u0430\u0441\u043e\u0442\u044b.

"},{"location":"chapter_introduction/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_introduction/algorithms_are_everywhere/","title":"1.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0432\u0441\u044e\u0434\u0443","text":"

\u041a\u043e\u0433\u0434\u0430 \u043c\u044b \u0441\u043b\u044b\u0448\u0438\u043c \u0441\u043b\u043e\u0432\u043e \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\", \u043c\u044b \u0435\u0441\u0442\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u0443\u043c\u0430\u0435\u043c \u043e \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u043d\u0430 \u0434\u0435\u043b\u0435 \u043c\u043d\u043e\u0433\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043c\u0430\u0442\u0435\u043c\u0430\u0442\u0438\u043a\u043e\u0439, \u0430 \u0432 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043e\u043f\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0431\u0430\u0437\u043e\u0432\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043f\u043e\u0432\u0441\u044e\u0434\u0443 \u0432 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438.

\u041f\u0440\u0435\u0436\u0434\u0435 \u0447\u0435\u043c \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u0440\u0430\u0437\u0433\u043e\u0432\u043e\u0440\u0443 \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445, \u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u043b\u044e\u0431\u043e\u043f\u044b\u0442\u043d\u044b\u043c \u0444\u0430\u043a\u0442\u043e\u043c: \u0442\u044b \u0443\u0436\u0435 \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0434\u043b\u044f \u0441\u0435\u0431\u044f \u043e\u0441\u0432\u043e\u0438\u043b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0438 \u043f\u0440\u0438\u0432\u044b\u043a \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0445 \u0432 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0439 \u0436\u0438\u0437\u043d\u0438. \u041d\u0438\u0436\u0435 \u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c.

\u041f\u0440\u0438\u043c\u0435\u0440 1: \u043f\u043e\u0438\u0441\u043a \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435. \u0412 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u0435 \u0441\u043b\u043e\u0432\u0430 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0441\u043b\u043e\u0432\u043e, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0435\u0435\u0441\u044f \u043d\u0430 \u0431\u0443\u043a\u0432\u0443 \\(r\\); \u043e\u0431\u044b\u0447\u043d\u043e \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435.

  1. \u041e\u0442\u043a\u0440\u043e\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043f\u043e\u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438, \u0441 \u043a\u0430\u043a\u043e\u0439 \u0431\u0443\u043a\u0432\u044b \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430; \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u044d\u0442\u043e \u0431\u0443\u043a\u0432\u0430 \\(m\\).
  2. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0435 \\(r\\) \u0438\u0434\u0435\u0442 \u043f\u043e\u0441\u043b\u0435 \\(m\\), \u043f\u0435\u0440\u0432\u0443\u044e \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u0431\u0440\u043e\u0441\u0438\u0442\u044c, \u0438 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0443\u0437\u0438\u0442\u0441\u044f \u0434\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b.
  3. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0439 \u0448\u0430\u0433\u0438 1. \u0438 2. \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0448\u044c \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0441\u043b\u043e\u0432\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0431\u0443\u043a\u0432\u044b \\(r\\).
<1><2><3><4><5>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1-1 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435

\u041f\u043e\u0438\u0441\u043a \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435, \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043d\u0430\u0432\u044b\u043a \u0434\u043b\u044f \u0448\u043a\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432, \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0438 \u0435\u0441\u0442\u044c \u0437\u043d\u0430\u043c\u0435\u043d\u0438\u0442\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \"\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430\". \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \"\u043c\u0430\u0441\u0441\u0438\u0432\"; \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u043f\u0440\u0438 \u043f\u043e\u0438\u0441\u043a\u0435 \u0441\u043b\u043e\u0432\u0430 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \"\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430\".

\u041f\u0440\u0438\u043c\u0435\u0440 2: \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u043a\u0430\u0440\u0442. \u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0438\u0433\u0440\u044b \u0432 \u043a\u0430\u0440\u0442\u044b \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0440\u0442\u044b \u0432 \u0440\u0443\u043a\u0435 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e; \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435.

  1. \u0420\u0430\u0437\u0434\u0435\u043b\u0438 \u043a\u0430\u0440\u0442\u044b \u043d\u0430 \"\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0443\u044e\" \u0438 \"\u043d\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0443\u044e\" \u0447\u0430\u0441\u0442\u0438 \u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438, \u0447\u0442\u043e \u0432 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u0430\u043c\u0430\u044f \u043b\u0435\u0432\u0430\u044f \u043a\u0430\u0440\u0442\u0430 \u0443\u0436\u0435 \u0441\u0442\u043e\u0438\u0442 \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435.
  2. \u0412\u043e\u0437\u044c\u043c\u0438 \u043e\u0434\u043d\u0443 \u043a\u0430\u0440\u0442\u0443 \u0438\u0437 \u043d\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u044c \u0435\u0435 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043d\u0443\u0442\u0440\u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438; \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0434\u0432\u0435 \u0441\u0430\u043c\u044b\u0435 \u043b\u0435\u0432\u044b\u0435 \u043a\u0430\u0440\u0442\u044b \u0443\u0436\u0435 \u0431\u0443\u0434\u0443\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u044b.
  3. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0439 \u0448\u0430\u0433 2. , \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u044f \u043e\u0434\u043d\u0443 \u043a\u0430\u0440\u0442\u0443 \u0438\u0437 \u043d\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u0432 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0443\u044e, \u043f\u043e\u043a\u0430 \u0432\u0441\u0435 \u043a\u0430\u0440\u0442\u044b \u043d\u0435 \u043e\u043a\u0430\u0436\u0443\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1-2 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043a\u043e\u043b\u043e\u0434\u044b \u043a\u0430\u0440\u0442

\u041e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 \u0440\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0442\u044c \u043a\u0430\u0440\u0442\u044b \u043f\u043e \u0441\u0443\u0442\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438\", \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u0435\u043d \u043d\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043d\u0430\u0431\u043e\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445. \u0412\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0444\u0443\u043d\u043a\u0446\u0438\u044f\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c.

\u041f\u0440\u0438\u043c\u0435\u0440 3: \u0432\u044b\u0434\u0430\u0447\u0430 \u0441\u0434\u0430\u0447\u0438. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0432 \u0441\u0443\u043f\u0435\u0440\u043c\u0430\u0440\u043a\u0435\u0442\u0435 \u043c\u044b \u043a\u0443\u043f\u0438\u043b\u0438 \u0442\u043e\u0432\u0430\u0440 \u043d\u0430 \\(69\\) \u0438 \u0434\u0430\u043b\u0438 \u043a\u0430\u0441\u0441\u0438\u0440\u0443 \\(100\\), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u043c \\(31\\) \u0441\u0434\u0430\u0447\u0438. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 1-3.

  1. \u0414\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b - \u044d\u0442\u043e \u043a\u0443\u043f\u044e\u0440\u044b \u0434\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432\u043e\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \\(31\\), \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \\(1\\), \\(5\\), \\(10\\) \u0438 \\(20\\).
  2. \u0412\u043e\u0437\u044c\u043c\u0438 \u0441\u0430\u043c\u0443\u044e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043a\u0443\u043f\u044e\u0440\u0443 \u0438\u0437 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445, \u0442\u043e \u0435\u0441\u0442\u044c \\(20\\), \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \\(31 - 20 = 11\\).
  3. \u0412\u043e\u0437\u044c\u043c\u0438 \u0441\u0430\u043c\u0443\u044e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043a\u0443\u043f\u044e\u0440\u0443 \u0438\u0437 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f, \u0442\u043e \u0435\u0441\u0442\u044c \\(10\\), \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \\(11 - 10 = 1\\).
  4. \u0412\u043e\u0437\u044c\u043c\u0438 \u0441\u0430\u043c\u0443\u044e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043a\u0443\u043f\u044e\u0440\u0443 \u0438\u0437 \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f, \u0442\u043e \u0435\u0441\u0442\u044c \\(1\\), \u0442\u043e\u0433\u0434\u0430 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \\(1 - 1 = 0\\).
  5. \u0412\u044b\u0434\u0430\u0447\u0430 \u0441\u0434\u0430\u0447\u0438 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430, \u0438\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044f: \\(20 + 10 + 1 = 31\\).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1-3 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u044b\u0434\u0430\u0447\u0438 \u0441\u0434\u0430\u0447\u0438

\u0412 \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0445 \u0448\u0430\u0433\u0430\u0445 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0438\u0437 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0432 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u0442\u043e \u0435\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u0443\u043f\u044e\u0440\u0430 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0433\u043e \u043d\u043e\u043c\u0438\u043d\u0430\u043b\u0430; \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0431\u043e\u0447\u0430\u044f \u0441\u0445\u0435\u043c\u0430 \u0432\u044b\u0434\u0430\u0447\u0438 \u0441\u0434\u0430\u0447\u0438. \u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u0436\u0430\u0434\u043d\u044b\u043c\" \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c.

\u041e\u0442 \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0434\u044b \u0434\u043e \u043c\u0435\u0436\u0437\u0432\u0435\u0437\u0434\u043d\u044b\u0445 \u043f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u0439 \u043f\u043e\u0447\u0442\u0438 \u043b\u044e\u0431\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438. \u041f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043e\u0432 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u043d\u0430\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442 CPU \u0438 GPU \u0434\u043b\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0432 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440 \u0438 \u0440\u0435\u0448\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0435 \u0440\u0430\u0437\u043d\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

Tip

\u0415\u0441\u043b\u0438 \u0442\u044b \u0432\u0441\u0435 \u0435\u0449\u0435 \u0441\u043c\u0443\u0442\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0448\u044c \u0441\u0435\u0431\u0435 \u0442\u0430\u043a\u0438\u0435 \u043f\u043e\u043d\u044f\u0442\u0438\u044f, \u043a\u0430\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0439 \u0447\u0438\u0442\u0430\u0442\u044c. \u042d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u0432\u0432\u0435\u0434\u0435\u0442 \u0442\u0435\u0431\u044f \u0432 \u043c\u0438\u0440 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.

"},{"location":"chapter_introduction/summary/","title":"1.3 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_introduction/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_introduction/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u042f \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u0438 \u0432 \u043f\u043e\u0432\u0441\u0435\u0434\u043d\u0435\u0432\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0440\u0435\u0448\u0430\u043b \u0437\u0430\u0434\u0430\u0447\u0438 \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438\": \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0443\u0436\u0435 \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0438 \u0438\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e. \u0417\u043d\u0430\u0447\u0438\u0442 \u043b\u0438 \u044d\u0442\u043e, \u0447\u0442\u043e \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0435\u0449\u0435 \u043d\u0435 \u0434\u043e\u0448\u043b\u0438 \u0434\u043e \u0443\u0440\u043e\u0432\u043d\u044f, \u0433\u0434\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0443\u0436\u043d\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b?

\u0415\u0441\u043b\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0440\u0430\u0431\u043e\u0447\u0438\u0435 \u043d\u0430\u0432\u044b\u043a\u0438 \u0441 \"\u043f\u0440\u0438\u0435\u043c\u0430\u043c\u0438\" \u0432 \u0431\u043e\u0435\u0432\u044b\u0445 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0430\u0445, \u0442\u043e \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0434\u0438\u0441\u0446\u0438\u043f\u043b\u0438\u043d\u044b \u0441\u043a\u043e\u0440\u0435\u0435 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u044e\u0442 \"\u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044e\u044e \u0441\u0438\u043b\u0443\".

\u042f \u0441\u0447\u0438\u0442\u0430\u044e, \u0447\u0442\u043e \u0441\u043c\u044b\u0441\u043b \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 (\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0444\u0443\u043d\u0434\u0430\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u0438\u0441\u0446\u0438\u043f\u043b\u0438\u043d) \u043d\u0435 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u0441 \u043d\u0443\u043b\u044f \u0432 \u0440\u0430\u0431\u043e\u0442\u0435, \u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b, \u043e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u043d\u0438\u044f, \u0443\u043c\u0435\u0442\u044c \u043f\u0440\u043e\u0444\u0435\u0441\u0441\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u0437\u0430\u0434\u0430\u0447, \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u0432\u044b\u0448\u0430\u044f \u043e\u0431\u0449\u0435\u0435 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0430\u0431\u043e\u0442\u044b. \u0412\u043e\u0442 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0435\u0441\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.

\u0412 \u0438\u043d\u0436\u0435\u043d\u0435\u0440\u043d\u043e\u0439 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u0434\u0430\u0447 \u0442\u0440\u0443\u0434\u043d\u043e \u0434\u043e\u0432\u0435\u0441\u0442\u0438 \u0434\u043e \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u0438 \u043c\u043d\u043e\u0433\u0438\u0435 \u0438\u0437 \u043d\u0438\u0445 \u0440\u0435\u0448\u0430\u044e\u0442\u0441\u044f \u043b\u0438\u0448\u044c \"\u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e\". \u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u0442, \u0441 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u043e\u0442 \u0435\u0435 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043f\u0440\u0438\u0440\u043e\u0434\u044b, \u0430 \u0441 \u0434\u0440\u0443\u0433\u043e\u0439 - \u043e\u0442 \u0437\u0430\u043f\u0430\u0441\u0430 \u0437\u043d\u0430\u043d\u0438\u0439 \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430 \u043d\u0435\u0435 \u0441\u043c\u043e\u0442\u0440\u0438\u0442. \u0427\u0435\u043c \u043f\u043e\u043b\u043d\u0435\u0435 \u0437\u043d\u0430\u043d\u0438\u044f \u0438 \u0447\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u044b\u0442, \u0442\u0435\u043c \u0433\u043b\u0443\u0431\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0430\u043d\u0430\u043b\u0438\u0437 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0442\u0435\u043c \u0438\u0437\u044f\u0449\u043d\u0435\u0435 \u0435\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c.

"},{"location":"chapter_introduction/what_is_dsa/","title":"1.2 \u00a0 \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":""},{"location":"chapter_introduction/what_is_dsa/#121","title":"1.2.1 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c (algorithm) - \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u0448\u0430\u0433\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0435\u0448\u0430\u0435\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443 \u0437\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u041e\u043d \u043e\u0431\u043b\u0430\u0434\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438.

"},{"location":"chapter_introduction/what_is_dsa/#122","title":"1.2.2 \u00a0 \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445","text":"

\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (data structure) - \u044d\u0442\u043e \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u0441\u0430\u043c\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u0432\u044f\u0437\u0438 \u043c\u0435\u0436\u0434\u0443 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438. \u0423 \u043d\u0435\u0435 \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0446\u0435\u043b\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u041f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 - \u044d\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u043c\u043f\u0440\u043e\u043c\u0438\u0441\u0441\u043e\u0432. \u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043e\u0434\u043d\u043e, \u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0432\u044b\u043d\u0443\u0436\u0434\u0435\u043d\u044b \u0443\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u0432 \u0447\u0435\u043c-\u0442\u043e \u0434\u0440\u0443\u0433\u043e\u043c. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0434\u0432\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0430.

"},{"location":"chapter_introduction/what_is_dsa/#123","title":"1.2.3 \u00a0 \u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 1-4, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0442\u0435\u0441\u043d\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u0438 \u0441\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430; \u044d\u0442\u043e \u043f\u0440\u043e\u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0445 \u0442\u0440\u0435\u0445 \u0430\u0441\u043f\u0435\u043a\u0442\u0430\u0445.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1-4 \u00a0 \u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438

\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 \u0441\u0431\u043e\u0440\u043a\u0443 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u0443\u044e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 1-5. \u0412 \u043d\u0430\u0431\u043e\u0440 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u043f\u043e\u043c\u0438\u043c\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0434\u0435\u0442\u0430\u043b\u0435\u0439, \u0432\u0445\u043e\u0434\u0438\u0442 \u0438 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f \u043f\u043e \u0441\u0431\u043e\u0440\u043a\u0435. \u0415\u0441\u043b\u0438 \u0448\u0430\u0433 \u0437\u0430 \u0448\u0430\u0433\u043e\u043c \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043c\u043e\u0436\u043d\u043e \u0441\u043e\u0431\u0440\u0430\u0442\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u0443\u044e \u043c\u043e\u0434\u0435\u043b\u044c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 1-5 \u00a0 \u0421\u0431\u043e\u0440\u043a\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430

\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u043e\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 1-1.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 1-1 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0441\u043e \u0441\u0431\u043e\u0440\u043a\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430

\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0421\u0431\u043e\u0440\u043a\u0430 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u041d\u0435\u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u041e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0435\u0442\u0430\u043b\u0435\u0439: \u0444\u043e\u0440\u043c\u0430, \u0440\u0430\u0437\u043c\u0435\u0440, \u0441\u043f\u043e\u0441\u043e\u0431 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u0438 \u0442. \u0434. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0448\u0430\u0433\u043e\u0432 \u043f\u043e \u0441\u0431\u043e\u0440\u043a\u0435 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0432 \u0446\u0435\u043b\u0435\u0432\u0443\u044e \u0444\u043e\u0440\u043c\u0443 \u0412\u044b\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0421\u043e\u0431\u0440\u0430\u043d\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0418\u043c\u0435\u043d\u043d\u043e \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u041f\u0440\u0438\u043d\u044f\u0442\u043e\u0435 \u0441\u043e\u043a\u0440\u0430\u0449\u0435\u043d\u0438\u0435

\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f\u0445 \u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \"\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b\" \u0434\u043e \u043f\u0440\u043e\u0441\u0442\u043e \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b\". \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 LeetCode \u043d\u0430 \u0434\u0435\u043b\u0435 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0442 \u0437\u043d\u0430\u043d\u0438\u044f \u0438 \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c.

"},{"location":"chapter_preface/","title":"\u0413\u043b\u0430\u0432\u0430 0. \u00a0 \u041f\u0440\u0435\u0434\u0438\u0441\u043b\u043e\u0432\u0438\u0435","text":"

Abstract

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0434\u043e\u0431\u043d\u044b \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u043e\u0439 \u0441\u0438\u043c\u0444\u043e\u043d\u0438\u0438, \u0430 \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u043a\u043e\u0434\u0430 \u0442\u0435\u0447\u0435\u0442, \u043a\u0430\u043a \u043c\u0435\u043b\u043e\u0434\u0438\u044f.

\u041f\u0443\u0441\u0442\u044c \u044d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043c\u044f\u0433\u043a\u043e \u0437\u0430\u0437\u0432\u0443\u0447\u0438\u0442 \u0432 \u0442\u0432\u043e\u0435\u043c \u0441\u043e\u0437\u043d\u0430\u043d\u0438\u0438 \u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u0442 \u043f\u043e\u0441\u043b\u0435 \u0441\u0435\u0431\u044f \u043e\u0441\u043e\u0431\u0443\u044e \u0438 \u0433\u043b\u0443\u0431\u043e\u043a\u0443\u044e \u043c\u0435\u043b\u043e\u0434\u0438\u044e.

"},{"location":"chapter_preface/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_preface/about_the_book/","title":"0.1 \u00a0 \u041e\u0431 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435","text":"

\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d \u043d\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e, \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u043e\u0433\u043e \u0438 \u0434\u0440\u0443\u0436\u0435\u043b\u044e\u0431\u043d\u043e\u0433\u043e \u043a \u043d\u043e\u0432\u0438\u0447\u043a\u0430\u043c \u0432\u0432\u043e\u0434\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u043e\u0431\u0438\u044f \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c.

"},{"location":"chapter_preface/about_the_book/#011","title":"0.1.1 \u00a0 \u0426\u0435\u043b\u0435\u0432\u0430\u044f \u0430\u0443\u0434\u0438\u0442\u043e\u0440\u0438\u044f","text":"

\u0415\u0441\u043b\u0438 \u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0448\u044c \u0438\u0437\u0443\u0447\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0440\u0430\u043d\u044c\u0448\u0435 \u0441 \u043d\u0438\u043c\u0438 \u043d\u0435 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u043b\u0441\u044f \u0438\u043b\u0438 \u0443\u0436\u0435 \u0440\u0435\u0448\u0430\u043b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u043e \u0432\u0441\u0435 \u0435\u0449\u0435 \u0441\u043c\u0443\u0442\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0448\u044c \u0441\u0435\u0431\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0438 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043a\u043e\u043b\u0435\u0431\u043b\u0435\u0448\u044c\u0441\u044f \u043c\u0435\u0436\u0434\u0443 \"\u043f\u043e\u043d\u0438\u043c\u0430\u044e\" \u0438 \"\u043d\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u044e\", \u0442\u043e \u044d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u0441\u043e\u0437\u0434\u0430\u043d\u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0442\u0435\u0431\u044f!

\u0415\u0441\u043b\u0438 \u0443 \u0442\u0435\u0431\u044f \u0443\u0436\u0435 \u043d\u0430\u043a\u043e\u043f\u0438\u043b\u0441\u044f \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0439 \u043e\u043f\u044b\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447 \u0438 \u0442\u044b \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e\u043c \u0442\u0438\u043f\u043e\u0432\u044b\u0445 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043d\u0438\u0433\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0442\u0435\u0431\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0438\u0442\u044c \u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u043d\u0438\u044f \u043e\u0431 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445, \u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0438\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a \"\u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\" \u0438\u043b\u0438 \u043a\u0430\u043a \"\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c\".

\u0415\u0441\u043b\u0438 \u0436\u0435 \u0442\u044b \u0443\u0436\u0435 \u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \"\u0433\u0443\u0440\u0443\" \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0434\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0442\u0432\u043e\u0438 \u0446\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u0438\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043d\u0438\u0433\u0443 \u0432\u043c\u0435\u0441\u0442\u0435.

\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f

\u0422\u0435\u0431\u0435 \u043d\u0443\u0436\u043d\u0430 \u0445\u043e\u0442\u044f \u0431\u044b \u0431\u0430\u0437\u043e\u0432\u0430\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u044f\u0437\u044b\u043a\u043e\u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u0447\u0442\u043e\u0431\u044b \u0447\u0438\u0442\u0430\u0442\u044c \u0438 \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043a\u043e\u0434.

"},{"location":"chapter_preface/about_the_book/#012","title":"0.1.2 \u00a0 \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u044f","text":"

\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043a\u043d\u0438\u0433\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-1.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043a\u043d\u0438\u0433\u0438

"},{"location":"chapter_preface/about_the_book/#013","title":"0.1.3 \u00a0 \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u043d\u043e\u0441\u0442\u0438","text":"

\u042d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0441\u043e\u0432\u0435\u0440\u0448\u0435\u043d\u0441\u0442\u0432\u0443\u0435\u0442\u0441\u044f \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u044b\u043c \u0443\u0441\u0438\u043b\u0438\u044f\u043c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430 open source. \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0430\u0432\u0442\u043e\u0440\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043b\u043e\u0436\u0438\u043b \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0438 \u0441\u0438\u043b\u044b; \u0438\u0445 \u0438\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u044b \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c GitHub: krahets, coderonion, Gonglja, nuomi1, Reanon, justin-tse, hpstory, danielsss, curtishd, night-cruise, S-N-O-R-L-A-X, rongyi, msk397, gvenusleo, khoaxuantu, rivertwilight, K3v123, gyt95, zhuoqinyue, yuelinxin, Zuoxun, mingXta, Phoenix0415, FangYuan33, GN-Yu, longsizhuo, IsChristina, xBLACKICEx, guowei-gong, Cathay-Chen, pengchzn, QiLOL, magentaqin, hello-ikun, JoseHung, qualifier1024, thomasq0, sunshinesDL, L-Super, Guanngxu, Transmigration-zhou, WSL0809, Slone123c, lhxsm, yuan0221, what-is-me, Shyam-Chen, theNefelibatas, longranger2, codeberg-user, xiongsp, JeffersonHuang, prinpal, seven1240, Wonderdch, malone6, xiaomiusa87, gaofer, bluebean-cloud, a16su, SamJin98, hongyun-robot, nanlei, XiaChuerwu, yd-j, iron-irax, mgisr, steventimes, junminhong, heshuyue, danny900714, MolDuM, Nigh, Dr-XYZ, XC-Zero, reeswell, PXG-XPG, NI-SW, Horbin-Magician, Enlightenus, YangXuanyi, beatrix-chan, DullSword, xjr7670, jiaxianhua, qq909244296, iStig, boloboloda, hts0000, gledfish, wenjianmin, keshida, kilikilikid, lclc6, lwbaptx, linyejoe2, liuxjerry, llql1211, fbigm, echo1937, szu17dmy, dshlstarr, Yucao-cy, coderlef, czruby, bongbongbakudan, beintentional, ZongYangL, ZhongYuuu, ZhongGuanbin, hezhizhen, linzeyan, ZJKung, luluxia, xb534, ztkuaikuai, yw-1021, ElaBosak233, baagod, zhouLion, yishangzhang, yi427, yanedie, yabo083, weibk, wangwang105, th1nk3r-ing, tao363, 4yDX3906, syd168, sslmj2020, smilelsb, siqyka, selear, sdshaoda, Xi-Row, popozhu, nuquist19, noobcodemaker, XiaoK29, chadyi, lyl625760, lucaswangdev, 0130w, shanghai-Jerry, EJackYang, Javesun99, eltociear, lipusheng, KNChiu, BlindTerran, ShiMaRing, lovelock, FreddieLi, FloranceYeh, fanchenggang, gltianwen, goerll, nedchu, curly210102, CuB3y0nd, KraHsu, CarrotDLaw, youshaoXG, bubble9um, Asashishi, Asa0oo0o0o, fanenr, eagleanurag, akshiterate, 52coder, foursevenlove, KorsChen, GaochaoZhu, hopkings2008, yang-le, realwujing, Evilrabbit520, Umer-Jahangir, Turing-1024-Lee, Suremotoo, paoxiaomooo, Chieko-Seren, Allen-Scai, ymmmas, Risuntsy, Richard-Zhang1019, RafaelCaso, qingpeng9802, primexiao, Urbaner3, zhongfq, nidhoggfgg, MwumLi, CreatorMetaSky, martinx, ZnYang2018, hugtyftg, logan-qiu, psychelzh, Keynman, KeiichiKasai \u0438 KawaiiAsh.

\u0420\u0435\u0446\u0435\u043d\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 coderonion, curtishd, Gonglja, gvenusleo, hpstory, justin-tse, khoaxuantu, krahets, night-cruise, nuomi1, Reanon \u0438 rongyi (\u0432 \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435). \u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0438\u043c \u0437\u0430 \u0432\u0440\u0435\u043c\u044f \u0438 \u0441\u0438\u043b\u044b: \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u043b\u0438 \u0435\u0434\u0438\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u0435 \u0438 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0434\u0430 \u043d\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445.

\u0422\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u0443\u044e \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e \u043a\u043d\u0438\u0433\u0438 \u0432\u044b\u0447\u0438\u0442\u0430\u043b\u0438 Shyam-Chen \u0438 Dr-XYZ, \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e - yuelinxin, K3v123, QiLOL, Phoenix0415, SamJin98, yanedie, RafaelCaso, pengchzn, thomasq0 \u0438 magentaqin, \u0430 \u044f\u043f\u043e\u043d\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e - eltociear. \u0418\u043c\u0435\u043d\u043d\u043e \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0438\u0445 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u043c\u0443 \u0432\u043a\u043b\u0430\u0434\u0443 \u044d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0448\u0438\u0440\u043e\u043a\u043e\u043c\u0443 \u043a\u0440\u0443\u0433\u0443 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0435\u0439, \u0438 \u043c\u044b \u0438\u0441\u043a\u0440\u0435\u043d\u043d\u0435 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0438\u043c \u0438\u0445.

\u0418\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 ePub-\u0432\u0435\u0440\u0441\u0438\u0438 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0430\u043b zhongfq. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u0438\u043c \u0435\u0433\u043e \u0437\u0430 \u0432\u043a\u043b\u0430\u0434, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u0430\u043b \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f\u043c \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u0438\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0447\u0442\u0435\u043d\u0438\u044f.

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0434 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u043e\u0439 \u043c\u043d\u0435 \u043f\u043e\u043c\u043e\u0433\u0430\u043b\u043e \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e \u043b\u044e\u0434\u0435\u0439.

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043a\u043d\u0438\u0433\u0438 \u044f \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u043e\u0432 \u0438 \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c. \u042d\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0442\u0430\u043b\u0438 \u0434\u043b\u044f \u043a\u043d\u0438\u0433\u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0441\u043d\u044b\u043c\u0438 \u043e\u0431\u0440\u0430\u0437\u0446\u0430\u043c\u0438 \u0438 \u043f\u043e\u043c\u043e\u0433\u043b\u0438 \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c \u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u043e \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430. \u042f \u0438\u0441\u043a\u0440\u0435\u043d\u043d\u0435 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u0432\u0441\u0435\u0445 \u043f\u0440\u0435\u043f\u043e\u0434\u0430\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\u043e\u0432 \u0437\u0430 \u0438\u0445 \u0432\u044b\u0434\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0432\u043a\u043b\u0430\u0434!

\u042d\u0442\u0430 \u043a\u043d\u0438\u0433\u0430 \u043f\u0440\u043e\u043f\u0430\u0433\u0430\u043d\u0434\u0438\u0440\u0443\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0438 \u0440\u0443\u043a\u0438, \u0438 \u0433\u043e\u043b\u043e\u0432\u0430; \u0432 \u044d\u0442\u043e\u043c \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0438 \u043d\u0430 \u043c\u0435\u043d\u044f \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0432\u043b\u0438\u044f\u043b\u0430 Dive into Deep Learning. \u042f \u0433\u043e\u0440\u044f\u0447\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u044d\u0442\u0443 \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0432\u0441\u0435\u043c \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f\u043c.

\u041e\u0442 \u0432\u0441\u0435\u0433\u043e \u0441\u0435\u0440\u0434\u0446\u0430 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044e \u043c\u043e\u0438\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u0439: \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0430\u0448\u0430 \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u0430\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0430 \u0438 \u043e\u0431\u043e\u0434\u0440\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u0438 \u043c\u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u0438\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u043c \u0434\u0435\u043b\u043e\u043c.

"},{"location":"chapter_preface/suggestions/","title":"0.2 \u00a0 \u041a\u0430\u043a \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u043e\u0439","text":"

Tip

\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0438\u043b\u0443\u0447\u0448\u0438\u0439 \u043e\u043f\u044b\u0442 \u0447\u0442\u0435\u043d\u0438\u044f, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0440\u0430\u0437\u0434\u0435\u043b.

"},{"location":"chapter_preface/suggestions/#021","title":"0.2.1 \u00a0 \u0421\u043e\u0433\u043b\u0430\u0448\u0435\u043d\u0438\u044f \u043e \u0441\u0442\u0438\u043b\u0435 \u0438\u0437\u043b\u043e\u0436\u0435\u043d\u0438\u044f","text":" PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
\"\"\"\u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f.\"\"\"\n\n# \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n\"\"\"\n\u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n\"\"\"\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
/* \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. */\n\n// \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n/**\n * \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n * \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n */\n
### \u041a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439-\u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432 \u0438 \u0442. \u043f. ###\n\n# \u0421\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439: \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442 \u043a\u043e\u0434\n\n# \u041c\u043d\u043e\u0433\u043e\u0441\u0442\u0440\u043e\u0447\u043d\u044b\u0439\n# \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439\n
"},{"location":"chapter_preface/suggestions/#022","title":"0.2.2 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0439","text":"

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0442\u0435\u043a\u0441\u0442\u043e\u043c \u0432\u0438\u0434\u0435\u043e \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u043f\u043b\u043e\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0438 \u0431\u043e\u043b\u0435\u0435 \u0447\u0435\u0442\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043b\u0435\u0433\u0447\u0435 \u0432\u043e\u0441\u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c. \u0412 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0438 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0438\u0434\u0435\u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0439, \u0430 \u0442\u0435\u043a\u0441\u0442 \u0431\u0443\u0434\u0435\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u0438 \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.

\u0415\u0441\u043b\u0438 \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0442\u044b \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0448\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u0441 \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0435\u0439, \u043a\u0430\u043a \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-2, \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u0439\u0441\u044f \u043d\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0430 \u0442\u0435\u043a\u0441\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439 \u043a\u0430\u043a \u0434\u043e\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044f \u043e\u0431\u0430 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a\u0430 \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-2 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0430\u043d\u0438\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0438\u043b\u043b\u044e\u0441\u0442\u0440\u0430\u0446\u0438\u0438

"},{"location":"chapter_preface/suggestions/#023","title":"0.2.3 \u00a0 \u0423\u0433\u043b\u0443\u0431\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0443 \u043a\u043e\u0434\u0430","text":"

\u0421\u043e\u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043e\u0434 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 GitHub. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435, \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0441\u043d\u0430\u0431\u0436\u0435\u043d \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u0438 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u043d\u0430\u0436\u0430\u0442\u0438\u0435\u043c.

\u0415\u0441\u043b\u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u0440\u0435\u043c\u044f, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c \u043a\u043e\u0434. \u0415\u0441\u043b\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435 \u043c\u0430\u043b\u043e, \u0442\u043e \u0445\u043e\u0442\u044f \u0431\u044b \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0439 \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438 \u0432\u0435\u0441\u044c \u043a\u043e\u0434.

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u043a\u043e\u0434\u0430 \u0441\u0430\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0435\u0433\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0434\u0430\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u043b\u044c\u0437\u044b. \u0423\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 - \u0437\u043d\u0430\u0447\u0438\u0442 \u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043f\u043e-\u043d\u0430\u0441\u0442\u043e\u044f\u0449\u0435\u043c\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043a\u043e\u0434\u0430

\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043a \u0437\u0430\u043f\u0443\u0441\u043a\u0443 \u043a\u043e\u0434\u0430 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0448\u0430\u0433\u043e\u0432.

\u0428\u0430\u0433 1: \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0440\u0435\u0434\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0439\u0441\u044f \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u043e\u043c \u0438\u0437 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0415\u0441\u043b\u0438 \u0441\u0440\u0435\u0434\u0430 \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0430, \u044d\u0442\u043e\u0442 \u0448\u0430\u0433 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c.

\u0428\u0430\u0433 2: \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043a\u043e\u0434\u043e\u043c. \u041f\u0435\u0440\u0435\u0439\u0434\u0438 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 GitHub. \u0415\u0441\u043b\u0438 \u0443 \u0442\u0435\u0431\u044f \u0443\u0436\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d Git, \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043c\u043e\u0436\u043d\u043e \u043a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u043a\u043e\u043c\u0430\u043d\u0434\u043e\u0439:

git clone https://github.com/krahets/hello-algo.git\n

\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0436\u0430\u0442\u044c \u043a\u043d\u043e\u043f\u043a\u0443 \"Download ZIP\" \u0432 \u043c\u0435\u0441\u0442\u0435, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u043c \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-4, \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0430\u0440\u0445\u0438\u0432 \u0441 \u043a\u043e\u0434\u043e\u043c \u0438 \u0437\u0430\u0442\u0435\u043c \u0440\u0430\u0441\u043f\u0430\u043a\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-4 \u00a0 \u041a\u043b\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f \u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u043a\u043e\u0434\u0430

\u0428\u0430\u0433 3: \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-5, \u0434\u043b\u044f \u0431\u043b\u043e\u043a\u043e\u0432 \u043a\u043e\u0434\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0432\u0435\u0440\u0445\u0443 \u0443\u043a\u0430\u0437\u0430\u043d\u043e \u0438\u043c\u044f \u0444\u0430\u0439\u043b\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u043f\u0430\u043f\u043a\u0435 codes \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u044f. \u042d\u0442\u0438 \u0444\u0430\u0439\u043b\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u044e\u0442\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u043d\u0430\u0436\u0430\u0442\u0438\u0435\u043c, \u0447\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u043d\u0435 \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u043b\u0438\u0448\u043d\u0435\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u0438 \u0441\u043e\u0441\u0440\u0435\u0434\u043e\u0442\u043e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-5 \u00a0 \u0411\u043b\u043e\u043a\u0438 \u043a\u043e\u0434\u0430 \u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0444\u0430\u0439\u043b\u044b

\u041f\u043e\u043c\u0438\u043c\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430, \u0432\u0435\u0431-\u0432\u0435\u0440\u0441\u0438\u044f \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a Python-\u043a\u043e\u0434\u0430 (\u043d\u0430 \u0431\u0430\u0437\u0435 pythontutor). \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0438\u0436\u0435, \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0436\u0430\u0442\u044c \"\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\" \u043f\u043e\u0434 \u0431\u043b\u043e\u043a\u043e\u043c \u043a\u043e\u0434\u0430, \u0447\u0442\u043e\u0431\u044b \u0440\u0430\u0441\u043a\u0440\u044b\u0442\u044c \u043e\u043a\u043d\u043e \u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0430\u0442\u044c \u0437\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430; \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0436\u0430\u0442\u044c \"\u041f\u043e\u043b\u043d\u043e\u044d\u043a\u0440\u0430\u043d\u043d\u044b\u0439 \u0440\u0435\u0436\u0438\u043c\", \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-6 \u00a0 \u0412\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0443\u0441\u043a Python-\u043a\u043e\u0434\u0430

"},{"location":"chapter_preface/suggestions/#024","title":"0.2.4 \u00a0 \u0421\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u044b\u0439 \u0440\u043e\u0441\u0442 \u0447\u0435\u0440\u0435\u0437 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0438 \u043e\u0431\u0441\u0443\u0436\u0434\u0435\u043d\u0438\u044f","text":"

\u0412\u043e \u0432\u0440\u0435\u043c\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u043a\u043d\u0438\u0433\u0438 \u043d\u0435 \u0441\u0442\u043e\u0438\u0442 \u043b\u0435\u0433\u043a\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0442\u0435 \u043c\u0435\u0441\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c\u0438. \u0421\u043c\u0435\u043b\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0439 \u0441\u0432\u043e\u0438 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432: \u044f \u0438 \u043c\u043e\u0438 \u0434\u0440\u0443\u0437\u044c\u044f \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u0435\u043c\u0441\u044f \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u0442\u0435\u0431\u0435 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0442\u0449\u0430\u0442\u0435\u043b\u044c\u043d\u0435\u0435, \u043e\u0431\u044b\u0447\u043d\u043e \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0434\u043d\u0435\u0439.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-7, \u0432 \u0432\u0435\u0431-\u0432\u0435\u0440\u0441\u0438\u0438 \u0443 \u043a\u0430\u0436\u0434\u043e\u0439 \u0433\u043b\u0430\u0432\u044b \u0432\u043d\u0438\u0437\u0443 \u0435\u0441\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432. \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0442\u044b \u0431\u0443\u0434\u0435\u0448\u044c \u0447\u0430\u0449\u0435 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0435\u0433\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435. \u0421 \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u044d\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0441 \u043a\u0430\u043a\u0438\u043c\u0438 \u0442\u0440\u0443\u0434\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433\u0438\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438, \u0432\u043e\u0441\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u0435\u043b\u044b \u0438 \u043f\u043e\u0434\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c \u0441\u0435\u0431\u044f \u043a \u0431\u043e\u043b\u0435\u0435 \u0433\u043b\u0443\u0431\u043e\u043a\u0438\u043c \u0440\u0430\u0437\u043c\u044b\u0448\u043b\u0435\u043d\u0438\u044f\u043c. \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0431\u0443\u0434\u0443 \u0440\u0430\u0434, \u0435\u0441\u043b\u0438 \u0442\u044b \u0449\u0435\u0434\u0440\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0448\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u0434\u0440\u0443\u0433\u0438\u0445 \u0443\u0447\u0430\u0441\u0442\u043d\u0438\u043a\u043e\u0432, \u043f\u043e\u0434\u0435\u043b\u0438\u0448\u044c\u0441\u044f \u0441\u0432\u043e\u0438\u043c\u0438 \u043d\u0430\u0431\u043b\u044e\u0434\u0435\u043d\u0438\u044f\u043c\u0438 \u0438 \u043f\u043e\u043c\u043e\u0436\u0435\u0448\u044c \u0438\u043c \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u0432\u043f\u0435\u0440\u0435\u0434.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-7 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432

"},{"location":"chapter_preface/suggestions/#025","title":"0.2.5 \u00a0 \u0414\u043e\u0440\u043e\u0436\u043d\u0430\u044f \u043a\u0430\u0440\u0442\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432","text":"

\u0412 \u0446\u0435\u043b\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0442\u0440\u0438 \u044d\u0442\u0430\u043f\u0430.

  1. \u042d\u0442\u0430\u043f 1: \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b. \u041d\u0443\u0436\u043d\u043e \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b, \u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u044b, \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.
  2. \u042d\u0442\u0430\u043f 2: \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043a\u043e\u043f\u0438\u0442\u044c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 100 \u0440\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u0442\u0438\u043f\u0430\u043c\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043f\u0440\u043e\u0431\u043b\u0435\u043c. \u041d\u0430 \u043f\u0435\u0440\u0432\u044b\u0445 \u043f\u043e\u0440\u0430\u0445 \"\u0437\u0430\u0431\u044b\u0432\u0430\u043d\u0438\u0435 \u0437\u043d\u0430\u043d\u0438\u0439\" \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0438\u0441\u043f\u044b\u0442\u0430\u043d\u0438\u0435\u043c, \u043d\u043e \u044d\u0442\u043e \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e. \u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e \"\u043a\u0440\u0438\u0432\u043e\u0439 \u0437\u0430\u0431\u044b\u0432\u0430\u043d\u0438\u044f \u042d\u0431\u0431\u0438\u043d\u0433\u0430\u0443\u0437\u0430\", \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0441\u043b\u0435 3-5 \u0446\u0438\u043a\u043b\u043e\u0432 \u043f\u043e\u0432\u0442\u043e\u0440\u0435\u043d\u0438\u044f \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u043f\u0440\u043e\u0447\u043d\u043e \u0437\u0430\u043a\u0440\u0435\u043f\u043b\u044f\u0435\u0442\u0441\u044f. \u0420\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u044b\u0435 \u0441\u043f\u0438\u0441\u043a\u0438 \u0437\u0430\u0434\u0430\u0447 \u0438 \u043f\u043b\u0430\u043d\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0441\u043c. \u0432 \u044d\u0442\u043e\u043c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 GitHub.
  3. \u042d\u0442\u0430\u043f 3: \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0437\u043d\u0430\u043d\u0438\u0439. \u0412 \u0443\u0447\u0435\u0431\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0447\u0438\u0442\u0430\u0442\u044c \u0441\u0442\u0430\u0442\u044c\u0438 \u043f\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c, \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u0430\u0440\u043a\u0430\u0441\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u0438 \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0438, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u043e\u0431\u043e\u0433\u0430\u0449\u0430\u0442\u044c \u0441\u0432\u043e\u044e \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0437\u043d\u0430\u043d\u0438\u0439. \u0412 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u043f\u043e \u0442\u0435\u043c\u0430\u043c, \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0439 \u043e\u0434\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438\u043b\u0438 \u043e\u0434\u043d\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447; \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043e\u043f\u044b\u0442 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0430\u0445.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 0-8, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u043a\u043d\u0438\u0433\u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \"\u044d\u0442\u0430\u043f 1\" \u0438 \u043f\u0440\u0438\u0437\u0432\u0430\u043d\u043e \u043f\u043e\u043c\u043e\u0447\u044c \u0442\u0435\u0431\u0435 \u0431\u043e\u043b\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e \u043d\u0430 \u044d\u0442\u0430\u043f\u0430\u0445 2 \u0438 3.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 0-8 \u00a0 \u0414\u043e\u0440\u043e\u0436\u043d\u0430\u044f \u043a\u0430\u0440\u0442\u0430 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432

"},{"location":"chapter_preface/summary/","title":"0.3 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_preface/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_reference/","title":"\u0421\u043f\u0438\u0441\u043e\u043a \u043b\u0438\u0442\u0435\u0440\u0430\u0442\u0443\u0440\u044b","text":"

[1] Thomas H. Cormen, et al. Introduction to Algorithms (3rd Edition).

[2] Aditya Bhargava. Grokking Algorithms: An Illustrated Guide for Programmers and Other Curious People (1st Edition).

[3] Robert Sedgewick, et al. Algorithms (4th Edition).

[4] Yan Weimin. Data Structures (C Language Edition).

[5] Deng Junhui. Data Structures (C++ Language Edition, 3rd Edition).

[6] Mark Allen Weiss; translated by Chen Yue. Data Structures and Algorithm Analysis: Java Description (3rd Edition).

[7] Cheng Jie. A Plainspoken Guide to Data Structures.

[8] Wang Zheng. The Beauty of Data Structures and Algorithms.

[9] Gayle Laakmann McDowell. Cracking the Coding Interview: 189 Programming Questions and Solutions (6th Edition).

[10] Aston Zhang, et al. Dive into Deep Learning.

"},{"location":"chapter_searching/","title":"\u0413\u043b\u0430\u0432\u0430 10. \u00a0 \u041f\u043e\u0438\u0441\u043a","text":"

Abstract

\u041f\u043e\u0438\u0441\u043a - \u044d\u0442\u043e \u043f\u0440\u0438\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0432 \u043d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0441\u0442\u044c: \u0438\u043d\u043e\u0433\u0434\u0430 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0439\u0442\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0433\u043e\u043b\u043e\u043a \u0437\u0430\u0433\u0430\u0434\u043e\u0447\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430, \u0430 \u0438\u043d\u043e\u0433\u0434\u0430 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u0431\u044b\u0441\u0442\u0440\u043e \u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0446\u0435\u043b\u044c.

\u0412 \u044d\u0442\u043e\u043c \u043f\u0443\u0442\u0435\u0448\u0435\u0441\u0442\u0432\u0438\u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u043d\u043e\u0432\u044b\u0439 \u0448\u0430\u0433 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u043e\u0442\u0432\u0435\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043d\u0435 \u043e\u0436\u0438\u0434\u0430\u043b\u0438.

"},{"location":"chapter_searching/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_searching/binary_search/","title":"10.1 \u00a0 \u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a","text":"

\u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (binary search) - \u044d\u0442\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\". \u041e\u043d \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u0434\u0432\u043e\u0435, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u043b\u0438 \u043f\u043e\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435 \u043e\u043f\u0443\u0441\u0442\u0435\u0435\u0442.

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\), \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u044b \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f \u0438 \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442\u0441\u044f. \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0438 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 target \u0432 \u044d\u0442\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435. \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u0442\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \\(-1\\) . \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-1.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-1 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-2, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \\(i = 0\\) \u0438 \\(j = n - 1\\) , \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0437\u0430\u0434\u0430\u044e\u0442 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043f\u043e\u0438\u0441\u043a\u0430 \\([0, n - 1]\\) . \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u0435 \u0441\u043a\u043e\u0431\u043a\u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0442 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u0433\u0440\u0430\u043d\u0438\u0447\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.

\u0414\u0430\u043b\u0435\u0435 \u0432 \u0446\u0438\u043a\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0432\u0430 \u0448\u0430\u0433\u0430.

  1. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b \\(m = \\lfloor {(i + j) / 2} \\rfloor\\) , \u0433\u0434\u0435 \\(\\lfloor \\: \\rfloor\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437.
  2. \u0421\u0440\u0430\u0432\u043d\u0438\u0442\u044c nums[m] \u0438 target , \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0442\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u044f.
    1. \u0415\u0441\u043b\u0438 nums[m] < target , \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \\([m + 1, j]\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(i = m + 1\\) .
    2. \u0415\u0441\u043b\u0438 nums[m] > target , \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \\([i, m - 1]\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(j = m - 1\\) .
    3. \u0415\u0441\u043b\u0438 nums[m] = target , \u0437\u043d\u0430\u0447\u0438\u0442, \u044d\u043b\u0435\u043c\u0435\u043d\u0442 target \u043d\u0430\u0439\u0434\u0435\u043d, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0438\u043d\u0434\u0435\u043a\u0441 \\(m\\) .

\u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0438\u0442\u043e\u0433\u0435 \u0441\u0443\u0437\u0438\u0442\u0441\u044f \u0434\u043e \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \\(-1\\) .

<1><2><3><4><5><6><7>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-2 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438 \\(i\\) , \u0438 \\(j\\) \u0438\u043c\u0435\u044e\u0442 \u0442\u0438\u043f int , \u0442\u043e \u0441\u0443\u043c\u043c\u0430 \\(i + j\\) \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0439\u0442\u0438 \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \u0442\u0438\u043f\u0430 int. \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0444\u043e\u0440\u043c\u0443\u043b\u0443 \\(m = \\lfloor {i + (j - i) / 2} \\rfloor\\) \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b.

\u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
def binary_search(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b)\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    i, j = 0, len(nums) - 1\n    # \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i <= j:\n        # \u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0447\u0438\u0441\u043b\u0430 \u0432 Python \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c \u0443\u0433\u043e\u0434\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 (\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043c\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0447\u0438\u0441\u0435\u043b\n        m = (i + j) // 2  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target:\n            i = m + 1  # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        else:\n            return m  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    return -1  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n
binary_search.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearch(vector<int> &nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.size() - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)    // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearch(int[] nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.length - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint BinarySearch(int[] nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.Length - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        int m = i + (j - i) / 2;   // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        else                       // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunc binarySearch(nums []int, target int) int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    i, j := 0, len(nums)-1\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    for i <= j {\n        m := i + (j-i)/2      // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        } else { // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunc binarySearch(nums: [Int], target: Int) -> Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i <= j {\n        let m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1\n        } else if nums[m] > target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        } else { // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunction binarySearch(nums, target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let i = 0,\n        j = nums.length - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f parseInt() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        else return m; // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunction binarySearch(nums: number[], target: number): number {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let i = 0,\n        j = nums.length - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        } else {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n        }\n    }\n    return -1; // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n}\n
binary_search.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearch(List<int> nums, int target) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n  int i = 0, j = nums.length - 1;\n  // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    if (nums[m] < target) {\n      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n      j = m - 1;\n    } else {\n      // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n      return m;\n    }\n  }\n  // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1;\n}\n
binary_search.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfn binary_search(nums: &[i32], target: i32) -> i32 {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let mut i = 0;\n    let mut j = nums.len() as i32 - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i <= j {\n        let m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m as usize] < target {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        } else {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearch(int *nums, int len, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = len - 1;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)    // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfun binarySearch(nums: IntArray, target: Int): Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    var i = 0\n    var j = nums.size - 1\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        else  // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.rb
=begin\nFile: binary_search.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) ###\ndef binary_search(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n  i, j = 0, nums.length - 1\n\n  # \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n  while i <= j\n    # \u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0447\u0438\u0441\u043b\u0430 \u0432 Ruby \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c \u0443\u0433\u043e\u0434\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 (\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043c\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0447\u0438\u0441\u0435\u043b\n    m = (i + j) / 2   # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n\n    if nums[m] < target\n      i = m + 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    else\n      return m  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    end\n  end\n\n  -1  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(\\log n)\\) : \u0432 \u0446\u0438\u043a\u043b\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432\u0434\u0432\u043e\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0447\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0440\u0430\u0432\u043d\u043e \\(\\log_2 n\\) .

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) : \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \\(i\\) \u0438 \\(j\\) \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043c \u043f\u0430\u043c\u044f\u0442\u0438.

"},{"location":"chapter_searching/binary_search/#1011","title":"10.1.1 \u00a0 \u041c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u0432","text":"

\u041f\u043e\u043c\u0438\u043c\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u0434\u0432\u043e\u0439\u043d\u043e\u0433\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430, \u0447\u0430\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \"\u0441\u043b\u0435\u0432\u0430 \u0437\u0430\u043a\u0440\u044b\u0442, \u0441\u043f\u0440\u0430\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\", \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u0430\u043a \\([0, n)\\) , \u0442\u043e \u0435\u0441\u0442\u044c \u043b\u0435\u0432\u0430\u044f \u0433\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f, \u0430 \u043f\u0440\u0430\u0432\u0430\u044f - \u043d\u0435\u0442. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \\([i, j)\\) \u043f\u0443\u0441\u0442, \u043a\u043e\u0433\u0434\u0430 \\(i = j\\) .

\u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441 \u0442\u043e\u0439 \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c\u044e:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search.py
def binary_search_lcro(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b)\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    i, j = 0, len(nums)\n    # \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i < j:\n        m = (i + j) // 2  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target:\n            i = m + 1  # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n        elif nums[m] > target:\n            j = m  # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n        else:\n            return m  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    return -1  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n
binary_search.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearchLCRO(vector<int> &nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.size();\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)    // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearchLCRO(int[] nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.length;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint BinarySearchLCRO(int[] nums, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = nums.Length;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        int m = i + (j - i) / 2;   // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        else                       // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunc binarySearchLCRO(nums []int, target int) int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    i, j := 0, len(nums)\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    for i < j {\n        m := i + (j-i)/2      // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m\n        } else { // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunc binarySearchLCRO(nums: [Int], target: Int) -> Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    var i = nums.startIndex\n    var j = nums.endIndex\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i < j {\n        let m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1\n        } else if nums[m] > target { // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m\n        } else { // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunction binarySearchLCRO(nums, target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let i = 0,\n        j = nums.length;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f parseInt() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        const m = parseInt(i + (j - i) / 2);\n        if (nums[m] < target)\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target)\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n        else return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfunction binarySearchLCRO(nums: number[], target: number): number {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let i = 0,\n        j = nums.length;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        const m = Math.floor(i + (j - i) / 2);\n        if (nums[m] < target) {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        } else if (nums[m] > target) {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        } else {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n        }\n    }\n    return -1; // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n}\n
binary_search.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearchLCRO(List<int> nums, int target) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n  int i = 0, j = nums.length;\n  // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n  while (i < j) {\n    int m = i + (j - i) ~/ 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    if (nums[m] < target) {\n      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n      i = m + 1;\n    } else if (nums[m] > target) {\n      // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n      j = m;\n    } else {\n      // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n      return m;\n    }\n  }\n  // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1;\n}\n
binary_search.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfn binary_search_lcro(nums: &[i32], target: i32) -> i32 {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    let mut i = 0;\n    let mut j = nums.len() as i32;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while i < j {\n        let m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m as usize] < target {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        } else if nums[m as usize] > target {\n            // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        } else {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n        }\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nint binarySearchLCRO(int *nums, int len, int target) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    int i = 0, j = len;\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target)    // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1;\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m;\n        else // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m;\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1;\n}\n
binary_search.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) */\nfun binarySearchLCRO(nums: IntArray, target: Int): Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n    var i = 0\n    var j = nums.size\n    // \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n    while (i < j) {\n        val m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n            i = m + 1\n        else if (nums[m] > target) // \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n            j = m\n        else  // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n            return m\n    }\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    return -1\n}\n
binary_search.rb
=begin\nFile: binary_search.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) ###\ndef binary_search(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1], \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n  i, j = 0, nums.length - 1\n\n  # \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i > j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n  while i <= j\n    # \u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0447\u0438\u0441\u043b\u0430 \u0432 Ruby \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0441\u043a\u043e\u043b\u044c \u0443\u0433\u043e\u0434\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 (\u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0431\u044a\u0435\u043c\u043e\u043c \u043f\u0430\u043c\u044f\u0442\u0438), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0447\u0438\u0441\u0435\u043b\n    m = (i + j) / 2   # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n\n    if nums[m] < target\n      i = m + 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    else\n      return m  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    end\n  end\n\n  -1  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\nend\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a (\u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b) ###\ndef binary_search_lcro(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439, \u043f\u0440\u0430\u0432\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n), \u0442\u043e \u0435\u0441\u0442\u044c i \u0438 j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0441\u0440\u0430\u0437\u0443 \u0437\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\n  i, j = 0, nums.length\n\n  # \u0426\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0443\u0441\u0442 (\u043f\u0440\u0438 i = j \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0443\u0441\u0442)\n  while i < j\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j)\n    elsif nums[m] > target\n      j = m - 1 # \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m)\n    else\n      return m  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0435\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\n    end\n  end\n\n  -1  # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-3, \u0432 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0430\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0441\u0443\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u0437\u0430\u043f\u0438\u0441\u0438 \"\u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\" \u043e\u0431\u0435 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0437\u0430\u043a\u0440\u044b\u0442\u044b\u043c\u0438, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0443\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 \\(i\\) \u0438 \\(j\\) \u0442\u043e\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u043c\u0438. \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0432 \u0442\u0430\u043a\u043e\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435 \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u043c\u0435\u043d\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u044c \"\u0434\u0432\u043e\u0439\u043d\u043e\u0439 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-3 \u00a0 \u0414\u0432\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u043e\u0432

"},{"location":"chapter_searching/binary_search/#1012","title":"10.1.2 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f","text":"

\u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0445\u043e\u0440\u043e\u0448\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0438 \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0438 \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438.

\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043d\u0435 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439, \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0442\u0430\u043a\u043e\u0432\u044b.

"},{"location":"chapter_searching/binary_search_edge/","title":"10.3 \u00a0 \u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0433\u0440\u0430\u043d\u0438\u0446","text":""},{"location":"chapter_searching/binary_search_edge/#1031","title":"10.3.1 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043b\u0435\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b","text":"

Question

\u0414\u0430\u043d \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 target \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435. \u0415\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u0442\u043e\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \\(-1\\) .

\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043f\u0440\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435: \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(i\\) \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 target , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043f\u043e \u0441\u0443\u0442\u0438 \u0438 \u0435\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target.

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043f\u043e\u0438\u0441\u043a\u0430 \u043b\u0435\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0447\u0435\u0440\u0435\u0437 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c target , \u0438 \u0442\u043e\u0433\u0434\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0434\u0432\u0435 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438.

\u0415\u0441\u043b\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043b\u044e\u0431\u0430\u044f \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \\(-1\\) . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
def binary_search_left_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target\"\"\"\n    # \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    i = binary_search_insertion(nums, target)\n    # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i == len(nums) or nums[i] != target:\n        return -1\n    # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i\n
binary_search_edge.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nint binarySearchLeftEdge(vector<int> &nums, int target) {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    int i = binarySearchInsertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i == nums.size() || nums[i] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nint binarySearchLeftEdge(int[] nums, int target) {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    int i = binary_search_insertion.binarySearchInsertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i == nums.length || nums[i] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nint BinarySearchLeftEdge(int[] nums, int target) {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i == nums.Length || nums[i] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfunc binarySearchLeftEdge(nums []int, target int) int {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    i := binarySearchInsertion(nums, target)\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i == len(nums) || nums[i] != target {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i\n}\n
binary_search_edge.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfunc binarySearchLeftEdge(nums: [Int], target: Int) -> Int {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    let i = binarySearchInsertion(nums: nums, target: target)\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i == nums.endIndex || nums[i] != target {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i\n}\n
binary_search_edge.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfunction binarySearchLeftEdge(nums, target) {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    const i = binarySearchInsertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfunction binarySearchLeftEdge(nums: Array<number>, target: number): number {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    const i = binarySearchInsertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i === nums.length || nums[i] !== target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nint binarySearchLeftEdge(List<int> nums, int target) {\n  // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n  int i = binarySearchInsertion(nums, target);\n  // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  if (i == nums.length || nums[i] != target) {\n    return -1;\n  }\n  // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n  return i;\n}\n
binary_search_edge.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfn binary_search_left_edge(nums: &[i32], target: i32) -> i32 {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    let i = binary_search_insertion(nums, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if i == nums.len() as i32 || nums[i as usize] != target {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    i\n}\n
binary_search_edge.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nint binarySearchLeftEdge(int *nums, int numSize, int target) {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    int i = binarySearchInsertion(nums, numSize, target);\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i == numSize || nums[i] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i;\n}\n
binary_search_edge.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target */\nfun binarySearchLeftEdge(nums: IntArray, target: Int): Int {\n    // \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n    val i = binarySearchInsertion(nums, target)\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (i == nums.size || nums[i] != target) {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\n    return i\n}\n
binary_search_edge.rb
=begin\nFile: binary_search_edge.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\nrequire_relative './binary_search_insertion'\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target ###\ndef binary_search_left_edge(nums, target)\n  # \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n  i = binary_search_insertion(nums, target)\n\n  # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_searching/binary_search_edge/#1032","title":"10.3.2 \u00a0 \u041f\u043e\u0438\u0441\u043a \u043f\u0440\u0430\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b","text":"

\u041a\u0430\u043a \u0442\u043e\u0433\u0434\u0430 \u043d\u0430\u0439\u0442\u0438 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target ? \u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 - \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043a\u043e\u0434, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u0443\u0436\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 nums[m] == target . \u041c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043e\u0434, \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.

\u041d\u0438\u0436\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0434\u0432\u0430 \u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u044f\u0449\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430.

"},{"location":"chapter_searching/binary_search_edge/#1","title":"1. \u00a0 \u041f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u043b\u0435\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b","text":"

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0430\u044f \u0438\u0434\u0435\u044f \u0442\u0430\u043a\u043e\u0432\u0430: \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-7, \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(i\\) \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 target + 1 (\u0435\u0441\u043b\u0438 \u043e\u043d \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442), \u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(j\\) \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c \\(j\\).

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-7 \u00a0 \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u0440\u0430\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0432 \u043f\u043e\u0438\u0441\u043a \u043b\u0435\u0432\u043e\u0439

\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \\(i\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0437 \u043d\u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0447\u0435\u0441\u0442\u044c \\(1\\) , \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \\(j\\) :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_edge.py
def binary_search_right_edge(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target\"\"\"\n    # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    i = binary_search_insertion(nums, target + 1)\n    # j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    j = i - 1\n    # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if j == -1 or nums[j] != target:\n        return -1\n    # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j\n
binary_search_edge.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nint binarySearchRightEdge(vector<int> &nums, int target) {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    int i = binarySearchInsertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    int j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nint binarySearchRightEdge(int[] nums, int target) {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    int i = binary_search_insertion.binarySearchInsertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    int j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nint BinarySearchRightEdge(int[] nums, int target) {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    int i = binary_search_insertion.BinarySearchInsertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    int j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfunc binarySearchRightEdge(nums []int, target int) int {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    i := binarySearchInsertion(nums, target+1)\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    j := i - 1\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j\n}\n
binary_search_edge.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfunc binarySearchRightEdge(nums: [Int], target: Int) -> Int {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    let i = binarySearchInsertion(nums: nums, target: target + 1)\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    let j = i - 1\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if j == -1 || nums[j] != target {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j\n}\n
binary_search_edge.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfunction binarySearchRightEdge(nums, target) {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    const j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfunction binarySearchRightEdge(nums: Array<number>, target: number): number {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    const i = binarySearchInsertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    const j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j === -1 || nums[j] !== target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nint binarySearchRightEdge(List<int> nums, int target) {\n  // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n  int i = binarySearchInsertion(nums, target + 1);\n  // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n  int j = i - 1;\n  // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  if (j == -1 || nums[j] != target) {\n    return -1;\n  }\n  // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n  return j;\n}\n
binary_search_edge.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfn binary_search_right_edge(nums: &[i32], target: i32) -> i32 {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    let i = binary_search_insertion(nums, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    let j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if j == -1 || nums[j as usize] != target {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    j\n}\n
binary_search_edge.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nint binarySearchRightEdge(int *nums, int numSize, int target) {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    int i = binarySearchInsertion(nums, numSize, target + 1);\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    int j = i - 1;\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j == -1 || nums[j] != target) {\n        return -1;\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j;\n}\n
binary_search_edge.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target */\nfun binarySearchRightEdge(nums: IntArray, target: Int): Int {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n    val i = binarySearchInsertion(nums, target + 1)\n    // j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n    val j = i - 1\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n    if (j == -1 || nums[j] != target) {\n        return -1\n    }\n    // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\n    return j\n}\n
binary_search_edge.rb
=begin\nFile: binary_search_edge.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\nrequire_relative './binary_search_insertion'\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target ###\ndef binary_search_left_edge(nums, target)\n  # \u042d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u043e \u043f\u043e\u0438\u0441\u043a\u0443 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 target\n  i = binary_search_insertion(nums, target)\n\n  # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1 if i == nums.length || nums[i] != target\n\n  i # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 i\nend\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0430\u0432\u043e\u0433\u043e target ###\ndef binary_search_right_edge(nums, target)\n  # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043f\u043e\u0438\u0441\u043a \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target + 1\n  i = binary_search_insertion(nums, target + 1)\n\n  # j \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 target, \u0430 i \u2014 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 target\n  j = i - 1\n\n  # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c -1\n  return -1 if j == -1 || nums[j] != target\n\n  j # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_searching/binary_search_edge/#2","title":"2. \u00a0 \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430","text":"

\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 target , \u0442\u043e \u0432 \u043a\u043e\u043d\u0446\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \\(i\\) \u0438 \\(j\\) \u0431\u0443\u0434\u0443\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0431\u043e\u043b\u044c\u0448\u0438\u0439 target , \u0438 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043c\u0435\u043d\u044c\u0448\u0438\u0439 target .

\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-8, \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043b\u0435\u0432\u043e\u0439 \u0438 \u043f\u0440\u0430\u0432\u043e\u0439 \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0435\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-8 \u00a0 \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0433\u0440\u0430\u043d\u0438\u0446 \u0432 \u043f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430

\u041a\u043e\u0434 \u0437\u0434\u0435\u0441\u044c \u043e\u043f\u0443\u0449\u0435\u043d, \u043d\u043e \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430.

"},{"location":"chapter_searching/binary_search_insertion/","title":"10.2 \u00a0 \u0422\u043e\u0447\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043f\u0440\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435","text":"

\u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u043d\u043e \u0438 \u0434\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0432\u0430\u0440\u0438\u0430\u0446\u0438\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.

"},{"location":"chapter_searching/binary_search_insertion/#1021","title":"10.2.1 \u00a0 \u0421\u043b\u0443\u0447\u0430\u0439 \u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432","text":"

Question

\u0414\u0430\u043d \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\) \u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 target , \u043f\u0440\u0438\u0447\u0435\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043d\u0435\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u041d\u0443\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c target \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 nums , \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0432 \u043f\u043e\u0440\u044f\u0434\u043e\u043a. \u0415\u0441\u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 target \u0443\u0436\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0432\u0441\u0442\u0430\u0432\u044c\u0442\u0435 \u0435\u0433\u043e \u0441\u043b\u0435\u0432\u0430 \u043e\u0442 \u043d\u0435\u0433\u043e. \u0412\u0435\u0440\u043d\u0438\u0442\u0435 \u0438\u043d\u0434\u0435\u043a\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c target \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0442\u0430\u0432\u043a\u0438. \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-4.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-4 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043b\u044f \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438

\u0415\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u0435\u0440\u0435\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430, \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430.

\u0412\u043e\u043f\u0440\u043e\u0441 1: \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 target , \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u044d\u0442\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430?

\u041f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e target \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0432\u0430 \u043e\u0442 \u0440\u0430\u0432\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0430 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u043e\u0432\u044b\u0439 target \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u043c\u0435\u0441\u0442\u043e \u0441\u0442\u0430\u0440\u043e\u0433\u043e target . \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 target , \u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u044d\u0442\u043e\u0433\u043e target.

\u0412\u043e\u043f\u0440\u043e\u0441 2: \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 target , \u0438\u043d\u0434\u0435\u043a\u0441 \u043a\u0430\u043a\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u0442\u043e\u0447\u043a\u043e\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0438?

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435: \u043a\u043e\u0433\u0434\u0430 nums[m] < target , \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(i\\) \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0430\u0435\u0442\u0441\u044f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u043e\u043b\u044c\u0448\u0435 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u0435\u043d target . \u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(j\\) \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0430\u0435\u0442\u0441\u044f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u0435\u043d\u044c\u0448\u0435 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u0435\u043d target .

\u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435: \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(i\\) \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0431\u043e\u043b\u044c\u0448\u0438\u0439 target , \u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(j\\) \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043c\u0435\u043d\u044c\u0448\u0438\u0439 target . \u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432\u044b\u0432\u043e\u0434, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 target , \u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0440\u0430\u0432\u0435\u043d \\(i\\) . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
def binary_search_insertion_simple(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\"\"\"\n    i, j = 0, len(nums) - 1  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target:\n            i = m + 1  # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        else:\n            return m  # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n    # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n
binary_search_insertion.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nint binarySearchInsertionSimple(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nint binarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nint BinarySearchInsertionSimple(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfunc binarySearchInsertionSimple(nums []int, target int) int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        } else {\n            // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n            return m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfunc binarySearchInsertionSimple(nums: [Int], target: Int) -> Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target {\n            i = m + 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfunction binarySearchInsertionSimple(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Math.floor() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfunction binarySearchInsertionSimple(\n    nums: Array<number>,\n    target: number\n): number {\n    let i = 0,\n        j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Math.floor() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nint binarySearchInsertionSimple(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    if (nums[m] < target) {\n      i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    } else {\n      return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n    }\n  }\n  // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n  return i;\n}\n
binary_search_insertion.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfn binary_search_insertion_simple(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m;\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    i\n}\n
binary_search_insertion.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nint binarySearchInsertionSimple(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m; // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) */\nfun binarySearchInsertionSimple(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            return m // \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n        }\n    }\n    // target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.rb
=begin\nFile: binary_search_insertion.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) ###\ndef binary_search_insertion_simple(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    else\n      return m  # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n    end\n  end\n\n  i # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_searching/binary_search_insertion/#1022","title":"10.2.2 \u00a0 \u0421\u043b\u0443\u0447\u0430\u0439 \u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438","text":"

Question

\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0430 \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0431\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.

\u0415\u0441\u043b\u0438 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 target , \u0442\u043e \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445, \u043d\u043e \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0432\u0430 \u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043e\u0442 \u043d\u0435\u0433\u043e.

\u041f\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044e \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0430\u043c\u0443\u044e \u043b\u0435\u0432\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e target \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435. \u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-5.

  1. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u044e\u0431\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 target , \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a \\(k\\) .
  2. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \\(k\\) , \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0432\u043b\u0435\u0432\u043e \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0439\u0434\u0435\u043d \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 target .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-5 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0441\u0440\u0435\u0434\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432

\u042d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u043e\u0434\u043d\u0430\u043a\u043e \u0432 \u043d\u0435\u043c \u0435\u0441\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) . \u041a\u043e\u0433\u0434\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f target , \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043a\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-6, \u043e\u0431\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c: \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b \\(m\\) , \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c target \u0438 nums[m] , \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0441\u043b\u0443\u0447\u0430\u0438.

\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(i\\) \u0431\u0443\u0434\u0435\u0442 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043d\u0430 \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 target , \u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \\(j\\) - \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043c\u0435\u043d\u044c\u0448\u0438\u0439 target , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441 \\(i\\) \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u0447\u043a\u043e\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0438.

<1><2><3><4><5><6><7><8>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-6 \u00a0 \u0428\u0430\u0433\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0434\u043b\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434, \u0442\u043e \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432 \u0432\u0435\u0442\u0432\u044f\u0445 nums[m] > target \u0438 nums[m] == target \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u0438 \u0434\u0432\u0435 \u0432\u0435\u0442\u0432\u0438 \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c.

\u0414\u0430\u0436\u0435 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044b\u043c\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0442\u0430\u043a \u043b\u043e\u0433\u0438\u043a\u0430 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0431\u043e\u043b\u0435\u0435 \u044f\u0441\u043d\u043e\u0439 \u0438 \u043a\u043e\u0434 \u043b\u0435\u0433\u0447\u0435 \u0447\u0438\u0442\u0430\u0442\u044c.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_insertion.py
def binary_search_insertion(nums: list[int], target: int) -> int:\n    \"\"\"\u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438)\"\"\"\n    i, j = 0, len(nums) - 1  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while i <= j:\n        m = (i + j) // 2  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target:\n            i = m + 1  # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        elif nums[m] > target:\n            j = m - 1  # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        else:\n            j = m - 1  # \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n
binary_search_insertion.cpp
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nint binarySearchInsertion(vector<int> &nums, int target) {\n    int i = 0, j = nums.size() - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.java
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nint binarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.cs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nint BinarySearchInsertion(int[] nums, int target) {\n    int i = 0, j = nums.Length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.go
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nfunc binarySearchInsertion(nums []int, target int) int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    i, j := 0, len(nums)-1\n    for i <= j {\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        m := i + (j-i)/2\n        if nums[m] < target {\n            // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n            i = m + 1\n        } else if nums[m] > target {\n            // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        } else {\n            // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n            j = m - 1\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.swift
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nfunc binarySearchInsertion(nums: [Int], target: Int) -> Int {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    var i = nums.startIndex\n    var j = nums.endIndex - 1\n    while i <= j {\n        let m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m] < target {\n            i = m + 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if nums[m] > target {\n            j = m - 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1 // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.js
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nfunction binarySearchInsertion(nums, target) {\n    let i = 0,\n        j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Math.floor() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.ts
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nfunction binarySearchInsertion(nums: Array<number>, target: number): number {\n    let i = 0,\n        j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        const m = Math.floor(i + (j - i) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f Math.floor() \u0434\u043b\u044f \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u044f \u0432\u043d\u0438\u0437\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.dart
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nint binarySearchInsertion(List<int> nums, int target) {\n  int i = 0, j = nums.length - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n  while (i <= j) {\n    int m = i + (j - i) ~/ 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    if (nums[m] < target) {\n      i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    } else if (nums[m] > target) {\n      j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    } else {\n      j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    }\n  }\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n  return i;\n}\n
binary_search_insertion.rs
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\npub fn binary_search_insertion(nums: &[i32], target: i32) -> i32 {\n    let (mut i, mut j) = (0, nums.len() as i32 - 1); // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while i <= j {\n        let m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if nums[m as usize] < target {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if nums[m as usize] > target {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    i\n}\n
binary_search_insertion.c
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nint binarySearchInsertion(int *nums, int numSize, int target) {\n    int i = 0, j = numSize - 1; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        int m = i + (j - i) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1; // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1; // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i;\n}\n
binary_search_insertion.kt
/* \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) */\nfun binarySearchInsertion(nums: IntArray, target: Int): Int {\n    var i = 0\n    var j = nums.size - 1 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n    while (i <= j) {\n        val m = i + (j - i) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n        if (nums[m] < target) {\n            i = m + 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n        } else if (nums[m] > target) {\n            j = m - 1 // target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        } else {\n            j = m - 1 // \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\n    return i\n}\n
binary_search_insertion.rb
=begin\nFile: binary_search_insertion.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0431\u0435\u0437 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432) ###\ndef binary_search_insertion_simple(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    else\n      return m  # \u041d\u0430\u0439\u0442\u0438 target \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 m\n    end\n  end\n\n  i # target \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\nend\n\n# ## \u0411\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0442\u043e\u0447\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 (\u0441 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438) ###\ndef binary_search_insertion(nums, target)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [0, n-1]\n  i, j = 0, nums.length - 1\n\n  while i <= j\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b m\n    m = (i + j) / 2\n\n    if nums[m] < target\n      i = m + 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [m+1, j]\n    elsif nums[m] > target\n      j = m - 1 # target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    else\n      j = m - 1 # \u041f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u043d\u044c\u0448\u0435 target \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 [i, m-1]\n    end\n  end\n\n  i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0442\u043e\u0447\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 i\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

Tip

\u041a\u043e\u0434 \u0432 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043d \u0432 \u0441\u0442\u0438\u043b\u0435 \"\u0434\u0432\u043e\u0439\u043d\u043e\u0433\u043e \u0437\u0430\u043c\u043a\u043d\u0443\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0430\". \u041f\u0440\u0438 \u0436\u0435\u043b\u0430\u043d\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \"\u0441\u043b\u0435\u0432\u0430 \u0437\u0430\u043a\u0440\u044b\u0442, \u0441\u043f\u0440\u0430\u0432\u0430 \u043e\u0442\u043a\u0440\u044b\u0442\".

\u0415\u0441\u043b\u0438 \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u0446\u0435\u043b\u043e\u043c, \u0441\u0443\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0434\u043b\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 \\(i\\) \u0438 \\(j\\) \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u0446\u0435\u043b\u0438 \u043f\u043e\u0438\u0441\u043a\u0430; \u0446\u0435\u043b\u044c\u044e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, target ), \u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0438 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u043c\u0435\u043d\u044c\u0448\u0438\u0435 target ).

\u0412 \u0445\u043e\u0434\u0435 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 \\(i\\) \u0438 \\(j\\) \u043f\u043e\u0441\u0442\u0435\u043f\u0435\u043d\u043d\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0436\u0430\u044e\u0442\u0441\u044f \u043a \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0446\u0435\u043b\u0438. \u0412 \u043a\u043e\u043d\u0446\u0435 \u043e\u043d\u0438 \u043b\u0438\u0431\u043e \u0443\u0441\u043f\u0435\u0448\u043d\u043e \u043d\u0430\u0445\u043e\u0434\u044f\u0442 \u043e\u0442\u0432\u0435\u0442, \u043b\u0438\u0431\u043e \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b.

"},{"location":"chapter_searching/replace_linear_by_hashing/","title":"10.4 \u00a0 \u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u0445\u0435\u0448-\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u0412 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043c\u044b \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u043d\u0430 \u0445\u0435\u0448-\u043f\u043e\u0438\u0441\u043a, \u0447\u0442\u043e\u0431\u044b \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043e\u0434\u043d\u0443 \u0437\u0430\u0434\u0430\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u043b\u0443\u0447\u0448\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u0438\u0435\u043c.

Question

\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b nums \u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 target . \u041d\u0430\u0439\u0434\u0438\u0442\u0435 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0441\u0443\u043c\u043c\u0430 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0432\u043d\u0430 target , \u0438 \u0432\u0435\u0440\u043d\u0438\u0442\u0435 \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u044b. \u041f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u043b\u044e\u0431\u043e\u0439 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442.

"},{"location":"chapter_searching/replace_linear_by_hashing/#1041","title":"10.4.1 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u043e\u0431\u043c\u0435\u043d \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u044f\u043c\u043e\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u0439. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-9, \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0434\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0440\u0430\u0432\u043d\u0430 \u043b\u0438 \u0441\u0443\u043c\u043c\u0430 \u0434\u0432\u0443\u0445 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b target ; \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-9 \u00a0 \u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0434\u0432\u0443\u0445 \u0441\u0443\u043c\u043c\u0430\u0445

\u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
def two_sum_brute_force(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440\"\"\"\n    # \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for i in range(len(nums) - 1):\n        for j in range(i + 1, len(nums)):\n            if nums[i] + nums[j] == target:\n                return [i, j]\n    return []\n
two_sum.cpp
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nvector<int> twoSumBruteForce(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return {i, j};\n        }\n    }\n    return {};\n}\n
two_sum.java
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint[] twoSumBruteForce(int[] nums, int target) {\n    int size = nums.length;\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return new int[] { i, j };\n        }\n    }\n    return new int[0];\n}\n
two_sum.cs
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint[] TwoSumBruteForce(int[] nums, int target) {\n    int size = nums.Length;\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (int i = 0; i < size - 1; i++) {\n        for (int j = i + 1; j < size; j++) {\n            if (nums[i] + nums[j] == target)\n                return [i, j];\n        }\n    }\n    return [];\n}\n
two_sum.go
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc twoSumBruteForce(nums []int, target int) []int {\n    size := len(nums)\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for i := 0; i < size-1; i++ {\n        for j := i + 1; j < size; j++ {\n            if nums[i]+nums[j] == target {\n                return []int{i, j}\n            }\n        }\n    }\n    return nil\n}\n
two_sum.swift
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunc twoSumBruteForce(nums: [Int], target: Int) -> [Int] {\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for i in nums.indices.dropLast() {\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[i] + nums[j] == target {\n                return [i, j]\n            }\n        }\n    }\n    return [0]\n}\n
two_sum.js
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction twoSumBruteForce(nums, target) {\n    const n = nums.length;\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
two_sum.ts
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfunction twoSumBruteForce(nums: number[], target: number): number[] {\n    const n = nums.length;\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (let i = 0; i < n; i++) {\n        for (let j = i + 1; j < n; j++) {\n            if (nums[i] + nums[j] === target) {\n                return [i, j];\n            }\n        }\n    }\n    return [];\n}\n
two_sum.dart
/* \u0421\u043f\u043e\u0441\u043e\u0431 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nList<int> twoSumBruteForce(List<int> nums, int target) {\n  int size = nums.length;\n  // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n  for (var i = 0; i < size - 1; i++) {\n    for (var j = i + 1; j < size; j++) {\n      if (nums[i] + nums[j] == target) return [i, j];\n    }\n  }\n  return [0];\n}\n
two_sum.rs
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\npub fn two_sum_brute_force(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    let size = nums.len();\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for i in 0..size - 1 {\n        for j in i + 1..size {\n            if nums[i] + nums[j] == target {\n                return Some(vec![i as i32, j as i32]);\n            }\n        }\n    }\n    None\n}\n
two_sum.c
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nint *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {\n    for (int i = 0; i < numsSize; ++i) {\n        for (int j = i + 1; j < numsSize; ++j) {\n            if (nums[i] + nums[j] == target) {\n                int *res = malloc(sizeof(int) * 2);\n                res[0] = i, res[1] = j;\n                *returnSize = 2;\n                return res;\n            }\n        }\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
two_sum.kt
/* \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 */\nfun twoSumBruteForce(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n    for (i in 0..<size - 1) {\n        for (j in i + 1..<size) {\n            if (nums[i] + nums[j] == target) return intArrayOf(i, j)\n        }\n    }\n    return IntArray(0)\n}\n
two_sum.rb
=begin\nFile: two_sum.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef two_sum_brute_force(nums, target)\n  # \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u043c\u0435\u0442\u043e\u0434\u0430 \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) , \u0430 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(1)\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u043d \u043e\u0447\u0435\u043d\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0439.

"},{"location":"chapter_searching/replace_linear_by_hashing/#1042-","title":"10.4.2 \u00a0 \u0425\u0435\u0448-\u043f\u043e\u0438\u0441\u043a: \u043e\u0431\u043c\u0435\u043d \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430 \u043d\u0430 \u0432\u0440\u0435\u043c\u044f","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b, \u0433\u0434\u0435 \u043a\u043b\u044e\u0447\u0430\u043c\u0438 \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u0431\u0443\u0434\u0443\u0442 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0438\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u044b. \u041f\u0440\u0438 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-10.

  1. \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0438 \u0447\u0438\u0441\u043b\u043e target - nums[i] \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435; \u0435\u0441\u043b\u0438 \u0434\u0430, \u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432.
  2. \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u043f\u0430\u0440\u0443 \u0438\u0437 \u043a\u043b\u044e\u0447\u0430 nums[i] \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 i .
<1><2><3>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-10 \u00a0 \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0434\u043b\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043e \u0434\u0432\u0443\u0445 \u0441\u0443\u043c\u043c\u0430\u0445

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0438\u0436\u0435, \u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby two_sum.py
def two_sum_hash_table(nums: list[int], target: int) -> list[int]:\n    \"\"\"\u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430\"\"\"\n    # \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    dic = {}\n    # \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for i in range(len(nums)):\n        if target - nums[i] in dic:\n            return [dic[target - nums[i]], i]\n        dic[nums[i]] = i\n    return []\n
two_sum.cpp
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nvector<int> twoSumHashTable(vector<int> &nums, int target) {\n    int size = nums.size();\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    unordered_map<int, int> dic;\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.find(target - nums[i]) != dic.end()) {\n            return {dic[target - nums[i]], i};\n        }\n        dic.emplace(nums[i], i);\n    }\n    return {};\n}\n
two_sum.java
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nint[] twoSumHashTable(int[] nums, int target) {\n    int size = nums.length;\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    Map<Integer, Integer> dic = new HashMap<>();\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.containsKey(target - nums[i])) {\n            return new int[] { dic.get(target - nums[i]), i };\n        }\n        dic.put(nums[i], i);\n    }\n    return new int[0];\n}\n
two_sum.cs
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nint[] TwoSumHashTable(int[] nums, int target) {\n    int size = nums.Length;\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    Dictionary<int, int> dic = [];\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (int i = 0; i < size; i++) {\n        if (dic.ContainsKey(target - nums[i])) {\n            return [dic[target - nums[i]], i];\n        }\n        dic.Add(nums[i], i);\n    }\n    return [];\n}\n
two_sum.go
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nfunc twoSumHashTable(nums []int, target int) []int {\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    hashTable := map[int]int{}\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for idx, val := range nums {\n        if preIdx, ok := hashTable[target-val]; ok {\n            return []int{preIdx, idx}\n        }\n        hashTable[val] = idx\n    }\n    return nil\n}\n
two_sum.swift
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nfunc twoSumHashTable(nums: [Int], target: Int) -> [Int] {\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    var dic: [Int: Int] = [:]\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for i in nums.indices {\n        if let j = dic[target - nums[i]] {\n            return [j, i]\n        }\n        dic[nums[i]] = i\n    }\n    return [0]\n}\n
two_sum.js
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nfunction twoSumHashTable(nums, target) {\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    let m = {};\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (let i = 0; i < nums.length; i++) {\n        if (m[target - nums[i]] !== undefined) {\n            return [m[target - nums[i]], i];\n        } else {\n            m[nums[i]] = i;\n        }\n    }\n    return [];\n}\n
two_sum.ts
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nfunction twoSumHashTable(nums: number[], target: number): number[] {\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    let m: Map<number, number> = new Map();\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (let i = 0; i < nums.length; i++) {\n        let index = m.get(target - nums[i]);\n        if (index !== undefined) {\n            return [index, i];\n        } else {\n            m.set(nums[i], i);\n        }\n    }\n    return [];\n}\n
two_sum.dart
/* \u0421\u043f\u043e\u0441\u043e\u0431 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nList<int> twoSumHashTable(List<int> nums, int target) {\n  int size = nums.length;\n  // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n  Map<int, int> dic = HashMap();\n  // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n  for (var i = 0; i < size; i++) {\n    if (dic.containsKey(target - nums[i])) {\n      return [dic[target - nums[i]]!, i];\n    }\n    dic.putIfAbsent(nums[i], () => i);\n  }\n  return [0];\n}\n
two_sum.rs
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\npub fn two_sum_hash_table(nums: &Vec<i32>, target: i32) -> Option<Vec<i32>> {\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    let mut dic = HashMap::new();\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (i, num) in nums.iter().enumerate() {\n        match dic.get(&(target - num)) {\n            Some(v) => return Some(vec![*v as i32, i as i32]),\n            None => dic.insert(num, i as i32),\n        };\n    }\n    None\n}\n
two_sum.c
/* \u0425\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\ntypedef struct {\n    int key;\n    int val;\n    UT_hash_handle hh; // \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 uthash.h\n} HashTable;\n\n/* \u041f\u043e\u0438\u0441\u043a \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0435 */\nHashTable *find(HashTable *h, int key) {\n    HashTable *tmp;\n    HASH_FIND_INT(h, &key, tmp);\n    return tmp;\n}\n\n/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443 */\nvoid insert(HashTable **h, int key, int val) {\n    HashTable *t = find(*h, key);\n    if (t == NULL) {\n        HashTable *tmp = malloc(sizeof(HashTable));\n        tmp->key = key, tmp->val = val;\n        HASH_ADD_INT(*h, key, tmp);\n    } else {\n        t->val = val;\n    }\n}\n\n/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nint *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {\n    HashTable *hashtable = NULL;\n    for (int i = 0; i < numsSize; i++) {\n        HashTable *t = find(hashtable, target - nums[i]);\n        if (t != NULL) {\n            int *res = malloc(sizeof(int) * 2);\n            res[0] = t->val, res[1] = i;\n            *returnSize = 2;\n            return res;\n        }\n        insert(&hashtable, nums[i], i);\n    }\n    *returnSize = 0;\n    return NULL;\n}\n
two_sum.kt
/* \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 */\nfun twoSumHashTable(nums: IntArray, target: Int): IntArray {\n    val size = nums.size\n    // \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    val dic = HashMap<Int, Int>()\n    // \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n    for (i in 0..<size) {\n        if (dic.containsKey(target - nums[i])) {\n            return intArrayOf(dic[target - nums[i]]!!, i)\n        }\n        dic[nums[i]] = i\n    }\n    return IntArray(0)\n}\n
two_sum.rb
=begin\nFile: two_sum.rb\nCreated Time: 2024-04-09\nAuthor: Blue Bean (lonnnnnnner@gmail.com)\n=end\n\n# ## \u041c\u0435\u0442\u043e\u0434 1: \u043f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 ###\ndef two_sum_brute_force(nums, target)\n  # \u0414\u0432\u0430 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u0430, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n^2)\n  for i in 0...(nums.length - 1)\n    for j in (i + 1)...nums.length\n      return [i, j] if nums[i] + nums[j] == target\n    end\n  end\n\n  []\nend\n\n# ## \u041c\u0435\u0442\u043e\u0434 2: \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430 ###\ndef two_sum_hash_table(nums, target)\n  # \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n  dic = {}\n  # \u041e\u0434\u0438\u043d \u0446\u0438\u043a\u043b, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\n  for i in 0...nums.length\n    return [dic[target - nums[i]], i] if dic.has_key?(target - nums[i])\n\n    dic[nums[i]] = i\n  end\n\n  []\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0445\u0435\u0448-\u043f\u043e\u0438\u0441\u043a\u0443 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0441\u043d\u0438\u0436\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \\(O(n^2)\\) \u0434\u043e \\(O(n)\\) , \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0432\u044b\u0448\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b.

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \\(O(n)\\) . \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u044d\u0442\u043e, \u0432 \u0446\u0435\u043b\u043e\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043b\u0443\u0447\u0448\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u043d \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

"},{"location":"chapter_searching/searching_algorithm_revisited/","title":"10.5 \u00a0 \u041f\u0435\u0440\u0435\u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 (searching algorithm) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0438\u043b\u0438 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0449\u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c, \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a \u043c\u0430\u0441\u0441\u0438\u0432\u044b, \u0441\u043f\u0438\u0441\u043a\u0438, \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u0438\u043b\u0438 \u0433\u0440\u0430\u0444\u044b.

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0435 \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u0438 \u043f\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438.

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u044d\u0442\u0438 \u0442\u0435\u043c\u044b \u0443\u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u043b\u0438\u0441\u044c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0433\u043b\u0430\u0432\u0430\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0430\u043c \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u044b. \u0412 \u044d\u0442\u043e\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u0435\u0449\u0435 \u0440\u0430\u0437 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043d\u0438\u0445, \u043d\u043e \u0443\u0436\u0435 \u0431\u043e\u043b\u0435\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e.

"},{"location":"chapter_searching/searching_algorithm_revisited/#1051","title":"10.5.1 \u00a0 \u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440","text":"

\u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043c\u044b \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.

\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u043f\u0435\u0440\u0435\u0431\u043e\u0440\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0435\u0433\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u0435 \u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041e\u0434\u043d\u0430\u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0440\u0430\u0432\u043d\u0430 \\(O(n)\\) , \u0433\u0434\u0435 \\(n\\) - \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0445 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043d\u0435\u0432\u044b\u0441\u043e\u043a\u0430.

"},{"location":"chapter_searching/searching_algorithm_revisited/#1052","title":"10.5.2 \u00a0 \u0410\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a","text":"

\u0410\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c), \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442.

\u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u044d\u0442\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0432 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438, \u0438\u0445 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0442\u044c \\(O(\\log n)\\) \u0438 \u0434\u0430\u0436\u0435 \\(O(1)\\) .

\u041e\u0434\u043d\u0430\u043a\u043e \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0442\u0430\u043a\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432, \u0430 \u0445\u0435\u0448-\u043f\u043e\u0438\u0441\u043a \u0438 \u043f\u043e\u0438\u0441\u043a \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0442\u043e\u0436\u0435 \u043e\u0442\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0440\u0435\u043c\u044f \u0438 \u043f\u0430\u043c\u044f\u0442\u044c.

Tip

\u0410\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0443\u0437\u043a\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d\u0438 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.

"},{"location":"chapter_searching/searching_algorithm_revisited/#1053","title":"10.5.3 \u00a0 \u0412\u044b\u0431\u043e\u0440 \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u043e\u0438\u0441\u043a\u0430","text":"

\u0414\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043d\u0430\u0431\u043e\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \\(n\\) \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a, \u043f\u043e\u0438\u0441\u043a \u0432 \u0434\u0435\u0440\u0435\u0432\u0435, \u0445\u0435\u0448-\u043f\u043e\u0438\u0441\u043a \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u044b. \u041f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0440\u0430\u0431\u043e\u0442\u044b \u044d\u0442\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 10-11.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 10-11 \u00a0 \u0420\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \u043f\u043e\u0438\u0441\u043a\u0430

\u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u0435\u0440\u0435\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 10-1.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 10-1 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430

\u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u041f\u043e\u0438\u0441\u043a \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u0425\u0435\u0448-\u043f\u043e\u0438\u0441\u043a \u041f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(\\log n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(1)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(n)\\) \\(O(\\log n)\\) \\(O(1)\\) \u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u043e \\(O(1)\\) \\(O(1)\\) \\(O(n)\\) \\(O(n)\\) \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 / \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \\(O(n \\log n)\\) \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0435\u0432\u0430 \\(O(n \\log n)\\) \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0445\u0435\u0448-\u0442\u0430\u0431\u043b\u0438\u0446\u044b \\(O(n)\\) \u0423\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445 \u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0422\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f

\u0412\u044b\u0431\u043e\u0440 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439 \u043a \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0447\u0430\u0441\u0442\u043e\u0442\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0439 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041b\u0438\u043d\u0435\u0439\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a

\u0414\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a

\u0425\u0435\u0448-\u043f\u043e\u0438\u0441\u043a

\u041f\u043e\u0438\u0441\u043a \u0432 \u0434\u0435\u0440\u0435\u0432\u0435

"},{"location":"chapter_searching/summary/","title":"10.6 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_searching/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":""},{"location":"chapter_sorting/","title":"\u0413\u043b\u0430\u0432\u0430 11. \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

Abstract

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u0435\u0442 \u0445\u0430\u043e\u0442\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438.

\u0417\u0430 \u043a\u0430\u0436\u0443\u0449\u0435\u0439\u0441\u044f \u043f\u0440\u043e\u0441\u0442\u043e\u0442\u043e\u0439 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0446\u0435\u043b\u0430\u044f \u0433\u0440\u0443\u043f\u043f\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0434\u043e\u0441\u0442\u043e\u0438\u043d\u0441\u0442\u0432\u0430\u043c\u0438 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438.

"},{"location":"chapter_sorting/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_sorting/bubble_sort/","title":"11.3 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c","text":"

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c (bubble sort) \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0433\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u0442 \u0432\u0441\u043f\u043b\u044b\u0442\u0438\u0435 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432 \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u043e\u0442\u043a\u0443\u0434\u0430 \u0438 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-4, \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \"\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\" \u043c\u043e\u0436\u043d\u043e \u0441\u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043e\u0431\u043c\u0435\u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432: \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u043b\u0435\u0432\u043e\u0433\u043e \u043a\u0440\u0430\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0434\u0432\u0438\u0433\u0430\u044f\u0441\u044c \u0432\u043f\u0440\u0430\u0432\u043e, \u043c\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438, \u0435\u0441\u043b\u0438 \"\u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 > \u043f\u0440\u0430\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\", \u043c\u0435\u043d\u044f\u0435\u043c \u0438\u0445 \u043c\u0435\u0441\u0442\u0430\u043c\u0438. \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d \u0432 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.

<1><2><3><4><5><6><7>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-4 \u00a0 \u041c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432

"},{"location":"chapter_sorting/bubble_sort/#1131","title":"11.3.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(n\\) ; \u0442\u043e\u0433\u0434\u0430 \u0448\u0430\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-5.

  1. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u043f\u0440\u043e\u0445\u043e\u0434 \"\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\" \u043f\u043e \\(n\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0432 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e.
  2. \u0417\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u0435\" \u043f\u043e \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u043c\u0441\u044f \\(n - 1\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c, \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0432 \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e.
  3. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438; \u043f\u043e\u0441\u043b\u0435 \\(n - 1\\) \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \"\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\" \u043f\u0435\u0440\u0432\u044b\u0435 \\(n - 1\\) \u043f\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u043a\u0430\u0436\u0443\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u0445.
  4. \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0443\u0436\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-5 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c

\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
def bubble_sort(nums: list[int]):\n    \"\"\"\u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\"\"\"\n    n = len(nums)\n    # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in range(n - 1, 0, -1):\n        # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n
bubble_sort.cpp
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid bubbleSort(vector<int> &nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                // \u0417\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f std::swap()\n                swap(nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
bubble_sort.java
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid bubbleSort(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
bubble_sort.cs
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid BubbleSort(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n            }\n        }\n    }\n}\n
bubble_sort.go
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc bubbleSort(nums []int) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n            }\n        }\n    }\n}\n
bubble_sort.swift
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc bubbleSort(nums: inout [Int]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums.swapAt(j, j + 1)\n            }\n        }\n    }\n}\n
bubble_sort.js
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunction bubbleSort(nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
bubble_sort.ts
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunction bubbleSort(nums: number[]): void {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n            }\n        }\n    }\n}\n
bubble_sort.dart
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid bubbleSort(List<int> nums) {\n  // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n      }\n    }\n  }\n}\n
bubble_sort.rs
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfn bubble_sort(nums: &mut [i32]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in (1..nums.len()).rev() {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums.swap(j, j + 1);\n            }\n        }\n    }\n}\n
bubble_sort.c
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid bubbleSort(int nums[], int size) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n            }\n        }\n    }\n}\n
bubble_sort.kt
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfun bubbleSort(nums: IntArray) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n            }\n        }\n    }\n}\n
bubble_sort.rb
=begin\nFile: bubble_sort.rb\nCreated Time: 2024-05-02\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 ###\ndef bubble_sort(nums)\n  n = nums.length\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (n - 1).downto(1)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/bubble_sort/#1132","title":"11.3.2 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438","text":"

\u041c\u044b \u0437\u0430\u043c\u0435\u0447\u0430\u0435\u043c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432 \u043a\u0430\u043a\u043e\u043c-\u043b\u0438\u0431\u043e \u0440\u0430\u0443\u043d\u0434\u0435 \"\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\" \u043d\u0435 \u043f\u0440\u043e\u0438\u0437\u043e\u0448\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0437\u043d\u0430\u0447\u0438\u0442, \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0444\u043b\u0430\u0433 flag \u0434\u043b\u044f \u043e\u0442\u0441\u043b\u0435\u0436\u0438\u0432\u0430\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0438 \u043d\u0435\u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0445\u043e\u0434\u0430.

\u041f\u043e\u0441\u043b\u0435 \u0442\u0430\u043a\u043e\u0439 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0445\u0443\u0434\u0448\u0430\u044f \u0438 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0440\u0430\u0432\u043d\u044b \\(O(n^2)\\) ; \u043e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0443\u0436\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d, \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\) .

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bubble_sort.py
def bubble_sort_with_flag(nums: list[int]):\n    \"\"\"\u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c)\"\"\"\n    n = len(nums)\n    # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in range(n - 1, 0, -1):\n        flag = False  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in range(i):\n            if nums[j] > nums[j + 1]:\n                # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums[j], nums[j + 1] = nums[j + 1], nums[j]\n                flag = True  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        if not flag:\n            break  # \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n
bubble_sort.cpp
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nvoid bubbleSortWithFlag(vector<int> &nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.size() - 1; i > 0; i--) {\n        bool flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                // \u0417\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f std::swap()\n                swap(nums[j], nums[j + 1]);\n                flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag)\n            break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.java
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nvoid bubbleSortWithFlag(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.length - 1; i > 0; i--) {\n        boolean flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                int tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag)\n            break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.cs
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nvoid BubbleSortWithFlag(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = nums.Length - 1; i > 0; i--) {\n        bool flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                (nums[j + 1], nums[j]) = (nums[j], nums[j + 1]);\n                flag = true;  // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag) break;     // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.go
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfunc bubbleSortWithFlag(nums []int) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i := len(nums) - 1; i > 0; i-- {\n        flag := false // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j := 0; j < i; j++ {\n            if nums[j] > nums[j+1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums[j], nums[j+1] = nums[j+1], nums[j]\n                flag = true // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if flag == false { // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n            break\n        }\n    }\n}\n
bubble_sort.swift
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfunc bubbleSortWithFlag(nums: inout [Int]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in nums.indices.dropFirst().reversed() {\n        var flag = false // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        for j in 0 ..< i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums.swapAt(j, j + 1)\n                flag = true // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if !flag { // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n            break\n        }\n    }\n}\n
bubble_sort.js
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfunction bubbleSortWithFlag(nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag) break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.ts
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfunction bubbleSortWithFlag(nums: number[]): void {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (let i = nums.length - 1; i > 0; i--) {\n        let flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (let j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                let tmp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = tmp;\n                flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag) break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.dart
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nvoid bubbleSortWithFlag(List<int> nums) {\n  // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for (int i = nums.length - 1; i > 0; i--) {\n    bool flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n    // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for (int j = 0; j < i; j++) {\n      if (nums[j] > nums[j + 1]) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        int tmp = nums[j];\n        nums[j] = nums[j + 1];\n        nums[j + 1] = tmp;\n        flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n      }\n    }\n    if (!flag) break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n  }\n}\n
bubble_sort.rs
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfn bubble_sort_with_flag(nums: &mut [i32]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for i in (1..nums.len()).rev() {\n        let mut flag = false; // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for j in 0..i {\n            if nums[j] > nums[j + 1] {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                nums.swap(j, j + 1);\n                flag = true; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if !flag {\n            break; // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n        };\n    }\n}\n
bubble_sort.c
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nvoid bubbleSortWithFlag(int nums[], int size) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (int i = size - 1; i > 0; i--) {\n        bool flag = false;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (int j = 0; j < i; j++) {\n            if (nums[j] > nums[j + 1]) {\n                int temp = nums[j];\n                nums[j] = nums[j + 1];\n                nums[j + 1] = temp;\n                flag = true;\n            }\n        }\n        if (!flag)\n            break;\n    }\n}\n
bubble_sort.kt
/* \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) */\nfun bubbleSortWithFlag(nums: IntArray) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n    for (i in nums.size - 1 downTo 1) {\n        var flag = false // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n        for (j in 0..<i) {\n            if (nums[j] > nums[j + 1]) {\n                // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n                val temp = nums[j]\n                nums[j] = nums[j + 1]\n                nums[j + 1] = temp\n                flag = true // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n            }\n        }\n        if (!flag) break // \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n    }\n}\n
bubble_sort.rb
=begin\nFile: bubble_sort.rb\nCreated Time: 2024-05-02\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 ###\ndef bubble_sort(nums)\n  n = nums.length\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (n - 1).downto(1)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n      end\n    end\n  end\nend\n\n# ## \u041f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u0432\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0444\u043b\u0430\u0433\u043e\u043c) ###\ndef bubble_sort_with_flag(nums)\n  n = nums.length\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i]\n  for i in (n - 1).downto(1)\n    flag = false # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u043b\u0430\u0433\n\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i] \u0432 \u0435\u0433\u043e \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446\n    for j in 0...i\n      if nums[j] > nums[j + 1]\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 nums[j] \u0438 nums[j + 1]\n        nums[j], nums[j + 1] = nums[j + 1], nums[j]\n        flag = true # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n      end\n    end\n\n    break unless flag # \u041d\u0430 \u044d\u0442\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u00ab\u0432\u0441\u043f\u043b\u044b\u0442\u0438\u044f\u00bb \u043d\u0435 \u0431\u044b\u043b\u043e \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0431\u043c\u0435\u043d\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u044b\u0439\u0442\u0438\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/bubble_sort/#1133","title":"11.3.3 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/bucket_sort/","title":"11.8 \u00a0 \u0411\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u0435 \u0432\u044b\u0448\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439\": \u043e\u043d\u0438 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u044e\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0434\u0440\u0443\u0433 \u0441 \u0434\u0440\u0443\u0433\u043e\u043c. \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u0443\u0447\u0448\u0435 \\(O(n \\log n)\\) . \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043e\u043a \u0431\u0435\u0437 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439\", \u0447\u044c\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0442\u044c \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430.

\u0411\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (bucket sort) \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0438\u043f\u0438\u0447\u043d\u044b\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\". \u041e\u043d\u0430 \u0437\u0430\u0434\u0430\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u043f\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\u043c \u0431\u043b\u043e\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u043b\u043e\u043a \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439; \u0437\u0430\u0442\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c, \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u0430 \u0432 \u043a\u043e\u043d\u0446\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u043b\u043e\u043a\u043e\u0432 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.

"},{"location":"chapter_sorting/bucket_sort/#1181","title":"11.8.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b \\(n\\), \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438\u0437 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \\([0, 1)\\) . \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-13.

  1. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \\(k\\) \u0431\u043b\u043e\u043a\u043e\u0432 \u0438 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \\(n\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u044d\u0442\u0438\u043c \\(k\\) \u0431\u043b\u043e\u043a\u0430\u043c.
  2. \u041e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u043b\u043e\u043a \u043f\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 (\u0437\u0434\u0435\u0441\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f).
  3. \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u043e\u0432 \u043e\u0442 \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u043a \u0431\u043e\u043b\u044c\u0448\u0435\u043c\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-13 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438

\u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby bucket_sort.py
def bucket_sort(nums: list[float]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    k = len(nums) // 2\n    buckets = [[] for _ in range(k)]\n    # 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for num in nums:\n        # \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        i = int(num * k)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].append(num)\n    # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for bucket in buckets:\n        # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.sort()\n    # 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    i = 0\n    for bucket in buckets:\n        for num in bucket:\n            nums[i] = num\n            i += 1\n
bucket_sort.cpp
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nvoid bucketSort(vector<float> &nums) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    int k = nums.size() / 2;\n    vector<vector<float>> buckets(k);\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (float num : nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        int i = num * k;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 bucket_idx\n        buckets[i].push_back(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (vector<float> &bucket : buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        sort(bucket.begin(), bucket.end());\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    int i = 0;\n    for (vector<float> &bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
bucket_sort.java
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nvoid bucketSort(float[] nums) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    int k = nums.length / 2;\n    List<List<Float>> buckets = new ArrayList<>();\n    for (int i = 0; i < k; i++) {\n        buckets.add(new ArrayList<>());\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (float num : nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        int i = (int) (num * k);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets.get(i).add(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (List<Float> bucket : buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        Collections.sort(bucket);\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    int i = 0;\n    for (List<Float> bucket : buckets) {\n        for (float num : bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
bucket_sort.cs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nvoid BucketSort(float[] nums) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    int k = nums.Length / 2;\n    List<List<float>> buckets = [];\n    for (int i = 0; i < k; i++) {\n        buckets.Add([]);\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    foreach (float num in nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        int i = (int)(num * k);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].Add(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    foreach (List<float> bucket in buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.Sort();\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    int j = 0;\n    foreach (List<float> bucket in buckets) {\n        foreach (float num in bucket) {\n            nums[j++] = num;\n        }\n    }\n}\n
bucket_sort.go
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfunc bucketSort(nums []float64) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    k := len(nums) / 2\n    buckets := make([][]float64, k)\n    for i := 0; i < k; i++ {\n        buckets[i] = make([]float64, 0)\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for _, num := range nums {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        i := int(num * float64(k))\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i] = append(buckets[i], num)\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for i := 0; i < k; i++ {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u0440\u0435\u0437\u0430; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        sort.Float64s(buckets[i])\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    i := 0\n    for _, bucket := range buckets {\n        for _, num := range bucket {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
bucket_sort.swift
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfunc bucketSort(nums: inout [Double]) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    let k = nums.count / 2\n    var buckets = (0 ..< k).map { _ in [Double]() }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for num in nums {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        let i = Int(num * Double(k))\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].append(num)\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for i in buckets.indices {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        buckets[i].sort()\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    var i = nums.startIndex\n    for bucket in buckets {\n        for num in bucket {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
bucket_sort.js
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfunction bucketSort(nums) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    const k = nums.length / 2;\n    const buckets = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (const num of nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].push(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (const bucket of buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
bucket_sort.ts
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfunction bucketSort(nums: number[]): void {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    const k = nums.length / 2;\n    const buckets: number[][] = [];\n    for (let i = 0; i < k; i++) {\n        buckets.push([]);\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (const num of nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        const i = Math.floor(num * k);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].push(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (const bucket of buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.sort((a, b) => a - b);\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    let i = 0;\n    for (const bucket of buckets) {\n        for (const num of bucket) {\n            nums[i++] = num;\n        }\n    }\n}\n
bucket_sort.dart
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nvoid bucketSort(List<double> nums) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n  int k = nums.length ~/ 2;\n  List<List<double>> buckets = List.generate(k, (index) => []);\n\n  // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n  for (double _num in nums) {\n    // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1), \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c _num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n    int i = (_num * k).toInt();\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c _num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 bucket_idx\n    buckets[i].add(_num);\n  }\n  // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n  for (List<double> bucket in buckets) {\n    bucket.sort();\n  }\n  // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n  int i = 0;\n  for (List<double> bucket in buckets) {\n    for (double _num in bucket) {\n      nums[i++] = _num;\n    }\n  }\n}\n
bucket_sort.rs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfn bucket_sort(nums: &mut [f64]) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    let k = nums.len() / 2;\n    let mut buckets = vec![vec![]; k];\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for &num in nums.iter() {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        let i = (num * k as f64) as usize;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].push(num);\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for bucket in &mut buckets {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.sort_by(|a, b| a.partial_cmp(b).unwrap());\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    let mut i = 0;\n    for bucket in buckets.iter() {\n        for &num in bucket.iter() {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
bucket_sort.c
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nvoid bucketSort(float nums[], int n) {\n    int k = n / 2;                                 // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d\n    int *sizes = malloc(k * sizeof(int));          // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    float **buckets = malloc(k * sizeof(float *)); // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 (\u043a\u043e\u0440\u0437\u0438\u043d\u044b)\n    // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u0435\u0441\u0442\u0430 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (int i = 0; i < k; ++i) {\n        buckets[i] = (float *)malloc(n * sizeof(float));\n        sizes[i] = 0;\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (int i = 0; i < n; ++i) {\n        int idx = (int)(nums[i] * k);\n        buckets[idx][sizes[idx]++] = nums[i];\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (int i = 0; i < k; ++i) {\n        qsort(buckets[i], sizes[i], sizeof(float), compare);\n    }\n    // 3. \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    int idx = 0;\n    for (int i = 0; i < k; ++i) {\n        for (int j = 0; j < sizes[i]; ++j) {\n            nums[idx++] = buckets[i][j];\n        }\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        free(buckets[i]);\n    }\n}\n
bucket_sort.kt
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 */\nfun bucketSort(nums: FloatArray) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n    val k = nums.size / 2\n    val buckets = mutableListOf<MutableList<Float>>()\n    for (i in 0..<k) {\n        buckets.add(mutableListOf())\n    }\n    // 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n    for (num in nums) {\n        // \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n        val i = (num * k).toInt()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n        buckets[i].add(num)\n    }\n    // 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n    for (bucket in buckets) {\n        // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n        bucket.sort()\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n    var i = 0\n    for (bucket in buckets) {\n        for (num in bucket) {\n            nums[i++] = num\n        }\n    }\n}\n
bucket_sort.rb
=begin\nFile: bucket_sort.rb\nCreated Time: 2024-04-17\nAuthor: Martin Xu (martin.xus@gmail.com)\n=end\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\u0438 ###\ndef bucket_sort(nums)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c k = n/2 \u043a\u043e\u0440\u0437\u0438\u043d, \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u044f \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u0443\u044e \u043a\u043e\u0440\u0437\u0438\u043d\u0443\n  k = nums.length / 2\n  buckets = Array.new(k) { [] }\n\n  # 1. \u0420\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u043e \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u043c\n  nums.each do |num|\n    # \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0, 1); \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c num * k \u0434\u043b\u044f \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 [0, k-1]\n    i = (num * k).to_i\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0443 i\n    buckets[i] << num\n  end\n\n  # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043e\u0440\u0437\u0438\u043d\u044b\n  buckets.each do |bucket|\n    # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438; \u0435\u0435 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\n    bucket.sort!\n  end\n\n  # 3. \u041e\u0431\u043e\u0439\u0442\u0438 \u043a\u043e\u0440\u0437\u0438\u043d\u044b \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b\n  i = 0\n  buckets.each do |bucket|\n    bucket.each do |num|\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/bucket_sort/#1182","title":"11.8.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0411\u043b\u043e\u0447\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 1 \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u0438\u0437-\u0437\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0438\u0445 \u0432\u0441\u0435 \u0441\u0440\u0430\u0437\u0443, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 1000 \u0431\u043b\u043e\u043a\u043e\u0432, \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0431\u043b\u043e\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e, \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.

"},{"location":"chapter_sorting/bucket_sort/#1183","title":"11.8.3 \u00a0 \u041a\u0430\u043a \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f","text":"

\u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0442\u044c \\(O(n)\\) ; \u043a\u043b\u044e\u0447 \u043a \u044d\u0442\u043e\u043c\u0443 - \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u044b \u043d\u0435\u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0435 \u0442\u043e\u0432\u0430\u0440\u044b \u043d\u0430 Taobao \u043f\u043e 10 \u0431\u043b\u043e\u043a\u0430\u043c \u0446\u0435\u043d, \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0434\u0435\u0448\u0435\u0432\u043b\u0435 100 \u044e\u0430\u043d\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0430 \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0434\u043e\u0440\u043e\u0436\u0435 1000 \u044e\u0430\u043d\u0435\u0439 - \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u043c. \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0446\u0435\u043d \u043d\u0430 10 \u0440\u0430\u0432\u043d\u044b\u0445 \u0447\u0430\u0441\u0442\u0435\u0439, \u0447\u0438\u0441\u043b\u043e \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f.

\u0427\u0442\u043e\u0431\u044b \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u0434\u0430\u0442\u044c \u0433\u0440\u0443\u0431\u0443\u044e \u043b\u0438\u043d\u0438\u044e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u0438 \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e 3 \u0431\u043b\u043e\u043a\u0430\u043c. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0438 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0447\u0438\u0441\u043b\u043e\u043c \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u043e\u0432\u0430 \u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 3 \u0431\u043b\u043e\u043a\u0430 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0431\u043b\u043e\u043a\u0435 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-14, \u043f\u043e \u0441\u0443\u0442\u0438 \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0441\u0442\u0440\u043e\u0438\u0442 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u0446\u0435\u043b\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e - \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043b\u0438\u0441\u0442\u044c\u044f\u0445 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0434\u0435\u043b\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430 3 \u0431\u043b\u043e\u043a\u0430; \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0443\u044e \u0441\u0445\u0435\u043c\u0443 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-14 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c

\u0415\u0441\u043b\u0438 \u043d\u0430\u043c \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0446\u0435\u043d \u0442\u043e\u0432\u0430\u0440\u043e\u0432, \u0442\u043e \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u044d\u0442\u0438\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435\u043c. \u0412\u0430\u0436\u043d\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c - \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0431\u043b\u0438\u0437\u0438\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u0439 \u043c\u043e\u0434\u0435\u043b\u044c\u044e \u0438\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u0434\u0430\u043d\u043d\u044b\u0445.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-15, \u0435\u0441\u043b\u0438 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c, \u0447\u0442\u043e \u0446\u0435\u043d\u044b \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043d\u043e\u0440\u043c\u0430\u043b\u044c\u043d\u043e\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u044b \u0446\u0435\u043d \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u043e\u0432\u0430\u0440\u044b \u043f\u043e \u0431\u043b\u043e\u043a\u0430\u043c \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0440\u0430\u0432\u043d\u043e\u043c\u0435\u0440\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-15 \u00a0 \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u043e\u0432 \u043f\u043e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u043d\u043e\u043c\u0443 \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044e

"},{"location":"chapter_sorting/counting_sort/","title":"11.9 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c","text":"

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (counting sort) \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0437\u0430 \u0441\u0447\u0435\u0442 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b.

"},{"location":"chapter_sorting/counting_sort/#1191","title":"11.9.1 \u00a0 \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f","text":"

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440. \u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \"\u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438\". \u041e\u0431\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-16.

  1. \u041f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u043d\u0430\u0439\u0442\u0438 \u0432 \u043d\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a \\(m\\) , \u0430 \u0437\u0430\u0442\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 counter \u0434\u043b\u0438\u043d\u044b \\(m + 1\\) .
  2. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e counter \u043f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u043a\u0430\u0436\u0434\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 nums; \u043f\u0440\u0438 \u044d\u0442\u043e\u043c counter[num] \u0445\u0440\u0430\u043d\u0438\u0442 \u0447\u0438\u0441\u043b\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f num . \u0414\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e: \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e nums (\u043f\u0443\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0432\u043d\u043e num ) \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c counter[num] \u043d\u0430 \\(1\\) .
  3. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 counter \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u044b, \u043c\u043e\u0436\u043d\u043e \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0441\u0435 \u0447\u0438\u0441\u043b\u0430 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b. \u0414\u0430\u043b\u0435\u0435 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u043e counter \u0438 \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0447\u0438\u0441\u043b\u043e\u043c \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 nums \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-16 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c

\u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
def counting_sort_naive(nums: list[int]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c\"\"\"\n    # \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n    # 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    m = max(nums)\n    # 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    # counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    i = 0\n    for num in range(m + 1):\n        for _ in range(counter[num]):\n            nums[i] = num\n            i += 1\n
counting_sort.cpp
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nvoid countingSortNaive(vector<int> &nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
counting_sort.java
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nvoid countingSortNaive(int[] nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
counting_sort.cs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nvoid CountingSortNaive(int[] nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
counting_sort.go
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfunc countingSortNaive(nums []int) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    for i, num := 0, 0; num < m+1; num++ {\n        for j := 0; j < counter[num]; j++ {\n            nums[i] = num\n            i++\n        }\n    }\n}\n
counting_sort.swift
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfunc countingSortNaive(nums: inout [Int]) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = nums.max()!\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    var i = 0\n    for num in 0 ..< m + 1 {\n        for _ in 0 ..< counter[num] {\n            nums[i] = num\n            i += 1\n        }\n    }\n}\n
counting_sort.js
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfunction countingSortNaive(nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = Math.max(...nums);\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
counting_sort.ts
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfunction countingSortNaive(nums: number[]): void {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m: number = Math.max(...nums);\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    let i = 0;\n    for (let num = 0; num < m + 1; num++) {\n        for (let j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n}\n
counting_sort.dart
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nvoid countingSortNaive(List<int> nums) {\n  // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n  // counter[_num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n  int i = 0;\n  for (int _num = 0; _num < m + 1; _num++) {\n    for (int j = 0; j < counter[_num]; j++, i++) {\n      nums[i] = _num;\n    }\n  }\n}\n
counting_sort.rs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfn counting_sort_naive(nums: &mut [i32]) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = *nums.iter().max().unwrap();\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    let mut counter = vec![0; m as usize + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    let mut i = 0;\n    for num in 0..m + 1 {\n        for _ in 0..counter[num as usize] {\n            nums[i] = num;\n            i += 1;\n        }\n    }\n}\n
counting_sort.c
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nvoid countingSortNaive(int nums[], int size) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int *counter = calloc(m + 1, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    int i = 0;\n    for (int num = 0; num < m + 1; num++) {\n        for (int j = 0; j < counter[num]; j++, i++) {\n            nums[i] = num;\n        }\n    }\n    // 4. \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(counter);\n}\n
counting_sort.kt
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\nfun countingSortNaive(nums: IntArray) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n    var i = 0\n    for (num in 0..<m + 1) {\n        var j = 0\n        while (j < counter[num]) {\n            nums[i] = num\n            j++\n            i++\n        }\n    }\n}\n
counting_sort.rb
=begin\nFile: counting_sort.rb\nCreated Time: 2024-05-02\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c ###\ndef counting_sort_naive(nums)\n  # \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n  # counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u0438 \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439

\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0442\u043e \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 counter \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0431\u043b\u043e\u043a, \u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u0430 - \u043a\u0430\u043a \u0440\u0430\u0441\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0431\u043b\u043e\u043a\u0430\u043c. \u041f\u043e \u0441\u0443\u0442\u0438, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u043d\u044b\u043c \u0441\u043b\u0443\u0447\u0430\u0435\u043c \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u043b\u044f \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.

"},{"location":"chapter_sorting/counting_sort/#1192","title":"11.9.2 \u00a0 \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f","text":"

\u0412\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0433 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438, \u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0448\u0430\u0433 3. \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u0445\u043e\u0434\u043d\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0442\u043e\u0432\u0430\u0440\u043e\u0432 \u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0445 \u043f\u043e \u0446\u0435\u043d\u0435 (\u043f\u043e\u043b\u044e \u043a\u043b\u0430\u0441\u0441\u0430), \u0442\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0434\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0440\u044f\u0434 \u0446\u0435\u043d, \u043d\u043e \u043d\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043d\u0443\u0436\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.

\u041a\u0430\u043a \u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445? \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u043c \"\u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443\" \u043c\u0430\u0441\u0441\u0438\u0432\u0430 counter . \u041a\u0430\u043a \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435 i , \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u043c\u0430\u044f \u043a\u0430\u043a prefix[i] , \u0440\u0430\u0432\u043d\u0430 \u0441\u0443\u043c\u043c\u0435 \u043f\u0435\u0440\u0432\u044b\u0445 i \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430:

\\[ \\text{prefix}[i] = \\sum_{j=0}^i \\text{counter[j]} \\]

\u041f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0430\u044f \u0441\u0443\u043c\u043c\u0430 \u0438\u043c\u0435\u0435\u0442 \u0447\u0435\u0442\u043a\u0438\u0439 \u0441\u043c\u044b\u0441\u043b: prefix[num] - 1 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 res. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u0432 \u043a\u0430\u043a\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043f\u0430\u0441\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u0434\u0432\u0430 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f.

  1. \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c num \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 res \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 prefix[num] - 1 .
  2. \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 prefix[num] \u043d\u0430 \\(1\\) , \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num .

\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u043c\u0430\u0441\u0441\u0438\u0432 res \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442; \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u0430\u0442\u044c res \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 nums . \u041f\u043e\u043b\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-17.

<1><2><3><4><5><6><7><8>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-17 \u00a0 \u0428\u0430\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby counting_sort.py
def counting_sort(nums: list[int]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c\"\"\"\n    # \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\n    # 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    m = max(nums)\n    # 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    # counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    counter = [0] * (m + 1)\n    for num in nums:\n        counter[num] += 1\n    # 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    # \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for i in range(m):\n        counter[i + 1] += counter[i]\n    # 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    n = len(nums)\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        num = nums[i]\n        res[counter[num] - 1] = num  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num] -= 1  # \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for i in range(n):\n        nums[i] = res[i]\n
counting_sort.cpp
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nvoid countingSort(vector<int> &nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int num : nums) {\n        m = max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    vector<int> counter(m + 1, 0);\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    int n = nums.size();\n    vector<int> res(n);\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--;              // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    nums = res;\n}\n
counting_sort.java
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nvoid countingSort(int[] nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int num : nums) {\n        m = Math.max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int[] counter = new int[m + 1];\n    for (int num : nums) {\n        counter[num]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    int n = nums.length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
counting_sort.cs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nvoid CountingSort(int[] nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    foreach (int num in nums) {\n        m = Math.Max(m, num);\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int[] counter = new int[m + 1];\n    foreach (int num in nums) {\n        counter[num]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    int n = nums.Length;\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
counting_sort.go
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfunc countingSort(nums []int) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    m := 0\n    for _, num := range nums {\n        if num > m {\n            m = num\n        }\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    counter := make([]int, m+1)\n    for _, num := range nums {\n        counter[num]++\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for i := 0; i < m; i++ {\n        counter[i+1] += counter[i]\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    n := len(nums)\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        num := nums[i]\n        // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        res[counter[num]-1] = num\n        // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n        counter[num]--\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    copy(nums, res)\n}\n
counting_sort.swift
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfunc countingSort(nums: inout [Int]) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = nums.max()!\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    var counter = Array(repeating: 0, count: m + 1)\n    for num in nums {\n        counter[num] += 1\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for i in 0 ..< m {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let num = nums[i]\n        res[counter[num] - 1] = num // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num] -= 1 // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n
counting_sort.js
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfunction countingSort(nums) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = Math.max(...nums);\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    const counter = new Array(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    const n = nums.length;\n    const res = new Array(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
counting_sort.ts
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfunction countingSort(nums: number[]): void {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m: number = Math.max(...nums);\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    const counter: number[] = new Array<number>(m + 1).fill(0);\n    for (const num of nums) {\n        counter[num]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (let i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    const n = nums.length;\n    const res: number[] = new Array<number>(n);\n    for (let i = n - 1; i >= 0; i--) {\n        const num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n
counting_sort.dart
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nvoid countingSort(List<int> nums) {\n  // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n  int m = 0;\n  for (int _num in nums) {\n    m = max(m, _num);\n  }\n  // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n  // counter[_num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 _num\n  List<int> counter = List.filled(m + 1, 0);\n  for (int _num in nums) {\n    counter[_num]++;\n  }\n  // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n  // \u0422\u043e \u0435\u0441\u0442\u044c counter[_num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f _num \u0432 res\n  for (int i = 0; i < m; i++) {\n    counter[i + 1] += counter[i];\n  }\n  // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n  int n = nums.length;\n  List<int> res = List.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int _num = nums[i];\n    res[counter[_num] - 1] = _num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c _num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n    counter[_num]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f _num\n  }\n  // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n  nums.setAll(0, res);\n}\n
counting_sort.rs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfn counting_sort(nums: &mut [i32]) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    let m = *nums.iter().max().unwrap() as usize;\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    let mut counter = vec![0; m + 1];\n    for &num in nums.iter() {\n        counter[num as usize] += 1;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for i in 0..m {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    let n = nums.len();\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let num = nums[i];\n        res[counter[num as usize] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num as usize] -= 1; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    nums.copy_from_slice(&res)\n}\n
counting_sort.c
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nvoid countingSort(int nums[], int size) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    int m = 0;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > m) {\n            m = nums[i];\n        }\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    int *counter = calloc(m, sizeof(int));\n    for (int i = 0; i < size; i++) {\n        counter[nums[i]]++;\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (int i = 0; i < m; i++) {\n        counter[i + 1] += counter[i];\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    int *res = malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int num = nums[i];\n        res[counter[num] - 1] = num; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]--;              // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    memcpy(nums, res, size * sizeof(int));\n    // 5. \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(res);\n    free(counter);\n}\n
counting_sort.kt
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c */\n// \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\nfun countingSort(nums: IntArray) {\n    // 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n    var m = 0\n    for (num in nums) {\n        m = max(m, num)\n    }\n    // 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n    // counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n    val counter = IntArray(m + 1)\n    for (num in nums) {\n        counter[num]++\n    }\n    // 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n    // \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n    for (i in 0..<m) {\n        counter[i + 1] += counter[i]\n    }\n    // 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n    val n = nums.size\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val num = nums[i]\n        res[counter[num] - 1] = num // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n        counter[num]-- // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n    for (i in 0..<n) {\n        nums[i] = res[i]\n    }\n}\n
counting_sort.rb
=begin\nFile: counting_sort.rb\nCreated Time: 2024-05-02\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c ###\ndef counting_sort_naive(nums)\n  # \u041f\u0440\u043e\u0441\u0442\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n  m = 0\n  nums.each { |num| m = [m, num].max }\n  # 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n  # counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u041e\u0431\u043e\u0439\u0442\u0438 counter \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438\n  i = 0\n  for num in 0...(m + 1)\n    (0...counter[num]).each do\n      nums[i] = num\n      i += 1\n    end\n  end\nend\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c ###\ndef counting_sort(nums)\n  # \u041f\u043e\u043b\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 m\n  m = nums.max\n  # 2. \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b\n  # counter[num] \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 num\n  counter = Array.new(m + 1, 0)\n  nums.each { |num| counter[num] += 1 }\n  # 3. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b counter \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441\u00bb\n  # \u0422\u043e \u0435\u0441\u0442\u044c counter[num]-1 \u2014 \u044d\u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f num \u0432 res\n  (0...m).each { |i| counter[i + 1] += counter[i] }\n  # 4. \u041e\u0431\u043e\u0439\u0442\u0438 nums \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 res\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 res \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\n  n = nums.length\n  res = Array.new(n, 0)\n  (n - 1).downto(0).each do |i|\n    num = nums[i]\n    res[counter[num] - 1] = num # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c num \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443\n    counter[num] -= 1 # \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 1, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u044f num\n  end\n  # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 res\n  (0...n).each { |i| nums[i] = res[i] }\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/counting_sort/#1193","title":"11.9.3 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/counting_sort/#1194","title":"11.9.4 \u00a0 \u041e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f","text":"

\u041d\u0430 \u044d\u0442\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0438\u0437\u044f\u0449\u043d\u043e\u0439: \u043e\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u0447\u0438\u0441\u043b\u0430 \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0435\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u0442\u0440\u043e\u0433\u0438\u0435.

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u043c\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u043a \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0446\u0435\u043b\u044b\u043c \u0447\u0438\u0441\u043b\u0430\u043c. \u0427\u0442\u043e\u0431\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0443\u0436\u043d\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u0438 \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u0435\u043b \u0441 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e \u0432\u0441\u0435\u043c \u0447\u0438\u0441\u043b\u0430\u043c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0443, \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0432 \u0438\u0445 \u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435, \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0438 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e.

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0435\u043b\u0438\u043a, \u043d\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043d\u0435\u0432\u0435\u043b\u0438\u043a. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u043c \u0432\u044b\u0448\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \\(m\\) \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043c, \u0438\u043d\u0430\u0447\u0435 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u043d\u044f\u0442\u043e \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u0410 \u043a\u043e\u0433\u0434\u0430 \\(n \\ll m\\) , \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \\(O(m)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435, \u0447\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441 \\(O(n \\log n)\\) .

"},{"location":"chapter_sorting/heap_sort/","title":"11.7 \u00a0 \u041f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

Tip

\u041f\u0435\u0440\u0435\u0434 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u044d\u0442\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0430 \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0432\u044b \u0443\u0436\u0435 \u0438\u0437\u0443\u0447\u0438\u043b\u0438 \u0433\u043b\u0430\u0432\u0443 \"\u041a\u0443\u0447\u0430\".

\u041f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (heap sort) - \u044d\u0442\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \"\u043a\u0443\u0447\u0430\". \u0414\u043b\u044f \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0438\u0437\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u043d\u0430\u043c\u0438 \"\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438\" \u0438 \"\u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438\u0437 \u043a\u0443\u0447\u0438\".

  1. \u041f\u043e\u0434\u0430\u0442\u044c \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u0438\u043d-\u043a\u0443\u0447\u0443; \u0432 \u044d\u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u0442 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438.
  2. \u041d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0438 \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b - \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e.

\u0425\u043e\u0442\u044f \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0440\u0430\u0431\u043e\u0442\u043e\u0441\u043f\u043e\u0441\u043e\u0431\u0435\u043d, \u043e\u043d \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u043e\u0442\u043e\u043c\u0443 \u0440\u0430\u0441\u0445\u043e\u0434\u0443\u0435\u0442 \u043b\u0438\u0448\u043d\u044e\u044e \u043f\u0430\u043c\u044f\u0442\u044c. \u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u0438\u0437\u044f\u0449\u043d\u0443\u044e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e.

"},{"location":"chapter_sorting/heap_sort/#1171","title":"11.7.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(n\\) ; \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-12.

  1. \u041f\u043e\u0434\u0430\u0442\u044c \u043d\u0430 \u0432\u0445\u043e\u0434 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043c\u0430\u043a\u0441-\u043a\u0443\u0447\u0443. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438.
  2. \u041e\u0431\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043a\u0443\u0447\u0438 (\u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442) \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432\u043d\u0438\u0437\u0443 \u043a\u0443\u0447\u0438 (\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442). \u041f\u043e\u0441\u043b\u0435 \u043e\u0431\u043c\u0435\u043d\u0430 \u0434\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \\(1\\) , \u0430 \u0447\u0438\u0441\u043b\u043e \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \\(1\\) .
  3. \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 (sift down) \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u043a\u0443\u0447\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e.
  4. \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433 2. \u0438 \u0448\u0430\u0433 3. . \u041f\u043e\u0441\u043b\u0435 \\(n - 1\\) \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d.

Tip

\u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043a\u0443\u0447\u0438 \u0442\u043e\u0436\u0435 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0448\u0430\u0433 2. \u0438 \u0448\u0430\u0433 3. , \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430.

<1><2><3><4><5><6><7><8><9><10><11><12>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-12 \u00a0 \u0428\u0430\u0433\u0438 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438

\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0442\u0430 \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 sift_down(), \u0447\u0442\u043e \u0438 \u0432 \u0433\u043b\u0430\u0432\u0435 \"\u041a\u0443\u0447\u0430\". \u0412\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0434\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043c\u0435\u0440\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 sift_down() \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 \u0434\u043b\u0438\u043d\u044b \\(n\\) , \u0447\u0442\u043e\u0431\u044b \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u043a\u0443\u0447\u0438. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby heap_sort.py
def sift_down(nums: list[int], n: int, i: int):\n    \"\"\"\u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\"\"\"\n    while True:\n        # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        l = 2 * i + 1\n        r = 2 * i + 2\n        ma = i\n        if l < n and nums[l] > nums[ma]:\n            ma = l\n        if r < n and nums[r] > nums[ma]:\n            ma = r\n        # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i:\n            break\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        nums[i], nums[ma] = nums[ma], nums[i]\n        # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n\ndef heap_sort(nums: list[int]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439\"\"\"\n    # \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in range(len(nums) // 2 - 1, -1, -1):\n        sift_down(nums, len(nums), i)\n    # \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for i in range(len(nums) - 1, 0, -1):\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        nums[0], nums[i] = nums[i], nums[0]\n        # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        sift_down(nums, i, 0)\n
heap_sort.cpp
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(vector<int> &nums, int n, int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(nums[i], nums[ma]);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nvoid heapSort(vector<int> &nums) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = nums.size() / 2 - 1; i >= 0; --i) {\n        siftDown(nums, nums.size(), i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (int i = nums.size() - 1; i > 0; --i) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        swap(nums[0], nums[i]);\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0);\n    }\n}\n
heap_sort.java
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i)\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nvoid heapSort(int[] nums) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = nums.length / 2 - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (int i = nums.length - 1; i > 0; i--) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0);\n    }\n}\n
heap_sort.cs
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid SiftDown(int[] nums, int n, int i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i)\n            break;\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        (nums[ma], nums[i]) = (nums[i], nums[ma]);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nvoid HeapSort(int[] nums) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = nums.Length / 2 - 1; i >= 0; i--) {\n        SiftDown(nums, nums.Length, i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (int i = nums.Length - 1; i > 0; i--) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        (nums[i], nums[0]) = (nums[0], nums[i]);\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        SiftDown(nums, i, 0);\n    }\n}\n
heap_sort.go
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunc siftDown(nums *[]int, n, i int) {\n    for true {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        l := 2*i + 1\n        r := 2*i + 2\n        ma := i\n        if l < n && (*nums)[l] > (*nums)[ma] {\n            ma = l\n        }\n        if r < n && (*nums)[r] > (*nums)[ma] {\n            ma = r\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        (*nums)[i], (*nums)[ma] = (*nums)[ma], (*nums)[i]\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfunc heapSort(nums *[]int) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i := len(*nums)/2 - 1; i >= 0; i-- {\n        siftDown(nums, len(*nums), i)\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for i := len(*nums) - 1; i > 0; i-- {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        (*nums)[0], (*nums)[i] = (*nums)[i], (*nums)[0]\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0)\n    }\n}\n
heap_sort.swift
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunc siftDown(nums: inout [Int], n: Int, i: Int) {\n    var i = i\n    while true {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let l = 2 * i + 1\n        let r = 2 * i + 2\n        var ma = i\n        if l < n, nums[l] > nums[ma] {\n            ma = l\n        }\n        if r < n, nums[r] > nums[ma] {\n            ma = r\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i {\n            break\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        nums.swapAt(i, ma)\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfunc heapSort(nums: inout [Int]) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in stride(from: nums.count / 2 - 1, through: 0, by: -1) {\n        siftDown(nums: &nums, n: nums.count, i: i)\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for i in nums.indices.dropFirst().reversed() {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        nums.swapAt(0, i)\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums: &nums, n: i, i: 0)\n    }\n}\n
heap_sort.js
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunction siftDown(nums, n, i) {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma === i) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfunction heapSort(nums) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0);\n    }\n}\n
heap_sort.ts
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfunction siftDown(nums: number[], n: number, i: number): void {\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let ma = i;\n        if (l < n && nums[l] > nums[ma]) {\n            ma = l;\n        }\n        if (r < n && nums[r] > nums[ma]) {\n            ma = r;\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma === i) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        [nums[i], nums[ma]] = [nums[ma], nums[i]];\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfunction heapSort(nums: number[]): void {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (let i = Math.floor(nums.length / 2) - 1; i >= 0; i--) {\n        siftDown(nums, nums.length, i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (let i = nums.length - 1; i > 0; i--) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        [nums[0], nums[i]] = [nums[i], nums[0]];\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0);\n    }\n}\n
heap_sort.dart
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(List<int> nums, int n, int i) {\n  while (true) {\n    // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n    int l = 2 * i + 1;\n    int r = 2 * i + 2;\n    int ma = i;\n    if (l < n && nums[l] > nums[ma]) ma = l;\n    if (r < n && nums[r] > nums[ma]) ma = r;\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n    if (ma == i) break;\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    int temp = nums[i];\n    nums[i] = nums[ma];\n    nums[ma] = temp;\n    // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n    i = ma;\n  }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nvoid heapSort(List<int> nums) {\n  // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  for (int i = nums.length ~/ 2 - 1; i >= 0; i--) {\n    siftDown(nums, nums.length, i);\n  }\n  // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n  for (int i = nums.length - 1; i > 0; i--) {\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    int tmp = nums[0];\n    nums[0] = nums[i];\n    nums[i] = tmp;\n    // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    siftDown(nums, i, 0);\n  }\n}\n
heap_sort.rs
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfn sift_down(nums: &mut [i32], n: usize, mut i: usize) {\n    loop {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        let l = 2 * i + 1;\n        let r = 2 * i + 2;\n        let mut ma = i;\n        if l < n && nums[l] > nums[ma] {\n            ma = l;\n        }\n        if r < n && nums[r] > nums[ma] {\n            ma = r;\n        }\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if ma == i {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        nums.swap(i, ma);\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfn heap_sort(nums: &mut [i32]) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for i in (0..nums.len() / 2).rev() {\n        sift_down(nums, nums.len(), i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for i in (1..nums.len()).rev() {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        nums.swap(0, i);\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        sift_down(nums, i, 0);\n    }\n}\n
heap_sort.c
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nvoid siftDown(int nums[], int n, int i) {\n    while (1) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        int l = 2 * i + 1;\n        int r = 2 * i + 2;\n        int ma = i;\n        if (l < n && nums[l] > nums[ma])\n            ma = l;\n        if (r < n && nums[r] > nums[ma])\n            ma = r;\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i) {\n            break;\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        int temp = nums[i];\n        nums[i] = nums[ma];\n        nums[ma] = temp;\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma;\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nvoid heapSort(int nums[], int n) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (int i = n / 2 - 1; i >= 0; --i) {\n        siftDown(nums, n, i);\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (int i = n - 1; i > 0; --i) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        int tmp = nums[0];\n        nums[0] = nums[i];\n        nums[i] = tmp;\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0);\n    }\n}\n
heap_sort.kt
/* \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 */\nfun siftDown(nums: IntArray, n: Int, li: Int) {\n    var i = li\n    while (true) {\n        // \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n        val l = 2 * i + 1\n        val r = 2 * i + 2\n        var ma = i\n        if (l < n && nums[l] > nums[ma]) \n            ma = l\n        if (r < n && nums[r] > nums[ma]) \n            ma = r\n        // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n        if (ma == i) \n            break\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        val temp = nums[i]\n        nums[i] = nums[ma]\n        nums[ma] = temp\n        // \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n        i = ma\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 */\nfun heapSort(nums: IntArray) {\n    // \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n    for (i in nums.size / 2 - 1 downTo 0) {\n        siftDown(nums, nums.size, i)\n    }\n    // \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n    for (i in nums.size - 1 downTo 1) {\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n        val temp = nums[0]\n        nums[0] = nums[i]\n        nums[i] = temp\n        // \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n        siftDown(nums, i, 0)\n    }\n}\n
heap_sort.rb
=begin\nFile: heap_sort.rb\nCreated Time: 2024-04-10\nAuthor: junminhong (junminhong1110@gmail.com)\n=end\n\n# ## \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 ###\ndef sift_down(nums, n, i)\n  while true\n    # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n    break if ma == i\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n    i = ma\n  end\nend\n\n=begin\nFile: heap_sort.rb\nCreated Time: 2024-04-10\nAuthor: junminhong (junminhong1110@gmail.com)\n=end\n\n# ## \u0414\u043b\u0438\u043d\u0430 \u043a\u0443\u0447\u0438 \u0440\u0430\u0432\u043d\u0430 n; \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0443\u0437\u043b\u0430 i, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 ###\ndef sift_down(nums, n, i)\n  while true\n    # \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0441 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0440\u0435\u0434\u0438 i, l \u0438 r \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a ma\n    l = 2 * i + 1\n    r = 2 * i + 2\n    ma = i\n    ma = l if l < n && nums[l] > nums[ma]\n    ma = r if r < n && nums[r] > nums[ma]\n    # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b i \u0443\u0436\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u0435\u043d \u0438\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u044b l \u0438 r \u0432\u043d\u0435 \u0433\u0440\u0430\u043d\u0438\u0446, \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u044b\u0439\u0442\u0438\n    break if ma == i\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    nums[i], nums[ma] = nums[ma], nums[i]\n    # \u0426\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0432\u043d\u0438\u0437\n    i = ma\n  end\nend\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043a\u0443\u0447\u0435\u0439 ###\ndef heap_sort(nums)\n  # \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u043a\u0443\u0447\u0438: \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c heapify \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445\n  (nums.length / 2 - 1).downto(0) do |i|\n    sift_down(nums, nums.length, i)\n  end\n  # \u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043a\u0443\u0447\u0438 \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 n-1 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439\n  (nums.length - 1).downto(1) do |i|\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0441 \u0441\u0430\u043c\u044b\u043c \u043f\u0440\u0430\u0432\u044b\u043c \u043b\u0438\u0441\u0442\u043e\u043c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 (\u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b)\n    nums[0], nums[i] = nums[i], nums[0]\n    # \u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0435\u0438\u0432\u0430\u043d\u0438\u0435 \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437\n    sift_down(nums, i, 0)\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/heap_sort/#1172","title":"11.7.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/insertion_sort/","title":"11.4 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438","text":"

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 (insertion sort) - \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0440\u0443\u0447\u043d\u043e\u0435 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u043b\u043e\u0434\u044b \u043a\u0430\u0440\u0442.

\u0422\u043e\u0447\u043d\u0435\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043e\u043d \u043f\u043e\u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0441\u043b\u0435\u0432\u0430 \u0432 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 \u0438 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-6 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432. \u041f\u0443\u0441\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d \u043a\u0430\u043a base ; \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0432\u0438\u043d\u0443\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043e\u0442 \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0434\u043e base \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c base \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0446\u0435\u043b\u0435\u0432\u043e\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-6 \u00a0 \u041e\u0434\u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438

"},{"location":"chapter_sorting/insertion_sort/#1141","title":"11.4.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u041e\u0431\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-7.

  1. \u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.
  2. \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u0432\u0442\u043e\u0440\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u0430\u043a base ; \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u0435\u0440\u0432\u044b\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u043a\u0430\u0436\u0443\u0442\u0441\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438.
  3. \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u0442\u0440\u0435\u0442\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u0430\u043a base ; \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u0435\u0440\u0432\u044b\u0435 3 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u043a\u0430\u0436\u0443\u0442\u0441\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438.
  4. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438; \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 base \u0431\u0435\u0440\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0438 \u043f\u043e\u0441\u043b\u0435 \u0435\u0433\u043e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-7 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438

\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby insertion_sort.py
def insertion_sort(nums: list[int]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438\"\"\"\n    # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for i in range(1, len(nums)):\n        base = nums[i]\n        j = i - 1\n        # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while j >= 0 and nums[j] > base:\n            nums[j + 1] = nums[j]  # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j -= 1\n        nums[j + 1] = base  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n
insertion_sort.cpp
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nvoid insertionSort(vector<int> &nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (int i = 1; i < nums.size(); i++) {\n        int base = nums[i], j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--;\n        }\n        nums[j + 1] = base; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.java
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nvoid insertionSort(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (int i = 1; i < nums.length; i++) {\n        int base = nums[i], j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--;\n        }\n        nums[j + 1] = base;        // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.cs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nvoid InsertionSort(int[] nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (int i = 1; i < nums.Length; i++) {\n        int bas = nums[i], j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > bas) {\n            nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--;\n        }\n        nums[j + 1] = bas;         // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.go
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfunc insertionSort(nums []int) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for i := 1; i < len(nums); i++ {\n        base := nums[i]\n        j := i - 1\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        for j >= 0 && nums[j] > base {\n            nums[j+1] = nums[j] // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--\n        }\n        nums[j+1] = base // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.swift
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfunc insertionSort(nums: inout [Int]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for i in nums.indices.dropFirst() {\n        let base = nums[i]\n        var j = i - 1\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while j >= 0, nums[j] > base {\n            nums[j + 1] = nums[j] // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j -= 1\n        }\n        nums[j + 1] = base // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.js
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfunction insertionSort(nums) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        let base = nums[i],\n            j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--;\n        }\n        nums[j + 1] = base; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.ts
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfunction insertionSort(nums: number[]): void {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (let i = 1; i < nums.length; i++) {\n        const base = nums[i];\n        let j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--;\n        }\n        nums[j + 1] = base; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.dart
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nvoid insertionSort(List<int> nums) {\n  // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n  for (int i = 1; i < nums.length; i++) {\n    int base = nums[i], j = i - 1;\n    // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n    while (j >= 0 && nums[j] > base) {\n      nums[j + 1] = nums[j]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n      j--;\n    }\n    nums[j + 1] = base; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n  }\n}\n
insertion_sort.rs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfn insertion_sort(nums: &mut [i32]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for i in 1..nums.len() {\n        let (base, mut j) = (nums[i], (i - 1) as i32);\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while j >= 0 && nums[j as usize] > base {\n            nums[(j + 1) as usize] = nums[j as usize]; // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j -= 1;\n        }\n        nums[(j + 1) as usize] = base; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.c
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nvoid insertionSort(int nums[], int size) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n    for (int i = 1; i < size; i++) {\n        int base = nums[i], j = i - 1;\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            nums[j + 1] = nums[j];\n            j--;\n        }\n        // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        nums[j + 1] = base;\n    }\n}\n
insertion_sort.kt
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 */\nfun insertionSort(nums: IntArray) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0440\u0430\u0432\u043d\u044b 1, 2, ..., n\n    for (i in nums.indices) {\n        val base = nums[i]\n        var j = i - 1\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n        while (j >= 0 && nums[j] > base) {\n            nums[j + 1] = nums[j] // \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n            j--\n        }\n        nums[j + 1] = base        // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    }\n}\n
insertion_sort.rb
=begin\nFile: insertion_sort.rb\nCreated Time: 2024-04-02\nAuthor: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 ###\ndef insertion_sort(nums)\n  n = nums.length\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [0, i-1]\n  for i in 1...n\n    base = nums[i]\n    j = i - 1\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 [0, i-1]\n    while j >= 0 && nums[j] > base\n      nums[j + 1] = nums[j] # \u0421\u0434\u0432\u0438\u043d\u0443\u0442\u044c nums[j] \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043f\u0440\u0430\u0432\u043e\n      j -= 1\n    end\n    nums[j + 1] = base # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c base \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/insertion_sort/#1142","title":"11.4.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/insertion_sort/#1143","title":"11.4.3 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438","text":"

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) , \u0430 \u0443 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u044b \u0441\u043a\u043e\u0440\u043e \u0438\u0437\u0443\u0447\u0438\u043c, \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) . \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u043d\u0430 \u043c\u0430\u043b\u044b\u0445 \u043e\u0431\u044a\u0435\u043c\u0430\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435.

\u042d\u0442\u043e\u0442 \u0432\u044b\u0432\u043e\u0434 \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0433\u043e \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0443\u0440\u043e\u0432\u043d\u044f \\(O(n \\log n)\\) , \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430, \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\" \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439. \u041a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0430\u043b, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \\(n^2\\) \u0438 \\(n \\log n\\) \u0431\u043b\u0438\u0437\u043a\u0438 \u0434\u0440\u0443\u0433 \u043a \u0434\u0440\u0443\u0433\u0443, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0430\u0441\u0438\u043c\u043f\u0442\u043e\u0442\u0438\u043a\u0430 \u043d\u0435 \u0434\u043e\u043c\u0438\u043d\u0438\u0440\u0443\u0435\u0442, \u0430 \u0440\u0435\u0448\u0430\u044e\u0449\u0438\u043c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0440\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435.

\u041d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u043e \u043c\u043d\u043e\u0433\u0438\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 Java) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438. \u041e\u0431\u0449\u0430\u044f \u0438\u0434\u0435\u044f \u0442\u0430\u043a\u043e\u0432\u0430: \u0434\u043b\u044f \u0434\u043b\u0438\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\", \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443; \u0434\u043b\u044f \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0441\u0440\u0430\u0437\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438.

\u0425\u043e\u0442\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c, \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u0438\u043c\u0435\u044e\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0443\u044e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n^2)\\) , \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0447\u0430\u0449\u0435, \u0447\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0442\u0430\u043a\u043e\u0432\u044b.

"},{"location":"chapter_sorting/merge_sort/","title":"11.6 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c","text":"

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c (merge sort) - \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\", \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u044d\u0442\u0430\u043f\u044b \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f\" \u0438 \"\u0441\u043b\u0438\u044f\u043d\u0438\u044f\", \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-10.

  1. \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f: \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u044b, \u0438 \u0437\u0430\u0434\u0430\u0447\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.
  2. \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f: \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u043e\u0439 1, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f \u0438 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441\u043b\u0438\u044f\u043d\u0438\u0435; \u043b\u0435\u0432\u044b\u0435 \u0438 \u043f\u0440\u0430\u0432\u044b\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u044b \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0439 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u043f\u043e\u043a\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-10 \u00a0 \u042d\u0442\u0430\u043f\u044b \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c

"},{"location":"chapter_sorting/merge_sort/#1161","title":"11.6.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-11, \u043d\u0430 \u044d\u0442\u0430\u043f\u0435 \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f\" \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 \u043f\u043e \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0435 \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430.

  1. \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 mid \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 (\u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [left, mid] ) \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 (\u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b [mid + 1, right] ).
  2. \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433 1. , \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1.

\u042d\u0442\u0430\u043f \"\u0441\u043b\u0438\u044f\u043d\u0438\u044f\" \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u0442 \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b \u0432 \u043e\u0434\u0438\u043d \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0434\u043b\u0438\u043d\u044b 1, \u043a\u0430\u0436\u0434\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 \u0432 \u0444\u0430\u0437\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u044f \u0443\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u043c.

<1><2><3><4><5><6><7><8><9><10>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-11 \u00a0 \u0428\u0430\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u043f\u043e\u0440\u044f\u0434\u043a\u043e\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043f\u0440\u0438 \u043f\u043e\u0441\u0442\u0444\u0438\u043a\u0441\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430.

\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u0430 \u0432 \u043a\u043e\u0434\u0435 \u043d\u0438\u0436\u0435. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0432 nums \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u0440\u0430\u0432\u0435\u043d [left, right] , \u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u0432 tmp \u0440\u0430\u0432\u0435\u043d [0, right - left] .

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby merge_sort.py
def merge(nums: list[int], left: int, mid: int, right: int):\n    \"\"\"\u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\"\"\"\n    # \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    tmp = [0] * (right - left + 1)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    i, j, k = left, mid + 1, 0\n    # \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid and j <= right:\n        if nums[i] <= nums[j]:\n            tmp[k] = nums[i]\n            i += 1\n        else:\n            tmp[k] = nums[j]\n            j += 1\n        k += 1\n    # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid:\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    while j <= right:\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for k in range(0, len(tmp)):\n        nums[left + k] = tmp[k]\n\ndef merge_sort(nums: list[int], left: int, right: int):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c\"\"\"\n    # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if left >= right:\n        return  # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    # \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    mid = (left + right) // 2 # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    merge_sort(nums, left, mid)  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    merge_sort(nums, mid + 1, right)  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    # \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right)\n
merge_sort.cpp
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nvoid merge(vector<int> &nums, int left, int mid, int right) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    vector<int> tmp(right - left + 1);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    int i = left, j = mid + 1, k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmp.size(); k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nvoid mergeSort(vector<int> &nums, int left, int right) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right)\n        return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    int mid = left + (right - left) / 2;    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid);      // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.java
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nvoid merge(int[] nums, int left, int mid, int right) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    int[] tmp = new int[right - left + 1];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    int i = left, j = mid + 1, k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nvoid mergeSort(int[] nums, int left, int right) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right)\n        return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    int mid = left + (right - left) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.cs
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nvoid Merge(int[] nums, int left, int mid, int right) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    int[] tmp = new int[right - left + 1];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    int i = left, j = mid + 1, k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++];\n        else\n            tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmp.Length; ++k) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nvoid MergeSort(int[] nums, int left, int right) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right) return;       // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    int mid = left + (right - left) / 2;    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    MergeSort(nums, left, mid);      // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    MergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    Merge(nums, left, mid, right);\n}\n
merge_sort.go
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfunc merge(nums []int, left, mid, right int) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    tmp := make([]int, right-left+1)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    i, j, k := left, mid+1, 0\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i++\n        } else {\n            tmp[k] = nums[j]\n            j++\n        }\n        k++\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    for i <= mid {\n        tmp[k] = nums[i]\n        i++\n        k++\n    }\n    for j <= right {\n        tmp[k] = nums[j]\n        j++\n        k++\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for k := 0; k < len(tmp); k++ {\n        nums[left+k] = tmp[k]\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfunc mergeSort(nums []int, left, right int) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if left >= right {\n        return\n    }\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    mid := left + (right - left) / 2\n    mergeSort(nums, left, mid)\n    mergeSort(nums, mid+1, right)\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right)\n}\n
merge_sort.swift
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfunc merge(nums: inout [Int], left: Int, mid: Int, right: Int) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    var tmp = Array(repeating: 0, count: right - left + 1)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    var i = left, j = mid + 1, k = 0\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid, j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i]\n            i += 1\n        } else {\n            tmp[k] = nums[j]\n            j += 1\n        }\n        k += 1\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid {\n        tmp[k] = nums[i]\n        i += 1\n        k += 1\n    }\n    while j <= right {\n        tmp[k] = nums[j]\n        j += 1\n        k += 1\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for k in tmp.indices {\n        nums[left + k] = tmp[k]\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfunc mergeSort(nums: inout [Int], left: Int, right: Int) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if left >= right { // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n        return\n    }\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    let mid = left + (right - left) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums: &nums, left: left, right: mid) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums: &nums, left: mid + 1, right: right) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums: &nums, left: left, mid: mid, right: right)\n}\n
merge_sort.js
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfunction merge(nums, left, mid, right) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    const tmp = new Array(right - left + 1);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfunction mergeSort(nums, left, right) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right) return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    let mid = Math.floor(left + (right - left) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.ts
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfunction merge(nums: number[], left: number, mid: number, right: number): void {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    const tmp = new Array(right - left + 1);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    let i = left,\n        j = mid + 1,\n        k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmp.length; k++) {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfunction mergeSort(nums: number[], left: number, right: number): void {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right) return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    let mid = Math.floor(left + (right - left) / 2); // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.dart
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nvoid merge(List<int> nums, int left, int mid, int right) {\n  // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n  // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n  List<int> tmp = List.filled(right - left + 1, 0);\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  int i = left, j = mid + 1, k = 0;\n  // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while (i <= mid && j <= right) {\n    if (nums[i] <= nums[j])\n      tmp[k++] = nums[i++];\n    else\n      tmp[k++] = nums[j++];\n  }\n  // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while (i <= mid) {\n    tmp[k++] = nums[i++];\n  }\n  while (j <= right) {\n    tmp[k++] = nums[j++];\n  }\n  // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n  for (k = 0; k < tmp.length; k++) {\n    nums[left + k] = tmp[k];\n  }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nvoid mergeSort(List<int> nums, int left, int right) {\n  // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  if (left >= right) return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n  // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n  int mid = left + (right - left) ~/ 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n  mergeSort(nums, left, mid); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n  mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n  // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n  merge(nums, left, mid, right);\n}\n
merge_sort.rs
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfn merge(nums: &mut [i32], left: usize, mid: usize, right: usize) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    let tmp_size = right - left + 1;\n    let mut tmp = vec![0; tmp_size];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    let (mut i, mut j, mut k) = (left, mid + 1, 0);\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid && j <= right {\n        if nums[i] <= nums[j] {\n            tmp[k] = nums[i];\n            i += 1;\n        } else {\n            tmp[k] = nums[j];\n            j += 1;\n        }\n        k += 1;\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while i <= mid {\n        tmp[k] = nums[i];\n        k += 1;\n        i += 1;\n    }\n    while j <= right {\n        tmp[k] = nums[j];\n        k += 1;\n        j += 1;\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for k in 0..tmp_size {\n        nums[left + k] = tmp[k];\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfn merge_sort(nums: &mut [i32], left: usize, right: usize) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if left >= right {\n        return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    }\n\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    let mid = left + (right - left) / 2; // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    merge_sort(nums, left, mid); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    merge_sort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.c
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nvoid merge(int *nums, int left, int mid, int right) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    int tmpSize = right - left + 1;\n    int *tmp = (int *)malloc(tmpSize * sizeof(int));\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    int i = left, j = mid + 1, k = 0;\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j]) {\n            tmp[k++] = nums[i++];\n        } else {\n            tmp[k++] = nums[j++];\n        }\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++];\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++];\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (k = 0; k < tmpSize; ++k) {\n        nums[left + k] = tmp[k];\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(tmp);\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nvoid mergeSort(int *nums, int left, int right) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right)\n        return; // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    int mid = left + (right - left) / 2;    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid);      // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right);\n}\n
merge_sort.kt
/* \u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b */\nfun merge(nums: IntArray, left: Int, mid: Int, right: Int) {\n    // \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n    // \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    val tmp = IntArray(right - left + 1)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    var i = left\n    var j = mid + 1\n    var k = 0\n    // \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid && j <= right) {\n        if (nums[i] <= nums[j])\n            tmp[k++] = nums[i++]\n        else\n            tmp[k++] = nums[j++]\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n    while (i <= mid) {\n        tmp[k++] = nums[i++]\n    }\n    while (j <= right) {\n        tmp[k++] = nums[j++]\n    }\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n    for (l in tmp.indices) {\n        nums[left + l] = tmp[l]\n    }\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c */\nfun mergeSort(nums: IntArray, left: Int, right: Int) {\n    // \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n    if (left >= right) return  // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    // \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n    val mid = left + (right - left) / 2 // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n    mergeSort(nums, left, mid) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    mergeSort(nums, mid + 1, right) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n    // \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n    merge(nums, left, mid, right)\n}\n
merge_sort.rb
=begin\nFile: merge_sort.rb\nCreated Time: 2024-04-10\nAuthor: junminhong (junminhong1110@gmail.com)\n=end\n\n# ## \u0421\u043b\u0438\u044f\u043d\u0438\u0435 \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 ###\ndef merge(nums, left, mid, right)\n  # \u0418\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n  tmp = Array.new(right - left + 1, 0)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  i, j, k = left, mid + 1, 0\n  # \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n=begin\nFile: merge_sort.rb\nCreated Time: 2024-04-10\nAuthor: junminhong (junminhong1110@gmail.com)\n=end\n\n# ## \u0421\u043b\u0438\u044f\u043d\u0438\u0435 \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 ###\ndef merge(nums, left, mid, right)\n  # \u0418\u043d\u0442\u0435\u0440\u0432\u0430\u043b \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [left, mid], \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430: [mid+1, right]\n  # \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 tmp \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n  tmp = Array.new(right - left + 1, 0)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  i, j, k = left, mid + 1, 0\n  # \u041f\u043e\u043a\u0430 \u0432 \u043b\u0435\u0432\u043e\u043c \u0438 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\u0445 \u0435\u0449\u0435 \u0435\u0441\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b, \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0438\u0445 \u0438 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while i <= mid && j <= right\n    if nums[i] <= nums[j]\n      tmp[k] = nums[i]\n      i += 1\n    else\n      tmp[k] = nums[j]\n      j += 1\n    end\n    k += 1\n  end\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\n  while i <= mid\n    tmp[k] = nums[i]\n    i += 1\n    k += 1\n  end\n  while j <= right\n    tmp[k] = nums[j]\n    j += 1\n    k += 1\n  end\n  # \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 tmp \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 nums\n  (0...tmp.length).each do |k|\n    nums[left + k] = tmp[k]\n  end\nend\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c ###\ndef merge_sort(nums, left, right)\n  # \u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f\n  # \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0437\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u0442\u0441\u044f\n  return if left >= right\n  # \u042d\u0442\u0430\u043f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f\n  mid = left + (right - left) / 2 # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0441\u0435\u0440\u0435\u0434\u0438\u043d\u0443\n  merge_sort(nums, left, mid) # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n  merge_sort(nums, mid + 1, right) # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n  # \u042d\u0442\u0430\u043f \u0441\u043b\u0438\u044f\u043d\u0438\u044f\n  merge(nums, left, mid, right)\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/merge_sort/#1162","title":"11.6.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/merge_sort/#1163","title":"11.6.3 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u0414\u043b\u044f \u0441\u0432\u044f\u0437\u043d\u044b\u0445 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c \u0438\u043c\u0435\u0435\u0442 \u0437\u0430\u043c\u0435\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u043e \u043f\u0435\u0440\u0435\u0434 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438: \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e \\(O(1)\\).

\u0414\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u044b; \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u044b \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.

"},{"location":"chapter_sorting/quick_sort/","title":"11.5 \u00a0 \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

\u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (quick sort) - \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\"; \u043e\u043d \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0448\u0438\u0440\u043e\u043a\u043e.

\u041a\u043b\u044e\u0447\u0435\u0432\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 - \u044d\u0442\u043e \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c\". \u0415\u0435 \u0446\u0435\u043b\u044c \u0442\u0430\u043a\u043e\u0432\u0430: \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \"\u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\" \u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u043b\u0435\u0432\u043e \u043e\u0442 \u043d\u0435\u0433\u043e, \u0430 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e - \u0432\u043f\u0440\u0430\u0432\u043e. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-8.

  1. \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f i \u0438 j , \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043b\u0435\u0432\u0443\u044e \u0438 \u043f\u0440\u0430\u0432\u0443\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430.
  2. \u0417\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0446\u0438\u043a\u043b, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c i \u0438 j \u0438\u0449\u0443\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0441\u0442\u0430\u043c\u0438.
  3. \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c \u0448\u0430\u0433 2. , \u043f\u043e\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438 i \u0438 j \u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0442\u044f\u0442\u0441\u044f, \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0431\u043c\u0435\u043d\u044f\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0435 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.
<1><2><3><4><5><6><7><8><9>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-8 \u00a0 \u0428\u0430\u0433\u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c

\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0442\u0438: \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432, \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432; \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \"\u043b\u044e\u0431\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \\(\\leq\\) \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \\(\\leq\\) \u043b\u044e\u0431\u043e\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430\". \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430.

\u0421\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u0432 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435

\u041f\u043e \u0441\u0443\u0442\u0438, \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u0432\u043e\u0434\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0434\u043b\u0438\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a \u0434\u0432\u0443\u043c \u0437\u0430\u0434\u0430\u0447\u0430\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
def partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\"\"\"\n    # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n
quick_sort.cpp
/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nint partition(vector<int> &nums, int left, int right) {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums[i], nums[j]); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums[i], nums[left]);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;                   // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.java
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nvoid swap(int[] nums, int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nint partition(int[] nums, int left, int right) {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums, i, left);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;             // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.cs
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nvoid Swap(int[] nums, int i, int j) {\n    (nums[j], nums[i]) = (nums[i], nums[j]);\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nint Partition(int[] nums, int left, int right) {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        Swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    Swap(nums, i, left);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;             // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.go
/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nfunc (q *quickSort) partition(nums []int, left, right int) int {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.swift
/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nfunc partition(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    var i = left\n    var j = right\n    while i < j {\n        while i < j, nums[j] >= nums[left] {\n            j -= 1 // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while i < j, nums[i] <= nums[left] {\n            i += 1 // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        nums.swapAt(i, j) // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    nums.swapAt(i, left) // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.js
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nswap(nums, i, j) {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\npartition(nums, left, right) {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this.swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    this.swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.ts
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nswap(nums: number[], i: number, j: number): void {\n    let tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\npartition(nums: number[], left: number, right: number): number {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j -= 1; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i += 1; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        this.swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    this.swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.dart
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nvoid _swap(List<int> nums, int i, int j) {\n  int tmp = nums[i];\n  nums[i] = nums[j];\n  nums[j] = tmp;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nint _partition(List<int> nums, int left, int right) {\n  // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    while (i < j && nums[i] <= nums[left]) i++; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    _swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n  }\n  _swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.rs
/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        nums.swap(i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    nums.swap(i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    i // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.c
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nvoid swap(int nums[], int i, int j) {\n    int tmp = nums[i];\n    nums[i] = nums[j];\n    nums[j] = tmp;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nint partition(int nums[], int left, int right) {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n        swap(nums, i, j);\n    }\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    swap(nums, i, left);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    return i;\n}\n
quick_sort.kt
/* \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 */\nfun swap(nums: IntArray, i: Int, j: Int) {\n    val temp = nums[i]\n    nums[i] = nums[j]\n    nums[j] = temp\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 */\nfun partition(nums: IntArray, left: Int, right: Int): Int {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--           // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++           // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums, i, j)  // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums, i, left)   // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i              // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n

```ruby title=\"quick_sort.rb\"

=begin File: quick_sort.rb Created Time: 2024-04-01 Author: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

"},{"location":"chapter_sorting/quick_sort/#_1","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

class QuickSort class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 i, j = left, right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end ```

\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/quick_sort/#1151","title":"11.5.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u041e\u0431\u0449\u0438\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-9.

  1. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c\" \u0434\u043b\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b.
  2. \u0417\u0430\u0442\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c\" \u0434\u043b\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432.
  3. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1; \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-9 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\"\"\"\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if left >= right:\n        return\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = self.partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    self.quick_sort(nums, left, pivot - 1)\n    self.quick_sort(nums, pivot + 1, right)\n
quick_sort.cpp
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right)\n        return;\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    int pivot = partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
quick_sort.java
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right)\n        return;\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    int pivot = partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
quick_sort.cs
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right)\n        return;\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    int pivot = Partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    QuickSort(nums, left, pivot - 1);\n    QuickSort(nums, pivot + 1, right);\n}\n
quick_sort.go
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc (q *quickSort) quickSort(nums []int, left, right int) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if left >= right {\n        return\n    }\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot := q.partition(nums, left, right)\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    q.quickSort(nums, left, pivot-1)\n    q.quickSort(nums, pivot+1, right)\n}\n
quick_sort.swift
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc quickSort(nums: inout [Int], left: Int, right: Int) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if left >= right {\n        return\n    }\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    let pivot = partition(nums: &nums, left: left, right: right)\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quickSort(nums: &nums, left: left, right: pivot - 1)\n    quickSort(nums: &nums, left: pivot + 1, right: right)\n}\n
quick_sort.js
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nquickSort(nums, left, right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right) return;\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    const pivot = this.partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.ts
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right) {\n        return;\n    }\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    const pivot = this.partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    this.quickSort(nums, left, pivot - 1);\n    this.quickSort(nums, pivot + 1, right);\n}\n
quick_sort.dart
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n  if (left >= right) return;\n  // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n  int pivot = _partition(nums, left, right);\n  // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n  quickSort(nums, left, pivot - 1);\n  quickSort(nums, pivot + 1, right);\n}\n
quick_sort.rs
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\npub fn quick_sort(left: i32, right: i32, nums: &mut [i32]) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if left >= right {\n        return;\n    }\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    Self::quick_sort(left, pivot - 1, nums);\n    Self::quick_sort(pivot + 1, right, nums);\n}\n
quick_sort.c
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid quickSort(int nums[], int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right) {\n        return;\n    }\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    int pivot = partition(nums, left, right);\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quickSort(nums, left, pivot - 1);\n    quickSort(nums, pivot + 1, right);\n}\n
quick_sort.kt
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfun quickSort(nums: IntArray, left: Int, right: Int) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    if (left >= right) return\n    // \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    val pivot = partition(nums, left, right)\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quickSort(nums, left, pivot - 1)\n    quickSort(nums, pivot + 1, right)\n}\n

```ruby title=\"quick_sort.rb\"

=begin File: quick_sort.rb Created Time: 2024-04-01 Author: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

"},{"location":"chapter_sorting/quick_sort/#_2","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

class QuickSort class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 i, j = left, right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end

# ## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  if left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/quick_sort/#1152","title":"11.5.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":""},{"location":"chapter_sorting/quick_sort/#1153","title":"11.5.3 \u00a0 \u041f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0431\u044b\u0441\u0442\u0440\u0430\u044f","text":"

\u0423\u0436\u0435 \u043f\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044e \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u0430 \u0438\u043c\u0435\u0442\u044c \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u043f\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438. \u0425\u043e\u0442\u044f \u0435\u0435 \u0441\u0440\u0435\u0434\u043d\u044f\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0441\u043b\u0438\u044f\u043d\u0438\u0435\u043c\" \u0438 \"\u043f\u0438\u0440\u0430\u043c\u0438\u0434\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438\", \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u044b \u0442\u0430\u043a\u043e\u0432\u044b.

"},{"location":"chapter_sorting/quick_sort/#1154","title":"11.5.4 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430","text":"

\u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u0443\u0445\u0443\u0434\u0448\u0430\u0442\u044c\u0441\u044f. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439: \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043c\u0435\u043d\u044f\u043d \u0432 \u0441\u0430\u043c\u044b\u0439 \u043f\u0440\u0430\u0432\u044b\u0439 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0434\u043b\u0438\u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u0442\u0430\u043d\u0435\u0442 \\(n - 1\\) , \u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e - \\(0\\) . \u0415\u0441\u043b\u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c\u0441\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043e\u0434\u0438\u043d \u0438\u0437 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \\(0\\) , \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f divide and conquer \u043f\u043e\u0442\u0435\u0440\u044f\u0435\u0442 \u0441\u043c\u044b\u0441\u043b, \u0430 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0440\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043d\u0435\u0447\u0442\u043e \u0431\u043b\u0438\u0437\u043a\u043e\u0435 \u043a \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u043f\u0443\u0437\u044b\u0440\u044c\u043a\u043e\u043c\".

\u0427\u0442\u043e\u0431\u044b \u043f\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e \u0432\u044b\u0431\u043e\u0440\u0430 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u043e\u0432\u0435\u0437\u0435\u0442 \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u044b\u0439 \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043d\u0435\u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439.

\u041d\u0443\u0436\u043d\u043e \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u044f\u0437\u044b\u043a\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u044e\u0442 \"\u043f\u0441\u0435\u0432\u0434\u043e\u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0435 \u0447\u0438\u0441\u043b\u0430\". \u0415\u0441\u043b\u0438 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0434 \u0442\u0430\u043a\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u0433\u0440\u0430\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c.

\u0427\u0442\u043e\u0431\u044b \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e, \u043c\u043e\u0436\u043d\u043e \u0432\u0437\u044f\u0442\u044c \u0442\u0440\u0438 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u0430 (\u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0435\u0440\u0432\u044b\u0439, \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0438 \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430) \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u044d\u0442\u0438\u0445 \u0442\u0440\u0435\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \"\u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u043c \u0438 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0431\u043e\u043b\u044c\u0448\u0438\u043c\", \u0437\u0430\u043c\u0435\u0442\u043d\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u0435\u0442. \u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u0430\u0442\u044c \u0438 \u0431\u043e\u043b\u044c\u0448\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u0435\u0449\u0435 \u0441\u0438\u043b\u044c\u043d\u0435\u0435 \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u0443\u0441\u0442\u043e\u0439\u0447\u0438\u0432\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0434\u043e \\(O(n^2)\\) \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f.

\u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
def median_three(self, nums: list[int], left: int, mid: int, right: int) -> int:\n    \"\"\"\u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\"\"\"\n    l, m, r = nums[left], nums[mid], nums[right]\n    if (l <= m <= r) or (r <= m <= l):\n        return mid  # m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if (m <= l <= r) or (r <= l <= m):\n        return left  # l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right\n\ndef partition(self, nums: list[int], left: int, right: int) -> int:\n    \"\"\"\u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445)\"\"\"\n    # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    med = self.median_three(nums, left, (left + right) // 2, right)\n    # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums[left], nums[med] = nums[med], nums[left]\n    # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    i, j = left, right\n    while i < j:\n        while i < j and nums[j] >= nums[left]:\n            j -= 1  # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while i < j and nums[i] <= nums[left]:\n            i += 1  # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        nums[i], nums[j] = nums[j], nums[i]\n    # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    nums[i], nums[left] = nums[left], nums[i]\n    return i  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n
quick_sort.cpp
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nint medianThree(vector<int> &nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nint partition(vector<int> &nums, int left, int right) {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    swap(nums[left], nums[med]);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;                // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;                // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums[i], nums[j]); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums[i], nums[left]);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;                   // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.java
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nint medianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nint partition(int[] nums, int left, int right) {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    swap(nums, left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums, i, left);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;             // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.cs
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nint MedianThree(int[] nums, int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nint Partition(int[] nums, int left, int right) {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    int med = MedianThree(nums, left, (left + right) / 2, right);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    Swap(nums, left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--;          // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        Swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    Swap(nums, i, left);  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;             // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.go
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nfunc (q *quickSortMedian) medianThree(nums []int, left, mid, right int) int {\n    l, m, r := nums[left], nums[mid], nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    }\n    return right\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nfunc (q *quickSortMedian) partition(nums []int, left, right int) int {\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    med := q.medianThree(nums, left, (left+right)/2, right)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums[left], nums[med] = nums[med], nums[left]\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    i, j := left, right\n    for i < j {\n        for i < j && nums[j] >= nums[left] {\n            j-- // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        for i < j && nums[i] <= nums[left] {\n            i++ // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        // \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n        nums[i], nums[j] = nums[j], nums[i]\n    }\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    nums[i], nums[left] = nums[left], nums[i]\n    return i // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.swift
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nfunc medianThree(nums: [Int], left: Int, mid: Int, right: Int) -> Int {\n    let l = nums[left]\n    let m = nums[mid]\n    let r = nums[right]\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    }\n    return right\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nfunc partitionMedian(nums: inout [Int], left: Int, right: Int) -> Int {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    let med = medianThree(nums: nums, left: left, mid: left + (right - left) / 2, right: right)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums.swapAt(left, med)\n    return partition(nums: &nums, left: left, right: right)\n}\n
quick_sort.js
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nmedianThree(nums, left, mid, right) {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\npartition(nums, left, right) {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    this.swap(nums, left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left]) i++; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        this.swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    this.swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.ts
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nmedianThree(\n    nums: number[],\n    left: number,\n    mid: number,\n    right: number\n): number {\n    let l = nums[left],\n        m = nums[mid],\n        r = nums[right];\n    // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((l <= m && m <= r) || (r <= m && m <= l)) return mid;\n    // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m)) return left;\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\npartition(nums: number[], left: number, right: number): number {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    let med = this.medianThree(\n        nums,\n        left,\n        Math.floor((left + right) / 2),\n        right\n    );\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    this.swap(nums, left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let i = left,\n        j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left]) {\n            j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while (i < j && nums[i] <= nums[left]) {\n            i++; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        this.swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    this.swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.dart
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nint _medianThree(List<int> nums, int left, int mid, int right) {\n  int l = nums[left], m = nums[mid], r = nums[right];\n  if ((l <= m && m <= r) || (r <= m && m <= l))\n    return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n  if ((m <= l && l <= r) || (r <= l && l <= m))\n    return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n  return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nint _partition(List<int> nums, int left, int right) {\n  // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n  int med = _medianThree(nums, left, (left + right) ~/ 2, right);\n  // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  _swap(nums, left, med);\n  // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n  int i = left, j = right;\n  while (i < j) {\n    while (i < j && nums[j] >= nums[left]) j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    while (i < j && nums[i] <= nums[left]) i++; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    _swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n  }\n  _swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  return i; // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.rs
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nfn median_three(nums: &mut [i32], left: usize, mid: usize, right: usize) -> usize {\n    let (l, m, r) = (nums[left], nums[mid], nums[right]);\n    if (l <= m && m <= r) || (r <= m && m <= l) {\n        return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    }\n    if (m <= l && l <= r) || (r <= l && l <= m) {\n        return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    }\n    right\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nfn partition(nums: &mut [i32], left: usize, right: usize) -> usize {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    let med = Self::median_three(nums, left, (left + right) / 2, right);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    nums.swap(left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    let (mut i, mut j) = (left, right);\n    while i < j {\n        while i < j && nums[j] >= nums[left] {\n            j -= 1; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        while i < j && nums[i] <= nums[left] {\n            i += 1; // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        }\n        nums.swap(i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    nums.swap(i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    i // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.c
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nint medianThree(int nums[], int left, int mid, int right) {\n    int l = nums[left], m = nums[mid], r = nums[right];\n    if ((l <= m && m <= r) || (r <= m && m <= l))\n        return mid; // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((m <= l && l <= r) || (r <= l && l <= m))\n        return left; // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right;\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nint partitionMedian(int nums[], int left, int right) {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    int med = medianThree(nums, left, (left + right) / 2, right);\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    swap(nums, left, med);\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int i = left, j = right;\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--; // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++;          // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums, i, j); // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums, i, left); // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i;            // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n
quick_sort.kt
/* \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 */\nfun medianThree(nums: IntArray, left: Int, mid: Int, right: Int): Int {\n    val l = nums[left]\n    val m = nums[mid]\n    val r = nums[right]\n    if ((m in l..r) || (m in r..l))\n        return mid  // m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r\n    if ((l in m..r) || (l in r..m))\n        return left // l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r\n    return right\n}\n\n/* \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) */\nfun partitionMedian(nums: IntArray, left: Int, right: Int): Int {\n    // \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432\n    val med = medianThree(nums, left, (left + right) / 2, right)\n    // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    swap(nums, left, med)\n    // \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    var i = left\n    var j = right\n    while (i < j) {\n        while (i < j && nums[j] >= nums[left])\n            j--                      // \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        while (i < j && nums[i] <= nums[left])\n            i++                      // \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n        swap(nums, i, j)             // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u0438 \u0434\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u0441\u0442\u0430\u043c\u0438\n    }\n    swap(nums, i, left)              // \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    return i                         // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n}\n

```ruby title=\"quick_sort.rb\"

=begin File: quick_sort.rb Created Time: 2024-04-01 Author: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

"},{"location":"chapter_sorting/quick_sort/#_3","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

class QuickSort class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 i, j = left, right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end

# ## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  if left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n

end end

"},{"location":"chapter_sorting/quick_sort/#_4","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0434\u0438\u0430\u043d\u043e\u0439)","text":"

class QuickSortMedian class << self # ## \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 ### def median_three(nums, left, mid, right) # \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 _l, _m, _r = nums[left], nums[mid], nums[right] # m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l) # l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m) return right end

=begin File: quick_sort.rb Created Time: 2024-04-01 Author: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

"},{"location":"chapter_sorting/quick_sort/#_5","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

class QuickSort class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 i, j = left, right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end

# ## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  if left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n

end end

"},{"location":"chapter_sorting/quick_sort/#_6","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0434\u0438\u0430\u043d\u043e\u0439)","text":"

class QuickSortMedian class << self # ## \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 ### def median_three(nums, left, mid, right) # \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 _l, _m, _r = nums[left], nums[mid], nums[right] # m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l) # l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m) return right end

# ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) ###\ndef partition(nums, left, right)\n  # ## \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c nums[left] \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  med = median_three(nums, left, (left + right) / 2, right)\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    end\n    # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/quick_sort/#1155","title":"11.5.5 \u00a0 \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438","text":"

\u041d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u043f\u0430\u043c\u044f\u0442\u0438. \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0445\u043e\u0434\u043d\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0440\u0430\u0432\u043d\u0430 \\(m\\) ; \u0442\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c\u0441\u044f \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b \\(0\\) \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u0438\u043d\u044b \\(m - 1\\) . \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u0437\u0430\u0434\u0430\u0447\u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e (\u043b\u0438\u0448\u044c \u043d\u0430 \u043e\u0434\u0438\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442), \u0430 \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0435\u0442 \\(n - 1\\) , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \\(O(n)\\) \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0434 \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432.

\u0427\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043d\u0430\u043a\u043e\u043f\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u043e\u0432\u044b\u0445 \u043a\u0430\u0434\u0440\u043e\u0432, \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0438 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0439 \u0438\u0437 \u043d\u0438\u0445. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0434\u043b\u0438\u043d\u0430 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 \\(n / 2\\) , \u0442\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 \\(\\log n\\) , \u0430 \u0445\u0443\u0434\u0448\u0430\u044f \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u0442 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0430 \u0434\u043e \\(O(\\log n)\\) . \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby quick_sort.py
def quick_sort(self, nums: list[int], left: int, right: int):\n    \"\"\"\u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438)\"\"\"\n    # \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while left < right:\n        # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        pivot = self.partition(nums, left, right)\n        # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if pivot - left < right - pivot:\n            self.quick_sort(nums, left, pivot - 1)  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1  # \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        else:\n            self.quick_sort(nums, pivot + 1, right)  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1  # \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n
quick_sort.cpp
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nvoid quickSort(vector<int> &nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        int pivot = partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1;                 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1;                 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.java
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nvoid quickSort(int[] nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        int pivot = partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            quickSort(nums, left, pivot - 1); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.cs
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nvoid QuickSort(int[] nums, int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        int pivot = Partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            QuickSort(nums, left, pivot - 1);  // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1;  // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            QuickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.go
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nfunc (q *quickSortTailCall) quickSort(nums []int, left, right int) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    for left < right {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        pivot := q.partition(nums, left, right)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if pivot-left < right-pivot {\n            q.quickSort(nums, left, pivot-1) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1                 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            q.quickSort(nums, pivot+1, right) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1                 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.swift
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nfunc quickSortTailCall(nums: inout [Int], left: Int, right: Int) {\n    var left = left\n    var right = right\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while left < right {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        let pivot = partition(nums: &nums, left: left, right: right)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left) < (right - pivot) {\n            quickSortTailCall(nums: &nums, left: left, right: pivot - 1) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            quickSortTailCall(nums: &nums, left: pivot + 1, right: right) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.js
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nquickSort(nums, left, right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        let pivot = this.partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.ts
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nquickSort(nums: number[], left: number, right: number): void {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        let pivot = this.partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            this.quickSort(nums, left, pivot - 1); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            this.quickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.dart
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nvoid quickSort(List<int> nums, int left, int right) {\n  // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n  while (left < right) {\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    int pivot = _partition(nums, left, right);\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    if (pivot - left < right - pivot) {\n      quickSort(nums, left, pivot - 1); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n      left = pivot + 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n    } else {\n      quickSort(nums, pivot + 1, right); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n      right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n    }\n  }\n}\n
quick_sort.rs
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\npub fn quick_sort(mut left: i32, mut right: i32, nums: &mut [i32]) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while left < right {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        let pivot = Self::partition(nums, left as usize, right as usize) as i32;\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if pivot - left < right - pivot {\n            Self::quick_sort(left, pivot - 1, nums); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            left = pivot + 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            Self::quick_sort(pivot + 1, right, nums); // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            right = pivot - 1; // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n
quick_sort.c
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nvoid quickSortTailCall(int nums[], int left, int right) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    while (left < right) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        int pivot = partition(nums, left, right);\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - left < right - pivot) {\n            // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            quickSortTailCall(nums, left, pivot - 1);\n            // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n            left = pivot + 1;\n        } else {\n            // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            quickSortTailCall(nums, pivot + 1, right);\n            // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n            right = pivot - 1;\n        }\n    }\n}\n
quick_sort.kt
/* \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) */\nfun quickSortTailCall(nums: IntArray, left: Int, right: Int) {\n    // \u0417\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u044c, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 1\n    var l = left\n    var r = right\n    while (l < r) {\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0440\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u044f \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n        val pivot = partition(nums, l, r)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n        if (pivot - l < r - pivot) {\n            quickSort(nums, l, pivot - 1) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            l = pivot + 1 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n        } else {\n            quickSort(nums, pivot + 1, r) // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\n            r = pivot - 1 // \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n        }\n    }\n}\n

```ruby title=\"quick_sort.rb\"

=begin File: quick_sort.rb Created Time: 2024-04-01 Author: Cy (3739004@gmail.com), Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

"},{"location":"chapter_sorting/quick_sort/#_7","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

class QuickSort class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0412\u0437\u044f\u0442\u044c nums[left] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 i, j = left, right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end

# ## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  if left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n

end end

"},{"location":"chapter_sorting/quick_sort/#_8","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0435\u0434\u0438\u0430\u043d\u043e\u0439)","text":"

class QuickSortMedian class << self # ## \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 ### def median_three(nums, left, mid, right) # \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0438\u0437 \u0442\u0440\u0435\u0445 \u043a\u0430\u043d\u0434\u0438\u0434\u0430\u0442\u043e\u0432 _l, _m, _r = nums[left], nums[mid], nums[right] # m \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 l \u0438 r return mid if (_l <= _m && _m <= _r) || (_r <= _m && _m <= _l) # l \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043c\u0435\u0436\u0434\u0443 m \u0438 r return left if (_m <= _l && _l <= _r) || (_r <= _l && _l <= _m) return right end

# ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 (\u043c\u0435\u0434\u0438\u0430\u043d\u0430 \u0442\u0440\u0435\u0445) ###\ndef partition(nums, left, right)\n  # ## \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c nums[left] \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\n  med = median_three(nums, left, (left + right) / 2, right)\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043c\u0435\u0434\u0438\u0430\u043d\u0443 \u0432 \u043a\u0440\u0430\u0439\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n  nums[left], nums[med] = nums[med], nums[left]\n  i, j = left, right\n  while i < j\n    while i < j && nums[j] >= nums[left]\n      j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    end\n    while i < j && nums[i] <= nums[left]\n      i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e\n    end\n    # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432\n    nums[i], nums[j] = nums[j], nums[i]\n  end\n  # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n  nums[i], nums[left] = nums[left], nums[i]\n  i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\nend\n\n# ## \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  if left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0438 \u043f\u0440\u0430\u0432\u044b\u0439 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u044b\n    quick_sort(nums, left, pivot - 1)\n    quick_sort(nums, pivot + 1, right)\n  end\n  nums\nend\n

end end

"},{"location":"chapter_sorting/quick_sort/#_9","title":"## \u041a\u043b\u0430\u0441\u0441 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438)","text":"

class QuickSortTailCall class << self # ## \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438 ### def partition(nums, left, right) # \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c nums[left] \u043a\u0430\u043a \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 i = left j = right while i < j while i < j && nums[j] >= nums[left] j -= 1 # \u0418\u0434\u0442\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end while i < j && nums[i] <= nums[left] i += 1 # \u0418\u0434\u0442\u0438 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e \u0432 \u043f\u043e\u0438\u0441\u043a\u0430\u0445 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e end # \u041e\u0431\u043c\u0435\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 nums[i], nums[j] = nums[j], nums[i] end # \u041f\u0435\u0440\u0435\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043e\u043f\u043e\u0440\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u0443 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 nums[i], nums[left] = nums[left], nums[i] i # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 end

# ## \u0411\u044b\u0441\u0442\u0440\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (\u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438) ###\ndef quick_sort(nums, left, right)\n  # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c, \u043f\u043e\u043a\u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \u0440\u0430\u0432\u043d\u043e\u0439 1\n  while left < right\n    # \u0420\u0430\u0437\u0431\u0438\u0435\u043d\u0438\u0435 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c\u0438 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c\u0438\n    pivot = partition(nums, left, right)\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u0438\u0437 \u0434\u0432\u0443\u0445 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432\n    if pivot - left < right - pivot\n      quick_sort(nums, left, pivot - 1)\n      left = pivot + 1 # \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [pivot + 1, right]\n    else\n      quick_sort(nums, pivot + 1, right)\n      right = pivot - 1 # \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d: [left, pivot - 1]\n    end\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/radix_sort/","title":"11.10 \u00a0 \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430","text":"

\u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043c\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c: \u043e\u043d\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u044a\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \\(n\\) \u0432\u0435\u043b\u0438\u043a, \u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \\(m\\) \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u0430\u043b. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0442\u0435\u043f\u0435\u0440\u044c, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \\(n = 10^6\\) \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432, \u043f\u0440\u0438\u0447\u0435\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \\(8\\)-\u0437\u043d\u0430\u0447\u043d\u044b\u043c \u0447\u0438\u0441\u043b\u043e\u043c. \u0422\u043e\u0433\u0434\u0430 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u0430\u043d\u043d\u044b\u0445 \\(m = 10^8\\) \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c; \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043c\u0430 \u043f\u0430\u043c\u044f\u0442\u0438, \u0430 \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c.

\u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 (radix sort) \u043f\u043e \u0441\u0432\u043e\u0435\u0439 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0438\u0434\u0435\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u0438 \u0442\u043e\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430. \u041f\u043e\u0432\u0435\u0440\u0445 \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u0447\u0438\u0441\u043b\u0430 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443, \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u0438\u0442\u043e\u0433\u043e\u0432\u044b\u0439 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.

"},{"location":"chapter_sorting/radix_sort/#11101","title":"11.10.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c","text":"

\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u043c\u0438 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438: \u0431\u0443\u0434\u0435\u043c \u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0447\u0442\u043e \u043c\u043b\u0430\u0434\u0448\u0438\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u0438\u043c\u0435\u0435\u0442 \u043d\u043e\u043c\u0435\u0440 \\(1\\) , \u0430 \u0441\u0442\u0430\u0440\u0448\u0438\u0439 - \u043d\u043e\u043c\u0435\u0440 \\(8\\) . \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-18.

  1. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0440\u0430\u0437\u0440\u044f\u0434\u0430 \\(k = 1\\) .
  2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c\" \u043f\u043e \\(k\\)-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043d\u043e\u043c\u0435\u0440\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u044b \u043f\u043e \\(k\\)-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044e.
  3. \u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \\(k\\) \u043d\u0430 \\(1\\) \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u0448\u0430\u0433\u0443 2. , \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043f\u043e\u043a\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-18 \u00a0 \u041f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438

\u041d\u0438\u0436\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0434\u0430. \u0414\u043b\u044f \u0447\u0438\u0441\u043b\u0430 \\(x\\) \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u0441\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c \\(d\\) \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0435\u0433\u043e \\(k\\)-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \\(x_k\\) \u043c\u043e\u0436\u043d\u043e \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435:

\\[ x_k = \\lfloor\\frac{x}{d^{k-1}}\\rfloor \\bmod d \\]

\u0433\u0434\u0435 \\(\\lfloor a \\rfloor\\) \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u043e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u043b\u0430 \\(a\\) \u0432\u043d\u0438\u0437, \u0430 \\(\\bmod \\: d\\) \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0432\u0437\u044f\u0442\u0438\u0435 \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \\(d\\) . \u0414\u043b\u044f \u0441\u0442\u0443\u0434\u0435\u043d\u0447\u0435\u0441\u043a\u0438\u0445 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(d = 10\\) \u0438 \\(k \\in [1, 8]\\) .

\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043b\u0435\u0433\u043a\u0430 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u043a\u043e\u0434 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043c\u043e\u0433 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u0430 \u043f\u043e \u0438\u0445 \\(k\\)-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby radix_sort.py
def digit(num: int, exp: int) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1)\"\"\"\n    # \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num // exp) % 10\n\ndef counting_sort_digit(nums: list[int], exp: int):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums)\"\"\"\n    # \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    counter = [0] * 10\n    n = len(nums)\n    # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for i in range(n):\n        d = digit(nums[i], exp)  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d] += 1  # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for i in range(1, 10):\n        counter[i] += counter[i - 1]\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    res = [0] * n\n    for i in range(n - 1, -1, -1):\n        d = digit(nums[i], exp)\n        j = counter[d] - 1  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d] -= 1  # \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for i in range(n):\n        nums[i] = res[i]\n\ndef radix_sort(nums: list[int]):\n    \"\"\"\u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430\"\"\"\n    # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    m = max(nums)\n    # \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    exp = 1\n    while exp <= m:\n        # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        # k = 1 -> exp = 1\n        # k = 2 -> exp = 10\n        # \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        counting_sort_digit(nums, exp)\n        exp *= 10\n
radix_sort.cpp
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nvoid countingSortDigit(vector<int> &nums, int exp) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    vector<int> counter(10, 0);\n    int n = nums.size();\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++;                // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    vector<int> res(n, 0);\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i];       // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--;           // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid radixSort(vector<int> &nums) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    int m = *max_element(nums.begin(), nums.end());\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (int exp = 1; exp <= m; exp *= 10)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n}\n
radix_sort.java
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nvoid countingSortDigit(int[] nums, int exp) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    int[] counter = new int[10];\n    int n = nums.length;\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (int i = 0; i < n; i++) {\n        int d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++;                // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i];       // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--;           // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (int i = 0; i < n; i++)\n        nums[i] = res[i];\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid radixSort(int[] nums) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    int m = Integer.MIN_VALUE;\n    for (int num : nums)\n        if (num > m)\n            m = num;\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
radix_sort.cs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nint Digit(int num, int exp) {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nvoid CountingSortDigit(int[] nums, int exp) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    int[] counter = new int[10];\n    int n = nums.Length;\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (int i = 0; i < n; i++) {\n        int d = Digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++;                // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    int[] res = new int[n];\n    for (int i = n - 1; i >= 0; i--) {\n        int d = Digit(nums[i], exp);\n        int j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i];       // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--;           // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (int i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid RadixSort(int[] nums) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    int m = int.MinValue;\n    foreach (int num in nums) {\n        if (num > m) m = num;\n    }\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (int exp = 1; exp <= m; exp *= 10) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        CountingSortDigit(nums, exp);\n    }\n}\n
radix_sort.go
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfunc digit(num, exp int) int {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfunc countingSortDigit(nums []int, exp int) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    counter := make([]int, 10)\n    n := len(nums)\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for i := 0; i < n; i++ {\n        d := digit(nums[i], exp) // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++             // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for i := 1; i < 10; i++ {\n        counter[i] += counter[i-1]\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    res := make([]int, n)\n    for i := n - 1; i >= 0; i-- {\n        d := digit(nums[i], exp)\n        j := counter[d] - 1 // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--        // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for i := 0; i < n; i++ {\n        nums[i] = res[i]\n    }\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc radixSort(nums []int) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    max := math.MinInt\n    for _, num := range nums {\n        if num > max {\n            max = num\n        }\n    }\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for exp := 1; max >= exp; exp *= 10 {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n    }\n}\n
radix_sort.swift
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfunc digit(num: Int, exp: Int) -> Int {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    (num / exp) % 10\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfunc countingSortDigit(nums: inout [Int], exp: Int) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    var counter = Array(repeating: 0, count: 10)\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for i in nums.indices {\n        let d = digit(num: nums[i], exp: exp) // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d] += 1 // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for i in 1 ..< 10 {\n        counter[i] += counter[i - 1]\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    var res = Array(repeating: 0, count: nums.count)\n    for i in nums.indices.reversed() {\n        let d = digit(num: nums[i], exp: exp)\n        let j = counter[d] - 1 // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i] // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d] -= 1 // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for i in nums.indices {\n        nums[i] = res[i]\n    }\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunc radixSort(nums: inout [Int]) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    var m = Int.min\n    for num in nums {\n        if num > m {\n            m = num\n        }\n    }\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for exp in sequence(first: 1, next: { m >= ($0 * 10) ? $0 * 10 : nil }) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums: &nums, exp: exp)\n    }\n}\n
radix_sort.js
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfunction digit(num, exp) {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfunction countingSortDigit(nums, exp) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++; // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunction radixSort(nums) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    let m = Math.max(... nums);\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
radix_sort.ts
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfunction digit(num: number, exp: number): number {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return Math.floor(num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfunction countingSortDigit(nums: number[], exp: number): void {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    const counter = new Array(10).fill(0);\n    const n = nums.length;\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (let i = 0; i < n; i++) {\n        const d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++; // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (let i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    const res = new Array(n).fill(0);\n    for (let i = n - 1; i >= 0; i--) {\n        const d = digit(nums[i], exp);\n        const j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (let i = 0; i < n; i++) {\n        nums[i] = res[i];\n    }\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfunction radixSort(nums: number[]): void {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    let m: number = Math.max(... nums);\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (let exp = 1; exp <= m; exp *= 10) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp);\n    }\n}\n
radix_sort.dart
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 _num, \u0433\u0434\u0435 exp = 10^(k-1) */\nint digit(int _num, int exp) {\n  // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n  return (_num ~/ exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nvoid countingSortDigit(List<int> nums, int exp) {\n  // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n  List<int> counter = List<int>.filled(10, 0);\n  int n = nums.length;\n  // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n  for (int i = 0; i < n; i++) {\n    int d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n    counter[d]++; // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n  }\n  // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n  for (int i = 1; i < 10; i++) {\n    counter[i] += counter[i - 1];\n  }\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n  List<int> res = List<int>.filled(n, 0);\n  for (int i = n - 1; i >= 0; i--) {\n    int d = digit(nums[i], exp);\n    int j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n    res[j] = nums[i]; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n    counter[d]--; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n  }\n  // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n  for (int i = 0; i < n; i++) nums[i] = res[i];\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid radixSort(List<int> nums) {\n  // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n  // \u0412 dart \u0434\u043b\u0438\u043d\u0430 int \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 64 \u0431\u0438\u0442\u0430\n  int m = -1 << 63;\n  for (int _num in nums) if (_num > m) m = _num;\n  // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n  for (int exp = 1; exp <= m; exp *= 10)\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    // k = 1 -> exp = 1\n    // k = 2 -> exp = 10\n    // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n    countingSortDigit(nums, exp);\n}\n
radix_sort.rs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfn digit(num: i32, exp: i32) -> usize {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return ((num / exp) % 10) as usize;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfn counting_sort_digit(nums: &mut [i32], exp: i32) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    let mut counter = [0; 10];\n    let n = nums.len();\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for i in 0..n {\n        let d = digit(nums[i], exp); // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d] += 1; // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for i in 1..10 {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    let mut res = vec![0; n];\n    for i in (0..n).rev() {\n        let d = digit(nums[i], exp);\n        let j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]; // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d] -= 1; // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    nums.copy_from_slice(&res);\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfn radix_sort(nums: &mut [i32]) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    let m = *nums.into_iter().max().unwrap();\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    let mut exp = 1;\n    while exp <= m {\n        counting_sort_digit(nums, exp);\n        exp *= 10;\n    }\n}\n
radix_sort.c
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nint digit(int num, int exp) {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10;\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nvoid countingSortDigit(int nums[], int size, int exp) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    int *counter = (int *)malloc((sizeof(int) * 10));\n    memset(counter, 0, sizeof(int) * 10); // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0443\u043b\u0435\u043c \u0434\u043b\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (int i = 0; i < size; i++) {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        int d = digit(nums[i], exp);\n        // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n        counter[d]++;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (int i = 1; i < 10; i++) {\n        counter[i] += counter[i - 1];\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    int *res = (int *)malloc(sizeof(int) * size);\n    for (int i = size - 1; i >= 0; i--) {\n        int d = digit(nums[i], exp);\n        int j = counter[d] - 1; // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i];       // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--;           // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (int i = 0; i < size; i++) {\n        nums[i] = res[i];\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(res);\n    free(counter);\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nvoid radixSort(int nums[], int size) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    int max = INT32_MIN;\n    for (int i = 0; i < size; i++) {\n        if (nums[i] > max) {\n            max = nums[i];\n        }\n    }\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    for (int exp = 1; max >= exp; exp *= 10)\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, size, exp);\n}\n
radix_sort.kt
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) */\nfun digit(num: Int, exp: Int): Int {\n    // \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n    return (num / exp) % 10\n}\n\n/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) */\nfun countingSortDigit(nums: IntArray, exp: Int) {\n    // \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n    val counter = IntArray(10)\n    val n = nums.size\n    // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n    for (i in 0..<n) {\n        val d = digit(nums[i], exp) // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n        counter[d]++                // \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n    for (i in 1..9) {\n        counter[i] += counter[i - 1]\n    }\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n    val res = IntArray(n)\n    for (i in n - 1 downTo 0) {\n        val d = digit(nums[i], exp)\n        val j = counter[d] - 1 // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n        res[j] = nums[i]       // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n        counter[d]--           // \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n    }\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n    for (i in 0..<n)\n        nums[i] = res[i]\n}\n\n/* \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 */\nfun radixSort(nums: IntArray) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n    var m = Int.MIN_VALUE\n    for (num in nums) if (num > m) m = num\n    var exp = 1\n    // \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n    while (exp <= m) {\n        // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // k = 1 -> exp = 1\n        // k = 2 -> exp = 10\n        // \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n        countingSortDigit(nums, exp)\n        exp *= 10\n    }\n}\n
radix_sort.rb
=begin\nFile: radix_sort.rb\nCreated Time: 2024-05-03\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) ###\ndef digit(num, exp)\n  # \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n  (num / exp) % 10\nend\n\n=begin\nFile: radix_sort.rb\nCreated Time: 2024-05-03\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) ###\ndef digit(num, exp)\n  # \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n  (num / exp) % 10\nend\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) ###\ndef counting_sort_digit(nums, exp)\n  # \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n  counter = Array.new(10, 0)\n  n = nums.length\n  # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n  for i in 0...n\n    d = digit(nums[i], exp) # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n    counter[d] += 1 # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n  end\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n    res[j] = nums[i] # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n    counter[d] -= 1 # \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n  end\n  # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n=begin\nFile: radix_sort.rb\nCreated Time: 2024-05-03\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 num, \u0433\u0434\u0435 exp = 10^(k-1) ###\ndef digit(num, exp)\n  # \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 exp \u0432\u043c\u0435\u0441\u0442\u043e k \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0434\u043e\u0440\u043e\u0433\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0445 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438\n  (num / exp) % 10\nend\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c (\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 nums) ###\ndef counting_sort_digit(nums, exp)\n  # \u0420\u0430\u0437\u0440\u044f\u0434\u044b \u0434\u0435\u0441\u044f\u0442\u0438\u0447\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u043b\u0435\u0436\u0430\u0442 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 0~9, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u0435\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u043e\u0440\u0437\u0438\u043d \u0434\u043b\u0438\u043d\u044b 10\n  counter = Array.new(10, 0)\n  n = nums.length\n  # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u043a\u0430\u0436\u0434\u043e\u0439 \u0446\u0438\u0444\u0440\u044b \u043e\u0442 0 \u0434\u043e 9\n  for i in 0...n\n    d = digit(nums[i], exp) # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c k-\u0439 \u0440\u0430\u0437\u0440\u044f\u0434 nums[i], \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0433\u043e \u043a\u0430\u043a d\n    counter[d] += 1 # \u041f\u043e\u0434\u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439 \u0446\u0438\u0444\u0440\u044b d\n  end\n  # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0443\u043c\u043c\u044b \u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u0447\u0438\u0441\u043b\u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0439\u00bb \u0432 \u00ab\u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u00bb\n  (1...10).each { |i| counter[i] += counter[i - 1] }\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u0445\u043e\u0434, \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c res \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u043f\u043e \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0435 \u0432 \u043a\u043e\u0440\u0437\u0438\u043d\u0430\u0445\n  res = Array.new(n, 0)\n  for i in (n - 1).downto(0)\n    d = digit(nums[i], exp)\n    j = counter[d] - 1 # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 j \u0446\u0438\u0444\u0440\u044b d \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435\n    res[j] = nums[i] # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 j\n    counter[d] -= 1 # \u0423\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e d \u043d\u0430 1\n  end\n  # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 nums \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c\n  (0...n).each { |i| nums[i] = res[i] }\nend\n\n# ## \u041f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 ###\ndef radix_sort(nums)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432\n  m = nums.max\n  # \u041f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443\n  exp = 1\n  while exp <= m\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e k-\u043c\u0443 \u0440\u0430\u0437\u0440\u044f\u0434\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    # k = 1 -> exp = 1\n    # k = 2 -> exp = 10\n    # \u0442\u043e \u0435\u0441\u0442\u044c exp = 10^(k-1)\n    counting_sort_digit(nums, exp)\n    exp *= 10\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041f\u043e\u0447\u0435\u043c\u0443 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u0430 \u043a \u0441\u0442\u0430\u0440\u0448\u0435\u043c\u0443?

\u0412 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0440\u0430\u0443\u043d\u0434\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0437\u0434\u043d\u0435\u0433\u043e \u0440\u0430\u0443\u043d\u0434\u0430 \u043f\u0435\u0440\u0435\u043a\u0440\u044b\u0432\u0430\u044e\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u0440\u0430\u0443\u043d\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \\(a < b\\) , \u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e - \\(a > b\\) , \u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0440\u0430\u0443\u043d\u0434\u0430 \u0441\u0442\u0430\u043d\u0435\u0442 \u043e\u043a\u043e\u043d\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u0442\u0430\u0440\u0448\u0438\u0435 \u0440\u0430\u0437\u0440\u044f\u0434\u044b \u0438\u043c\u0435\u044e\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0439 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u043c\u043b\u0430\u0434\u0448\u0438\u043c \u0440\u0430\u0437\u0440\u044f\u0434\u0430\u043c, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e \u0441\u0442\u0430\u0440\u0448\u0438\u043c.

"},{"location":"chapter_sorting/radix_sort/#11102","title":"11.10.2 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u041f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e\u0434\u0441\u0447\u0435\u0442\u043e\u043c \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u043e\u043c \u0447\u0438\u0441\u0435\u043b, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0432\u0438\u0434\u0435 \u0447\u0438\u0441\u0435\u043b \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0438 \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0432\u0435\u043b\u0438\u043a\u043e. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u043f\u043b\u043e\u0445\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0442 \u0434\u043b\u044f \u043f\u043e\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0440\u0430\u0437\u0440\u044f\u0434\u043e\u0432 \\(k\\) \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0432\u0435\u043b\u0438\u043a\u043e \u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u043a \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \\(O(nk) \\gg O(n^2)\\) .

"},{"location":"chapter_sorting/selection_sort/","title":"11.2 \u00a0 \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c","text":"

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c (selection sort) \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e: \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0446\u0438\u043a\u043b, \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0438\u0437 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043f\u043e\u0441\u043b\u0435 \u0447\u0435\u0433\u043e \u043e\u043d \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430.

\u041f\u0443\u0441\u0442\u044c \u0434\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(n\\) ; \u0442\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-2.

  1. \u0412 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043d\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b, \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u0440\u0430\u0432\u0435\u043d \\([0, n-1]\\) .
  2. \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \\([0, n-1]\\) \u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \\(0\\) . \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0432\u044b\u0435 1 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b.
  3. \u0412\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430 \\([1, n-1]\\) \u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0435\u0433\u043e \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0432 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \\(1\\) . \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0440\u0432\u044b\u0435 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b.
  4. \u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u043f\u043e \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0438. \u041f\u043e\u0441\u043b\u0435 \\(n - 1\\) \u0440\u0430\u0443\u043d\u0434\u043e\u0432 \u0432\u044b\u0431\u043e\u0440\u0430 \u0438 \u043e\u0431\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0435 \\(n - 1\\) \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0443\u0434\u0443\u0442 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b.
  5. \u041e\u0441\u0442\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c.
<1><2><3><4><5><6><7><8><9><10><11>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-2 \u00a0 \u0428\u0430\u0433\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u043c

\u0412 \u043a\u043e\u0434\u0435 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \\(k\\) \u0434\u043b\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby selection_sort.py
def selection_sort(nums: list[int]):\n    \"\"\"\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c\"\"\"\n    n = len(nums)\n    # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for i in range(n - 1):\n        # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        k = i\n        for j in range(i + 1, n):\n            if nums[j] < nums[k]:\n                k = j  # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        nums[i], nums[k] = nums[k], nums[i]\n
selection_sort.cpp
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nvoid selectionSort(vector<int> &nums) {\n    int n = nums.size();\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        swap(nums[i], nums[k]);\n    }\n}\n
selection_sort.java
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nvoid selectionSort(int[] nums) {\n    int n = nums.length;\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
selection_sort.cs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nvoid SelectionSort(int[] nums) {\n    int n = nums.Length;\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        (nums[k], nums[i]) = (nums[i], nums[k]);\n    }\n}\n
selection_sort.go
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfunc selectionSort(nums []int) {\n    n := len(nums)\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for i := 0; i < n-1; i++ {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        k := i\n        for j := i + 1; j < n; j++ {\n            if nums[j] < nums[k] {\n                // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n                k = j\n            }\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        nums[i], nums[k] = nums[k], nums[i]\n\n    }\n}\n
selection_sort.swift
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfunc selectionSort(nums: inout [Int]) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for i in nums.indices.dropLast() {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        var k = i\n        for j in nums.indices.dropFirst(i + 1) {\n            if nums[j] < nums[k] {\n                k = j // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n            }\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        nums.swapAt(i, k)\n    }\n}\n
selection_sort.js
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfunction selectionSort(nums) {\n    let n = nums.length;\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n            }\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
selection_sort.ts
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfunction selectionSort(nums: number[]): void {\n    let n = nums.length;\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (let i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        let k = i;\n        for (let j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k]) {\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n            }\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        [nums[i], nums[k]] = [nums[k], nums[i]];\n    }\n}\n
selection_sort.dart
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nvoid selectionSort(List<int> nums) {\n  int n = nums.length;\n  // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n  for (int i = 0; i < n - 1; i++) {\n    // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n    int k = i;\n    for (int j = i + 1; j < n; j++) {\n      if (nums[j] < nums[k]) k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    }\n    // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n    int temp = nums[i];\n    nums[i] = nums[k];\n    nums[k] = temp;\n  }\n}\n
selection_sort.rs
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfn selection_sort(nums: &mut [i32]) {\n    if nums.is_empty() {\n        return;\n    }\n    let n = nums.len();\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for i in 0..n - 1 {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        let mut k = i;\n        for j in i + 1..n {\n            if nums[j] < nums[k] {\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n            }\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        nums.swap(i, k);\n    }\n}\n
selection_sort.c
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nvoid selectionSort(int nums[], int n) {\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (int i = 0; i < n - 1; i++) {\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        int k = i;\n        for (int j = i + 1; j < n; j++) {\n            if (nums[j] < nums[k])\n                k = j; // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        int temp = nums[i];\n        nums[i] = nums[k];\n        nums[k] = temp;\n    }\n}\n
selection_sort.kt
/* \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c */\nfun selectionSort(nums: IntArray) {\n    val n = nums.size\n    // \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n    for (i in 0..<n - 1) {\n        var k = i\n        // \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n        for (j in i + 1..<n) {\n            if (nums[j] < nums[k])\n                k = j // \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        }\n        // \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n        val temp = nums[i]\n        nums[i] = nums[k]\n        nums[k] = temp\n    }\n}\n
selection_sort.rb
=begin\nFile: selection_sort.rb\nCreated Time: 2024-05-03\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0432\u044b\u0431\u043e\u0440\u043e\u043c ###\ndef selection_sort(nums)\n  n = nums.length\n  # \u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d [i, n-1]\n  for i in 0...(n - 1)\n    # \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b: \u043d\u0430\u0439\u0442\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435\n    k = i\n    for j in (i + 1)...n\n      if nums[j] < nums[k]\n        k = j # \u0417\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n      end\n    end\n    # \u041f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u044d\u0442\u043e\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441 \u043f\u0435\u0440\u0432\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u043d\u0435\u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430\n    nums[i], nums[k] = nums[k], nums[i]\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_sorting/selection_sort/#1121","title":"11.2.1 \u00a0 \u0425\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430","text":"

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u044b\u0431\u043e\u0440\u043e\u043c

"},{"location":"chapter_sorting/sorting_algorithm/","title":"11.1 \u00a0 \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (sorting algorithm) \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u043b\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443. \u041e\u043d\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0448\u0438\u0440\u043e\u043a\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u043e\u0449\u0435 \u0438 \u0431\u044b\u0441\u0442\u0440\u0435\u0435 \u0438\u0441\u043a\u0430\u0442\u044c, \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 11-1, \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u0446\u0435\u043b\u044b\u0435 \u0447\u0438\u0441\u043b\u0430, \u0447\u0438\u0441\u043b\u0430 \u0441 \u043f\u043b\u0430\u0432\u0430\u044e\u0449\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439, \u0441\u0438\u043c\u0432\u043e\u043b\u044b, \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0442\u0438\u043f\u044b. \u041a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043f\u043e-\u0440\u0430\u0437\u043d\u043e\u043c\u0443, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0435 \u0447\u0438\u0441\u0435\u043b, \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443 ASCII-\u043a\u043e\u0434\u043e\u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438\u043b\u0438 \u043f\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-1 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0442\u0438\u043f\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u043f\u0440\u0430\u0432\u0438\u043b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f

"},{"location":"chapter_sorting/sorting_algorithm/#1111","title":"11.1.1 \u00a0 \u041a\u0440\u0438\u0442\u0435\u0440\u0438\u0438 \u043e\u0446\u0435\u043d\u043a\u0438","text":"

\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f: \u043c\u044b \u043e\u0436\u0438\u0434\u0430\u0435\u043c, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043d\u0438\u0436\u0435, \u0430 \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0431\u0443\u0434\u0435\u0442 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435 (\u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442\u0430 \u0432\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439). \u0414\u043b\u044f \u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u043e\u0431\u044a\u0435\u043c\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u044d\u0442\u043e\u0442 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u0435\u043d.

\u0421\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435: \u043a\u0430\u043a \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u044f\u043c\u043e \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u044d\u043a\u043e\u043d\u043e\u043c\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c. \u041e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u043d\u0430 \u043c\u0435\u0441\u0442\u0435 \u043f\u0435\u0440\u0435\u043d\u043e\u0441\u043e\u0432 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u0435\u043d\u044c\u0448\u0435, \u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u044b \u0432\u044b\u0448\u0435.

\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c: \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043d\u0435 \u043c\u0435\u043d\u044f\u0435\u0442 \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435.

\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f\u043c\u0438 \u043e \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u0430\u0445, \u0433\u0434\u0435 \u0432 \u043f\u0435\u0440\u0432\u043e\u043c \u0438 \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u0442\u043e\u043b\u0431\u0446\u0430\u0445 \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0438\u043c\u044f \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0440\u0430\u0437\u0440\u0443\u0448\u0438\u0442\u044c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445:

# \u0412\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0443\u0436\u0435 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u043f\u043e \u0438\u043c\u0435\u043d\u0438\n# (name, age)\n  ('A', 19)\n  ('B', 18)\n  ('C', 21)\n  ('D', 19)\n  ('E', 23)\n\n# \u0415\u0441\u043b\u0438 \u0437\u0430\u0442\u0435\u043c \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0443,\n# \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a ('D', 19) \u0438 ('A', 19) \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f,\n# \u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0442\u0435\u0440\u044f\u043d\u043e\n  ('B', 18)\n  ('D', 19)\n  ('A', 19)\n  ('C', 21)\n  ('E', 23)\n

\u0410\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c: \u0430\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0443\u043c\u0435\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0438 \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043b\u0443\u0447\u0448\u0435\u0439 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438. \u041b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0430\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043b\u0443\u0447\u0448\u0435 \u0438\u0445 \u0441\u0440\u0435\u0434\u043d\u0435\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438.

\u041e\u0441\u043d\u043e\u0432\u0430\u043d\u043d\u043e\u0441\u0442\u044c \u043d\u0430 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438: \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f (\\(<\\), \\(=\\), \\(>\\)), \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432; \u0435\u0435 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043b\u0443\u0447\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u0430 \\(O(n \\log n)\\) . \u0410 \u0432\u043e\u0442 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0431\u0435\u0437 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0439 \u043d\u0435 \u043e\u043f\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u0442\u044c \\(O(n)\\) , \u043d\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443 \u043d\u0435\u0435 \u043d\u0438\u0436\u0435.

"},{"location":"chapter_sorting/sorting_algorithm/#1112","title":"11.1.2 \u00a0 \u0418\u0434\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438","text":"

\u0411\u044b\u0441\u0442\u0440\u044b\u0439, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u043d\u0430 \u043c\u0435\u0441\u0442\u0435, \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0439, \u0430\u0434\u0430\u043f\u0442\u0438\u0432\u043d\u044b\u0439 \u0438 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u044b\u0439. \u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u043d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f\u0448\u043d\u0438\u0439 \u0434\u0435\u043d\u044c \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u043e\u0431\u043b\u0430\u0434\u0430\u043b \u0431\u044b \u0432\u0441\u0435\u043c\u0438 \u044d\u0442\u0438\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438 \u0432\u044b\u0431\u043e\u0440\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439 \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0438\u0437\u0443\u0447\u0438\u043c \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u044b\u0445 \u0432\u044b\u0448\u0435 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0435\u0432 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0438\u0445 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043a\u0438.

"},{"location":"chapter_sorting/summary/","title":"11.11 \u00a0 \u0420\u0435\u0437\u044e\u043c\u0435","text":""},{"location":"chapter_sorting/summary/#1","title":"1. \u00a0 \u041a\u043b\u044e\u0447\u0435\u0432\u044b\u0435 \u0432\u044b\u0432\u043e\u0434\u044b","text":"

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 11-19 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438

"},{"location":"chapter_sorting/summary/#2","title":"2. \u00a0 \u0412\u043e\u043f\u0440\u043e\u0441\u044b \u0438 \u043e\u0442\u0432\u0435\u0442\u044b","text":"

\u0412: \u0412 \u043a\u0430\u043a\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439?

\u0412 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u043d\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b \u043f\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0443. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u043e\u0432 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430: \u0438\u043c\u044f \u0438 \u0440\u043e\u0441\u0442. \u041c\u044b \u0445\u043e\u0442\u0438\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043c\u043d\u043e\u0433\u043e\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u0443\u044e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0443: \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c (A, 180) (B, 185) (C, 170) (D, 170) , \u0430 \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e \u0440\u043e\u0441\u0442\u0443. \u0415\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u0435\u043d, \u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c (D, 170) (C, 170) (A, 180) (B, 185) .

\u041d\u0435\u0442\u0440\u0443\u0434\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0442\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0441\u0442\u0443\u0434\u0435\u043d\u0442\u044b D \u0438 C \u043f\u043e\u043c\u0435\u043d\u044f\u043b\u0438\u0441\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438, \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u0440\u0430\u0437\u0440\u0443\u0448\u0438\u043b\u0441\u044f, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0438 \u043d\u0435 \u0445\u043e\u0442\u0438\u043c.

\u0412: \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u043e\u0440\u044f\u0434\u043e\u043a \"\u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e\" \u0438 \"\u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e\" \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0438 \u0441 \u043e\u043f\u043e\u0440\u043d\u044b\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c?

\u041d\u0435\u0442. \u0415\u0441\u043b\u0438 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \"\u043f\u043e\u0438\u0441\u043a \u0441\u043f\u0440\u0430\u0432\u0430 \u043d\u0430\u043b\u0435\u0432\u043e\", \u0430 \u0443\u0436\u0435 \u0437\u0430\u0442\u0435\u043c - \"\u043f\u043e\u0438\u0441\u043a \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e\". \u042d\u0442\u043e\u0442 \u0432\u044b\u0432\u043e\u0434 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0435\u0433\u043e \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435.

\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0448\u0430\u0433 partition() - \u044d\u0442\u043e \u043e\u0431\u043c\u0435\u043d nums[left] \u0438 nums[i] . \u041f\u043e\u0441\u043b\u0435 \u043e\u0431\u043c\u0435\u043d\u0430 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043b\u0435\u0432\u0430 \u043e\u0442 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c <= \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u043f\u0435\u0440\u0435\u0434 \u044d\u0442\u0438\u043c \u043e\u0431\u043c\u0435\u043d\u043e\u043c \u0434\u043e\u043b\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0443\u0441\u043b\u043e\u0432\u0438\u0435 nums[left] >= nums[i]. \u0415\u0441\u043b\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \"\u043f\u043e\u0438\u0441\u043a \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e\", \u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0443\u0434\u0430\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u0446\u0438\u043a\u043b \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 i == j , \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f, \u0447\u0442\u043e nums[j] == nums[i] > nums[left]. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0448\u0430\u0433\u0435 \u043e\u0431\u043c\u0435\u043d\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0431\u043e\u043b\u044c\u0448\u0438\u0439 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0435\u043d \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u0442\u0441\u044f \u043d\u0435\u0432\u0435\u0440\u043d\u043e.

\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 [0, 0, 0, 0, 1] , \u0435\u0441\u043b\u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \"\u043f\u043e\u0438\u0441\u043a \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e\", \u043f\u043e\u0441\u043b\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f [1, 0, 0, 0, 0] , \u0430 \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442.

\u0415\u0441\u043b\u0438 \u0436\u0435 \u0432\u044b\u0431\u0440\u0430\u0442\u044c nums[right] \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0442\u043e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u043e\u0439, \u0438 \u0442\u043e\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \"\u043f\u043e\u0438\u0441\u043a \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e\".

\u0412: \u041f\u043e\u0447\u0435\u043c\u0443 \u043f\u0440\u0438 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0432 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0435 \u0432\u044b\u0431\u043e\u0440 \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442, \u0447\u0442\u043e \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0441\u0438\u0442 \\(\\log n\\) ?

\u0413\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 - \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u0432\u044b\u0437\u043e\u0432\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0449\u0435 \u043d\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0438\u043b\u0438\u0441\u044c. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0440\u0430\u0443\u043d\u0434\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0440\u0430\u0437\u0431\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0434\u0432\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041f\u043e\u0441\u043b\u0435 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0438 \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0434\u043b\u0438\u043d\u0430 \u043f\u043e\u0434\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0441\u043f\u0443\u0441\u043a, \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u044b \u0434\u043b\u0438\u043d\u044b \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0415\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439, \u043a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u043e\u0432\u043d\u043e \u0432\u0434\u0432\u043e\u0435 \u043c\u0435\u043d\u044c\u0448\u0435, \u0438\u0442\u043e\u0433\u043e\u0432\u0430\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u043d\u0430 \\(\\log n\\) .

\u0412 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 \u0434\u043b\u044f \u0431\u043e\u043b\u0435\u0435 \u0434\u043b\u0438\u043d\u043d\u044b\u0445 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432; \u0432 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u044d\u0442\u043e \u0431\u0443\u0434\u0443\u0442 \u0434\u043b\u0438\u043d\u044b \\(n\\) , \\(n - 1\\) , \\(\\dots\\) , \\(2\\) , \\(1\\) , \u0430 \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u043e\u0439 \\(n\\) . \u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u043b\u0443\u0431\u0438\u043d\u044b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438 \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f.

\u0412: \u0415\u0441\u043b\u0438 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u043d\u044b, \u0431\u0443\u0434\u0435\u0442 \u043b\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) ? \u041a\u0430\u043a \u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c \u0432\u044b\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u0435\u043c?

\u0414\u0430. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0430 \u0442\u0440\u0438 \u0447\u0430\u0441\u0442\u0438: \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0435\u043d\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e, \u0440\u0430\u0432\u043d\u044b\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u043c\u0443 \u0438 \u0431\u043e\u043b\u044c\u0448\u0438\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e. \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u044e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0447\u0430\u0441\u0442\u0435\u0439 \u043c\u0435\u043d\u044c\u0448\u0435 \u0438 \u0431\u043e\u043b\u044c\u0448\u0435 \u043e\u043f\u043e\u0440\u043d\u043e\u0433\u043e. \u041f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043c\u0430\u0441\u0441\u0438\u0432, \u0446\u0435\u043b\u0438\u043a\u043e\u043c \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u0432\u0441\u0435\u0433\u043e \u0437\u0430 \u043e\u0434\u0438\u043d \u0440\u0430\u0443\u043d\u0434 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f.

\u0412: \u041f\u043e\u0447\u0435\u043c\u0443 \u0445\u0443\u0434\u0448\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0431\u043b\u043e\u0447\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0440\u0430\u0432\u043d\u0430 \\(O(n^2)\\) ?

\u0412 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0441\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u0432 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0431\u043b\u043e\u043a. \u0415\u0441\u043b\u0438 \u0437\u0430\u0442\u0435\u043c \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \\(O(n^2)\\) , \u0442\u043e \u043e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u043e\u0436\u0435 \u0441\u0442\u0430\u043d\u0435\u0442 \\(O(n^2)\\) .

"},{"location":"chapter_stack_and_queue/","title":"\u0413\u043b\u0430\u0432\u0430 5. \u00a0 \u0421\u0442\u0435\u043a \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c","text":"

Abstract

\u0421\u0442\u0435\u043a \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0441\u0442\u043e\u043f\u043a\u0443 \u043a\u043e\u0448\u0435\u043a, \u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c - \u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438\u0437 \u043a\u043e\u0448\u0435\u043a.

\u042d\u0442\u0438 \u0434\u0432\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \"\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\" \u0438 \"\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\".

"},{"location":"chapter_stack_and_queue/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_stack_and_queue/deque/","title":"5.3 \u00a0 \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c","text":"

\u0412 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u0438\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438\u0445 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0445\u0432\u043e\u0441\u0442. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-7, \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c (double-ended queue) \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0433\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u0438 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u0430\u043a \u0441 \u0433\u043e\u043b\u043e\u0432\u044b, \u0442\u0430\u043a \u0438 \u0441 \u0445\u0432\u043e\u0441\u0442\u0430.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-7 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438

"},{"location":"chapter_stack_and_queue/deque/#531","title":"5.3.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e","text":"

\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u044b \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 5-3. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 5-3 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438

\u0418\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c push_first() \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) push_last() \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) pop_first() \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) pop_last() \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) peek_first() \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) peek_last() \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\)

\u0422\u043e\u0447\u043d\u043e \u0442\u0430\u043a \u0436\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0443\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby deque.py
from collections import deque\n\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\ndeq: deque[int] = deque()\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\ndeq.append(2)      # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeq.append(5)\ndeq.append(4)\ndeq.appendleft(3)  # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeq.appendleft(1)\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\nfront: int = deq[0]  # \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nrear: int = deq[-1]  # \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\npop_front: int = deq.popleft()  # \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\npop_rear: int = deq.pop()       # \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\nsize: int = len(deq)\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nis_empty: bool = len(deq) == 0\n
deque.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\ndeque<int> deque;\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.push_back(2);   // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3);  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.push_front(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nint front = deque.front(); // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nint back = deque.back();   // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\ndeque.pop_front();  // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\ndeque.pop_back();   // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = deque.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty = deque.empty();\n
deque.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nDeque<Integer> deque = new LinkedList<>();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.offerLast(2);   // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.offerLast(5);\ndeque.offerLast(4);\ndeque.offerFirst(3);  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.offerFirst(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nint peekFirst = deque.peekFirst();  // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nint peekLast = deque.peekLast();    // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popFirst = deque.pollFirst();  // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\nint popLast = deque.pollLast();    // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = deque.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nboolean isEmpty = deque.isEmpty();\n
deque.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 C# \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a LinkedList\nLinkedList<int> deque = new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.AddLast(2);   // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.AddLast(5);\ndeque.AddLast(4);\ndeque.AddFirst(3);  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.AddFirst(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nint peekFirst = deque.First.Value;  // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nint peekLast = deque.Last.Value;    // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\ndeque.RemoveFirst();  // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\ndeque.RemoveLast();   // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = deque.Count;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool isEmpty = deque.Count == 0;\n
deque_test.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Go list \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\ndeque := list.New()\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.PushBack(2)      // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.PushBack(5)\ndeque.PushBack(4)\ndeque.PushFront(3)     // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.PushFront(1)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nfront := deque.Front() // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nrear := deque.Back()   // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\ndeque.Remove(front)    // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\ndeque.Remove(rear)     // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nsize := deque.Len()\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nisEmpty := deque.Len() == 0\n
deque.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Swift \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nvar deque: [Int] = []\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.append(2) // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.append(5)\ndeque.append(4)\ndeque.insert(3, at: 0) // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.insert(1, at: 0)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nlet peekFirst = deque.first! // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nlet peekLast = deque.last! // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u041f\u0440\u0438 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 Array \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c popFirst \u0440\u0430\u0432\u043d\u0430 O(n)\nlet popFirst = deque.removeFirst() // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\nlet popLast = deque.removeLast() // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet size = deque.count\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nlet isEmpty = deque.isEmpty\n
deque.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 JavaScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nconst deque = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 unshift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nconst peekFirst = deque[0];\nconst peekLast = deque[deque.length - 1];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 shift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\nconst popFront = deque.shift();\nconst popBack = deque.pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst size = deque.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nconst isEmpty = size === 0;\n
deque.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 TypeScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nconst deque: number[] = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.push(2);\ndeque.push(5);\ndeque.push(4);\n// \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 unshift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\ndeque.unshift(3);\ndeque.unshift(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nconst peekFirst: number = deque[0];\nconst peekLast: number = deque[deque.length - 1];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 shift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\nconst popFront: number = deque.shift() as number;\nconst popBack: number = deque.pop() as number;\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst size: number = deque.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nconst isEmpty: boolean = size === 0;\n
deque.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Dart Queue \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0430 \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nQueue<int> deque = Queue<int>();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.addLast(2);  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.addLast(5);\ndeque.addLast(4);\ndeque.addFirst(3); // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.addFirst(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nint peekFirst = deque.first; // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nint peekLast = deque.last;   // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popFirst = deque.removeFirst(); // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\nint popLast = deque.removeLast();   // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = deque.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool isEmpty = deque.isEmpty;\n
deque.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.push_back(2);  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.push_back(5);\ndeque.push_back(4);\ndeque.push_front(3); // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.push_front(1);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nif let Some(front) = deque.front() { // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\n}\nif let Some(rear) = deque.back() {   // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nif let Some(pop_front) = deque.pop_front() { // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\n}\nif let Some(pop_rear) = deque.pop_back() {   // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet size = deque.len();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nlet is_empty = deque.is_empty();\n
deque.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n
deque.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval deque = LinkedList<Int>()\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.offerLast(2)  // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0445\u0432\u043e\u0441\u0442\ndeque.offerLast(5)\ndeque.offerLast(4)\ndeque.offerFirst(3) // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\ndeque.offerFirst(1)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b */\nval peekFirst = deque.peekFirst() // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435\nval peekLast = deque.peekLast()   // \u042d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval popFirst = deque.pollFirst() // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b\nval popLast = deque.pollLast()   // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval size = deque.size\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nval isEmpty = deque.isEmpty()\n
deque.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n# \u0412 Ruby \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c\ndeque = []\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\ndeque << 2\ndeque << 5\ndeque << 4\n# \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 Array#unshift \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\ndeque.unshift(3)\ndeque.unshift(1)\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b\npeek_first = deque.first\npeek_last = deque.last\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n# \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 Array#shift \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\npop_front = deque.shift\npop_back = deque.pop\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\nsize = deque.length\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nis_empty = size.zero?\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D1%8E%D1%8E%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20deq%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BC%D0%B5%D1%81%D1%82%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20deq.append%282%29%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D0%B2%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20deq.append%285%29%0A%20%20%20%20deq.append%284%29%0A%20%20%20%20deq.appendleft%283%29%20%20%23%20%D0%94%D0%BE%D0%B1%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D0%B2%20%D0%B3%D0%BE%D0%BB%D0%BE%D0%B2%D1%83%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20deq.appendleft%281%29%0A%20%20%20%20print%28%22%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D1%8F%D1%8F%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%20deque%20%3D%22%2C%20deq%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%BE%D1%81%D1%82%D1%83%D0%BF%20%D0%BA%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%D1%83%0A%20%20%20%20front%20%3D%20deq%5B0%5D%20%20%23%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%B3%D0%BE%D0%BB%D0%BE%D0%B2%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20print%28%22%D0%AD%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20front%20%3D%22%2C%20front%29%0A%20%20%20%20rear%20%3D%20deq%5B-1%5D%20%20%23%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20print%28%22%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20rear%20%3D%22%2C%20rear%29%0A%0A%20%20%20%20%23%20%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20pop_front%20%3D%20deq.popleft%28%29%20%20%23%20%D0%B3%D0%BE%D0%BB%D0%BE%D0%B2%D0%B0%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20print%28%22%D0%AD%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%2C%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%B8%D0%B7%20%D0%B3%D0%BE%D0%BB%D0%BE%D0%B2%D1%8B%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%2C%20pop_front%20%3D%22%2C%20pop_front%29%0A%20%20%20%20print%28%22deque%20%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20%D0%B8%D0%B7%20%D0%B3%D0%BE%D0%BB%D0%BE%D0%B2%D1%8B%20%3D%22%2C%20deq%29%0A%20%20%20%20pop_rear%20%3D%20deq.pop%28%29%20%20%23%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20print%28%22%D0%AD%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%2C%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%B8%D0%B7%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%D0%B0%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%2C%20pop_rear%20%3D%22%2C%20pop_rear%29%0A%20%20%20%20print%28%22deque%20%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20%D0%B8%D0%B7%20%D1%85%D0%B2%D0%BE%D1%81%D1%82%D0%B0%20%3D%22%2C%20deq%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%BB%D0%B8%D0%BD%D1%83%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D0%B5%D0%B9%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20size%20%3D%20len%28deq%29%0A%20%20%20%20print%28%22%D0%94%D0%BB%D0%B8%D0%BD%D0%B0%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D0%B5%D0%B9%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20size%20%3D%22%2C%20size%29%0A%0A%20%20%20%20%23%20%D0%9F%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%B8%D1%82%D1%8C%2C%20%D0%BF%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D1%8F%D1%8F%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20is_empty%20%3D%20len%28deq%29%20%3D%3D%200%0A%20%20%20%20print%28%22%D0%9F%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D1%8F%D1%8F%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%20%3D%22%2C%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_stack_and_queue/deque/#532","title":"5.3.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 *","text":"

\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438: \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u043b\u0438 \u043c\u0430\u0441\u0441\u0438\u0432.

"},{"location":"chapter_stack_and_queue/deque/#1","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u0412\u0441\u043f\u043e\u043c\u043d\u0438\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0440\u0430\u0437\u0434\u0435\u043b: \u0442\u0430\u043c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u043e\u0434\u043d\u043e\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u043e \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b (\u044d\u0442\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 dequeue) \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 (\u044d\u0442\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 enqueue).

\u0414\u043b\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u0433\u043e\u043b\u043e\u0432\u0430, \u0438 \u0445\u0432\u043e\u0441\u0442 \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u044e\u0442 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0435\u0449\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \"\u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a\".

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u043c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0438 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u043b\u044b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u0430\u043a \u0433\u043e\u043b\u043e\u0432\u0443 \u0438 \u0445\u0432\u043e\u0441\u0442 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u043e\u0432 \u0441 \u043e\u0431\u0435\u0438\u0445 \u0441\u0442\u043e\u0440\u043e\u043d.

LinkedListDequepush_last()push_first()pop_last()pop_first()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-8 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 enqueue \u0438 dequeue \u0434\u043b\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435

\u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_deque.py
class ListNode:\n    \"\"\"\u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n\n    def __init__(self, val: int):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self.val: int = val\n        self.next: ListNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n        self.prev: ListNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\nclass LinkedListDeque:\n    \"\"\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._front: ListNode | None = None  # \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n        self._rear: ListNode | None = None  # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n        self._size: int = 0  # \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        return self._size == 0\n\n    def push(self, num: int, is_front: bool):\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        node = ListNode(num)\n        # \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if self.is_empty():\n            self._front = self._rear = node\n        # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        elif is_front:\n            # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            self._front.prev = node\n            node.next = self._front\n            self._front = node  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else:\n            # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            self._rear.next = node\n            node.prev = self._rear\n            self._rear = node  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        self._size += 1  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    def push_first(self, num: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        self.push(num, True)\n\n    def push_last(self, num: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        self.push(num, False)\n\n    def pop(self, is_front: bool) -> int:\n        \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if is_front:\n            val: int = self._front.val  # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            fnext: ListNode | None = self._front.next\n            if fnext is not None:\n                fnext.prev = None\n                self._front.next = None\n            self._front = fnext  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else:\n            val: int = self._rear.val  # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            rprev: ListNode | None = self._rear.prev\n            if rprev is not None:\n                rprev.next = None\n                self._rear.prev = None\n            self._rear = rprev  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        self._size -= 1  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return val\n\n    def pop_first(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self.pop(True)\n\n    def pop_last(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self.pop(False)\n\n    def peek_first(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        return self._front.val\n\n    def peek_last(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        return self._rear.val\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        node = self._front\n        res = [0] * self.size()\n        for i in range(self.size()):\n            res[i] = node.val\n            node = node.next\n        return res\n
linkedlist_deque.cpp
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nstruct DoublyListNode {\n    int val;              // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    DoublyListNode *next; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    DoublyListNode *prev; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n    DoublyListNode(int val) : val(val), prev(nullptr), next(nullptr) {\n    }\n};\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n  private:\n    DoublyListNode *front, *rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    int queSize = 0;              // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    LinkedListDeque() : front(nullptr), rear(nullptr) {\n    }\n\n    /* \u041c\u0435\u0442\u043e\u0434-\u0434\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ~LinkedListDeque() {\n        // \u041e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0443\u0437\u043b\u044b \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        DoublyListNode *pre, *cur = front;\n        while (cur != nullptr) {\n            pre = cur;\n            cur = cur->next;\n            delete pre;\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    void push(int num, bool isFront) {\n        DoublyListNode *node = new DoublyListNode(num);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (isEmpty())\n            front = rear = node;\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else if (isFront) {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            front->prev = node;\n            node->next = front;\n            front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            rear->next = node;\n            node->prev = rear;\n            rear = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int pop(bool isFront) {\n        if (isEmpty())\n            throw out_of_range(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        int val;\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if (isFront) {\n            val = front->val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            DoublyListNode *fNext = front->next;\n            if (fNext != nullptr) {\n                fNext->prev = nullptr;\n                front->next = nullptr;\n            }\n            delete front;\n            front = fNext; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            val = rear->val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            DoublyListNode *rPrev = rear->prev;\n            if (rPrev != nullptr) {\n                rPrev->next = nullptr;\n                rear->prev = nullptr;\n            }\n            delete rear;\n            rear = rPrev; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize--; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return val;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int popFirst() {\n        return pop(true);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int popLast() {\n        return pop(false);\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        return front->val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        return rear->val;\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    vector<int> toVector() {\n        DoublyListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
linkedlist_deque.java
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    int val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    ListNode next; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    ListNode prev; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\n    ListNode(int val) {\n        this.val = val;\n        prev = next = null;\n    }\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    private ListNode front, rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private int queSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    public LinkedListDeque() {\n        front = rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    private void push(int num, boolean isFront) {\n        ListNode node = new ListNode(num);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (isEmpty())\n            front = rear = node;\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else if (isFront) {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            front.prev = node;\n            node.next = front;\n            front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            rear.next = node;\n            node.prev = rear;\n            rear = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void pushFirst(int num) {\n        push(num, true);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void pushLast(int num) {\n        push(num, false);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    private int pop(boolean isFront) {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        int val;\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if (isFront) {\n            val = front.val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            ListNode fNext = front.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front.next = null;\n            }\n            front = fNext; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            val = rear.val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            ListNode rPrev = rear.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear.prev = null;\n            }\n            rear = rPrev; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize--; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return val;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int popFirst() {\n        return pop(true);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int popLast() {\n        return pop(false);\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return rear.val;\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_deque.cs
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode(int val) {\n    public int val = val;       // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    public ListNode? next = null; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    public ListNode? prev = null; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    ListNode? front, rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    int queSize = 0;      // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    public LinkedListDeque() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    void Push(int num, bool isFront) {\n        ListNode node = new(num);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (IsEmpty()) {\n            front = node;\n            rear = node;\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else if (isFront) {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            front!.prev = node;\n            node.next = front;\n            front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            rear!.next = node;\n            node.prev = rear;\n            rear = node;  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n\n        queSize++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void PushFirst(int num) {\n        Push(num, true);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void PushLast(int num) {\n        Push(num, false);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int? Pop(bool isFront) {\n        if (IsEmpty())\n            throw new Exception();\n        int? val;\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if (isFront) {\n            val = front?.val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            ListNode? fNext = front?.next;\n            if (fNext != null) {\n                fNext.prev = null;\n                front!.next = null;\n            }\n            front = fNext;   // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            val = rear?.val;  // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            ListNode? rPrev = rear?.prev;\n            if (rPrev != null) {\n                rPrev.next = null;\n                rear!.prev = null;\n            }\n            rear = rPrev;    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n\n        queSize--; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return val;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int? PopFirst() {\n        return Pop(true);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int? PopLast() {\n        return Pop(false);\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int? PeekFirst() {\n        if (IsEmpty())\n            throw new Exception();\n        return front?.val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int? PeekLast() {\n        if (IsEmpty())\n            throw new Exception();\n        return rear?.val;\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    public int?[] ToArray() {\n        ListNode? node = front;\n        int?[] res = new int?[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node?.val;\n            node = node?.next;\n        }\n\n        return res;\n    }\n}\n
linkedlist_deque.go
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype linkedListDeque struct {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 list\n    data *list.List\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc newLinkedListDeque() *linkedListDeque {\n    return &linkedListDeque{\n        data: list.New(),\n    }\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) pushFirst(value any) {\n    s.data.PushFront(value)\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) pushLast(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) popFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) popLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) peekFirst() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) peekLast() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListDeque) size() int {\n    return s.data.Len()\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (s *linkedListDeque) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c List \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (s *linkedListDeque) toList() *list.List {\n    return s.data\n}\n
linkedlist_deque.swift
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    var val: Int // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var next: ListNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    weak var prev: ListNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\n    init(val: Int) {\n        self.val = val\n    }\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    private var front: ListNode? // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    private var rear: ListNode? // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private var _size: Int // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    init() {\n        _size = 0\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    private func push(num: Int, isFront: Bool) {\n        let node = ListNode(val: num)\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if isEmpty() {\n            front = node\n            rear = node\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else if isFront {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            front?.prev = node\n            node.next = front\n            front = node // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        _size += 1 // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func pushFirst(num: Int) {\n        push(num: num, isFront: true)\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func pushLast(num: Int) {\n        push(num: num, isFront: false)\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    private func pop(isFront: Bool) -> Int {\n        if isEmpty() {\n            fatalError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        let val: Int\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if isFront {\n            val = front!.val // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            let fNext = front?.next\n            if fNext != nil {\n                fNext?.prev = nil\n                front?.next = nil\n            }\n            front = fNext // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            val = rear!.val // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            let rPrev = rear?.prev\n            if rPrev != nil {\n                rPrev?.next = nil\n                rear?.prev = nil\n            }\n            rear = rPrev // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        _size -= 1 // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return val\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func popFirst() -> Int {\n        pop(isFront: true)\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func popLast() -> Int {\n        pop(isFront: false)\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        return front!.val\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        return rear!.val\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
linkedlist_deque.js
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    prev; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c)\n    next; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c)\n    val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    #front; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    #rear; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    #queSize; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n        this.#queSize = 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushLast(val) {\n        const node = new ListNode(val);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            this.#rear.next = node;\n            node.prev = this.#rear;\n            this.#rear = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        this.#queSize++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushFirst(val) {\n        const node = new ListNode(val);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (this.#queSize === 0) {\n            this.#front = node;\n            this.#rear = node;\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            this.#front.prev = node;\n            node.next = this.#front;\n            this.#front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        this.#queSize++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popLast() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#rear.val; // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        let temp = this.#rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.#rear.prev = null;\n        }\n        this.#rear = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popFirst() {\n        if (this.#queSize === 0) {\n            return null;\n        }\n        const value = this.#front.val; // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        let temp = this.#front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.#front.next = null;\n        }\n        this.#front = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.#queSize--;\n        return value;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekLast() {\n        return this.#queSize === 0 ? null : this.#rear.val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekFirst() {\n        return this.#queSize === 0 ? null : this.#front.val;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    print() {\n        const arr = [];\n        let temp = this.#front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
linkedlist_deque.ts
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n    prev: ListNode; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c)\n    next: ListNode; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c)\n    val: number; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n\n    constructor(val: number) {\n        this.val = val;\n        this.next = null;\n        this.prev = null;\n    }\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    private front: ListNode; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    private rear: ListNode; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private queSize: number; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n        this.queSize = 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushLast(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            this.rear.next = node;\n            node.prev = this.rear;\n            this.rear = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        this.queSize++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushFirst(val: number): void {\n        const node: ListNode = new ListNode(val);\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (this.queSize === 0) {\n            this.front = node;\n            this.rear = node;\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            this.front.prev = node;\n            node.next = this.front;\n            this.front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        this.queSize++;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popLast(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.rear.val; // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        let temp: ListNode = this.rear.prev;\n        if (temp !== null) {\n            temp.next = null;\n            this.rear.prev = null;\n        }\n        this.rear = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.queSize--;\n        return value;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popFirst(): number {\n        if (this.queSize === 0) {\n            return null;\n        }\n        const value: number = this.front.val; // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        let temp: ListNode = this.front.next;\n        if (temp !== null) {\n            temp.prev = null;\n            this.front.next = null;\n        }\n        this.front = temp; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.queSize--;\n        return value;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekLast(): number {\n        return this.queSize === 0 ? null : this.rear.val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekFirst(): number {\n        return this.queSize === 0 ? null : this.front.val;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    print(): void {\n        const arr: number[] = [];\n        let temp: ListNode = this.front;\n        while (temp !== null) {\n            arr.push(temp.val);\n            temp = temp.next;\n        }\n        console.log('[' + arr.join(', ') + ']');\n    }\n}\n
linkedlist_deque.dart
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode {\n  int val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  ListNode? next; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n  ListNode? prev; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\n  ListNode(this.val, {this.next, this.prev});\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n  late ListNode? _front; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b _front\n  late ListNode? _rear; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b _rear\n  int _queSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  LinkedListDeque() {\n    this._front = null;\n    this._rear = null;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int size() {\n    return this._queSize;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  bool isEmpty() {\n    return size() == 0;\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  void push(int _num, bool isFront) {\n    final ListNode node = ListNode(_num);\n    if (isEmpty()) {\n      // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u043f\u0443\u0441\u0442\u044c _front \u0438 _rear \u043e\u0431\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 node\n      _front = _rear = node;\n    } else if (isFront) {\n      // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n      // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\n      _front!.prev = node;\n      node.next = _front;\n      _front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    } else {\n      // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n      // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u043a\u043e\u043d\u0435\u0446 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\n      _rear!.next = node;\n      node.prev = _rear;\n      _rear = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    }\n    _queSize++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  void pushFirst(int _num) {\n    push(_num, true);\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  void pushLast(int _num) {\n    push(_num, false);\n  }\n\n  /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int? pop(bool isFront) {\n    // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c null\n    if (isEmpty()) {\n      return null;\n    }\n    final int val;\n    if (isFront) {\n      // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n      val = _front!.val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n      // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n      ListNode? fNext = _front!.next;\n      if (fNext != null) {\n        fNext.prev = null;\n        _front!.next = null;\n      }\n      _front = fNext; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    } else {\n      // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n      val = _rear!.val; // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n      // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n      ListNode? rPrev = _rear!.prev;\n      if (rPrev != null) {\n        rPrev.next = null;\n        _rear!.prev = null;\n      }\n      _rear = rPrev; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    }\n    _queSize--; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    return val;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int? popFirst() {\n    return pop(true);\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int? popLast() {\n    return pop(false);\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int? peekFirst() {\n    return _front?.val;\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int? peekLast() {\n    return _rear?.val;\n  }\n\n  /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> res = [];\n    for (int i = 0; i < _queSize; i++) {\n      res.add(node!.val);\n      node = node.next;\n    }\n    return res;\n  }\n}\n
linkedlist_deque.rs
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\npub struct ListNode<T> {\n    pub val: T,                                 // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    pub next: Option<Rc<RefCell<ListNode<T>>>>, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    pub prev: Option<Rc<RefCell<ListNode<T>>>>, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n}\n\nimpl<T> ListNode<T> {\n    pub fn new(val: T) -> Rc<RefCell<ListNode<T>>> {\n        Rc::new(RefCell::new(ListNode {\n            val,\n            next: None,\n            prev: None,\n        }))\n    }\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[allow(dead_code)]\npub struct LinkedListDeque<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    que_size: usize,                         // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n}\n\nimpl<T: Copy> LinkedListDeque<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fn push(&mut self, num: T, is_front: bool) {\n        let node = ListNode::new(num);\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if is_front {\n            match self.front.take() {\n                // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n                None => {\n                    self.rear = Some(node.clone());\n                    self.front = Some(node);\n                }\n                // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n                Some(old_front) => {\n                    old_front.borrow_mut().prev = Some(node.clone());\n                    node.borrow_mut().next = Some(old_front);\n                    self.front = Some(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n                }\n            }\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            match self.rear.take() {\n                // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n                None => {\n                    self.front = Some(node.clone());\n                    self.rear = Some(node);\n                }\n                // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n                Some(old_rear) => {\n                    old_rear.borrow_mut().next = Some(node.clone());\n                    node.borrow_mut().prev = Some(old_rear);\n                    self.rear = Some(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n                }\n            }\n        }\n        self.que_size += 1; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn push_first(&mut self, num: T) {\n        self.push(num, true);\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn push_last(&mut self, num: T) {\n        self.push(num, false);\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn pop(&mut self, is_front: bool) -> Option<T> {\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c None\n        if self.is_empty() {\n            return None;\n        };\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if is_front {\n            self.front.take().map(|old_front| {\n                match old_front.borrow_mut().next.take() {\n                    Some(new_front) => {\n                        new_front.borrow_mut().prev.take();\n                        self.front = Some(new_front); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n                    }\n                    None => {\n                        self.rear.take();\n                    }\n                }\n                self.que_size -= 1; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n                old_front.borrow().val\n            })\n        }\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        else {\n            self.rear.take().map(|old_rear| {\n                match old_rear.borrow_mut().prev.take() {\n                    Some(new_rear) => {\n                        new_rear.borrow_mut().next.take();\n                        self.rear = Some(new_rear); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n                    }\n                    None => {\n                        self.front.take();\n                    }\n                }\n                self.que_size -= 1; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n                old_rear.borrow().val\n            })\n        }\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn pop_first(&mut self) -> Option<T> {\n        return self.pop(true);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn pop_last(&mut self) -> Option<T> {\n        return self.pop(false);\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn peek_first(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn peek_last(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.rear.as_ref()\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n        res\n    }\n}\n
linkedlist_deque.c
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct DoublyListNode {\n    int val;                     // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    struct DoublyListNode *next; // \u0423\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    struct DoublyListNode *prev; // \u0423\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n} DoublyListNode;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nDoublyListNode *newDoublyListNode(int num) {\n    DoublyListNode *new = (DoublyListNode *)malloc(sizeof(DoublyListNode));\n    new->val = num;\n    new->next = NULL;\n    new->prev = NULL;\n    return new;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delDoublyListNode(DoublyListNode *node) {\n    free(node);\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct {\n    DoublyListNode *front, *rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    int queSize;                  // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n} LinkedListDeque;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nLinkedListDeque *newLinkedListDeque() {\n    LinkedListDeque *deque = (LinkedListDeque *)malloc(sizeof(LinkedListDeque));\n    deque->front = NULL;\n    deque->rear = NULL;\n    deque->queSize = 0;\n    return deque;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delLinkedListdeque(LinkedListDeque *deque) {\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u0437\u043b\u044b\n    for (int i = 0; i < deque->queSize && deque->front != NULL; i++) {\n        DoublyListNode *tmp = deque->front;\n        deque->front = deque->front->next;\n        free(tmp);\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 deque\n    free(deque);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size(LinkedListDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty(LinkedListDeque *deque) {\n    return (size(deque) == 0);\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid push(LinkedListDeque *deque, int num, bool isFront) {\n    DoublyListNode *node = newDoublyListNode(num);\n    // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u043f\u0443\u0441\u0442\u044c front \u0438 rear \u043e\u0431\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 node\n    if (empty(deque)) {\n        deque->front = deque->rear = node;\n    }\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    else if (isFront) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n        deque->front->prev = node;\n        node->next = deque->front;\n        deque->front = node; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    }\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    else {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n        deque->rear->next = node;\n        node->prev = deque->rear;\n        deque->rear = node;\n    }\n    deque->queSize++; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nvoid pushFirst(LinkedListDeque *deque, int num) {\n    push(deque, num, true);\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nvoid pushLast(LinkedListDeque *deque, int num) {\n    push(deque, num, false);\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peekFirst(LinkedListDeque *deque) {\n    assert(size(deque) && deque->front);\n    return deque->front->val;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peekLast(LinkedListDeque *deque) {\n    assert(size(deque) && deque->rear);\n    return deque->rear->val;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop(LinkedListDeque *deque, bool isFront) {\n    if (empty(deque))\n        return -1;\n    int val;\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    if (isFront) {\n        val = peekFirst(deque); // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        DoublyListNode *fNext = deque->front->next;\n        if (fNext) {\n            fNext->prev = NULL;\n            deque->front->next = NULL;\n        }\n        delDoublyListNode(deque->front);\n        deque->front = fNext; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    }\n    // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    else {\n        val = peekLast(deque); // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        DoublyListNode *rPrev = deque->rear->prev;\n        if (rPrev) {\n            rPrev->next = NULL;\n            deque->rear->prev = NULL;\n        }\n        delDoublyListNode(deque->rear);\n        deque->rear = rPrev; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    }\n    deque->queSize--; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    return val;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popFirst(LinkedListDeque *deque) {\n    return pop(deque, true);\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popLast(LinkedListDeque *deque) {\n    return pop(deque, false);\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid printLinkedListDeque(LinkedListDeque *deque) {\n    int *arr = malloc(sizeof(int) * deque->queSize);\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\n    int i;\n    DoublyListNode *node;\n    for (i = 0, node = deque->front; i < deque->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, deque->queSize);\n    free(arr);\n}\n
linkedlist_deque.kt
/* \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass ListNode(var _val: Int) {\n    // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var next: ListNode? = null // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n    var prev: ListNode? = null // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n}\n\n/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListDeque {\n    private var front: ListNode? = null // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    private var rear: ListNode? = null // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private var queSize: Int = 0 // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun push(num: Int, isFront: Boolean) {\n        val node = ListNode(num)\n        // \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 front, \u0438 rear \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 node\n        if (isEmpty()) {\n            rear = node\n            front = rear\n            // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else if (isFront) {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n            front?.prev = node\n            node.next = front\n            front = node // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n            rear?.next = node\n            node.prev = rear\n            rear = node // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize++ // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pushFirst(num: Int) {\n        push(num, true)\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pushLast(num: Int) {\n        push(num, false)\n    }\n\n    /* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pop(isFront: Boolean): Int {\n        if (isEmpty()) \n            throw IndexOutOfBoundsException()\n        val _val: Int\n        // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        if (isFront) {\n            _val = front!!._val // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            val fNext = front!!.next\n            if (fNext != null) {\n                fNext.prev = null\n                front!!.next = null\n            }\n            front = fNext // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n            // \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        } else {\n            _val = rear!!._val // \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            val rPrev = rear!!.prev\n            if (rPrev != null) {\n                rPrev.next = null\n                rear!!.prev = null\n            }\n            rear = rPrev // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n        }\n        queSize-- // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        return _val\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun popFirst(): Int {\n        return pop(true)\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun popLast(): Int {\n        return pop(false)\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return rear!!._val\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
linkedlist_deque.rb
=begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n  attr_accessor :prev # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n=begin\nFile: linkedlist_deque.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0423\u0437\u0435\u043b \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\nclass ListNode\n  attr_accessor :val\n  attr_accessor :next # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0435\u043c\u043d\u0438\u043a\n  attr_accessor :prev # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0443\u0437\u0435\u043b-\u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u0438\u043a\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(val)\n    @val = val\n  end\nend\n\n# ## \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 ###\nclass LinkedListDeque\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  attr_reader :size\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @front = nil  # \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    @rear = nil   # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    @size = 0     # \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def is_empty?\n    size.zero?\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def push(num, is_front)\n    node = ListNode.new(num)\n    # \u0415\u0441\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0443\u0441\u0442, \u043f\u0443\u0441\u0442\u044c front \u0438 rear \u043e\u0431\u0430 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 node\n    if is_empty?\n      @front = @rear = node\n    # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    elsif is_front\n      # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u043f\u0438\u0441\u043a\u0430\n      @front.prev = node\n      node.next = @front\n      @front = node # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    else\n      # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442 \u0441\u043f\u0438\u0441\u043a\u0430\n      @rear.next = node\n      node.prev = @rear\n      @rear = node # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    end\n    @size += 1 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def push_first(num)\n    push(num, true)\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def push_last(num)\n    push(num, false)\n  end\n\n  # ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop(is_front)\n    raise IndexError, '\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    if is_front\n      val = @front.val # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n      # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n      fnext = @front.next\n      unless fnext.nil?\n        fnext.prev = nil\n        @front.next = nil\n      end\n      @front = fnext # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    # \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    else\n      val = @rear.val # \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n      # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n      rprev = @rear.prev\n      unless rprev.nil?\n        rprev.next = nil\n        @rear.prev = nil\n      end\n      @rear = rprev # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    end\n    @size -= 1 # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    val\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop_first\n    pop(true)\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop_last\n    pop(false)\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek_first\n    raise IndexError, '\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    @front.val\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek_last\n    raise IndexError, '\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    @rear.val\n  end\n\n  # ## \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 ###\n  def to_array\n    node = @front\n    res = Array.new(size, 0)\n    for i in 0...size\n      res[i] = node.val\n      node = node.next\n    end\n    res\n  end\nend\n
"},{"location":"chapter_stack_and_queue/deque/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.

ArrayDequepush_last()push_first()pop_last()pop_first()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-9 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 enqueue \u0438 dequeue \u0434\u043b\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435

\u041d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b \"enqueue \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\" \u0438 \"dequeue \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430\":

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_deque.py
class ArrayDeque:\n    \"\"\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n\n    def __init__(self, capacity: int):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._nums: list[int] = [0] * capacity\n        self._front: int = 0\n        self._size: int = 0\n\n    def capacity(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        return self._size == 0\n\n    def index(self, i: int) -> int:\n        \"\"\"\u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435\"\"\"\n        # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        # \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        # \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + self.capacity()) % self.capacity()\n\n    def push_first(self, num: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self._size == self.capacity():\n            print(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        self._front = self.index(self._front - 1)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self._nums[self._front] = num\n        self._size += 1\n\n    def push_last(self, num: int):\n        \"\"\"\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self._size == self.capacity():\n            print(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        rear = self.index(self._front + self._size)\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop_first(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        num = self.peek_first()\n        # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        self._front = self.index(self._front + 1)\n        self._size -= 1\n        return num\n\n    def pop_last(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        num = self.peek_last()\n        self._size -= 1\n        return num\n\n    def peek_first(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        return self._nums[self._front]\n\n    def peek_last(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        last = self.index(self._front + self._size - 1)\n        return self._nums[last]\n\n    def to_array(self) -> list[int]:\n        \"\"\"\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        res = []\n        for i in range(self._size):\n            res.append(self._nums[self.index(self._front + i)])\n        return res\n
array_deque.cpp
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n  private:\n    vector<int> nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize;      // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ArrayDeque(int capacity) {\n        nums.resize(capacity);\n        front = queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int capacity() {\n        return nums.size();\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    int index(int i) {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    void pushFirst(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\" << endl;\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        front = index(front - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    void pushLast(int num) {\n        if (queSize == capacity()) {\n            cout << \"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\" << endl;\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        int rear = index(front + queSize);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int popFirst() {\n        int num = peekFirst();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peekFirst() {\n        if (isEmpty())\n            throw out_of_range(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        return nums[front];\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peekLast() {\n        if (isEmpty())\n            throw out_of_range(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    vector<int> toVector() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        vector<int> res(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n};\n
array_deque.java
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n    private int[] nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private int front; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private int queSize; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public ArrayDeque(int capacity) {\n        this.nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    private int index(int i) {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + capacity()) % capacity();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void pushFirst(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        front = index(front - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void pushLast(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        int rear = index(front + queSize);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int popFirst() {\n        int num = peekFirst();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        front = index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int popLast() {\n        int num = peekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peekFirst() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peekLast() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        int last = index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    public int[] toArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[index(j)];\n        }\n        return res;\n    }\n}\n
array_deque.cs
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n    int[] nums;  // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;   // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public ArrayDeque(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    int Index(int i) {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + Capacity()) % Capacity();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void PushFirst(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        front = Index(front - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[front] = num;\n        queSize++;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public void PushLast(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        int rear = Index(front + queSize);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int PopFirst() {\n        int num = PeekFirst();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        front = Index(front + 1);\n        queSize--;\n        return num;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int PopLast() {\n        int num = PeekLast();\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int PeekFirst() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        return nums[front];\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int PeekLast() {\n        if (IsEmpty()) {\n            throw new InvalidOperationException();\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        int last = Index(front + queSize - 1);\n        return nums[last];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    public int[] ToArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[Index(j)];\n        }\n        return res;\n    }\n}\n
array_deque.go
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntype arrayDeque struct {\n    nums        []int // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    front       int   // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queSize     int   // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queCapacity int   // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (\u0442\u043e \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc newArrayDeque(queCapacity int) *arrayDeque {\n    return &arrayDeque{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) size() int {\n    return q.queSize\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (q *arrayDeque) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nfunc (q *arrayDeque) index(i int) int {\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n    return (i + q.queCapacity) % q.queCapacity\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) pushFirst(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n        return\n    }\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n    q.front = q.index(q.front - 1)\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    q.nums[q.front] = num\n    q.queSize++\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) pushLast(num int) {\n    if q.queSize == q.queCapacity {\n        fmt.Println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n        return\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    rear := q.index(q.front + q.queSize)\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) popFirst() any {\n    num := q.peekFirst()\n    if num == nil {\n        return nil\n    }\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    q.front = q.index(q.front + 1)\n    q.queSize--\n    return num\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) popLast() any {\n    num := q.peekLast()\n    if num == nil {\n        return nil\n    }\n    q.queSize--\n    return num\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) peekFirst() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayDeque) peekLast() any {\n    if q.isEmpty() {\n        return nil\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    last := q.index(q.front + q.queSize - 1)\n    return q.nums[last]\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c Slice \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (q *arrayDeque) toSlice() []int {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    res := make([]int, q.queSize)\n    for i, j := 0, q.front; i < q.queSize; i++ {\n        res[i] = q.nums[q.index(j)]\n        j++\n    }\n    return res\n}\n
array_deque.swift
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n    private var nums: [Int] // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var front: Int // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var _size: Int // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init(capacity: Int) {\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    private func index(i: Int) -> Int {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        (i + capacity()) % capacity()\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func pushFirst(num: Int) {\n        if size() == capacity() {\n            print(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        front = index(i: front - 1)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[front] = num\n        _size += 1\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func pushLast(num: Int) {\n        if size() == capacity() {\n            print(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        let rear = index(i: front + size())\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func popFirst() -> Int {\n        let num = peekFirst()\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        front = index(i: front + 1)\n        _size -= 1\n        return num\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func popLast() -> Int {\n        let num = peekLast()\n        _size -= 1\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peekFirst() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        return nums[front]\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peekLast() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        let last = index(i: front + size() - 1)\n        return nums[last]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    func toArray() -> [Int] {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        (front ..< front + size()).map { nums[index(i: $0)] }\n    }\n}\n
array_deque.js
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n    #nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    #front; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    #queSize; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n        this.#front = 0;\n        this.#queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    size() {\n        return this.#queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    index(i) {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushFirst(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        this.#front = this.index(this.#front - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.#nums[this.#front] = num;\n        this.#queSize++;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushLast(num) {\n        if (this.#queSize === this.capacity()) {\n            console.log('\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        const rear = this.index(this.#front + this.#queSize);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popFirst() {\n        const num = this.peekFirst();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        this.#front = this.index(this.#front + 1);\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popLast() {\n        const num = this.peekLast();\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekFirst() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.#nums[this.#front];\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekLast() {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        const last = this.index(this.#front + this.#queSize - 1);\n        return this.#nums[last];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    toArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const res = [];\n        for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {\n            res[i] = this.#nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
array_deque.ts
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n    private nums: number[]; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private front: number; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private queSize: number; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = 0;\n        this.queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    size(): number {\n        return this.queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    index(i: number): number {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + this.capacity()) % this.capacity();\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushFirst(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        this.front = this.index(this.front - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.nums[this.front] = num;\n        this.queSize++;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pushLast(num: number): void {\n        if (this.queSize === this.capacity()) {\n            console.log('\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        const rear: number = this.index(this.front + this.queSize);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popFirst(): number {\n        const num: number = this.peekFirst();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        this.front = this.index(this.front + 1);\n        this.queSize--;\n        return num;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    popLast(): number {\n        const num: number = this.peekLast();\n        this.queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekFirst(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        return this.nums[this.front];\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peekLast(): number {\n        if (this.isEmpty()) throw new Error('The Deque Is Empty.');\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        const last = this.index(this.front + this.queSize - 1);\n        return this.nums[last];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    toArray(): number[] {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const res: number[] = [];\n        for (let i = 0, j = this.front; i < this.queSize; i++, j++) {\n            res[i] = this.nums[this.index(j)];\n        }\n        return res;\n    }\n}\n
array_deque.dart
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayDeque {\n  late List<int> _nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  late int _front; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  late int _queSize; // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  ArrayDeque(int capacity) {\n    this._nums = List.filled(capacity, 0);\n    this._front = this._queSize = 0;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int capacity() {\n    return _nums.length;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n  int index(int i) {\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n    return (i + capacity()) % capacity();\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  void pushFirst(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n    }\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043b\u0435\u0432\u043e \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0442 _front \u043a \u0445\u0432\u043e\u0441\u0442\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    _front = index(_front - 1);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c _num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    _nums[_front] = _num;\n    _queSize++;\n  }\n\n  /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  void pushLast(int _num) {\n    if (_queSize == capacity()) {\n      throw Exception(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    int rear = index(_front + _queSize);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c _num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int popFirst() {\n    int _num = peekFirst();\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043f\u0440\u0430\u0432\u043e \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    _front = index(_front + 1);\n    _queSize--;\n    return _num;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int popLast() {\n    int _num = peekLast();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int peekFirst() {\n    if (isEmpty()) {\n      throw Exception(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int peekLast() {\n    if (isEmpty()) {\n      throw Exception(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    int last = index(_front + _queSize - 1);\n    return _nums[last];\n  }\n\n  /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n  List<int> toArray() {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[index(j)];\n    }\n    return res;\n  }\n}\n
array_deque.rs
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nstruct ArrayDeque<T> {\n    nums: Vec<T>,    // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    front: usize,    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    que_size: usize, // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n}\n\nimpl<T: Copy + Default> ArrayDeque<T> {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    pub fn new(capacity: usize) -> Self {\n        Self {\n            nums: vec![T::default(); capacity],\n            front: 0,\n            que_size: 0,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn capacity(&self) -> usize {\n        self.nums.len()\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn size(&self) -> usize {\n        self.que_size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    pub fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    fn index(&self, i: i32) -> usize {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        ((i + self.capacity() as i32) % self.capacity() as i32) as usize\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn push_first(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        self.front = self.index(self.front as i32 - 1);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self.nums[self.front] = num;\n        self.que_size += 1;\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn push_last(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        let rear = self.index(self.front as i32 + self.que_size as i32);\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self.nums[rear] = num;\n        self.que_size += 1;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn pop_first(&mut self) -> T {\n        let num = self.peek_first();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        self.front = self.index(self.front as i32 + 1);\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn pop_last(&mut self) -> T {\n        let num = self.peek_last();\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn peek_first(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        };\n        self.nums[self.front]\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn peek_last(&self) -> T {\n        if self.is_empty() {\n            panic!(\"\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        };\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        let last = self.index(self.front as i32 + self.que_size as i32 - 1);\n        self.nums[last]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    fn to_array(&self) -> Vec<T> {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        let mut res = vec![T::default(); self.que_size];\n        let mut j = self.front;\n        for i in 0..self.que_size {\n            res[i] = self.nums[self.index(j as i32)];\n            j += 1;\n        }\n        res\n    }\n}\n
array_deque.c
/* \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntypedef struct {\n    int *nums;       // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;       // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize;     // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u0430\n    int queCapacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n} ArrayDeque;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nArrayDeque *newArrayDeque(int capacity) {\n    ArrayDeque *deque = (ArrayDeque *)malloc(sizeof(ArrayDeque));\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    deque->queCapacity = capacity;\n    deque->nums = (int *)malloc(sizeof(int) * deque->queCapacity);\n    deque->front = deque->queSize = 0;\n    return deque;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delArrayDeque(ArrayDeque *deque) {\n    free(deque->nums);\n    free(deque);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint capacity(ArrayDeque *deque) {\n    return deque->queCapacity;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size(ArrayDeque *deque) {\n    return deque->queSize;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty(ArrayDeque *deque) {\n    return deque->queSize == 0;\n}\n\n/* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\nint dequeIndex(ArrayDeque *deque, int i) {\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0445\u0432\u043e\u0441\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043d\u0430\u0447\u0430\u043b\u0443\n    // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u043e\u043b\u043e\u0432\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\u0441\u044f \u043a \u043a\u043e\u043d\u0446\u0443\n    return ((i + capacity(deque)) % capacity(deque));\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nvoid pushFirst(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u0414\u0435\u043a \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\\r\\n\");\n        return;\n    }\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432\u043b\u0435\u0432\u043e \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0442 front \u043a \u0445\u0432\u043e\u0441\u0442\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    deque->front = dequeIndex(deque, deque->front - 1);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    deque->nums[deque->front] = num;\n    deque->queSize++;\n}\n\n/* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nvoid pushLast(ArrayDeque *deque, int num) {\n    if (deque->queSize == capacity(deque)) {\n        printf(\"\u0414\u0435\u043a \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\\r\\n\");\n        return;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    int rear = dequeIndex(deque, deque->front + deque->queSize);\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    deque->nums[rear] = num;\n    deque->queSize++;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peekFirst(ArrayDeque *deque) {\n    // \u041e\u0448\u0438\u0431\u043a\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430: \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\n    assert(empty(deque) == 0);\n    return deque->nums[deque->front];\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peekLast(ArrayDeque *deque) {\n    // \u041e\u0448\u0438\u0431\u043a\u0430 \u0434\u043e\u0441\u0442\u0443\u043f\u0430: \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\n    assert(empty(deque) == 0);\n    int last = dequeIndex(deque, deque->front + deque->queSize - 1);\n    return deque->nums[last];\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popFirst(ArrayDeque *deque) {\n    int num = peekFirst(deque);\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    deque->front = dequeIndex(deque, deque->front + 1);\n    deque->queSize--;\n    return num;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint popLast(ArrayDeque *deque) {\n    int num = peekLast(deque);\n    deque->queSize--;\n    return num;\n}\n\n/* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nint *toArray(ArrayDeque *deque, int *queSize) {\n    *queSize = deque->queSize;\n    int *res = (int *)calloc(deque->queSize, sizeof(int));\n    int j = deque->front;\n    for (int i = 0; i < deque->queSize; i++) {\n        res[i] = deque->nums[j % deque->queCapacity];\n        j++;\n    }\n    return res;\n}\n
array_deque.kt
/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nclass ArrayDeque(capacity: Int) {\n    private var nums: IntArray = IntArray(capacity) // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var front: Int = 0 // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var queSize: Int = 0 // \u0414\u043b\u0438\u043d\u0430 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 */\n    private fun index(i: Int): Int {\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n        // \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n        return (i + capacity()) % capacity()\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pushFirst(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n        front = index(front - 1)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[front] = num\n        queSize++\n    }\n\n    /* \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pushLast(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        val rear = index(front + queSize)\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun popFirst(): Int {\n        val num = peekFirst()\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n        front = index(front + 1)\n        queSize--\n        return num\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun popLast(): Int {\n        val num = peekLast()\n        queSize--\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peekFirst(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043a\u043e\u043d\u0446\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peekLast(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n        val last = index(front + queSize - 1)\n        return nums[last]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\n    fun toArray(): IntArray {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[index(j)]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
array_deque.rb
=begin\nFile: array_deque.rb\nCreated Time: 2024-04-05\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\nclass ArrayDeque\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  attr_reader :size\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(capacity)\n    @nums = Array.new(capacity, 0)\n    @front = 0\n    @size = 0\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def capacity\n    @nums.length\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def is_empty?\n    size.zero?\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def push_first(num)\n    if size == capacity\n      puts '\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430'\n      return\n    end\n\n    # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u043b\u0435\u0432\u043e\n    # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e front \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0445\u0432\u043e\u0441\u0442\n    @front = index(@front - 1)\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    @nums[@front] = num\n    @size += 1\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def push_last(num)\n    if size == capacity\n      puts '\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430'\n      return\n    end\n\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    rear = index(@front + size)\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    @nums[rear] = num\n    @size += 1\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop_first\n    num = peek_first\n    # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0433\u043e\u043b\u043e\u0432\u044b \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434\n    @front = index(@front + 1)\n    @size -= 1\n    num\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop_last\n    num = peek_last\n    @size -= 1\n    num\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek_first\n    raise IndexError, '\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    @nums[@front]\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u0445\u0432\u043e\u0441\u0442\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek_last\n    raise IndexError, '\u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\n    last = index(@front + size - 1)\n    @nums[last]\n  end\n\n  # ## \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 ###\n  def to_array\n    # \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    res = []\n    for i in 0...size\n      res << @nums[index(@front + i)]\n    end\n    res\n  end\n\n  private\n\n  # ## \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0432 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0435 ###\n  def index(i)\n    # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0441\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    # \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e\n    # \u041a\u043e\u0433\u0434\u0430 i \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043e\u043d \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0435\u0446\n    (i + capacity) % capacity\n  end\nend\n
"},{"location":"chapter_stack_and_queue/deque/#533","title":"5.3.3 \u00a0 \u041f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438","text":"

\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u043e\u0447\u0435\u0442\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u0435 \u043b\u043e\u0433\u0438\u043a\u0443 \u0441\u0442\u0435\u043a\u0430 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u043d\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0440\u044b\u0442\u044c \u0432\u0441\u0435 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0431\u0435\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0441\u0432\u043e\u0431\u043e\u0434\u044b.

\u041c\u044b \u0437\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f \"undo\" \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u043c \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u0442\u0435\u043a\u0430: \u0441\u0438\u0441\u0442\u0435\u043c\u0430 push-\u0438\u0442 \u043a\u0430\u0436\u0434\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043a, \u0430 \u0437\u0430\u0442\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 pop \u0434\u043b\u044f \u043e\u0442\u043c\u0435\u043d\u044b. \u041e\u0434\u043d\u0430\u043a\u043e, \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432, \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0432\u0430\u044e\u0442 \u0447\u0438\u0441\u043b\u043e \u0448\u0430\u0433\u043e\u0432 \u043e\u0442\u043c\u0435\u043d\u044b (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0440\u0430\u0437\u0440\u0435\u0448\u0430\u044e\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \\(50\\) \u0448\u0430\u0433\u043e\u0432). \u041a\u043e\u0433\u0434\u0430 \u0434\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 \\(50\\), \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0435 \u043d\u0443\u0436\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441 \u0434\u043d\u0430 \u0441\u0442\u0435\u043a\u0430 (\u0442\u043e \u0435\u0441\u0442\u044c \u0441 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438). \u041d\u043e \u0441\u0442\u0435\u043a \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0443\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e, \u0438 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0435\u0433\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0437\u0430\u043c\u0435\u043d\u044f\u0442\u044c \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e. \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \"undo\" \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u0442\u0435\u043a\u043e\u0432\u043e\u043c\u0443 \u043f\u0440\u0430\u0432\u0438\u043b\u0443 LIFO, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u044b.

"},{"location":"chapter_stack_and_queue/queue/","title":"5.2 \u00a0 \u041e\u0447\u0435\u0440\u0435\u0434\u044c","text":"

\u041e\u0447\u0435\u0440\u0435\u0434\u044c (queue) - \u044d\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \"\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\". \u041a\u0430\u043a \u0432\u0438\u0434\u043d\u043e \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u044b\u0447\u043d\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f: \u043d\u043e\u0432\u044b\u0435 \u043b\u044e\u0434\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442\u0441\u044f \u043a \u0445\u0432\u043e\u0441\u0442\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0430 \u0441\u0442\u043e\u044f\u0449\u0438\u0435 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0443\u0445\u043e\u0434\u044f\u0442.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-4, \u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u0433\u043e\u043b\u043e\u0432\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\", \u0430 \u043a\u043e\u043d\u0435\u0446 - \"\u0445\u0432\u043e\u0441\u0442\u043e\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"; \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0445\u0432\u043e\u0441\u0442 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \"enqueue\", \u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b - \"dequeue\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-4 \u00a0 \u041f\u0440\u0430\u0432\u0438\u043b\u043e FIFO \u0434\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438

"},{"location":"chapter_stack_and_queue/queue/#521","title":"5.2.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e","text":"

\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 5-2. \u0421\u043b\u0435\u0434\u0443\u0435\u0442 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u043c\u043e\u0433\u0443\u0442 \u0440\u0430\u0437\u043b\u0438\u0447\u0430\u0442\u044c\u0441\u044f. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0442\u0435 \u0436\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0447\u0442\u043e \u0438 \u0434\u043b\u044f \u0441\u0442\u0435\u043a\u0430.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 5-2 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e

\u0418\u043c\u044f \u043c\u0435\u0442\u043e\u0434\u0430 \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c push() \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0445\u0432\u043e\u0441\u0442 \\(O(1)\\) pop() \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\) peek() \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \\(O(1)\\)

\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u0442\u043e\u0432\u044b\u0435 \u043a\u043b\u0430\u0441\u0441\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u044f\u0437\u044b\u043a\u0430\u043c\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby queue.py
from collections import deque\n\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n# \u0412 Python \u043e\u0431\u044b\u0447\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044e\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c deque \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n# \u0425\u043e\u0442\u044f queue.Queue() \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \"\u0447\u0438\u0441\u0442\u043e\u0439\" \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e, \u043e\u043d\u0430 \u043d\u0435 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0443\u0434\u043e\u0431\u043d\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0435 \u043d\u0435 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e\u0442\nque: deque[int] = deque()\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nque.append(1)\nque.append(3)\nque.append(2)\nque.append(5)\nque.append(4)\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\nfront: int = que[0]\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\npop: int = que.popleft()\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\nsize: int = len(que)\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nis_empty: bool = len(que) == 0\n
queue.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nqueue<int> queue;\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint front = queue.front();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nqueue.pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = queue.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty = queue.empty();\n
queue.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nQueue<Integer> queue = new LinkedList<>();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.offer(1);\nqueue.offer(3);\nqueue.offer(2);\nqueue.offer(5);\nqueue.offer(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peek = queue.peek();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop = queue.poll();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = queue.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nboolean isEmpty = queue.isEmpty();\n
queue.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nQueue<int> queue = new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.Enqueue(1);\nqueue.Enqueue(3);\nqueue.Enqueue(2);\nqueue.Enqueue(5);\nqueue.Enqueue(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peek = queue.Peek();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop = queue.Dequeue();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = queue.Count;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool isEmpty = queue.Count == 0;\n
queue_test.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Go \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442 \u0447\u0435\u0440\u0435\u0437 list\nqueue := list.New()\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.PushBack(1)\nqueue.PushBack(3)\nqueue.PushBack(2)\nqueue.PushBack(5)\nqueue.PushBack(4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\npeek := queue.Front()\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\npop := queue.Front()\nqueue.Remove(pop)\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nsize := queue.Len()\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nisEmpty := queue.Len() == 0\n
queue.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Swift \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nvar queue: [Int] = []\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.append(1)\nqueue.append(3)\nqueue.append(2)\nqueue.append(5)\nqueue.append(4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet peek = queue.first!\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0432 \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, removeFirst \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\nlet pool = queue.removeFirst()\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet size = queue.count\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nlet isEmpty = queue.isEmpty\n
queue.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 JavaScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nconst queue = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst peek = queue[0];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 shift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\nconst pop = queue.shift();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst size = queue.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nconst empty = queue.length === 0;\n
queue.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 TypeScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nconst queue: number[] = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.push(1);\nqueue.push(3);\nqueue.push(2);\nqueue.push(5);\nqueue.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst peek = queue[0];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 \u043e\u0441\u043d\u043e\u0432\u0435 \u043b\u0435\u0436\u0438\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 shift() \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\nconst pop = queue.shift();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nconst size = queue.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nconst empty = queue.length === 0;\n
queue.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Dart \u043a\u043b\u0430\u0441\u0441 Queue \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u0438 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nQueue<int> queue = Queue();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.add(1);\nqueue.add(3);\nqueue.add(2);\nqueue.add(5);\nqueue.add(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peek = queue.first;\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop = queue.removeFirst();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size = queue.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool isEmpty = queue.isEmpty;\n
queue.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n// \u0412 Rust \u0434\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nlet mut deque: VecDeque<u32> = VecDeque::new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\ndeque.push_back(1);\ndeque.push_back(3);\ndeque.push_back(2);\ndeque.push_back(5);\ndeque.push_back(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nif let Some(front) = deque.front() {\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nif let Some(pop) = deque.pop_front() {\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nlet size = deque.len();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nlet is_empty = deque.is_empty();\n
queue.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n
queue.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval queue = LinkedList<Int>()\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nqueue.offer(1)\nqueue.offer(3)\nqueue.offer(2)\nqueue.offer(5)\nqueue.offer(4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval peek = queue.peek()\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval pop = queue.poll()\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nval size = queue.size\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nval isEmpty = queue.isEmpty()\n
queue.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n# \u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0432 Ruby (Thread::Queue) \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 peek \u0438 traverse, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nqueue = []\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nqueue.push(1)\nqueue.push(3)\nqueue.push(2)\nqueue.push(5)\nqueue.push(4)\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\npeek = queue.first\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n# \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044d\u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432, \u043c\u0435\u0442\u043e\u0434 Array#shift \u0438\u043c\u0435\u0435\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c O(n)\npop = queue.shift\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\nsize = queue.length\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\nis_empty = queue.empty?\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20%23%20%D0%92%20Python%20%D0%B4%D0%B2%D1%83%D1%81%D1%82%D0%BE%D1%80%D0%BE%D0%BD%D0%BD%D1%8E%D1%8E%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%20deque%20%D0%BE%D0%B1%D1%8B%D1%87%D0%BD%D0%BE%20%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D1%83%D1%8E%D1%82%20%D0%BA%D0%B0%D0%BA%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20%23%20%D0%A5%D0%BE%D1%82%D1%8F%20queue.Queue%28%29%20%D1%8F%D0%B2%D0%BB%D1%8F%D0%B5%D1%82%D1%81%D1%8F%20%D0%BD%D0%B0%D1%81%D1%82%D0%BE%D1%8F%D1%89%D0%B8%D0%BC%20%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%BE%D0%BC%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%2C%20%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F%20%D0%B8%D0%BC%20%D0%BD%D0%B5%20%D1%81%D0%BB%D0%B8%D1%88%D0%BA%D0%BE%D0%BC%20%D1%83%D0%B4%D0%BE%D0%B1%D0%BD%D0%BE%0A%20%20%20%20que%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BC%D0%B5%D1%81%D1%82%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20que.append%281%29%0A%20%20%20%20que.append%283%29%0A%20%20%20%20que.append%282%29%0A%20%20%20%20que.append%285%29%0A%20%20%20%20que.append%284%29%0A%20%20%20%20print%28%22%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%20que%20%3D%22%2C%20que%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20front%20%3D%20que%5B0%5D%0A%20%20%20%20print%28%22%D0%AD%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D0%BD%D0%B0%D1%87%D0%B0%D0%BB%D0%B5%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20front%20%3D%22%2C%20front%29%0A%0A%20%20%20%20%23%20%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20pop%20%3D%20que.popleft%28%29%0A%20%20%20%20print%28%22%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%B8%D0%B7%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20pop%20%3D%22%2C%20pop%29%0A%20%20%20%20print%28%22que%20%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20%3D%22%2C%20que%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%BB%D0%B8%D0%BD%D1%83%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%0A%20%20%20%20size%20%3D%20len%28que%29%0A%20%20%20%20print%28%22%D0%94%D0%BB%D0%B8%D0%BD%D0%B0%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D0%B8%20size%20%3D%22%2C%20size%29%0A%0A%20%20%20%20%23%20%D0%9F%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%B8%D1%82%D1%8C%2C%20%D0%BF%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%0A%20%20%20%20is_empty%20%3D%20len%28que%29%20%3D%3D%200%0A%20%20%20%20print%28%22%D0%9F%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D0%BE%D1%87%D0%B5%D1%80%D0%B5%D0%B4%D1%8C%20%3D%22%2C%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_stack_and_queue/queue/#522","title":"5.2.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438","text":"

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u0430 \u0442\u0430\u043a\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043d\u0446\u0430 \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0438\u0445 \u0441 \u0434\u0440\u0443\u0433\u043e\u0433\u043e; \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u044d\u0442\u0438\u043c \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442.

"},{"location":"chapter_stack_and_queue/queue/#1","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-5, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \"\u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\" \u0438 \"\u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\" \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u043a\u0430\u043a \"\u0433\u043e\u043b\u043e\u0432\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\" \u0438 \"\u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\" \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0432\u0448\u0438\u0441\u044c, \u0447\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0443\u0437\u043b\u044b \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0445\u0432\u043e\u0441\u0442, \u0430 \u0443\u0434\u0430\u043b\u044f\u0442\u044c - \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0433\u043e\u043b\u043e\u0432\u044b.

LinkedListQueuepush()pop()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-5 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 enqueue \u0438 dequeue \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_queue.py
class LinkedListQueue:\n    \"\"\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._front: ListNode | None = None  # \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n        self._rear: ListNode | None = None  # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        node = ListNode(num)\n        # \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if self._front is None:\n            self._front = node\n            self._rear = node\n        # \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        else:\n            self._rear.next = node\n            self._rear = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        num = self.peek()\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        self._front = self._front.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        return self._front.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        queue = []\n        temp = self._front\n        while temp:\n            queue.append(temp.val)\n            temp = temp.next\n        return queue\n
linkedlist_queue.cpp
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n  private:\n    ListNode *front, *rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    int queSize;\n\n  public:\n    LinkedListQueue() {\n        front = nullptr;\n        rear = nullptr;\n        queSize = 0;\n    }\n\n    ~LinkedListQueue() {\n        // \u041e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0443\u0437\u043b\u044b \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        freeMemoryLinkedList(front);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    bool isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    void push(int num) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        ListNode *node = new ListNode(num);\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (front == nullptr) {\n            front = node;\n            rear = node;\n        }\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        else {\n            rear->next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int pop() {\n        int num = peek();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        ListNode *tmp = front;\n        front = front->next;\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        delete tmp;\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peek() {\n        if (size() == 0)\n            throw out_of_range(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        return front->val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Vector \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    vector<int> toVector() {\n        ListNode *node = front;\n        vector<int> res(size());\n        for (int i = 0; i < res.size(); i++) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
linkedlist_queue.java
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n    private ListNode front, rear; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public void push(int num) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        ListNode node = new ListNode(num);\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (front == null) {\n            front = node;\n            rear = node;\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        } else {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int pop() {\n        int num = peek();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        front = front.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return front.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public int[] toArray() {\n        ListNode node = front;\n        int[] res = new int[size()];\n        for (int i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_queue.cs
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n    ListNode? front, rear;  // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    int queSize = 0;\n\n    public LinkedListQueue() {\n        front = null;\n        rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public void Push(int num) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        ListNode node = new(num);\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (front == null) {\n            front = node;\n            rear = node;\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        } else if (rear != null) {\n            rear.next = node;\n            rear = node;\n        }\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Pop() {\n        int num = Peek();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        front = front?.next;\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return front!.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public int[] ToArray() {\n        if (front == null)\n            return [];\n\n        ListNode? node = front;\n        int[] res = new int[Size()];\n        for (int i = 0; i < res.Length; i++) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_queue.go
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype linkedListQueue struct {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 list \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    data *list.List\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc newLinkedListQueue() *linkedListQueue {\n    return &linkedListQueue{\n        data: list.New(),\n    }\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (s *linkedListQueue) push(value any) {\n    s.data.PushBack(value)\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListQueue) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListQueue) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Front()\n    return e.Value\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (s *linkedListQueue) size() int {\n    return s.data.Len()\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (s *linkedListQueue) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c List \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (s *linkedListQueue) toList() *list.List {\n    return s.data\n}\n
linkedlist_queue.swift
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n    private var front: ListNode? // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    private var rear: ListNode? // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    private var _size: Int\n\n    init() {\n        _size = 0\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func push(num: Int) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        let node = ListNode(x: num)\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if front == nil {\n            front = node\n            rear = node\n        }\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        else {\n            rear?.next = node\n            rear = node\n        }\n        _size += 1\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        front = front?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        return front!.val\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    func toArray() -> [Int] {\n        var node = front\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
linkedlist_queue.js
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n    #front; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b #front\n    #rear; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b #rear\n    #queSize = 0;\n\n    constructor() {\n        this.#front = null;\n        this.#rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    push(num) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        const node = new ListNode(num);\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (!this.#front) {\n            this.#front = node;\n            this.#rear = node;\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        } else {\n            this.#rear.next = node;\n            this.#rear = node;\n        }\n        this.#queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pop() {\n        const num = this.peek();\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.#front = this.#front.next;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peek() {\n        if (this.size === 0) throw new Error('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n        return this.#front.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    toArray() {\n        let node = this.#front;\n        const res = new Array(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_queue.ts
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n    private front: ListNode | null; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    private rear: ListNode | null; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private queSize: number = 0;\n\n    constructor() {\n        this.front = null;\n        this.rear = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    push(num: number): void {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        const node = new ListNode(num);\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (!this.front) {\n            this.front = node;\n            this.rear = node;\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        } else {\n            this.rear!.next = node;\n            this.rear = node;\n        }\n        this.queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pop(): number {\n        const num = this.peek();\n        if (!this.front) throw new Error('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        this.front = this.front.next;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peek(): number {\n        if (this.size === 0) throw new Error('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n        return this.front!.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    toArray(): number[] {\n        let node = this.front;\n        const res = new Array<number>(this.size);\n        for (let i = 0; i < res.length; i++) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
linkedlist_queue.dart
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue {\n  ListNode? _front; // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b _front\n  ListNode? _rear; // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b _rear\n  int _queSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  LinkedListQueue() {\n    _front = null;\n    _rear = null;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  void push(int _num) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c _num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    final node = ListNode(_num);\n    // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n    if (_front == null) {\n      _front = node;\n      _rear = node;\n    } else {\n      // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n      _rear!.next = node;\n      _rear = node;\n    }\n    _queSize++;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int pop() {\n    final int _num = peek();\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    _front = _front!.next;\n    _queSize--;\n    return _num;\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int peek() {\n    if (_queSize == 0) {\n      throw Exception('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n    }\n    return _front!.val;\n  }\n\n  /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n  List<int> toArray() {\n    ListNode? node = _front;\n    final List<int> queue = [];\n    while (node != null) {\n      queue.add(node.val);\n      node = node.next;\n    }\n    return queue;\n  }\n}\n
linkedlist_queue.rs
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[allow(dead_code)]\npub struct LinkedListQueue<T> {\n    front: Option<Rc<RefCell<ListNode<T>>>>, // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    rear: Option<Rc<RefCell<ListNode<T>>>>,  // \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    que_size: usize,                         // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n}\n\nimpl<T: Copy> LinkedListQueue<T> {\n    pub fn new() -> Self {\n        Self {\n            front: None,\n            rear: None,\n            que_size: 0,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn size(&self) -> usize {\n        return self.que_size;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    pub fn is_empty(&self) -> bool {\n        return self.que_size == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    pub fn push(&mut self, num: T) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        let new_rear = ListNode::new(num);\n        match self.rear.take() {\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n            Some(old_rear) => {\n                old_rear.borrow_mut().next = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n            None => {\n                self.front = Some(new_rear.clone());\n                self.rear = Some(new_rear);\n            }\n        }\n        self.que_size += 1;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn pop(&mut self) -> Option<T> {\n        self.front.take().map(|old_front| {\n            match old_front.borrow_mut().next.take() {\n                Some(new_front) => {\n                    self.front = Some(new_front);\n                }\n                None => {\n                    self.rear.take();\n                }\n            }\n            self.que_size -= 1;\n            old_front.borrow().val\n        })\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.front.as_ref()\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n        let mut res: Vec<T> = Vec::new();\n\n        fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {\n            if let Some(cur) = cur {\n                res.push(cur.borrow().val);\n                recur(cur.borrow().next.as_ref(), res);\n            }\n        }\n\n        recur(head, &mut res);\n\n        res\n    }\n}\n
linkedlist_queue.c
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct {\n    ListNode *front, *rear;\n    int queSize;\n} LinkedListQueue;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nLinkedListQueue *newLinkedListQueue() {\n    LinkedListQueue *queue = (LinkedListQueue *)malloc(sizeof(LinkedListQueue));\n    queue->front = NULL;\n    queue->rear = NULL;\n    queue->queSize = 0;\n    return queue;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delLinkedListQueue(LinkedListQueue *queue) {\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u0443\u0437\u043b\u044b\n    while (queue->front != NULL) {\n        ListNode *tmp = queue->front;\n        queue->front = queue->front->next;\n        free(tmp);\n    }\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 queue\n    free(queue);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size(LinkedListQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty(LinkedListQueue *queue) {\n    return (size(queue) == 0);\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid push(LinkedListQueue *queue, int num) {\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c node \u0432 \u0445\u0432\u043e\u0441\u0442\n    ListNode *node = newListNode(num);\n    // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n    if (queue->front == NULL) {\n        queue->front = node;\n        queue->rear = node;\n    }\n    // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    else {\n        queue->rear->next = node;\n        queue->rear = node;\n    }\n    queue->queSize++;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peek(LinkedListQueue *queue) {\n    assert(size(queue) && queue->front);\n    return queue->front->val;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop(LinkedListQueue *queue) {\n    int num = peek(queue);\n    ListNode *tmp = queue->front;\n    queue->front = queue->front->next;\n    free(tmp);\n    queue->queSize--;\n    return num;\n}\n\n/* \u0412\u044b\u0432\u0435\u0441\u0442\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid printLinkedListQueue(LinkedListQueue *queue) {\n    int *arr = malloc(sizeof(int) * queue->queSize);\n    // \u0421\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\n    int i;\n    ListNode *node;\n    for (i = 0, node = queue->front; i < queue->queSize; i++) {\n        arr[i] = node->val;\n        node = node->next;\n    }\n    printArray(arr, queue->queSize);\n    free(arr);\n}\n
linkedlist_queue.kt
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListQueue(\n    // \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front, \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    private var front: ListNode? = null,\n    private var rear: ListNode? = null,\n    private var queSize: Int = 0\n) {\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun push(num: Int) {\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        val node = ListNode(num)\n        // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n        if (front == null) {\n            front = node\n            rear = node\n            // \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        } else {\n            rear?.next = node\n            rear = node\n        }\n        queSize++\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pop(): Int {\n        val num = peek()\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n        front = front?.next\n        queSize--\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return front!!._val\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    fun toArray(): IntArray {\n        var node = front\n        val res = IntArray(size())\n        for (i in res.indices) {\n            res[i] = node!!._val\n            node = node.next\n        }\n        return res\n    }\n}\n
linkedlist_queue.rb
=begin\nFile: linkedlist_queue.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\n\n# ## \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 ###\nclass LinkedListQueue\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  attr_reader :size\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @front = nil  # \u0413\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b front\n    @rear = nil   # \u0425\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b rear\n    @size = 0\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def is_empty?\n    @front.nil?\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def push(num)\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    node = ListNode.new(num)\n\n    # \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430, \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438 head, \u0438 tail \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0438 \u043d\u0430 \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b\n    if @front.nil?\n      @front = node\n      @rear = node\n    # \u0415\u0441\u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    else\n      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop\n    num = peek\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek\n    raise IndexError, '\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    @front.val\n  end\n\n  # ## \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_stack_and_queue/queue/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u0435\u0440\u0432\u043e\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u043c\u0435\u0435\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(n)\\) , \u0438\u0437-\u0437\u0430 \u0447\u0435\u0433\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f dequeue \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0435\u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0439. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u043f\u0440\u0438\u0435\u043c\u0430.

\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e front , \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0443\u044e \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u0433\u043e\u043b\u043e\u0432\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438, \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e size , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u0440\u0430\u043d\u0438\u0442 \u0434\u043b\u0438\u043d\u0443 \u043e\u0447\u0435\u0440\u0435\u0434\u0438. \u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c rear = front + size ; \u044d\u0442\u0430 \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u0434\u0430\u0435\u0442 \u043f\u043e\u0437\u0438\u0446\u0438\u044e rear, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0443\u044e \u043d\u0430 \u044f\u0447\u0435\u0439\u043a\u0443 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.

\u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e, \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0440\u0430\u0432\u0435\u043d [front, rear - 1], \u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-6.

\u041c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0447\u0442\u043e \u0438 enqueue, \u0438 dequeue \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0432\u0441\u0435\u0433\u043e \u043e\u0434\u043d\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 \u043e\u0431\u0435 \u0438\u043c\u0435\u044e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\) .

ArrayQueuepush()pop()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-6 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 enqueue \u0438 dequeue \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435

\u0422\u044b \u043c\u043e\u0436\u0435\u0448\u044c \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u043d\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u043f\u0440\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f\u0445 enqueue \u0438 dequeue \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f front \u0438 rear \u043e\u0431\u0430 \u0434\u0432\u0438\u0436\u0443\u0442\u0441\u044f \u0432\u043f\u0440\u0430\u0432\u043e, \u0438 \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u0434\u043e\u0445\u043e\u0434\u044f\u0442 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0434\u0430\u043b\u044c\u0448\u0435 \u0441\u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0443\u0436\u0435 \u043d\u0435\u043b\u044c\u0437\u044f. \u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043a\u0430\u043a \"\u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\", \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u044b.

\u0414\u043b\u044f \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b front \u0438\u043b\u0438 rear, \u043f\u0435\u0440\u0435\u0448\u0430\u0433\u043d\u0443\u0432 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0441\u0440\u0430\u0437\u0443 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u043b\u0438\u0441\u044c \u043a \u0435\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0443 \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u043b\u0438 \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435. \u0422\u0430\u043a\u0443\u044e \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u043d\u043e\u0441\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043e\u0441\u0442\u0430\u0442\u043a\u0430, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u043a\u043e\u0434\u0435 \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_queue.py
class ArrayQueue:\n    \"\"\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n\n    def __init__(self, size: int):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._nums: list[int] = [0] * size  # \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self._front: int = 0  # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self._size: int = 0  # \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    def capacity(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return len(self._nums)\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        return self._size == 0\n\n    def push(self, num: int):\n        \"\"\"\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\"\"\"\n        if self._size == self.capacity():\n            raise IndexError(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n        # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        rear: int = (self._front + self._size) % self.capacity()\n        # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self._nums[rear] = num\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        num: int = self.peek()\n        # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        self._front = (self._front + 1) % self.capacity()\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        return self._nums[self._front]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        res = [0] * self.size()\n        j: int = self._front\n        for i in range(self.size()):\n            res[i] = self._nums[(j % self.capacity())]\n            j += 1\n        return res\n
array_queue.cpp
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n  private:\n    int *nums;       // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;       // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize;     // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queCapacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  public:\n    ArrayQueue(int capacity) {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        nums = new int[capacity];\n        queCapacity = capacity;\n        front = queSize = 0;\n    }\n\n    ~ArrayQueue() {\n        delete[] nums;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int capacity() {\n        return queCapacity;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    void push(int num) {\n        if (queSize == queCapacity) {\n            cout << \"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\" << endl;\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        int rear = (front + queSize) % queCapacity;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int pop() {\n        int num = peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        front = (front + 1) % queCapacity;\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int peek() {\n        if (isEmpty())\n            throw out_of_range(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n        return nums[front];\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0432 Vector \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    vector<int> toVector() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        vector<int> arr(queSize);\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            arr[i] = nums[j % queCapacity];\n        }\n        return arr;\n    }\n};\n
array_queue.java
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n    private int[] nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private int front; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private int queSize; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int capacity() {\n        return nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public boolean isEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public void push(int num) {\n        if (queSize == capacity()) {\n            System.out.println(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        int rear = (front + queSize) % capacity();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int pop() {\n        int num = peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        front = (front + 1) % capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return nums[front];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 */\n    public int[] toArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % capacity()];\n        }\n        return res;\n    }\n}\n
array_queue.cs
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n    int[] nums;  // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;   // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    public ArrayQueue(int capacity) {\n        nums = new int[capacity];\n        front = queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    int Capacity() {\n        return nums.Length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Size() {\n        return queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public bool IsEmpty() {\n        return queSize == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    public void Push(int num) {\n        if (queSize == Capacity()) {\n            Console.WriteLine(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        int rear = (front + queSize) % Capacity();\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num;\n        queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Pop() {\n        int num = Peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        front = (front + 1) % Capacity();\n        queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return nums[front];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 */\n    public int[] ToArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        int[] res = new int[queSize];\n        for (int i = 0, j = front; i < queSize; i++, j++) {\n            res[i] = nums[j % this.Capacity()];\n        }\n        return res;\n    }\n}\n
array_queue.go
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntype arrayQueue struct {\n    nums        []int // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    front       int   // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queSize     int   // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queCapacity int   // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 (\u0442\u043e \u0435\u0441\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432)\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc newArrayQueue(queCapacity int) *arrayQueue {\n    return &arrayQueue{\n        nums:        make([]int, queCapacity),\n        queCapacity: queCapacity,\n        front:       0,\n        queSize:     0,\n    }\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayQueue) size() int {\n    return q.queSize\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (q *arrayQueue) isEmpty() bool {\n    return q.queSize == 0\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nfunc (q *arrayQueue) push(num int) {\n    // \u041a\u043e\u0433\u0434\u0430 rear == queCapacity, \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\n    if q.queSize == q.queCapacity {\n        return\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    rear := (q.front + q.queSize) % q.queCapacity\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    q.nums[rear] = num\n    q.queSize++\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayQueue) pop() any {\n    num := q.peek()\n    if num == nil {\n        return nil\n    }\n\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    q.front = (q.front + 1) % q.queCapacity\n    q.queSize--\n    return num\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nfunc (q *arrayQueue) peek() any {\n    if q.isEmpty() {\n        return nil\n    }\n    return q.nums[q.front]\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c Slice \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (q *arrayQueue) toSlice() []int {\n    rear := (q.front + q.queSize)\n    if rear >= q.queCapacity {\n        rear %= q.queCapacity\n        return append(q.nums[q.front:], q.nums[:rear]...)\n    }\n    return q.nums[q.front:rear]\n}\n
array_queue.swift
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n    private var nums: [Int] // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var front: Int // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var _size: Int // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    init(capacity: Int) {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        nums = Array(repeating: 0, count: capacity)\n        front = 0\n        _size = 0\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func capacity() -> Int {\n        nums.count\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    func push(num: Int) {\n        if size() == capacity() {\n            print(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        let rear = (front + size()) % capacity()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num\n        _size += 1\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        front = (front + 1) % capacity()\n        _size -= 1\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\")\n        }\n        return nums[front]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 */\n    func toArray() -> [Int] {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        (front ..< front + size()).map { nums[$0 % capacity()] }\n    }\n}\n
array_queue.js
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n    #nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    #front = 0; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    #queSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    constructor(capacity) {\n        this.#nums = new Array(capacity);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get capacity() {\n        return this.#nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get size() {\n        return this.#queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty() {\n        return this.#queSize === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    push(num) {\n        if (this.size === this.capacity) {\n            console.log('\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        const rear = (this.#front + this.size) % this.capacity;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.#nums[rear] = num;\n        this.#queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pop() {\n        const num = this.peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        this.#front = (this.#front + 1) % this.capacity;\n        this.#queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peek() {\n        if (this.isEmpty()) throw new Error('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n        return this.#nums[this.#front];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Array */\n    toArray() {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.#front; i < this.size; i++, j++) {\n            arr[i] = this.#nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
array_queue.ts
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n    private nums: number[]; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private front: number; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private queSize: number; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    constructor(capacity: number) {\n        this.nums = new Array(capacity);\n        this.front = this.queSize = 0;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get capacity(): number {\n        return this.nums.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    get size(): number {\n        return this.queSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    isEmpty(): boolean {\n        return this.queSize === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    push(num: number): void {\n        if (this.size === this.capacity) {\n            console.log('\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430');\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        const rear = (this.front + this.queSize) % this.capacity;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        this.nums[rear] = num;\n        this.queSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    pop(): number {\n        const num = this.peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        this.front = (this.front + 1) % this.capacity;\n        this.queSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    peek(): number {\n        if (this.isEmpty()) throw new Error('\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430');\n        return this.nums[this.front];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Array */\n    toArray(): number[] {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        const arr = new Array(this.size);\n        for (let i = 0, j = this.front; i < this.size; i++, j++) {\n            arr[i] = this.nums[j % this.capacity];\n        }\n        return arr;\n    }\n}\n
array_queue.dart
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue {\n  late List<int> _nums; // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  late int _front; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  late int _queSize; // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n  ArrayQueue(int capacity) {\n    _nums = List.filled(capacity, 0);\n    _front = _queSize = 0;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int capaCity() {\n    return _nums.length;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int size() {\n    return _queSize;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  bool isEmpty() {\n    return _queSize == 0;\n  }\n\n  /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n  void push(int _num) {\n    if (_queSize == capaCity()) {\n      throw Exception(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int rear = (_front + _queSize) % capaCity();\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c _num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    _nums[rear] = _num;\n    _queSize++;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int pop() {\n    int _num = peek();\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    _front = (_front + 1) % capaCity();\n    _queSize--;\n    return _num;\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430\");\n    }\n    return _nums[_front];\n  }\n\n  /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Array */\n  List<int> toArray() {\n    // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n    final List<int> res = List.filled(_queSize, 0);\n    for (int i = 0, j = _front; i < _queSize; i++, j++) {\n      res[i] = _nums[j % capaCity()];\n    }\n    return res;\n  }\n}\n
array_queue.rs
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nstruct ArrayQueue<T> {\n    nums: Vec<T>,      // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    front: i32,        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    que_size: i32,     // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    que_capacity: i32, // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n}\n\nimpl<T: Copy + Default> ArrayQueue<T> {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new(capacity: i32) -> ArrayQueue<T> {\n        ArrayQueue {\n            nums: vec![T::default(); capacity as usize],\n            front: 0,\n            que_size: 0,\n            que_capacity: capacity,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn capacity(&self) -> i32 {\n        self.que_capacity\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn size(&self) -> i32 {\n        self.que_size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fn is_empty(&self) -> bool {\n        self.que_size == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fn push(&mut self, num: T) {\n        if self.que_size == self.capacity() {\n            println!(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\");\n            return;\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        let rear = (self.front + self.que_size) % self.que_capacity;\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        self.nums[rear as usize] = num;\n        self.que_size += 1;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn pop(&mut self) -> T {\n        let num = self.peek();\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        self.front = (self.front + 1) % self.que_capacity;\n        self.que_size -= 1;\n        num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fn peek(&self) -> T {\n        if self.is_empty() {\n            panic!(\"index out of bounds\");\n        }\n        self.nums[self.front as usize]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 */\n    fn to_vector(&self) -> Vec<T> {\n        let cap = self.que_capacity;\n        let mut j = self.front;\n        let mut arr = vec![T::default(); cap as usize];\n        for i in 0..self.que_size {\n            arr[i as usize] = self.nums[(j % cap) as usize];\n            j += 1;\n        }\n        arr\n    }\n}\n
array_queue.c
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntypedef struct {\n    int *nums;       // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int front;       // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    int queSize;     // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043f\u043e\u0441\u043b\u0435 \u0445\u0432\u043e\u0441\u0442\u0430\n    int queCapacity; // \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n} ArrayQueue;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nArrayQueue *newArrayQueue(int capacity) {\n    ArrayQueue *queue = (ArrayQueue *)malloc(sizeof(ArrayQueue));\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    queue->queCapacity = capacity;\n    queue->nums = (int *)malloc(sizeof(int) * queue->queCapacity);\n    queue->front = queue->queSize = 0;\n    return queue;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delArrayQueue(ArrayQueue *queue) {\n    free(queue->nums);\n    free(queue);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint capacity(ArrayQueue *queue) {\n    return queue->queCapacity;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint size(ArrayQueue *queue) {\n    return queue->queSize;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nbool empty(ArrayQueue *queue) {\n    return queue->queSize == 0;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint peek(ArrayQueue *queue) {\n    assert(size(queue) != 0);\n    return queue->nums[queue->front];\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\nvoid push(ArrayQueue *queue, int num) {\n    if (size(queue) == capacity(queue)) {\n        printf(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\\r\\n\");\n        return;\n    }\n    // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    int rear = (queue->front + queue->queSize) % queue->queCapacity;\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    queue->nums[rear] = num;\n    queue->queSize++;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\nint pop(ArrayQueue *queue) {\n    int num = peek(queue);\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    queue->front = (queue->front + 1) % queue->queCapacity;\n    queue->queSize--;\n    return num;\n}\n\n/* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nint *toArray(ArrayQueue *queue, int *queSize) {\n    *queSize = queue->queSize;\n    int *res = (int *)calloc(queue->queSize, sizeof(int));\n    int j = queue->front;\n    for (int i = 0; i < queue->queSize; i++) {\n        res[i] = queue->nums[j % queue->queCapacity];\n        j++;\n    }\n    return res;\n}\n
array_queue.kt
/* \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayQueue(capacity: Int) {\n    private val nums: IntArray = IntArray(capacity) // \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var front: Int = 0 // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    private var queSize: Int = 0 // \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun capacity(): Int {\n        return nums.size\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun size(): Int {\n        return queSize\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun isEmpty(): Boolean {\n        return queSize == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    fun push(num: Int) {\n        if (queSize == capacity()) {\n            println(\"\u041e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430\")\n            return\n        }\n        // \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n        // \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        val rear = (front + queSize) % capacity()\n        // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        nums[rear] = num\n        queSize++\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun pop(): Int {\n        val num = peek()\n        // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n        front = (front + 1) % capacity()\n        queSize--\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return nums[front]\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 */\n    fun toArray(): IntArray {\n        // \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0432 \u043f\u0440\u0435\u0434\u0435\u043b\u0430\u0445 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0434\u043b\u0438\u043d\u044b\n        val res = IntArray(queSize)\n        var i = 0\n        var j = front\n        while (i < queSize) {\n            res[i] = nums[j % capacity()]\n            i++\n            j++\n        }\n        return res\n    }\n}\n
array_queue.rb
=begin\nFile: array_queue.rb\nCreated Time: 2024-04-05\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043e\u043b\u044c\u0446\u0435\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\nclass ArrayQueue\n  # ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  attr_reader :size\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(size)\n    @nums = Array.new(size, 0) # \u041c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    @front = 0 # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u043f\u0435\u0440\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    @size = 0 # \u0414\u043b\u0438\u043d\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def capacity\n    @nums.length\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442\u0430 \u043b\u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def is_empty?\n    size.zero?\n  end\n\n  # ## \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c ###\n  def push(num)\n    raise IndexError, '\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0430' if size == capacity\n\n    # \u0412\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u0445\u0432\u043e\u0441\u0442\u0430, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0438\u0439 \u043d\u0430 \u0438\u043d\u0434\u0435\u043a\u0441 \u0445\u0432\u043e\u0441\u0442\u0430 + 1\n    # \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0437\u044f\u0442\u0438\u044f \u043f\u043e \u043c\u043e\u0434\u0443\u043b\u044e \u0432\u0435\u0440\u043d\u0443\u0442\u044c rear \u043a \u043d\u0430\u0447\u0430\u043b\u0443 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    rear = (@front + size) % capacity\n    # \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c num \u0432 \u0445\u0432\u043e\u0441\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    @nums[rear] = num\n    @size += 1\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def pop\n    num = peek\n    # \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c head \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u043d\u0430\u0437\u0430\u0434; \u0435\u0441\u043b\u0438 \u043e\u043d \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u043a\u043e\u043d\u0435\u0446, \u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0432 \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 ###\n  def peek\n    raise IndexError, '\u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0443\u0441\u0442\u0430' if is_empty?\n\n    @nums[@front]\n  end\n\n  # ## \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u0414\u0430\u0436\u0435 \u0442\u0430\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u043e\u0439: \u0435\u0435 \u0434\u043b\u0438\u043d\u0430 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u0430. \u041e\u0434\u043d\u0430\u043a\u043e \u044d\u0442\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u0432\u0432\u0435\u0434\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f. \u0417\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.

\u0412\u044b\u0432\u043e\u0434\u044b \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0432 \u0446\u0435\u043b\u043e\u043c \u0442\u0430\u043a\u0438\u0435 \u0436\u0435, \u043a\u0430\u043a \u0438 \u0434\u043b\u044f \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u0442\u044c\u0441\u044f.

"},{"location":"chapter_stack_and_queue/queue/#523","title":"5.2.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438","text":""},{"location":"chapter_stack_and_queue/stack/","title":"5.1 \u00a0 \u0421\u0442\u0435\u043a","text":"

\u0421\u0442\u0435\u043a (stack) - \u044d\u0442\u043e \u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u044e\u0449\u0430\u044f\u0441\u044f \u043b\u043e\u0433\u0438\u043a\u0435 \"\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\".

\u0421\u0442\u0435\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0441\u043e \u0441\u0442\u043e\u043f\u043a\u043e\u0439 \u0442\u0430\u0440\u0435\u043b\u043e\u043a \u043d\u0430 \u0441\u0442\u043e\u043b\u0435. \u0415\u0441\u043b\u0438 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0443 \u0442\u0430\u0440\u0435\u043b\u043a\u0443 \u0437\u0430 \u0440\u0430\u0437, \u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0442\u0430\u0440\u0435\u043b\u043a\u0443 \u0441\u043d\u0438\u0437\u0443, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u0443\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0442\u0430\u0440\u0435\u043b\u043a\u0438 \u0441\u0432\u0435\u0440\u0445\u0443. \u0415\u0441\u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0442\u0430\u0440\u0435\u043b\u043a\u0438 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0446\u0435\u043b\u044b\u043c\u0438 \u0447\u0438\u0441\u043b\u0430\u043c\u0438, \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c\u0438, \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438 \u0438 \u0442.\u0434.), \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \"\u0441\u0442\u0435\u043a\".

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-1, \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0447\u0430\u0441\u0442\u044c \u0441\u0442\u043e\u043f\u043a\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c \"\u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 \u0441\u0442\u0435\u043a\u0430\", \u0430 \u043d\u0438\u0436\u043d\u044e\u044e - \"\u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0441\u0442\u0435\u043a\u0430\". \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"push\", \u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0445\u043d\u0435\u0433\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 - \"pop\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-1 \u00a0 \u041f\u0440\u0430\u0432\u0438\u043b\u043e LIFO \u0434\u043b\u044f \u0441\u0442\u0435\u043a\u0430

"},{"location":"chapter_stack_and_queue/stack/#511","title":"5.1.1 \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e \u0441\u0442\u0435\u043a\u043e\u043c","text":"

\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u043e \u0441\u0442\u0435\u043a\u043e\u043c \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 5-1. \u041a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0435 \u0438\u043c\u0435\u043d\u0430 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0417\u0434\u0435\u0441\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f push() , pop() \u0438 peek() .

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 5-1 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043e \u0441\u0442\u0435\u043a\u043e\u043c

\u041c\u0435\u0442\u043e\u0434 \u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c push() \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0441\u0442\u0435\u043a (\u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443) \\(O(1)\\) pop() \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u0442\u0435\u043a\u0430 \\(O(1)\\) peek() \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \\(O(1)\\)

\u041e\u0431\u044b\u0447\u043d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u0441\u0442\u0435\u043a, \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0439 \u044f\u0437\u044b\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u041e\u0434\u043d\u0430\u043a\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u044f\u0437\u044b\u043a\u0430\u0445 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0441\u0442\u0435\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c. \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \"\u043c\u0430\u0441\u0441\u0438\u0432\" \u0438\u043b\u0438 \"\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a\" \u044d\u0442\u043e\u0433\u043e \u044f\u0437\u044b\u043a\u0430 \u043a\u0430\u043a \u0441\u0442\u0435\u043a \u0438 \u0432 \u043b\u043e\u0433\u0438\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043d\u0435 \u043e\u0442\u043d\u043e\u0441\u044f\u0449\u0438\u0435\u0441\u044f \u043a \u0441\u0442\u0435\u043a\u0443.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby stack.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430\n# \u0412 Python \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c list \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nstack: list[int] = []\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\npeek: int = stack[-1]\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\npop: int = stack.pop()\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\nsize: int = len(stack)\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a\nis_empty: bool = len(stack) == 0\n
stack.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nstack<int> stack;\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint top = stack.top();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nstack.pop(); // \u0411\u0435\u0437 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint size = stack.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nbool empty = stack.empty();\n
stack.java
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nStack<Integer> stack = new Stack<>();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint peek = stack.peek();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint pop = stack.pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint size = stack.size();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nboolean isEmpty = stack.isEmpty();\n
stack.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nStack<int> stack = new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.Push(1);\nstack.Push(3);\nstack.Push(2);\nstack.Push(5);\nstack.Push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint peek = stack.Peek();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint pop = stack.Pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint size = stack.Count;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nbool isEmpty = stack.Count == 0;\n
stack_test.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0412 Go \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Slice \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nvar stack []int\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack = append(stack, 1)\nstack = append(stack, 3)\nstack = append(stack, 2)\nstack = append(stack, 5)\nstack = append(stack, 4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\npeek := stack[len(stack)-1]\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\npop := stack[len(stack)-1]\nstack = stack[:len(stack)-1]\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nsize := len(stack)\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nisEmpty := len(stack) == 0\n
stack.swift
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0412 Swift \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nvar stack: [Int] = []\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.append(1)\nstack.append(3)\nstack.append(2)\nstack.append(5)\nstack.append(4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nlet peek = stack.last!\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nlet pop = stack.removeLast()\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nlet size = stack.count\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nlet isEmpty = stack.isEmpty\n
stack.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0412 JavaScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nconst stack = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nconst peek = stack[stack.length-1];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nconst pop = stack.pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nconst size = stack.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nconst is_empty = stack.length === 0;\n
stack.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0412 TypeScript \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nconst stack: number[] = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nconst peek = stack[stack.length - 1];\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nconst pop = stack.pop();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nconst size = stack.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nconst is_empty = stack.length === 0;\n
stack.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0412 Dart \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c List \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nList<int> stack = [];\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.add(1);\nstack.add(3);\nstack.add(2);\nstack.add(5);\nstack.add(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint peek = stack.last;\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nint pop = stack.removeLast();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint size = stack.length;\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nbool isEmpty = stack.isEmpty;\n
stack.rs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c Vec \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nlet mut stack: Vec<i32> = Vec::new();\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1);\nstack.push(3);\nstack.push(2);\nstack.push(5);\nstack.push(4);\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nlet top = stack.last().unwrap();\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nlet pop = stack.pop().unwrap();\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nlet size = stack.len();\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nlet is_empty = stack.is_empty();\n
stack.c
// \u0412 C \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0442\u0435\u043a\u0430\n
stack.kt
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nval stack = Stack<Int>()\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a */\nstack.push(1)\nstack.push(3)\nstack.push(2)\nstack.push(5)\nstack.push(4)\n\n/* \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nval peek = stack.peek()\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 */\nval pop = stack.pop()\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 */\nval size = stack.size\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nval isEmpty = stack.isEmpty()\n
stack.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430\n# \u0412 Ruby \u043d\u0435\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Array \u043a\u0430\u043a \u0441\u0442\u0435\u043a\nstack = []\n\n# \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u0441\u0442\u0435\u043a\nstack << 1\nstack << 3\nstack << 2\nstack << 5\nstack << 4\n\n# \u041f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\npeek = stack.last\n\n# \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\npop = stack.pop\n\n# \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\nsize = stack.length\n\n# \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a\nis_empty = stack.empty?\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%81%D1%82%D0%B5%D0%BA%0A%20%20%20%20%23%20%D0%92%20Python%20%D0%BD%D0%B5%D1%82%20%D0%B2%D1%81%D1%82%D1%80%D0%BE%D0%B5%D0%BD%D0%BD%D0%BE%D0%B3%D0%BE%20%D0%BA%D0%BB%D0%B0%D1%81%D1%81%D0%B0%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%2C%20%D0%BF%D0%BE%D1%8D%D1%82%D0%BE%D0%BC%D1%83%20list%20%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE%20%D0%B8%D1%81%D0%BF%D0%BE%D0%BB%D1%8C%D0%B7%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%BA%D0%B0%D0%BA%20%D1%81%D1%82%D0%B5%D0%BA%0A%20%20%20%20stack%20%3D%20%5B%5D%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BC%D0%B5%D1%81%D1%82%D0%B8%D1%82%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B2%20%D1%81%D1%82%D0%B5%D0%BA%0A%20%20%20%20stack.append%281%29%0A%20%20%20%20stack.append%283%29%0A%20%20%20%20stack.append%282%29%0A%20%20%20%20stack.append%285%29%0A%20%20%20%20stack.append%284%29%0A%20%20%20%20print%28%22%D1%81%D1%82%D0%B5%D0%BA%20stack%20%3D%22%2C%20stack%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B2%D0%B5%D1%80%D1%85%D0%BD%D0%B8%D0%B9%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%0A%20%20%20%20peek%20%3D%20stack%5B-1%5D%0A%20%20%20%20print%28%22%D0%92%D0%B5%D1%80%D1%85%D0%BD%D0%B8%D0%B9%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%20peek%20%3D%22%2C%20peek%29%0A%0A%20%20%20%20%23%20%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D1%8C%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20%D0%B8%D0%B7%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%0A%20%20%20%20pop%20%3D%20stack.pop%28%29%0A%20%20%20%20print%28%22%D0%98%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%BD%D1%8B%D0%B9%20%D0%B8%D0%B7%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%20%D1%8D%D0%BB%D0%B5%D0%BC%D0%B5%D0%BD%D1%82%20pop%20%3D%22%2C%20pop%29%0A%20%20%20%20print%28%22%D0%9F%D0%BE%D1%81%D0%BB%D0%B5%20%D0%B8%D0%B7%D0%B2%D0%BB%D0%B5%D1%87%D0%B5%D0%BD%D0%B8%D1%8F%20stack%20%3D%22%2C%20stack%29%0A%0A%20%20%20%20%23%20%D0%9F%D0%BE%D0%BB%D1%83%D1%87%D0%B8%D1%82%D1%8C%20%D0%B4%D0%BB%D0%B8%D0%BD%D1%83%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%0A%20%20%20%20size%20%3D%20len%28stack%29%0A%20%20%20%20print%28%22%D0%94%D0%BB%D0%B8%D0%BD%D0%B0%20%D1%81%D1%82%D0%B5%D0%BA%D0%B0%20size%20%3D%22%2C%20size%29%0A%0A%20%20%20%20%23%20%D0%9F%D1%80%D0%BE%D0%B2%D0%B5%D1%80%D0%B8%D1%82%D1%8C%2C%20%D0%BF%D1%83%D1%81%D1%82%D0%B0%20%D0%BB%D0%B8%20%D1%81%D1%82%D1%80%D1%83%D0%BA%D1%82%D1%83%D1%80%D0%B0%0A%20%20%20%20is_empty%20%3D%20len%28stack%29%20%3D%3D%200%0A%20%20%20%20print%28%22%D0%9F%D1%83%D1%81%D1%82%20%D0%BB%D0%B8%20%D1%81%D1%82%D0%B5%D0%BA%20%3D%22%2C%20is_empty%29&cumulative=false&curInstr=2&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_stack_and_queue/stack/#512","title":"5.1.2 \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430","text":"

\u0427\u0442\u043e\u0431\u044b \u0433\u043b\u0443\u0431\u0436\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u0442\u0435\u043a\u0430, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0441\u0442\u0435\u043a\u0430.

\u0421\u0442\u0435\u043a \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443 LIFO, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0438 \u043c\u0430\u0441\u0441\u0438\u0432, \u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0441\u0442\u0435\u043a \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043b\u0438 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0418\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \"\u0441\u043a\u0440\u044b\u0442\u044c\" \u0447\u0430\u0441\u0442\u044c \u043d\u0435\u0440\u0435\u043b\u0435\u0432\u0430\u043d\u0442\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438\u043b\u0438 \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u0430\u043a \u0447\u0442\u043e\u0431\u044b \u0432\u043d\u0435\u0448\u043d\u044f\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u043b\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u0441\u0442\u0435\u043a\u0430.

"},{"location":"chapter_stack_and_queue/stack/#1","title":"1. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430","text":"

\u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0442\u043e \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u0441\u043f\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430, \u0430 \u0445\u0432\u043e\u0441\u0442\u043e\u0432\u043e\u0439 - \u043a\u0430\u043a \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0435.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-2, \u0434\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430. \u0422\u0430\u043a\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \"\u0432\u0441\u0442\u0430\u0432\u043a\u043e\u0439 \u0432 \u0433\u043e\u043b\u043e\u0432\u0443\". \u0414\u043b\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 pop \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u0438\u0437 \u0441\u043f\u0438\u0441\u043a\u0430.

LinkedListStackpush()pop()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-2 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0438 pop \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u0430 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby linkedlist_stack.py
class LinkedListStack:\n    \"\"\"\u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._peek: ListNode | None = None\n        self._size: int = 0\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430\"\"\"\n        return self._size\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a\"\"\"\n        return self._size == 0\n\n    def push(self, val: int):\n        \"\"\"\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a\"\"\"\n        node = ListNode(val)\n        node.next = self._peek\n        self._peek = node\n        self._size += 1\n\n    def pop(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\"\"\"\n        num = self.peek()\n        self._peek = self._peek.next\n        self._size -= 1\n        return num\n\n    def peek(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        return self._peek.val\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        arr = []\n        node = self._peek\n        while node:\n            arr.append(node.val)\n            node = node.next\n        arr.reverse()\n        return arr\n
linkedlist_stack.cpp
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n  private:\n    ListNode *stackTop; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    int stkSize;        // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n  public:\n    LinkedListStack() {\n        stackTop = nullptr;\n        stkSize = 0;\n    }\n\n    ~LinkedListStack() {\n        // \u041e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0443\u0437\u043b\u044b \u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        freeMemoryLinkedList(stackTop);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    int size() {\n        return stkSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    bool isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    void push(int num) {\n        ListNode *node = new ListNode(num);\n        node->next = stackTop;\n        stackTop = node;\n        stkSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    int pop() {\n        int num = top();\n        ListNode *tmp = stackTop;\n        stackTop = stackTop->next;\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        delete tmp;\n        stkSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\");\n        return stackTop->val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    vector<int> toVector() {\n        ListNode *node = stackTop;\n        vector<int> res(size());\n        for (int i = res.size() - 1; i >= 0; i--) {\n            res[i] = node->val;\n            node = node->next;\n        }\n        return res;\n    }\n};\n
linkedlist_stack.java
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n    private ListNode stackPeek; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    private int stkSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    public int size() {\n        return stkSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    public void push(int num) {\n        ListNode node = new ListNode(num);\n        node.next = stackPeek;\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    public int pop() {\n        int num = peek();\n        stackPeek = stackPeek.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stackPeek.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public int[] toArray() {\n        ListNode node = stackPeek;\n        int[] res = new int[size()];\n        for (int i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_stack.cs
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n    ListNode? stackPeek;  // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    int stkSize = 0;   // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n    public LinkedListStack() {\n        stackPeek = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    public int Size() {\n        return stkSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    public void Push(int num) {\n        ListNode node = new(num) {\n            next = stackPeek\n        };\n        stackPeek = node;\n        stkSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    public int Pop() {\n        int num = Peek();\n        stackPeek = stackPeek!.next;\n        stkSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stackPeek!.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public int[] ToArray() {\n        if (stackPeek == null)\n            return [];\n\n        ListNode? node = stackPeek;\n        int[] res = new int[Size()];\n        for (int i = res.Length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_stack.go
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntype linkedListStack struct {\n    // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043f\u0430\u043a\u0435\u0442 list \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u0430\n    data *list.List\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nfunc newLinkedListStack() *linkedListStack {\n    return &linkedListStack{\n        data: list.New(),\n    }\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\nfunc (s *linkedListStack) push(value int) {\n    s.data.PushBack(value)\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *linkedListStack) pop() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    s.data.Remove(e)\n    return e.Value\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *linkedListStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    e := s.data.Back()\n    return e.Value\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *linkedListStack) size() int {\n    return s.data.Len()\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nfunc (s *linkedListStack) isEmpty() bool {\n    return s.data.Len() == 0\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c List \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (s *linkedListStack) toList() *list.List {\n    return s.data\n}\n
linkedlist_stack.swift
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n    private var _peek: ListNode? // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    private var _size: Int // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n    init() {\n        _size = 0\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    func size() -> Int {\n        _size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    func isEmpty() -> Bool {\n        size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    func push(num: Int) {\n        let node = ListNode(x: num)\n        node.next = _peek\n        _peek = node\n        _size += 1\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    @discardableResult\n    func pop() -> Int {\n        let num = peek()\n        _peek = _peek?.next\n        _size -= 1\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        }\n        return _peek!.val\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    func toArray() -> [Int] {\n        var node = _peek\n        var res = Array(repeating: 0, count: size())\n        for i in res.indices.reversed() {\n            res[i] = node!.val\n            node = node?.next\n        }\n        return res\n    }\n}\n
linkedlist_stack.js
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n    #stackPeek; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    #stkSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n    constructor() {\n        this.#stackPeek = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    get size() {\n        return this.#stkSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    isEmpty() {\n        return this.size === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    peek() {\n        if (!this.#stackPeek) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.#stackPeek.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n
linkedlist_stack.ts
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n    private stackPeek: ListNode | null; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    private stkSize: number = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n    constructor() {\n        this.stackPeek = null;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    get size(): number {\n        return this.stkSize;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    isEmpty(): boolean {\n        return this.size === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    push(num: number): void {\n        const node = new ListNode(num);\n        node.next = this.stackPeek;\n        this.stackPeek = node;\n        this.stkSize++;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    pop(): number {\n        const num = this.peek();\n        if (!this.stackPeek) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        this.stackPeek = this.stackPeek.next;\n        this.stkSize--;\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    peek(): number {\n        if (!this.stackPeek) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.stackPeek.val;\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    toArray(): number[] {\n        let node = this.stackPeek;\n        const res = new Array<number>(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node!.val;\n            node = node!.next;\n        }\n        return res;\n    }\n}\n
linkedlist_stack.dart
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack {\n  ListNode? _stackPeek; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n  int _stkSize = 0; // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n\n  LinkedListStack() {\n    _stackPeek = null;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n  int size() {\n    return _stkSize;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n  bool isEmpty() {\n    return _stkSize == 0;\n  }\n\n  /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n  void push(int _num) {\n    final ListNode node = ListNode(_num);\n    node.next = _stackPeek;\n    _stackPeek = node;\n    _stkSize++;\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n  int pop() {\n    final int _num = peek();\n    _stackPeek = _stackPeek!.next;\n    _stkSize--;\n    return _num;\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n  int peek() {\n    if (_stackPeek == null) {\n      throw Exception(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\");\n    }\n    return _stackPeek!.val;\n  }\n\n  /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 List \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n  List<int> toList() {\n    ListNode? node = _stackPeek;\n    List<int> list = [];\n    while (node != null) {\n      list.add(node.val);\n      node = node.next;\n    }\n    list = list.reversed.toList();\n    return list;\n  }\n}\n
linkedlist_stack.rs
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\n#[allow(dead_code)]\npub struct LinkedListStack<T> {\n    stack_peek: Option<Rc<RefCell<ListNode<T>>>>, // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    stk_size: usize,                              // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n}\n\nimpl<T: Copy> LinkedListStack<T> {\n    pub fn new() -> Self {\n        Self {\n            stack_peek: None,\n            stk_size: 0,\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    pub fn size(&self) -> usize {\n        return self.stk_size;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    pub fn is_empty(&self) -> bool {\n        return self.size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    pub fn push(&mut self, num: T) {\n        let node = ListNode::new(num);\n        node.borrow_mut().next = self.stack_peek.take();\n        self.stack_peek = Some(node);\n        self.stk_size += 1;\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    pub fn pop(&mut self) -> Option<T> {\n        self.stack_peek.take().map(|old_head| {\n            self.stack_peek = old_head.borrow_mut().next.take();\n            self.stk_size -= 1;\n\n            old_head.borrow().val\n        })\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    pub fn peek(&self) -> Option<&Rc<RefCell<ListNode<T>>>> {\n        self.stack_peek.as_ref()\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    pub fn to_array(&self) -> Vec<T> {\n        fn _to_array<T: Sized + Copy>(head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {\n            if let Some(node) = head {\n                let mut nums = _to_array(node.borrow().next.as_ref());\n                nums.push(node.borrow().val);\n                return nums;\n            }\n            return Vec::new();\n        }\n\n        _to_array(self.peek())\n    }\n}\n
linkedlist_stack.c
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\ntypedef struct {\n    ListNode *top; // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    int size;      // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n} LinkedListStack;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nLinkedListStack *newLinkedListStack() {\n    LinkedListStack *s = malloc(sizeof(LinkedListStack));\n    s->top = NULL;\n    s->size = 0;\n    return s;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delLinkedListStack(LinkedListStack *s) {\n    while (s->top) {\n        ListNode *n = s->top->next;\n        free(s->top);\n        s->top = n;\n    }\n    free(s);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\nint size(LinkedListStack *s) {\n    return s->size;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nbool isEmpty(LinkedListStack *s) {\n    return size(s) == 0;\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\nvoid push(LinkedListStack *s, int num) {\n    ListNode *node = (ListNode *)malloc(sizeof(ListNode));\n    node->next = s->top; // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    node->val = num;     // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    s->top = node;       // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    s->size++;           // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u0442\u0435\u043a\u0430\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint peek(LinkedListStack *s) {\n    if (s->size == 0) {\n        printf(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\\n\");\n        return INT_MAX;\n    }\n    return s->top->val;\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\nint pop(LinkedListStack *s) {\n    int val = peek(s);\n    ListNode *tmp = s->top;\n    s->top = s->top->next;\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n    free(tmp);\n    s->size--;\n    return val;\n}\n
linkedlist_stack.kt
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 */\nclass LinkedListStack(\n    private var stackPeek: ListNode? = null, // \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0433\u043e\u043b\u043e\u0432\u043d\u043e\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430\n    private var stkSize: Int = 0 // \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430\n) {\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    fun size(): Int {\n        return stkSize\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    fun push(num: Int) {\n        val node = ListNode(num)\n        node.next = stackPeek\n        stackPeek = node\n        stkSize++\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    fun pop(): Int? {\n        val num = peek()\n        stackPeek = stackPeek?.next\n        stkSize--\n        return num\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    fun peek(): Int? {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stackPeek?._val\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    fun toArray(): IntArray {\n        var node = stackPeek\n        val res = IntArray(size())\n        for (i in res.size - 1 downTo 0) {\n            res[i] = node?._val!!\n            node = node.next\n        }\n        return res\n    }\n}\n
linkedlist_stack.rb
=begin\nFile: linkedlist_stack.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/list_node'\n\n# ## \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 ###\nclass LinkedListStack\n  attr_reader :size\n\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @size = 0\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a ###\n  def is_empty?\n    @peek.nil?\n  end\n\n  # ## \u041f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043a ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 ###\n  def peek\n    raise IndexError, '\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442' if is_empty?\n\n    @peek.val\n  end\n\n  # ## \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_stack_and_queue/stack/#2","title":"2. \u00a0 \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430","text":"

\u0415\u0441\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0442\u043e \u0445\u0432\u043e\u0441\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 5-3, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0438 pop \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0432 \u043a\u043e\u043d\u0435\u0446 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u0438\u0437 \u043a\u043e\u043d\u0446\u0430, \u043e\u0431\u0435 \u0438\u043c\u0435\u044e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \\(O(1)\\) .

ArrayStackpush()pop()

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 5-3 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0438 pop \u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0442\u0435\u043a\u0430 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c\u044b\u0445 \u0432 \u0441\u0442\u0435\u043a, \u043c\u043e\u0436\u0435\u0442 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0440\u0430\u0441\u0442\u0438, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043d\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u0442\u044c\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432\u0440\u0443\u0447\u043d\u0443\u044e. \u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_stack.py
class ArrayStack:\n    \"\"\"\u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\"\"\"\n\n    def __init__(self):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._stack: list[int] = []\n\n    def size(self) -> int:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430\"\"\"\n        return len(self._stack)\n\n    def is_empty(self) -> bool:\n        \"\"\"\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a\"\"\"\n        return self.size() == 0\n\n    def push(self, item: int):\n        \"\"\"\u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a\"\"\"\n        self._stack.append(item)\n\n    def pop(self) -> int:\n        \"\"\"\u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        return self._stack.pop()\n\n    def peek(self) -> int:\n        \"\"\"\u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430\"\"\"\n        if self.is_empty():\n            raise IndexError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        return self._stack[-1]\n\n    def to_list(self) -> list[int]:\n        \"\"\"\u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430\"\"\"\n        return self._stack\n
array_stack.cpp
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n  private:\n    vector<int> stack;\n\n  public:\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    int size() {\n        return stack.size();\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    bool isEmpty() {\n        return stack.size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    void push(int num) {\n        stack.push_back(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    int pop() {\n        int num = top();\n        stack.pop_back();\n        return num;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    int top() {\n        if (isEmpty())\n            throw out_of_range(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\");\n        return stack.back();\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Vector */\n    vector<int> toVector() {\n        return stack;\n    }\n};\n
array_stack.java
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    private ArrayList<Integer> stack;\n\n    public ArrayStack() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 (\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430)\n        stack = new ArrayList<>();\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    public int size() {\n        return stack.size();\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    public boolean isEmpty() {\n        return size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    public void push(int num) {\n        stack.add(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    public int pop() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.remove(size() - 1);\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    public int peek() {\n        if (isEmpty())\n            throw new IndexOutOfBoundsException();\n        return stack.get(size() - 1);\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public Object[] toArray() {\n        return stack.toArray();\n    }\n}\n
array_stack.cs
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    List<int> stack;\n    public ArrayStack() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 (\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430)\n        stack = [];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    public int Size() {\n        return stack.Count;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    public bool IsEmpty() {\n        return Size() == 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    public void Push(int num) {\n        stack.Add(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    public int Pop() {\n        if (IsEmpty())\n            throw new Exception();\n        var val = Peek();\n        stack.RemoveAt(Size() - 1);\n        return val;\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    public int Peek() {\n        if (IsEmpty())\n            throw new Exception();\n        return stack[Size() - 1];\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    public int[] ToArray() {\n        return [.. stack];\n    }\n}\n
array_stack.go
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntype arrayStack struct {\n    data []int // \u0414\u0430\u043d\u043d\u044b\u0435\n}\n\n/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\nfunc newArrayStack() *arrayStack {\n    return &arrayStack{\n        // \u0423\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 \u0440\u0430\u0432\u043d\u043e\u0439 0, \u0430 \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0440\u0430\u0432\u043d\u043e\u0439 16\n        data: make([]int, 0, 16),\n    }\n}\n\n/* \u0414\u043b\u0438\u043d\u0430 \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *arrayStack) size() int {\n    return len(s.data)\n}\n\n/* \u041f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nfunc (s *arrayStack) isEmpty() bool {\n    return s.size() == 0\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\nfunc (s *arrayStack) push(v int) {\n    // \u0421\u0440\u0435\u0437 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0435\u0442\u0441\u044f\n    s.data = append(s.data, v)\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *arrayStack) pop() any {\n    val := s.peek()\n    s.data = s.data[:len(s.data)-1]\n    return val\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0441\u0442\u0435\u043a\u0430 */\nfunc (s *arrayStack) peek() any {\n    if s.isEmpty() {\n        return nil\n    }\n    val := s.data[len(s.data)-1]\n    return val\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c Slice \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 */\nfunc (s *arrayStack) toSlice() []int {\n    return s.data\n}\n
array_stack.swift
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    private var stack: [Int]\n\n    init() {\n        // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 (\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430)\n        stack = []\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    func size() -> Int {\n        stack.count\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    func isEmpty() -> Bool {\n        stack.isEmpty\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    func push(num: Int) {\n        stack.append(num)\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    @discardableResult\n    func pop() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        }\n        return stack.removeLast()\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    func peek() -> Int {\n        if isEmpty() {\n            fatalError(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        }\n        return stack.last!\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    func toArray() -> [Int] {\n        stack\n    }\n}\n
array_stack.js
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    #stack;\n    constructor() {\n        this.#stack = [];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    get size() {\n        return this.#stack.length;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    isEmpty() {\n        return this.#stack.length === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    push(num) {\n        this.#stack.push(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    pop() {\n        if (this.isEmpty()) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.#stack.pop();\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    top() {\n        if (this.isEmpty()) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.#stack[this.#stack.length - 1];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Array */\n    toArray() {\n        return this.#stack;\n    }\n}\n
array_stack.ts
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    private stack: number[];\n    constructor() {\n        this.stack = [];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    get size(): number {\n        return this.stack.length;\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    isEmpty(): boolean {\n        return this.stack.length === 0;\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    push(num: number): void {\n        this.stack.push(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    pop(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.stack.pop();\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    top(): number | undefined {\n        if (this.isEmpty()) throw new Error('\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442');\n        return this.stack[this.stack.length - 1];\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c Array */\n    toArray() {\n        return this.stack;\n    }\n}\n
array_stack.dart
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n  late List<int> _stack;\n  ArrayStack() {\n    _stack = [];\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n  int size() {\n    return _stack.length;\n  }\n\n  /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n  bool isEmpty() {\n    return _stack.isEmpty;\n  }\n\n  /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n  void push(int _num) {\n    _stack.add(_num);\n  }\n\n  /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n  int pop() {\n    if (isEmpty()) {\n      throw Exception(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\");\n    }\n    return _stack.removeLast();\n  }\n\n  /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n  int peek() {\n    if (isEmpty()) {\n      throw Exception(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\");\n    }\n    return _stack.last;\n  }\n\n  /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0435\u043a \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n  List<int> toArray() => _stack;\n}\n
array_stack.rs
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nstruct ArrayStack<T> {\n    stack: Vec<T>,\n}\n\nimpl<T> ArrayStack<T> {\n    /* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 */\n    fn new() -> ArrayStack<T> {\n        ArrayStack::<T> {\n            stack: Vec::<T>::new(),\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    fn size(&self) -> usize {\n        self.stack.len()\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    fn is_empty(&self) -> bool {\n        self.size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    fn push(&mut self, num: T) {\n        self.stack.push(num);\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    fn pop(&mut self) -> Option<T> {\n        self.stack.pop()\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    fn peek(&self) -> Option<&T> {\n        if self.is_empty() {\n            panic!(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\")\n        };\n        self.stack.last()\n    }\n\n    /* \u0412\u0435\u0440\u043d\u0443\u0442\u044c &Vec */\n    fn to_array(&self) -> &Vec<T> {\n        &self.stack\n    }\n}\n
array_stack.c
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\ntypedef struct {\n    int *data;\n    int size;\n} ArrayStack;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nArrayStack *newArrayStack() {\n    ArrayStack *stack = malloc(sizeof(ArrayStack));\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0432\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f\n    stack->data = malloc(sizeof(int) * MAX_SIZE);\n    stack->size = 0;\n    return stack;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delArrayStack(ArrayStack *stack) {\n    free(stack->data);\n    free(stack);\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\nint size(ArrayStack *stack) {\n    return stack->size;\n}\n\n/* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\nbool isEmpty(ArrayStack *stack) {\n    return stack->size == 0;\n}\n\n/* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\nvoid push(ArrayStack *stack, int num) {\n    if (stack->size == MAX_SIZE) {\n        printf(\"\u0421\u0442\u0435\u043a \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\\n\");\n        return;\n    }\n    stack->data[stack->size] = num;\n    stack->size++;\n}\n\n/* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\nint peek(ArrayStack *stack) {\n    if (stack->size == 0) {\n        printf(\"\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442\\n\");\n        return INT_MAX;\n    }\n    return stack->data[stack->size - 1];\n}\n\n/* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\nint pop(ArrayStack *stack) {\n    int val = peek(stack);\n    stack->size--;\n    return val;\n}\n
array_stack.kt
/* \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 */\nclass ArrayStack {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043f\u0438\u0441\u043a\u0430 (\u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430)\n    private val stack = mutableListOf<Int>()\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430 */\n    fun size(): Int {\n        return stack.size\n    }\n\n    /* \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a */\n    fun isEmpty(): Boolean {\n        return size() == 0\n    }\n\n    /* \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u0441\u0442\u0435\u043a */\n    fun push(num: Int) {\n        stack.add(num)\n    }\n\n    /* \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 */\n    fun pop(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack.removeAt(size() - 1)\n    }\n\n    /* \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 */\n    fun peek(): Int {\n        if (isEmpty()) throw IndexOutOfBoundsException()\n        return stack[size() - 1]\n    }\n\n    /* \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c List \u0432 Array \u0438 \u0432\u0435\u0440\u043d\u0443\u0442\u044c */\n    fun toArray(): Array<Any> {\n        return stack.toTypedArray()\n    }\n}\n
array_stack.rb
=begin\nFile: array_stack.rb\nCreated Time: 2024-04-06\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\n# ## \u0421\u0442\u0435\u043a \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 ###\nclass ArrayStack\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize\n    @stack = []\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430 ###\n  def size\n    @stack.length\n  end\n\n  # ## \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430, \u043f\u0443\u0441\u0442 \u043b\u0438 \u0441\u0442\u0435\u043a ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  # ## \u041f\u043e\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0432 \u0441\u0442\u0435\u043a ###\n  def push(item)\n    @stack << item\n  end\n\n  # ## \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 ###\n  def pop\n    raise IndexError, '\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442' if is_empty?\n\n    @stack.pop\n  end\n\n  # ## \u0414\u043e\u0441\u0442\u0443\u043f \u043a \u0432\u0435\u0440\u0445\u043d\u0435\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0443 \u0441\u0442\u0435\u043a\u0430 ###\n  def peek\n    raise IndexError, '\u0441\u0442\u0435\u043a \u043f\u0443\u0441\u0442' if is_empty?\n\n    @stack.last\n  end\n\n  # ## \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 ###\n  def to_array\n    @stack\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_stack_and_queue/stack/#513","title":"5.1.3 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0434\u0432\u0443\u0445 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439","text":"

\u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u043c\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438

\u041e\u0431\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442 \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0441\u0442\u0435\u043a\u0430. \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0440\u0430\u043c\u043a\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f.

\u0412\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c

\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0438 push, \u0438 pop \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0439 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0445\u043e\u0440\u043e\u0448\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u044d\u0448\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0442\u0430\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043e\u0431\u044b\u0447\u043d\u043e \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435. \u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u043f\u0440\u0438 push \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0432\u044b\u0448\u0435\u043d\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f, \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u044d\u0442\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 push \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \\(O(n)\\) .

\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0430 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0433\u0438\u0431\u043a\u043e, \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0430\u0434\u0435\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0438\u0437-\u0437\u0430 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u0442. \u041d\u043e \u0441\u0430\u043c\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f push \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430-\u0443\u0437\u043b\u0430 \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0441\u0440\u0435\u0434\u043d\u0435\u043c \u043e\u043d\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043c\u0435\u043d\u0435\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0430. \u0412\u043f\u0440\u043e\u0447\u0435\u043c, \u0435\u0441\u043b\u0438 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c\u044b\u0435 \u0432 \u0441\u0442\u0435\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u0443\u0436\u0435 \u0441\u0430\u043c\u0438 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c\u0438-\u0443\u0437\u043b\u0430\u043c\u0438, \u0448\u0430\u0433 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0438 \u0442\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043f\u043e\u0432\u044b\u0441\u0438\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c.

\u0418\u0442\u0430\u043a, \u043a\u043e\u0433\u0434\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u043c\u044b\u043c\u0438 \u0438 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c\u044b\u043c\u0438 \u0438\u0437 \u0441\u0442\u0435\u043a\u0430, \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 int \u0438\u043b\u0438 double , \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0432\u043e\u0434\u044b.

\u041f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c

\u041f\u0440\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442 \"\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u0435\u043c\u043a\u043e\u0441\u0442\u044c\", \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0442\u044c \u0440\u0435\u0430\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442 \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u043f\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442\u0443 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432 2 \u0440\u0430\u0437\u0430), \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u0430\u044f \u0435\u043c\u043a\u043e\u0441\u0442\u044c \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0439. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u044c \u043a \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043f\u043e\u0442\u0435\u0440\u044f\u043c \u043f\u0430\u043c\u044f\u0442\u0438.

\u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443\u0437\u043b\u044b \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0434\u043e\u043b\u0436\u043d\u044b \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438, \u0443\u0437\u043b\u044b \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0430\u043c\u0438 \u043f\u043e \u0441\u0435\u0431\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0441\u0442\u0432\u0430.

\u0412 \u0438\u0442\u043e\u0433\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043a\u0430\u043a\u0430\u044f \u0438\u0437 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 \u0431\u043e\u043b\u0435\u0435 \u044d\u043a\u043e\u043d\u043e\u043c\u043d\u0430 \u043f\u043e \u043f\u0430\u043c\u044f\u0442\u0438; \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438.

"},{"location":"chapter_stack_and_queue/stack/#514","title":"5.1.4 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430","text":""},{"location":"chapter_stack_and_queue/summary/","title":"5.4 \u00a0 \u041a\u0440\u0430\u0442\u043a\u0438\u0435 \u0438\u0442\u043e\u0433\u0438","text":""},{"location":"chapter_stack_and_queue/summary/#1","title":"1. \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_stack_and_queue/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043b\u0438 \u043a\u043d\u043e\u043f\u043a\u0438 \"\u0432\u043f\u0435\u0440\u0435\u0434\" \u0438 \"\u043d\u0430\u0437\u0430\u0434\" \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0434\u0432\u0443\u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430?

\u041f\u043e \u0441\u0443\u0442\u0438, \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432 \"\u0432\u043f\u0435\u0440\u0435\u0434/\u043d\u0430\u0437\u0430\u0434\" \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \"\u0441\u0442\u0435\u043a\u0430\". \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043d\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443, \u043e\u043d\u0430 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0442\u0435\u043a\u0430; \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043d\u0430\u0436\u0438\u043c\u0430\u0435\u0442 \u043a\u043d\u043e\u043f\u043a\u0443 \"\u043d\u0430\u0437\u0430\u0434\", \u044d\u0442\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0441\u043d\u0438\u043c\u0430\u0435\u0442\u0441\u044f \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430. \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043e\u0431 \u044d\u0442\u043e\u043c \u0443\u0436\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0441\u044c \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \"\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c\".

Q: \u041d\u0443\u0436\u043d\u043e \u043b\u0438 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0443\u0437\u043b\u0430 \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0438\u0437 \u0441\u0442\u0435\u043a\u0430?

\u0415\u0441\u043b\u0438 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u0435\u0449\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f, \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043d\u0435 \u043d\u0443\u0436\u043d\u043e. \u0415\u0441\u043b\u0438 \u043e\u043d \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u0435\u043d, \u0442\u043e \u0432 \u044f\u0437\u044b\u043a\u0430\u0445 Java \u0438 Python \u0435\u0441\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0441\u0431\u043e\u0440\u0449\u0438\u043a \u043c\u0443\u0441\u043e\u0440\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0440\u0443\u0447\u043d\u043e\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f; \u0432 C \u0438 C++ \u043f\u0430\u043c\u044f\u0442\u044c \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e.

Q: \u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043a\u0430\u043a \u0434\u0432\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0435\u043a\u0430. \u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043e\u043d\u0430 \u043d\u0443\u0436\u043d\u0430?

\u0414\u0432\u0443\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u043a\u043e\u043c\u0431\u0438\u043d\u0430\u0446\u0438\u044e \u0441\u0442\u0435\u043a\u0430 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438\u043b\u0438 \u043d\u0430 \u0434\u0432\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u0442\u0435\u043a\u0430. \u041e\u043d\u0430 \u0432\u044b\u0440\u0430\u0436\u0430\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \"\u0441\u0442\u0435\u043a + \u043e\u0447\u0435\u0440\u0435\u0434\u044c\", \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0440\u044b\u0442\u044c \u0432\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0438 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u0433\u0438\u0431\u043a\u043e\u0439.

Q: \u041a\u0430\u043a \u0438\u043c\u0435\u043d\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u044e\u0442\u0441\u044f \u043e\u0442\u043c\u0435\u043d\u0430 (undo) \u0438 \u043f\u043e\u0432\u0442\u043e\u0440 (redo)?

\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0441\u0442\u0435\u043a\u0430: \u0441\u0442\u0435\u043a A \u0434\u043b\u044f \u043e\u0442\u043c\u0435\u043d\u044b \u0438 \u0441\u0442\u0435\u043a B \u0434\u043b\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u0430.

  1. \u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435, \u044d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0442\u0435\u043a A , \u0430 \u0441\u0442\u0435\u043a B \u043e\u0447\u0438\u0449\u0430\u0435\u0442\u0441\u044f.
  2. \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \"undo\", \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 A \u0438 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0442\u0435\u043a B .
  3. \u041a\u043e\u0433\u0434\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \"redo\", \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0438\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0441\u0442\u0435\u043a\u0430 B \u0438 \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0432 \u0441\u0442\u0435\u043a A .
"},{"location":"chapter_tree/","title":"\u0413\u043b\u0430\u0432\u0430 7. \u00a0 \u0414\u0435\u0440\u0435\u0432\u044c\u044f","text":"

Abstract

\u0412\u044b\u0441\u043e\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u043b\u043d\u043e \u0436\u0438\u0437\u043d\u0438: \u043c\u043e\u0449\u043d\u044b\u0435 \u043a\u043e\u0440\u043d\u0438, \u0433\u0443\u0441\u0442\u0430\u044f \u043a\u0440\u043e\u043d\u0430 \u0438 \u0440\u0430\u0441\u043a\u0438\u0434\u0438\u0441\u0442\u044b\u0435 \u0432\u0435\u0442\u0432\u0438.

\u041e\u043d\u043e \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u043e \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430\u043c \u0444\u043e\u0440\u043c\u0443 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0443\u044e \u043d\u0430 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435 \"\u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0439 \u0438 \u0432\u043b\u0430\u0441\u0442\u0432\u0443\u0439\".

"},{"location":"chapter_tree/#_1","title":"\u0421\u043e\u0434\u0435\u0440\u0436\u0430\u043d\u0438\u0435 \u0433\u043b\u0430\u0432\u044b","text":""},{"location":"chapter_tree/array_representation_of_tree/","title":"7.3 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c","text":"

\u0412 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0447\u0435\u0440\u0435\u0437 \u0441\u0432\u044f\u0437\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0435\u0434\u0438\u043d\u0438\u0446\u0435\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u0437\u0435\u043b TreeNode , \u0430 \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0441\u0432\u044f\u0437\u0438 \u0447\u0435\u0440\u0435\u0437 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438. \u0412 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u0431\u044b\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u0442\u0430\u043a\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438.

\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0432\u043e\u043f\u0440\u043e\u0441: \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u0430\u0441\u0441\u0438\u0432\u0430? \u041e\u0442\u0432\u0435\u0442: \u0434\u0430.

"},{"location":"chapter_tree/array_representation_of_tree/#731","title":"7.3.1 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043b\u0443\u0447\u0430\u0439. \u0415\u0441\u043b\u0438 \u0434\u0430\u043d\u0430 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0438 \u0432\u0441\u0435 \u0435\u0435 \u0443\u0437\u043b\u044b \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c, \u0442\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0443\u0437\u043b\u0443 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u0430.

\u0418\u0437 \u0441\u0432\u043e\u0439\u0441\u0442\u0432 \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438 \"\u0444\u043e\u0440\u043c\u0443\u043b\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f\" \u043c\u0435\u0436\u0434\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432: \u0435\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d \\(i\\) , \u0442\u043e \u0438\u043d\u0434\u0435\u043a\u0441 \u0435\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d \\(2i + 1\\) , \u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e - \\(2i + 2\\) . \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-12 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u043c\u0438 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-12 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c

\u042d\u0442\u0430 \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u044f \u0438\u0433\u0440\u0430\u0435\u0442 \u0442\u0443 \u0436\u0435 \u0440\u043e\u043b\u044c, \u0447\u0442\u043e \u0438 \u0441\u0441\u044b\u043b\u043a\u0438 \u043d\u0430 \u0443\u0437\u043b\u044b \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 . \u0418\u043c\u0435\u044f \u043b\u044e\u0431\u043e\u0439 \u0443\u0437\u0435\u043b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e \u0444\u043e\u0440\u043c\u0443\u043b\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0435\u0433\u043e \u043b\u0435\u0432\u043e\u043c\u0443 \u0438 \u043f\u0440\u0430\u0432\u043e\u043c\u0443 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u0443\u0437\u043b\u0430\u043c.

"},{"location":"chapter_tree/array_representation_of_tree/#732","title":"7.3.2 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e - \u043b\u0438\u0448\u044c \u0447\u0430\u0441\u0442\u043d\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439; \u0432 \u043e\u0431\u044b\u0447\u043d\u043e\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u043d\u0430 \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0445 \u0443\u0440\u043e\u0432\u043d\u044f\u0445 \u0447\u0430\u0441\u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e None . \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u044d\u0442\u0438\u0445 None , \u043c\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u043f\u043e \u043e\u0434\u043d\u043e\u0439 \u043b\u0438\u0448\u044c \u044d\u0442\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0438 \u0442\u043e\u043c\u0443 \u0436\u0435 \u043e\u0431\u0445\u043e\u0434\u0443 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-13, \u0434\u043b\u044f \u043d\u0435\u043f\u043e\u043b\u043d\u043e\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u0439 \u0432\u044b\u0448\u0435 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0443\u0436\u0435 \u043f\u0435\u0440\u0435\u0441\u0442\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-13 \u00a0 \u041e\u0434\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b

\u0427\u0442\u043e\u0431\u044b \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u044f\u0432\u043d\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 None \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c . \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-14, \u043f\u043e\u0441\u043b\u0435 \u0442\u0430\u043a\u043e\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u0443\u0436\u0435 \u0441\u043c\u043e\u0436\u0435\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0437\u0430\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
# \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c\n# \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c None \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\ntree = [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 int, INT_MAX, \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nvector<int> tree = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043e\u0431\u0435\u0440\u0442\u043a\u0443 Integer \u0434\u043b\u044f int, \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nInteger[] tree = { 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 };\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f nullable-\u0442\u0438\u043f int? , \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nint?[] tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0441\u0440\u0435\u0437 \u0442\u0438\u043f\u0430 any, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c nil \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\ntree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f nullable-\u0442\u0438\u043f Int? , \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c nil \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nlet tree: [Int?] = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nlet tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nlet tree: (number | null)[] = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f nullable-\u0442\u0438\u043f int? , \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nList<int?> tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c None \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nlet tree = [Some(1), Some(2), Some(3), Some(4), None, Some(6), Some(7), Some(8), Some(9), None, None, Some(12), None, None, Some(15)];\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 int \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0443\u0437\u043b\u044b \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 INT_MAX\nint tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};\n
/* \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\n// \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c null \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\nval tree = arrayOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )\n
### \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c ###\n# \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c nil \u0434\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0441\u0442\u044b\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0439\ntree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]\n

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-14 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c

\u0421\u0442\u043e\u0438\u0442 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c . \u0415\u0441\u043b\u0438 \u0432\u0441\u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e None \u043f\u043e\u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u043d\u0438\u0436\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0438 \u0441\u043f\u0440\u0430\u0432\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0432\u0441\u0435 None \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043a\u043e\u043d\u0446\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c.

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u043d\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432\u0441\u0435 None , \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u043d\u043e. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-15 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-15 \u00a0 \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c

\u041d\u0438\u0436\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c. \u041e\u043d \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby array_binary_tree.py
class ArrayBinaryTree:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438\"\"\"\n\n    def __init__(self, arr: list[int | None]):\n        \"\"\"\u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\"\"\"\n        self._tree = list(arr)\n\n    def size(self):\n        \"\"\"\u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430\"\"\"\n        return len(self._tree)\n\n    def val(self, i: int) -> int | None:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i\"\"\"\n        # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if i < 0 or i >= self.size():\n            return None\n        return self._tree[i]\n\n    def left(self, i: int) -> int | None:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i\"\"\"\n        return 2 * i + 1\n\n    def right(self, i: int) -> int | None:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i\"\"\"\n        return 2 * i + 2\n\n    def parent(self, i: int) -> int | None:\n        \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i\"\"\"\n        return (i - 1) // 2\n\n    def level_order(self) -> list[int]:\n        \"\"\"\u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443\"\"\"\n        self.res = []\n        # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for i in range(self.size()):\n            if self.val(i) is not None:\n                self.res.append(self.val(i))\n        return self.res\n\n    def dfs(self, i: int, order: str):\n        \"\"\"\u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443\"\"\"\n        if self.val(i) is None:\n            return\n        # \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"pre\":\n            self.res.append(self.val(i))\n        self.dfs(self.left(i), order)\n        # \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"in\":\n            self.res.append(self.val(i))\n        self.dfs(self.right(i), order)\n        # \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"post\":\n            self.res.append(self.val(i))\n\n    def pre_order(self) -> list[int]:\n        \"\"\"\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n        self.res = []\n        self.dfs(0, order=\"pre\")\n        return self.res\n\n    def in_order(self) -> list[int]:\n        \"\"\"\u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n        self.res = []\n        self.dfs(0, order=\"in\")\n        return self.res\n\n    def post_order(self) -> list[int]:\n        \"\"\"\u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n        self.res = []\n        self.dfs(0, order=\"post\")\n        return self.res\n
array_binary_tree.cpp
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n  public:\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    ArrayBinaryTree(vector<int> arr) {\n        tree = arr;\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    int size() {\n        return tree.size();\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    int val(int i) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c INT_MAX, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= size())\n            return INT_MAX;\n        return tree[i];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    int left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    int right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    int parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    vector<int> levelOrder() {\n        vector<int> res;\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != INT_MAX)\n                res.push_back(val(i));\n        }\n        return res;\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    vector<int> preOrder() {\n        vector<int> res;\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    vector<int> inOrder() {\n        vector<int> res;\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    vector<int> postOrder() {\n        vector<int> res;\n        dfs(0, \"post\", res);\n        return res;\n    }\n\n  private:\n    vector<int> tree;\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    void dfs(int i, string order, vector<int> &res) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (val(i) == INT_MAX)\n            return;\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"pre\")\n            res.push_back(val(i));\n        dfs(left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"in\")\n            res.push_back(val(i));\n        dfs(right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"post\")\n            res.push_back(val(i));\n    }\n};\n
array_binary_tree.java
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n    private List<Integer> tree;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    public ArrayBinaryTree(List<Integer> arr) {\n        tree = new ArrayList<>(arr);\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public int size() {\n        return tree.size();\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public Integer val(int i) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= size())\n            return null;\n        return tree.get(i);\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public Integer left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public Integer right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public Integer parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    public List<Integer> levelOrder() {\n        List<Integer> res = new ArrayList<>();\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (int i = 0; i < size(); i++) {\n            if (val(i) != null)\n                res.add(val(i));\n        }\n        return res;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    private void dfs(Integer i, String order, List<Integer> res) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (val(i) == null)\n            return;\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"pre\".equals(order))\n            res.add(val(i));\n        dfs(left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"in\".equals(order))\n            res.add(val(i));\n        dfs(right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"post\".equals(order))\n            res.add(val(i));\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<Integer> preOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<Integer> inOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"in\", res);\n        return res;\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<Integer> postOrder() {\n        List<Integer> res = new ArrayList<>();\n        dfs(0, \"post\", res);\n        return res;\n    }\n}\n
array_binary_tree.cs
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree(List<int?> arr) {\n    List<int?> tree = new(arr);\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    public int Size() {\n        return tree.Count;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public int? Val(int i) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= Size())\n            return null;\n        return tree[i];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public int Left(int i) {\n        return 2 * i + 1;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public int Right(int i) {\n        return 2 * i + 2;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    public int Parent(int i) {\n        return (i - 1) / 2;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    public List<int> LevelOrder() {\n        List<int> res = [];\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (int i = 0; i < Size(); i++) {\n            if (Val(i).HasValue)\n                res.Add(Val(i)!.Value);\n        }\n        return res;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    void DFS(int i, string order, List<int> res) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (!Val(i).HasValue)\n            return;\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"pre\")\n            res.Add(Val(i)!.Value);\n        DFS(Left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"in\")\n            res.Add(Val(i)!.Value);\n        DFS(Right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order == \"post\")\n            res.Add(Val(i)!.Value);\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<int> PreOrder() {\n        List<int> res = [];\n        DFS(0, \"pre\", res);\n        return res;\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<int> InOrder() {\n        List<int> res = [];\n        DFS(0, \"in\", res);\n        return res;\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    public List<int> PostOrder() {\n        List<int> res = [];\n        DFS(0, \"post\", res);\n        return res;\n    }\n}\n
array_binary_tree.go
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\ntype arrayBinaryTree struct {\n    tree []any\n}\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc newArrayBinaryTree(arr []any) *arrayBinaryTree {\n    return &arrayBinaryTree{\n        tree: arr,\n    }\n}\n\n/* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\nfunc (abt *arrayBinaryTree) size() int {\n    return len(abt.tree)\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\nfunc (abt *arrayBinaryTree) val(i int) any {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    if i < 0 || i >= abt.size() {\n        return nil\n    }\n    return abt.tree[i]\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\nfunc (abt *arrayBinaryTree) left(i int) int {\n    return 2*i + 1\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\nfunc (abt *arrayBinaryTree) right(i int) int {\n    return 2*i + 2\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\nfunc (abt *arrayBinaryTree) parent(i int) int {\n    return (i - 1) / 2\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfunc (abt *arrayBinaryTree) levelOrder() []any {\n    var res []any\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n    for i := 0; i < abt.size(); i++ {\n        if abt.val(i) != nil {\n            res = append(res, abt.val(i))\n        }\n    }\n    return res\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nfunc (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if abt.val(i) == nil {\n        return\n    }\n    // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if order == \"pre\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.left(i), order, res)\n    // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if order == \"in\" {\n        *res = append(*res, abt.val(i))\n    }\n    abt.dfs(abt.right(i), order, res)\n    // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if order == \"post\" {\n        *res = append(*res, abt.val(i))\n    }\n}\n\n/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc (abt *arrayBinaryTree) preOrder() []any {\n    var res []any\n    abt.dfs(0, \"pre\", &res)\n    return res\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc (abt *arrayBinaryTree) inOrder() []any {\n    var res []any\n    abt.dfs(0, \"in\", &res)\n    return res\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc (abt *arrayBinaryTree) postOrder() []any {\n    var res []any\n    abt.dfs(0, \"post\", &res)\n    return res\n}\n
array_binary_tree.swift
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n    private var tree: [Int?]\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    init(arr: [Int?]) {\n        tree = arr\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    func size() -> Int {\n        tree.count\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    func val(i: Int) -> Int? {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if i < 0 || i >= size() {\n            return nil\n        }\n        return tree[i]\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    func left(i: Int) -> Int {\n        2 * i + 1\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    func right(i: Int) -> Int {\n        2 * i + 2\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    func parent(i: Int) -> Int {\n        (i - 1) / 2\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    func levelOrder() -> [Int] {\n        var res: [Int] = []\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for i in 0 ..< size() {\n            if let val = val(i: i) {\n                res.append(val)\n            }\n        }\n        return res\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    private func dfs(i: Int, order: String, res: inout [Int]) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        guard let val = val(i: i) else {\n            return\n        }\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"pre\" {\n            res.append(val)\n        }\n        dfs(i: left(i: i), order: order, res: &res)\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"in\" {\n            res.append(val)\n        }\n        dfs(i: right(i: i), order: order, res: &res)\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"post\" {\n            res.append(val)\n        }\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    func preOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"pre\", res: &res)\n        return res\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    func inOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"in\", res: &res)\n        return res\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    func postOrder() -> [Int] {\n        var res: [Int] = []\n        dfs(i: 0, order: \"post\", res: &res)\n        return res\n    }\n}\n
array_binary_tree.js
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n    #tree;\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(arr) {\n        this.#tree = arr;\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    size() {\n        return this.#tree.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    val(i) {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    left(i) {\n        return 2 * i + 1;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    right(i) {\n        return 2 * i + 2;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    parent(i) {\n        return Math.floor((i - 1) / 2); // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    levelOrder() {\n        let res = [];\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    #dfs(i, order, res) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (this.val(i) === null) return;\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    preOrder() {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    inOrder() {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    postOrder() {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
array_binary_tree.ts
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n    #tree: (number | null)[];\n\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    constructor(arr: (number | null)[]) {\n        this.#tree = arr;\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    size(): number {\n        return this.#tree.length;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    val(i: number): number | null {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= this.size()) return null;\n        return this.#tree[i];\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    left(i: number): number {\n        return 2 * i + 1;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    right(i: number): number {\n        return 2 * i + 2;\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    parent(i: number): number {\n        return Math.floor((i - 1) / 2); // \u041e\u043a\u0440\u0443\u0433\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0438\u0437 \u043f\u0440\u0438 \u0434\u0435\u043b\u0435\u043d\u0438\u0438\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    levelOrder(): number[] {\n        let res = [];\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (let i = 0; i < this.size(); i++) {\n            if (this.val(i) !== null) res.push(this.val(i));\n        }\n        return res;\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    #dfs(i: number, order: Order, res: (number | null)[]): void {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (this.val(i) === null) return;\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'pre') res.push(this.val(i));\n        this.#dfs(this.left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'in') res.push(this.val(i));\n        this.#dfs(this.right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (order === 'post') res.push(this.val(i));\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    preOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'pre', res);\n        return res;\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    inOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'in', res);\n        return res;\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    postOrder(): (number | null)[] {\n        const res = [];\n        this.#dfs(0, 'post', res);\n        return res;\n    }\n}\n
array_binary_tree.dart
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree {\n  late List<int?> _tree;\n\n  /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n  ArrayBinaryTree(this._tree);\n\n  /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n  int size() {\n    return _tree.length;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n  int? val(int i) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    if (i < 0 || i >= size()) {\n      return null;\n    }\n    return _tree[i];\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n  int? left(int i) {\n    return 2 * i + 1;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n  int? right(int i) {\n    return 2 * i + 2;\n  }\n\n  /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n  int? parent(int i) {\n    return (i - 1) ~/ 2;\n  }\n\n  /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n  List<int> levelOrder() {\n    List<int> res = [];\n    for (int i = 0; i < size(); i++) {\n      if (val(i) != null) {\n        res.add(val(i)!);\n      }\n    }\n    return res;\n  }\n\n  /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n  void dfs(int i, String order, List<int?> res) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (val(i) == null) {\n      return;\n    }\n    // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (order == 'pre') {\n      res.add(val(i));\n    }\n    dfs(left(i)!, order, res);\n    // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (order == 'in') {\n      res.add(val(i));\n    }\n    dfs(right(i)!, order, res);\n    // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (order == 'post') {\n      res.add(val(i));\n    }\n  }\n\n  /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n  List<int?> preOrder() {\n    List<int?> res = [];\n    dfs(0, 'pre', res);\n    return res;\n  }\n\n  /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n  List<int?> inOrder() {\n    List<int?> res = [];\n    dfs(0, 'in', res);\n    return res;\n  }\n\n  /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n  List<int?> postOrder() {\n    List<int?> res = [];\n    dfs(0, 'post', res);\n    return res;\n  }\n}\n
array_binary_tree.rs
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nstruct ArrayBinaryTree {\n    tree: Vec<Option<i32>>,\n}\n\nimpl ArrayBinaryTree {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new(arr: Vec<Option<i32>>) -> Self {\n        Self { tree: arr }\n    }\n\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    fn size(&self) -> i32 {\n        self.tree.len() as i32\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fn val(&self, i: i32) -> Option<i32> {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c None, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if i < 0 || i >= self.size() {\n            None\n        } else {\n            self.tree[i as usize]\n        }\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fn left(&self, i: i32) -> i32 {\n        2 * i + 1\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fn right(&self, i: i32) -> i32 {\n        2 * i + 2\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fn parent(&self, i: i32) -> i32 {\n        (i - 1) / 2\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    fn level_order(&self) -> Vec<i32> {\n        self.tree.iter().filter_map(|&x| x).collect()\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    fn dfs(&self, i: i32, order: &'static str, res: &mut Vec<i32>) {\n        if self.val(i).is_none() {\n            return;\n        }\n        let val = self.val(i).unwrap();\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"pre\" {\n            res.push(val);\n        }\n        self.dfs(self.left(i), order, res);\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"in\" {\n            res.push(val);\n        }\n        self.dfs(self.right(i), order, res);\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if order == \"post\" {\n            res.push(val);\n        }\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fn pre_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"pre\", &mut res);\n        res\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fn in_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"in\", &mut res);\n        res\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fn post_order(&self) -> Vec<i32> {\n        let mut res = vec![];\n        self.dfs(0, \"post\", &mut res);\n        res\n    }\n}\n
array_binary_tree.c
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c */\ntypedef struct {\n    int *tree;\n    int size;\n} ArrayBinaryTree;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nArrayBinaryTree *newArrayBinaryTree(int *arr, int arrSize) {\n    ArrayBinaryTree *abt = (ArrayBinaryTree *)malloc(sizeof(ArrayBinaryTree));\n    abt->tree = malloc(sizeof(int) * arrSize);\n    memcpy(abt->tree, arr, sizeof(int) * arrSize);\n    abt->size = arrSize;\n    return abt;\n}\n\n/* \u0414\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nvoid delArrayBinaryTree(ArrayBinaryTree *abt) {\n    free(abt->tree);\n    free(abt);\n}\n\n/* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\nint size(ArrayBinaryTree *abt) {\n    return abt->size;\n}\n\n/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\nint val(ArrayBinaryTree *abt, int i) {\n    // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c INT_MAX, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n    if (i < 0 || i >= size(abt))\n        return INT_MAX;\n    return abt->tree[i];\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nint *levelOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n    for (int i = 0; i < size(abt); i++) {\n        if (val(abt, i) != INT_MAX)\n            res[index++] = val(abt, i);\n    }\n    *returnSize = index;\n    return res;\n}\n\n/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\nvoid dfs(ArrayBinaryTree *abt, int i, char *order, int *res, int *index) {\n    // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (val(abt, i) == INT_MAX)\n        return;\n    // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (strcmp(order, \"pre\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, left(i), order, res, index);\n    // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (strcmp(order, \"in\") == 0)\n        res[(*index)++] = val(abt, i);\n    dfs(abt, right(i), order, res, index);\n    // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    if (strcmp(order, \"post\") == 0)\n        res[(*index)++] = val(abt, i);\n}\n\n/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nint *preOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"pre\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nint *inOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"in\", res, &index);\n    *returnSize = index;\n    return res;\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nint *postOrder(ArrayBinaryTree *abt, int *returnSize) {\n    int *res = (int *)malloc(sizeof(int) * size(abt));\n    int index = 0;\n    dfs(abt, 0, \"post\", res, &index);\n    *returnSize = index;\n    return res;\n}\n
array_binary_tree.kt
/* \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 */\nclass ArrayBinaryTree(val tree: MutableList<Int?>) {\n    /* \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 */\n    fun size(): Int {\n        return tree.size\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fun _val(i: Int): Int? {\n        // \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e\n        if (i < 0 || i >= size()) return null\n        return tree[i]\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fun left(i: Int): Int {\n        return 2 * i + 1\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fun right(i: Int): Int {\n        return 2 * i + 2\n    }\n\n    /* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i */\n    fun parent(i: Int): Int {\n        return (i - 1) / 2\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\n    fun levelOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        // \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n        for (i in 0..<size()) {\n            if (_val(i) != null)\n                res.add(_val(i))\n        }\n        return res\n    }\n\n    /* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 */\n    fun dfs(i: Int, order: String, res: MutableList<Int?>) {\n        // \u0415\u0441\u043b\u0438 \u044d\u0442\u043e \u043f\u0443\u0441\u0442\u0430\u044f \u043f\u043e\u0437\u0438\u0446\u0438\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (_val(i) == null)\n            return\n        // \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"pre\" == order)\n            res.add(_val(i))\n        dfs(left(i), order, res)\n        // \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"in\" == order)\n            res.add(_val(i))\n        dfs(right(i), order, res)\n        // \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n        if (\"post\" == order)\n            res.add(_val(i))\n    }\n\n    /* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fun preOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"pre\", res)\n        return res\n    }\n\n    /* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fun inOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"in\", res)\n        return res\n    }\n\n    /* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\n    fun postOrder(): MutableList<Int?> {\n        val res = mutableListOf<Int?>()\n        dfs(0, \"post\", res)\n        return res\n    }\n}\n
array_binary_tree.rb
=begin\nFile: array_binary_tree.rb\nCreated Time: 2024-04-17\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041a\u043b\u0430\u0441\u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u043d\u043e\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438 ###\nclass ArrayBinaryTree\n  # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ###\n  def initialize(arr)\n    @tree = arr.to_a\n  end\n\n  # ## \u0412\u043c\u0435\u0441\u0442\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043f\u0438\u0441\u043a\u0430 ###\n  def size\n    @tree.length\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i ###\n  def val(i)\n    # \u0415\u0441\u043b\u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u0432\u044b\u0445\u043e\u0434\u0438\u0442 \u0437\u0430 \u0433\u0440\u0430\u043d\u0438\u0446\u044b, \u0432\u0435\u0440\u043d\u0443\u0442\u044c nil, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u044e\u0449\u0438\u0439 \u043f\u0443\u0441\u0442\u0443\u044e \u044f\u0447\u0435\u0439\u043a\u0443\n    return if i < 0 || i >= size\n\n    @tree[i]\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i ###\n  def left(i)\n    2 * i + 1\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i ###\n  def right(i)\n    2 * i + 2\n  end\n\n  # ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0443\u0437\u043b\u0430 \u0441 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c i ###\n  def parent(i)\n    (i - 1) / 2\n  end\n\n  # ## \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 ###\n  def level_order\n    @res = []\n\n    # \u041d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\n    for i in 0...size\n      @res << val(i) unless val(i).nil?\n    end\n\n    @res\n  end\n\n  # ## \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 ###\n  def dfs(i, order)\n    return if val(i).nil?\n    # \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    @res << val(i) if order == :pre\n    dfs(left(i), order)\n    # \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    @res << val(i) if order == :in\n    dfs(right(i), order)\n    # \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\n    @res << val(i) if order == :post\n  end\n\n  # ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\n  def pre_order\n    @res = []\n    dfs(0, :pre)\n    @res\n  end\n\n  # ## \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\n  def in_order\n    @res = []\n    dfs(0, :in)\n    @res\n  end\n\n  # ## \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\n  def post_order\n    @res = []\n    dfs(0, :post)\n    @res\n  end\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_tree/array_representation_of_tree/#733","title":"7.3.3 \u00a0 \u041f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f","text":"

\u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438\u043c\u0435\u0435\u0442 \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430.

\u041e\u0434\u043d\u0430\u043a\u043e \u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0435\u0441\u0442\u044c \u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f.

"},{"location":"chapter_tree/avl_tree/","title":"7.5 \u00a0 AVL-\u0434\u0435\u0440\u0435\u0432\u043e *","text":"

\u0412 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \"\u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430\" \u043c\u044b \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u0438, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u043e\u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0440\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0441\u0435\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442\u0441\u044f \u0441 \\(O(\\log n)\\) \u0434\u043e \\(O(n)\\) .

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-24, \u043f\u043e\u0441\u043b\u0435 \u0434\u0432\u0443\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u043e\u0432 \u044d\u0442\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-24 \u00a0 \u0414\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u044f AVL-\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u043e\u0432

\u0414\u0440\u0443\u0433\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440: \u0435\u0441\u043b\u0438 \u0432 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-25, \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0432\u0430 \u0443\u0437\u043b\u0430, \u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u043d\u0430\u043a\u043b\u043e\u043d\u0438\u0442\u0441\u044f \u0432\u043b\u0435\u0432\u043e, \u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0442\u043e\u0436\u0435 \u0443\u0445\u0443\u0434\u0448\u0438\u0442\u0441\u044f.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-25 \u00a0 \u0414\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u044f AVL-\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0443\u0437\u043b\u043e\u0432

\u0412 1962 \u0433\u043e\u0434\u0443 \u0413. \u041c. Adelson-Velsky \u0438 \u0415. \u041c. Landis \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 \"An algorithm for the organization of information\" \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b\u0438 AVL-\u0434\u0435\u0440\u0435\u0432\u043e. \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u043d \u043d\u0430\u0431\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0439, \u0447\u0442\u043e \u043f\u0440\u0438 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0438 \u0443\u0437\u043b\u043e\u0432 AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0435 \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0447\u0435\u043c\u0443 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \\(O(\\log n)\\) . \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0432 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445, \u0433\u0434\u0435 \u0447\u0430\u0441\u0442\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430, \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435, \u043f\u043e\u0438\u0441\u043a \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435, AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u043f\u043e\u0442\u043e\u043c\u0443 \u0438\u043c\u0435\u0435\u0442 \u0432\u044b\u0441\u043e\u043a\u0443\u044e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0446\u0435\u043d\u043d\u043e\u0441\u0442\u044c.

"},{"location":"chapter_tree/avl_tree/#751-avl-","title":"7.5.1 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b AVL-\u0434\u0435\u0440\u0435\u0432\u0430","text":"

AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430, \u0438 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0432\u0441\u0435\u043c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c \u043e\u0431\u0435\u0438\u0445 \u044d\u0442\u0438\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u043e\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 (balanced binary search tree).

"},{"location":"chapter_tree/avl_tree/#1","title":"1. \u00a0 \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430","text":"

\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u043a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e height :

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
class TreeNode:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                 # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        self.height: int = 0                # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n        self.left: TreeNode | None = None   # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n        self.right: TreeNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nstruct TreeNode {\n    int val{};          // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    int height = 0;     // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    TreeNode *left{};   // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    TreeNode *right{};  // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    TreeNode() = default;\n    explicit TreeNode(int x) : val(x){}\n};\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    public int val;        // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    public int height;     // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    public TreeNode left;  // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    public TreeNode right; // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    public TreeNode(int x) { val = x; }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    public int height;      // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    public TreeNode? left;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    public TreeNode? right; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\ntype TreeNode struct {\n    Val    int       // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    Height int       // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    Left   *TreeNode // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    Right  *TreeNode // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    var val: Int // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var height: Int // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    var left: TreeNode? // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    var right: TreeNode? // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n\n    init(x: Int) {\n        val = x\n        height = 0\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    height; // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    left; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    right; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    constructor(val, left, right, height) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    val: number;            // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    height: number;         // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    left: TreeNode | null;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    right: TreeNode | null; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val;\n        this.height = height === undefined ? 0 : height;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n  int val;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  int height;      // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n  TreeNode? left;  // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n  TreeNode? right; // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n  TreeNode(this.val, [this.height = 0, this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nstruct TreeNode {\n    val: i32,                               // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    height: i32,                            // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n\nimpl TreeNode {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            height: 0,\n            left: None,\n            right: None\n        }))\n    }\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\ntypedef struct TreeNode {\n    int val;\n    int height;\n    struct TreeNode *left;\n    struct TreeNode *right;\n} TreeNode;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode(val _val: Int) {  // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val height: Int = 0          // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    val left: TreeNode? = null   // \u041b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    val right: TreeNode? = null  // \u041f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n}\n
### \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 ###\nclass TreeNode\n  attr_accessor :val    # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  attr_accessor :height # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n  attr_accessor :left   # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n  attr_accessor :right  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n\n  def initialize(val)\n    @val = val\n    @height = 0\n  end\nend\n

\"\u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\" \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u0443\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \"\u0440\u0435\u0431\u0435\u0440\". \u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u0430 \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \\(-1\\) . \u041c\u044b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0432\u0435 \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438: \u043e\u0434\u043d\u0443 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b \u0443\u0437\u043b\u0430, \u0434\u0440\u0443\u0433\u0443\u044e \u0434\u043b\u044f \u0435\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def height(self, node: TreeNode | None) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\"\"\"\n    # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    if node is not None:\n        return node.height\n    return -1\n\ndef update_height(self, node: TreeNode | None):\n    \"\"\"\u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\"\"\"\n    # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node.height = max([self.height(node.left), self.height(node.right)]) + 1\n
avl_tree.cpp
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nint height(TreeNode *node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node == nullptr ? -1 : node->height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nvoid updateHeight(TreeNode *node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node->height = max(height(node->left), height(node->right)) + 1;\n}\n
avl_tree.java
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nint height(TreeNode node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nvoid updateHeight(TreeNode node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node.height = Math.max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.cs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nint Height(TreeNode? node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node == null ? -1 : node.height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nvoid UpdateHeight(TreeNode node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node.height = Math.Max(Height(node.left), Height(node.right)) + 1;\n}\n
avl_tree.go
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfunc (t *aVLTree) height(node *TreeNode) int {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    if node != nil {\n        return node.Height\n    }\n    return -1\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfunc (t *aVLTree) updateHeight(node *TreeNode) {\n    lh := t.height(node.Left)\n    rh := t.height(node.Right)\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    if lh > rh {\n        node.Height = lh + 1\n    } else {\n        node.Height = rh + 1\n    }\n}\n
avl_tree.swift
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfunc height(node: TreeNode?) -> Int {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    node?.height ?? -1\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfunc updateHeight(node: TreeNode?) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node?.height = max(height(node: node?.left), height(node: node?.right)) + 1\n}\n
avl_tree.js
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nheight(node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\n#updateHeight(node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.ts
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nheight(node: TreeNode): number {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node === null ? -1 : node.height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nupdateHeight(node: TreeNode): void {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node.height =\n        Math.max(this.height(node.left), this.height(node.right)) + 1;\n}\n
avl_tree.dart
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nint height(TreeNode? node) {\n  // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node == null ? -1 : node.height;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nvoid updateHeight(TreeNode? node) {\n  // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node!.height = max(height(node.left), height(node.right)) + 1;\n}\n
avl_tree.rs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfn height(node: OptionTreeNodeRc) -> i32 {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    match node {\n        Some(node) => node.borrow().height,\n        None => -1,\n    }\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfn update_height(node: OptionTreeNodeRc) {\n    if let Some(node) = node {\n        let left = node.borrow().left.clone();\n        let right = node.borrow().right.clone();\n        // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n        node.borrow_mut().height = std::cmp::max(Self::height(left), Self::height(right)) + 1;\n    }\n}\n
avl_tree.c
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nint height(TreeNode *node) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    if (node != NULL) {\n        return node->height;\n    }\n    return -1;\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nvoid updateHeight(TreeNode *node) {\n    int lh = height(node->left);\n    int rh = height(node->right);\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    if (lh > rh) {\n        node->height = lh + 1;\n    } else {\n        node->height = rh + 1;\n    }\n}\n
avl_tree.kt
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfun height(node: TreeNode?): Int {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n    return node?.height ?: -1\n}\n\n/* \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 */\nfun updateHeight(node: TreeNode?) {\n    // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n    node?.height = max(height(node?.left), height(node?.right)) + 1\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#2-","title":"2. \u00a0 \u0411\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 \u0443\u0437\u043b\u0430","text":"

\u0411\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 (balance factor) \u0443\u0437\u043b\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043c\u0438\u043d\u0443\u0441 \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430; \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0440\u0430\u0432\u043d\u044b\u043c \\(0\\) . \u041c\u044b \u0442\u0430\u043a\u0436\u0435 \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0442\u043e\u043c \u0431\u044b\u043b\u043e \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def balance_factor(self, node: TreeNode | None) -> int:\n    \"\"\"\u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430\"\"\"\n    # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if node is None:\n        return 0\n    # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return self.height(node.left) - self.height(node.right)\n
avl_tree.cpp
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nint balanceFactor(TreeNode *node) {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node == nullptr)\n        return 0;\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return height(node->left) - height(node->right);\n}\n
avl_tree.java
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nint balanceFactor(TreeNode node) {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node == null)\n        return 0;\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return height(node.left) - height(node.right);\n}\n
avl_tree.cs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nint BalanceFactor(TreeNode? node) {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node == null) return 0;\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return Height(node.left) - Height(node.right);\n}\n
avl_tree.go
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nfunc (t *aVLTree) balanceFactor(node *TreeNode) int {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if node == nil {\n        return 0\n    }\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return t.height(node.Left) - t.height(node.Right)\n}\n
avl_tree.swift
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nfunc balanceFactor(node: TreeNode?) -> Int {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    guard let node = node else { return 0 }\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return height(node: node.left) - height(node: node.right)\n}\n
avl_tree.js
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nbalanceFactor(node) {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node === null) return 0;\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.ts
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nbalanceFactor(node: TreeNode): number {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node === null) return 0;\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return this.height(node.left) - this.height(node.right);\n}\n
avl_tree.dart
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nint balanceFactor(TreeNode? node) {\n  // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  if (node == null) return 0;\n  // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  return height(node.left) - height(node.right);\n}\n
avl_tree.rs
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nfn balance_factor(node: OptionTreeNodeRc) -> i32 {\n    match node {\n        // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n        None => 0,\n        // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n        Some(node) => {\n            Self::height(node.borrow().left.clone()) - Self::height(node.borrow().right.clone())\n        }\n    }\n}\n
avl_tree.c
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nint balanceFactor(TreeNode *node) {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node == NULL) {\n        return 0;\n    }\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return height(node->left) - height(node->right);\n}\n
avl_tree.kt
/* \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 */\nfun balanceFactor(node: TreeNode?): Int {\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n    if (node == null) return 0\n    // \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return height(node.left) - height(node.right)\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n```\n

Tip

\u041f\u0443\u0441\u0442\u044c \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 \u0440\u0430\u0432\u0435\u043d \\(f\\) ; \u0442\u043e\u0433\u0434\u0430 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0443\u0437\u043b\u0430 AVL-\u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \\(-1 \\le f \\le 1\\) .

"},{"location":"chapter_tree/avl_tree/#752-avl-","title":"7.5.2 \u00a0 \u0412\u0440\u0430\u0449\u0435\u043d\u0438\u044f AVL-\u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u041e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u044c AVL-\u0434\u0435\u0440\u0435\u0432\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \"\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\", \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0437\u0430\u043d\u043e\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b, \u043d\u0435 \u043d\u0430\u0440\u0443\u0448\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0418\u043d\u0430\u0447\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \"\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430\" \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0434\u0435\u0440\u0435\u0432\u043e \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \"\u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430\".

\u0423\u0437\u043b\u044b, \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440\u0430 \u0431\u043e\u043b\u044c\u0448\u0435 \\(1\\) , \u043c\u044b \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c \"\u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438\". \u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0432\u0438\u0434\u0430 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 \u0447\u0435\u0442\u044b\u0440\u0435 \u0442\u0438\u043f\u0430: \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435, \u0438 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435. \u041d\u0438\u0436\u0435 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c \u0438\u0445 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e.

"},{"location":"chapter_tree/avl_tree/#1_1","title":"1. \u00a0 \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u043f\u043e\u0434 \u0443\u0437\u043b\u043e\u043c \u0443\u043a\u0430\u0437\u0430\u043d \u0435\u0433\u043e \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440. \u0415\u0441\u043b\u0438 \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u0442\u043e \u043f\u0435\u0440\u0432\u044b\u043c \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0443\u0437\u043b\u043e\u043c \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u0431\u0443\u0434\u0435\u0442 \"\u0443\u0437\u0435\u043b 3\". \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \u0441 \u044d\u0442\u0438\u043c \u0443\u0437\u043b\u043e\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043e\u0440\u043d\u044f, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043a\u0430\u043a node , \u0435\u0433\u043e \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u043a\u0430\u043a child \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043c \"\u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\". \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \u0441\u043d\u043e\u0432\u0430 \u0441\u0442\u0430\u043d\u0435\u0442 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0438 \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430.

<1><2><3><4>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-26 \u00a0 \u0428\u0430\u0433\u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-27, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0443\u0437\u043b\u0430 child \u0435\u0441\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043a\u0430\u043a grand_child , \u0432 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0448\u0430\u0433: \u0441\u0434\u0435\u043b\u0430\u0442\u044c grand_child \u043b\u0435\u0432\u044b\u043c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u0443\u0437\u043b\u043e\u043c node .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-27 \u00a0 \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 grand_child

\"\u041f\u043e\u0432\u043e\u0440\u043e\u0442 \u0432\u043f\u0440\u0430\u0432\u043e\" - \u044d\u0442\u043e \u043b\u0438\u0448\u044c \u043e\u0431\u0440\u0430\u0437\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435; \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u043d \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439 \u0443\u0437\u043b\u043e\u0432. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def right_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\"\"\"\n    child = node.left\n    grand_child = child.right\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node\n    node.left = grand_child\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    self.update_height(node)\n    self.update_height(child)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n
avl_tree.cpp
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child = node->left;\n    TreeNode *grandChild = child->right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child->right = node;\n    node->left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.java
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode rightRotate(TreeNode node) {\n    TreeNode child = node.left;\n    TreeNode grandChild = child.right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node;\n    node.left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.cs
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode? RightRotate(TreeNode? node) {\n    TreeNode? child = node?.left;\n    TreeNode? grandChild = child?.right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node;\n    node.left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.go
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfunc (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {\n    child := node.Left\n    grandChild := child.Right\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.Right = node\n    node.Left = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n
avl_tree.swift
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfunc rightRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.left\n    let grandChild = child?.right\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child?.right = node\n    node?.left = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n
avl_tree.js
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\n#rightRotate(node) {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node;\n    node.left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.ts
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nrightRotate(node: TreeNode): TreeNode {\n    const child = node.left;\n    const grandChild = child.right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node;\n    node.left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.dart
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode? rightRotate(TreeNode? node) {\n  TreeNode? child = node!.left;\n  TreeNode? grandChild = child!.right;\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node;\n  node.left = grandChild;\n  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  updateHeight(node);\n  updateHeight(child);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  return child;\n}\n
avl_tree.rs
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfn right_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().left.clone().unwrap();\n            let grand_child = child.borrow().right.clone();\n            // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n            child.borrow_mut().right = Some(node.clone());\n            node.borrow_mut().left = grand_child;\n            // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n            Some(child)\n        }\n        None => None,\n    }\n}\n
avl_tree.c
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode *rightRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->left;\n    grandChild = child->right;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child->right = node;\n    node->left = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.kt
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfun rightRotate(node: TreeNode?): TreeNode {\n    val child = node!!.left\n    val grandChild = child!!.right\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.right = node\n    node.left = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node)\n    updateHeight(child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#2","title":"2. \u00a0 \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435","text":"

\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \"\u0437\u0435\u0440\u043a\u0430\u043b\u044c\u043d\u0443\u044e\" \u0432\u0435\u0440\u0441\u0438\u044e \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0448\u0435 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\", \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-28.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-28 \u00a0 \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435

\u041f\u043e \u0442\u043e\u0439 \u0436\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u0435, \u043a\u043e\u0433\u0434\u0430 \u0443 \u0443\u0437\u043b\u0430 child \u0435\u0441\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043a\u0430\u043a grand_child , \u0432 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0448\u0430\u0433: \u0441\u0434\u0435\u043b\u0430\u0442\u044c grand_child \u043f\u0440\u0430\u0432\u044b\u043c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u0443\u0437\u043b\u043e\u043c node .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-29 \u00a0 \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u0438 \u043d\u0430\u043b\u0438\u0447\u0438\u0438 grand_child

\u041c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0438 \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0435\u0440\u043a\u0430\u043b\u044c\u043d\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b, \u0438 \u0434\u0432\u0430 \u0432\u0438\u0434\u0430 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u043d\u0438 \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u044e\u0442, \u0442\u043e\u0436\u0435 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u043e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u043d\u0430 \u044d\u0442\u0443 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u044e, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432 \u043a\u043e\u0434\u0435 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432\u0441\u0435 left \u043d\u0430 right , \u0430 \u0432\u0441\u0435 right \u043d\u0430 left , \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def left_rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\"\"\"\n    child = node.right\n    grand_child = child.left\n    # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node\n    node.right = grand_child\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    self.update_height(node)\n    self.update_height(child)\n    # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n
avl_tree.cpp
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child = node->right;\n    TreeNode *grandChild = child->left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child->left = node;\n    node->right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.java
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode leftRotate(TreeNode node) {\n    TreeNode child = node.right;\n    TreeNode grandChild = child.left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node;\n    node.right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.cs
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode? LeftRotate(TreeNode? node) {\n    TreeNode? child = node?.right;\n    TreeNode? grandChild = child?.left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node;\n    node.right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    UpdateHeight(node);\n    UpdateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.go
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfunc (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {\n    child := node.Right\n    grandChild := child.Left\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.Left = node\n    node.Right = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    t.updateHeight(node)\n    t.updateHeight(child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n
avl_tree.swift
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfunc leftRotate(node: TreeNode?) -> TreeNode? {\n    let child = node?.right\n    let grandChild = child?.left\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child?.left = node\n    node?.right = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node: node)\n    updateHeight(node: child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n
avl_tree.js
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\n#leftRotate(node) {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node;\n    node.right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    this.#updateHeight(node);\n    this.#updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.ts
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nleftRotate(node: TreeNode): TreeNode {\n    const child = node.right;\n    const grandChild = child.left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node;\n    node.right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    this.updateHeight(node);\n    this.updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.dart
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode? leftRotate(TreeNode? node) {\n  TreeNode? child = node!.right;\n  TreeNode? grandChild = child!.left;\n  // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node;\n  node.right = grandChild;\n  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  updateHeight(node);\n  updateHeight(child);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  return child;\n}\n
avl_tree.rs
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfn left_rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    match node {\n        Some(node) => {\n            let child = node.borrow().right.clone().unwrap();\n            let grand_child = child.borrow().left.clone();\n            // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n            child.borrow_mut().left = Some(node.clone());\n            node.borrow_mut().right = grand_child;\n            // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n            Self::update_height(Some(node));\n            Self::update_height(Some(child.clone()));\n            // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n            Some(child)\n        }\n        None => None,\n    }\n}\n
avl_tree.c
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nTreeNode *leftRotate(TreeNode *node) {\n    TreeNode *child, *grandChild;\n    child = node->right;\n    grandChild = child->left;\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child->left = node;\n    node->right = grandChild;\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    updateHeight(child);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child;\n}\n
avl_tree.kt
/* \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f */\nfun leftRotate(node: TreeNode?): TreeNode {\n    val child = node!!.right\n    val grandChild = child!!.left\n    // \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n    child.left = node\n    node.right = grandChild\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node)\n    updateHeight(child)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n    return child\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#3","title":"3. \u00a0 \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435","text":"

\u0414\u043b\u044f \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 3 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-30 \u043d\u0438 \u043e\u0434\u043d\u043e \u043b\u0438\u0448\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u043d\u0438 \u043e\u0434\u043d\u043e \u043b\u0438\u0448\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \u0432 \u0431\u0430\u043b\u0430\u043d\u0441. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\" \u0434\u043b\u044f child , \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\" \u0434\u043b\u044f node .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-30 \u00a0 \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435

"},{"location":"chapter_tree/avl_tree/#4","title":"4. \u00a0 \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-31, \u0434\u043b\u044f \u0437\u0435\u0440\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \"\u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\" \u0434\u043b\u044f child , \u0430 \u0437\u0430\u0442\u0435\u043c \"\u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\" \u0434\u043b\u044f node .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-31 \u00a0 \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435

"},{"location":"chapter_tree/avl_tree/#5","title":"5. \u00a0 \u0412\u044b\u0431\u043e\u0440 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f","text":"

\u0427\u0435\u0442\u044b\u0440\u0435 \u0432\u0438\u0434\u0430 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0430, \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-32, \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043d\u044b\u043c \u0432\u044b\u0448\u0435 \u0441\u043b\u0443\u0447\u0430\u044f\u043c; \u0434\u043b\u044f \u043d\u0438\u0445 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u044e\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435 \u0438 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-32 \u00a0 \u0427\u0435\u0442\u044b\u0440\u0435 \u0441\u043b\u0443\u0447\u0430\u044f \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0439 AVL-\u0434\u0435\u0440\u0435\u0432\u0430

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 7-3, \u043c\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c, \u043a\u0430\u043a\u043e\u043c\u0443 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0447\u0435\u0442\u044b\u0440\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b, \u043f\u043e \u0437\u043d\u0430\u043a\u0443 \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440\u0430 \u0441\u0430\u043c\u043e\u0433\u043e \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u043f\u043e \u0437\u043d\u0430\u043a\u0443 \u0431\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u043d\u0430 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 7-3 \u00a0 \u0423\u0441\u043b\u043e\u0432\u0438\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u0434\u043b\u044f \u0447\u0435\u0442\u044b\u0440\u0435\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0439

\u0411\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0411\u0430\u043b\u0430\u043d\u0441-\u0444\u0430\u043a\u0442\u043e\u0440 \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u041a\u0430\u043a\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \\(> 1\\) (\u043b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) \\(\\geq 0\\) \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \\(> 1\\) (\u043b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) \\(<0\\) \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435 \\(< -1\\) (\u043f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) \\(\\leq 0\\) \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \\(< -1\\) (\u043f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e) \\(>0\\) \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435

\u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043c\u044b \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e. \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0439 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0438 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0438 \u0441\u043d\u043e\u0432\u0430 \u043f\u0440\u0438\u0432\u0435\u0441\u0442\u0438 \u0443\u0437\u0435\u043b \u0432 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def rotate(self, node: TreeNode | None) -> TreeNode | None:\n    \"\"\"\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\"\"\"\n    # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    balance_factor = self.balance_factor(node)\n    # \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if balance_factor > 1:\n        if self.balance_factor(node.left) >= 0:\n            # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return self.right_rotate(node)\n        else:\n            # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.left = self.left_rotate(node.left)\n            return self.right_rotate(node)\n    # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    elif balance_factor < -1:\n        if self.balance_factor(node.right) <= 0:\n            # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return self.left_rotate(node)\n        else:\n            # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.right = self.right_rotate(node.right)\n            return self.left_rotate(node)\n    # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node\n
avl_tree.cpp
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode *rotate(TreeNode *node) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    int _balanceFactor = balanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (_balanceFactor > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return rightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (_balanceFactor < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return leftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.java
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode rotate(TreeNode node) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    int balanceFactor = balanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return rightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.left = leftRotate(node.left);\n            return rightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return leftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.right = rightRotate(node.right);\n            return leftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.cs
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode? Rotate(TreeNode? node) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    int balanceFactorInt = BalanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactorInt > 1) {\n        if (BalanceFactor(node?.left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return RightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node!.left = LeftRotate(node!.left);\n            return RightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactorInt < -1) {\n        if (BalanceFactor(node?.right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return LeftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node!.right = RightRotate(node!.right);\n            return LeftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.go
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nfunc (t *aVLTree) rotate(node *TreeNode) *TreeNode {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    // \u0412 Go \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u0438\u0435 \u0438\u043c\u0435\u043d\u0430 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445, \u0437\u0434\u0435\u0441\u044c bf \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 t.balanceFactor\n    bf := t.balanceFactor(node)\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if bf > 1 {\n        if t.balanceFactor(node.Left) >= 0 {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return t.rightRotate(node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.Left = t.leftRotate(node.Left)\n            return t.rightRotate(node)\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if bf < -1 {\n        if t.balanceFactor(node.Right) <= 0 {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return t.leftRotate(node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.Right = t.rightRotate(node.Right)\n            return t.leftRotate(node)\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node\n}\n
avl_tree.swift
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nfunc rotate(node: TreeNode?) -> TreeNode? {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    let balanceFactor = balanceFactor(node: node)\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if balanceFactor > 1 {\n        if self.balanceFactor(node: node?.left) >= 0 {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return rightRotate(node: node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node?.left = leftRotate(node: node?.left)\n            return rightRotate(node: node)\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if balanceFactor < -1 {\n        if self.balanceFactor(node: node?.right) <= 0 {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return leftRotate(node: node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node?.right = rightRotate(node: node?.right)\n            return leftRotate(node: node)\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node\n}\n
avl_tree.js
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n#rotate(node) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    const balanceFactor = this.balanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return this.#rightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.left = this.#leftRotate(node.left);\n            return this.#rightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return this.#leftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.right = this.#rightRotate(node.right);\n            return this.#leftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.ts
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nrotate(node: TreeNode): TreeNode {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    const balanceFactor = this.balanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor > 1) {\n        if (this.balanceFactor(node.left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return this.rightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.left = this.leftRotate(node.left);\n            return this.rightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor < -1) {\n        if (this.balanceFactor(node.right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return this.leftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.right = this.rightRotate(node.right);\n            return this.leftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.dart
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode? rotate(TreeNode? node) {\n  // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  int factor = balanceFactor(node);\n  // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n  if (factor > 1) {\n    if (balanceFactor(node!.left) >= 0) {\n      // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return rightRotate(node);\n    } else {\n      // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = leftRotate(node.left);\n      return rightRotate(node);\n    }\n  }\n  // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n  if (factor < -1) {\n    if (balanceFactor(node!.right) <= 0) {\n      // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return leftRotate(node);\n    } else {\n      // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = rightRotate(node.right);\n      return leftRotate(node);\n    }\n  }\n  // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  return node;\n}\n
avl_tree.rs
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nfn rotate(node: OptionTreeNodeRc) -> OptionTreeNodeRc {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    let balance_factor = Self::balance_factor(node.clone());\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if balance_factor > 1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().left.clone()) >= 0 {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            Self::right_rotate(Some(node))\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            let left = node.borrow().left.clone();\n            node.borrow_mut().left = Self::left_rotate(left);\n            Self::right_rotate(Some(node))\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    else if balance_factor < -1 {\n        let node = node.unwrap();\n        if Self::balance_factor(node.borrow().right.clone()) <= 0 {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            Self::left_rotate(Some(node))\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            let right = node.borrow().right.clone();\n            node.borrow_mut().right = Self::right_rotate(right);\n            Self::left_rotate(Some(node))\n        }\n    } else {\n        // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n        node\n    }\n}\n
avl_tree.c
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nTreeNode *rotate(TreeNode *node) {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    int bf = balanceFactor(node);\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (bf > 1) {\n        if (balanceFactor(node->left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return rightRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node->left = leftRotate(node->left);\n            return rightRotate(node);\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (bf < -1) {\n        if (balanceFactor(node->right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return leftRotate(node);\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node->right = rightRotate(node->right);\n            return leftRotate(node);\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node;\n}\n
avl_tree.kt
/* \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\nfun rotate(node: TreeNode): TreeNode {\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n    val balanceFactor = balanceFactor(node)\n    // \u041b\u0435\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor > 1) {\n        if (balanceFactor(node.left) >= 0) {\n            // \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return rightRotate(node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n            node.left = leftRotate(node.left)\n            return rightRotate(node)\n        }\n    }\n    // \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435 \u043f\u0435\u0440\u0435\u043a\u043e\u0448\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\n    if (balanceFactor < -1) {\n        if (balanceFactor(node.right) <= 0) {\n            // \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n            return leftRotate(node)\n        } else {\n            // \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n            node.right = rightRotate(node.right)\n            return leftRotate(node)\n        }\n    }\n    // \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n    return node\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e ###\ndef rotate(node)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  balance_factor = balance_factor(node)\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return right_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0431\u0445\u043e\u0434\u0430\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return left_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  node\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#753-avl-","title":"7.5.3 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 AVL-\u0434\u0435\u0440\u0435\u0432\u0430","text":""},{"location":"chapter_tree/avl_tree/#1_2","title":"1. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430","text":"

\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0443\u0437\u043b\u0430 \u0432 AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u043f\u043e\u0445\u043e\u0436\u0430 \u043d\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0443 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0440\u0430\u0437\u043d\u0438\u0446\u0430 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0432 AVL-\u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0430 \u043f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043a \u043a\u043e\u0440\u043d\u044e \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0446\u0435\u043f\u043e\u0447\u043a\u0430 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u0447\u0442\u043e\u0431\u044b \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0432 \u0431\u0430\u043b\u0430\u043d\u0441 \u0432\u0441\u0435 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0443\u0437\u043b\u044b. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def insert(self, val):\n    \"\"\"\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\"\"\"\n    self._root = self.insert_helper(self._root, val)\n\ndef insert_helper(self, node: TreeNode | None, val: int) -> TreeNode:\n    \"\"\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434)\"\"\"\n    if node is None:\n        return TreeNode(val)\n    # 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b\n    if val < node.val:\n        node.left = self.insert_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.insert_helper(node.right, val)\n    else:\n        # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        return node\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    self.update_height(node)\n    # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    return self.rotate(node)\n
avl_tree.cpp
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return new TreeNode(val);\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node->val)\n        node->left = insertHelper(node->left, val);\n    else if (val > node->val)\n        node->right = insertHelper(node->right, val);\n    else\n        return node;    // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.java
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int val) {\n    root = insertHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode insertHelper(TreeNode node, int val) {\n    if (node == null)\n        return new TreeNode(val);\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node.val)\n        node.left = insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = insertHelper(node.right, val);\n    else\n        return node; // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.cs
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid Insert(int val) {\n    root = InsertHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode? InsertHelper(TreeNode? node, int val) {\n    if (node == null) return new TreeNode(val);\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node.val)\n        node.left = InsertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = InsertHelper(node.right, val);\n    else\n        return node;     // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    UpdateHeight(node);  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = Rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.go
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfunc (t *aVLTree) insert(val int) {\n    t.root = t.insertHelper(t.root, val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f) */\nfunc (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return NewTreeNode(val)\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if val < node.Val.(int) {\n        node.Left = t.insertHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.insertHelper(node.Right, val)\n    } else {\n        // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        return node\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    t.updateHeight(node)\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = t.rotate(node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n
avl_tree.swift
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfunc insert(val: Int) {\n    root = insertHelper(node: root, val: val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfunc insertHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return TreeNode(x: val)\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if val < node!.val {\n        node?.left = insertHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = insertHelper(node: node?.right, val: val)\n    } else {\n        return node // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    }\n    updateHeight(node: node) // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node: node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n
avl_tree.js
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\ninsert(val) {\n    this.root = this.#insertHelper(this.root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\n#insertHelper(node, val) {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node.val) node.left = this.#insertHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#insertHelper(node.right, val);\n    else return node; // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    this.#updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = this.#rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.ts
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\ninsert(val: number): void {\n    this.root = this.insertHelper(this.root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\ninsertHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return new TreeNode(val);\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node.val) {\n        node.left = this.insertHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.insertHelper(node.right, val);\n    } else {\n        return node; // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    }\n    this.updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = this.rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.dart
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int val) {\n  root = insertHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode? insertHelper(TreeNode? node, int val) {\n  if (node == null) return TreeNode(val);\n  /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n  if (val < node.val)\n    node.left = insertHelper(node.left, val);\n  else if (val > node.val)\n    node.right = insertHelper(node.right, val);\n  else\n    return node; // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n  updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n  node = rotate(node);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  return node;\n}\n
avl_tree.rs
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfn insert(&mut self, val: i32) {\n    self.root = Self::insert_helper(self.root.clone(), val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfn insert_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n            match {\n                let node_val = node.borrow().val;\n                node_val\n            }\n            .cmp(&val)\n            {\n                Ordering::Greater => {\n                    let left = node.borrow().left.clone();\n                    node.borrow_mut().left = Self::insert_helper(left, val);\n                }\n                Ordering::Less => {\n                    let right = node.borrow().right.clone();\n                    node.borrow_mut().right = Self::insert_helper(right, val);\n                }\n                Ordering::Equal => {\n                    return Some(node); // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n                }\n            }\n            Self::update_height(Some(node.clone())); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n\n            /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n            Some(node)\n        }\n        None => Some(TreeNode::new(val)),\n    }\n}\n
avl_tree.c
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(AVLTree *tree, int val) {\n    tree->root = insertHelper(tree->root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f) */\nTreeNode *insertHelper(TreeNode *node, int val) {\n    if (node == NULL) {\n        return newTreeNode(val);\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (val < node->val) {\n        node->left = insertHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = insertHelper(node->right, val);\n    } else {\n        // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        return node;\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.kt
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfun insert(_val: Int) {\n    root = insertHelper(root, _val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfun insertHelper(n: TreeNode?, _val: Int): TreeNode {\n    if (n == null)\n        return TreeNode(_val)\n    var node = n\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b */\n    if (_val < node._val)\n        node.left = insertHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = insertHelper(node.right, _val)\n    else\n        return node // \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    updateHeight(node) // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e ###\ndef rotate(node)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  balance_factor = balance_factor(node)\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return right_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0431\u0445\u043e\u0434\u0430\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return left_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  node\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e ###\ndef rotate(node)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  balance_factor = balance_factor(node)\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return right_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0431\u0445\u043e\u0434\u0430\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return left_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  node\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) ###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    return node\n  end\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  rotate(node)\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#2_1","title":"2. \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430","text":"

\u0410\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e, \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u0438\u0437 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u0441\u043d\u0438\u0437\u0443 \u0432\u0432\u0435\u0440\u0445, \u0447\u0442\u043e\u0431\u044b \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0431\u0430\u043b\u0430\u043d\u0441 \u0432\u0441\u0435\u0445 \u0440\u0430\u0437\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u041a\u043e\u0434 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby avl_tree.py
def remove(self, val: int):\n    \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\"\"\"\n    self._root = self.remove_helper(self._root, val)\n\ndef remove_helper(self, node: TreeNode | None, val: int) -> TreeNode | None:\n    \"\"\"\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434)\"\"\"\n    if node is None:\n        return None\n    # 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e\n    if val < node.val:\n        node.left = self.remove_helper(node.left, val)\n    elif val > node.val:\n        node.right = self.remove_helper(node.right, val)\n    else:\n        if node.left is None or node.right is None:\n            child = node.left or node.right\n            # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if child is None:\n                return None\n            # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else:\n                node = child\n        else:\n            # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            temp = node.right\n            while temp.left is not None:\n                temp = temp.left\n            node.right = self.remove_helper(node.right, temp.val)\n            node.val = temp.val\n    # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    self.update_height(node)\n    # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    return self.rotate(node)\n
avl_tree.cpp
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    if (node == nullptr)\n        return nullptr;\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node->val)\n        node->left = removeHelper(node->left, val);\n    else if (val > node->val)\n        node->right = removeHelper(node->right, val);\n    else {\n        if (node->left == nullptr || node->right == nullptr) {\n            TreeNode *child = node->left != nullptr ? node->left : node->right;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child == nullptr) {\n                delete node;\n                return nullptr;\n            }\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else {\n                delete node;\n                node = child;\n            }\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            TreeNode *temp = node->right;\n            while (temp->left != nullptr) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.java
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int val) {\n    root = removeHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode removeHelper(TreeNode node, int val) {\n    if (node == null)\n        return null;\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node.val)\n        node.left = removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = removeHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode child = node.left != null ? node.left : node.right;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child == null)\n                return null;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else\n                node = child;\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            TreeNode temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.cs
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid Remove(int val) {\n    root = RemoveHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode? RemoveHelper(TreeNode? node, int val) {\n    if (node == null) return null;\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node.val)\n        node.left = RemoveHelper(node.left, val);\n    else if (val > node.val)\n        node.right = RemoveHelper(node.right, val);\n    else {\n        if (node.left == null || node.right == null) {\n            TreeNode? child = node.left ?? node.right;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child == null)\n                return null;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else\n                node = child;\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            TreeNode? temp = node.right;\n            while (temp.left != null) {\n                temp = temp.left;\n            }\n            node.right = RemoveHelper(node.right, temp.val!.Value);\n            node.val = temp.val;\n        }\n    }\n    UpdateHeight(node);  // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = Rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.go
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfunc (t *aVLTree) remove(val int) {\n    t.root = t.removeHelper(t.root, val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f) */\nfunc (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {\n    if node == nil {\n        return nil\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if val < node.Val.(int) {\n        node.Left = t.removeHelper(node.Left, val)\n    } else if val > node.Val.(int) {\n        node.Right = t.removeHelper(node.Right, val)\n    } else {\n        if node.Left == nil || node.Right == nil {\n            child := node.Left\n            if node.Right != nil {\n                child = node.Right\n            }\n            if child == nil {\n                // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n                return nil\n            } else {\n                // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n                node = child\n            }\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            temp := node.Right\n            for temp.Left != nil {\n                temp = temp.Left\n            }\n            node.Right = t.removeHelper(node.Right, temp.Val.(int))\n            node.Val = temp.Val\n        }\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    t.updateHeight(node)\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = t.rotate(node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n
avl_tree.swift
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfunc remove(val: Int) {\n    root = removeHelper(node: root, val: val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfunc removeHelper(node: TreeNode?, val: Int) -> TreeNode? {\n    var node = node\n    if node == nil {\n        return nil\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if val < node!.val {\n        node?.left = removeHelper(node: node?.left, val: val)\n    } else if val > node!.val {\n        node?.right = removeHelper(node: node?.right, val: val)\n    } else {\n        if node?.left == nil || node?.right == nil {\n            let child = node?.left ?? node?.right\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if child == nil {\n                return nil\n            }\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else {\n                node = child\n            }\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            var temp = node?.right\n            while temp?.left != nil {\n                temp = temp?.left\n            }\n            node?.right = removeHelper(node: node?.right, val: temp!.val)\n            node?.val = temp!.val\n        }\n    }\n    updateHeight(node: node) // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node: node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n
avl_tree.js
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nremove(val) {\n    this.root = this.#removeHelper(this.root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\n#removeHelper(node, val) {\n    if (node === null) return null;\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node.val) node.left = this.#removeHelper(node.left, val);\n    else if (val > node.val)\n        node.right = this.#removeHelper(node.right, val);\n    else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child === null) return null;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else node = child;\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.#removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.#updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = this.#rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.ts
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nremove(val: number): void {\n    this.root = this.removeHelper(this.root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nremoveHelper(node: TreeNode, val: number): TreeNode {\n    if (node === null) return null;\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node.val) {\n        node.left = this.removeHelper(node.left, val);\n    } else if (val > node.val) {\n        node.right = this.removeHelper(node.right, val);\n    } else {\n        if (node.left === null || node.right === null) {\n            const child = node.left !== null ? node.left : node.right;\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child === null) {\n                return null;\n            } else {\n                // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n                node = child;\n            }\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            let temp = node.right;\n            while (temp.left !== null) {\n                temp = temp.left;\n            }\n            node.right = this.removeHelper(node.right, temp.val);\n            node.val = temp.val;\n        }\n    }\n    this.updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = this.rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.dart
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int val) {\n  root = removeHelper(root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nTreeNode? removeHelper(TreeNode? node, int val) {\n  if (node == null) return null;\n  /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n  if (val < node.val)\n    node.left = removeHelper(node.left, val);\n  else if (val > node.val)\n    node.right = removeHelper(node.right, val);\n  else {\n    if (node.left == null || node.right == null) {\n      TreeNode? child = node.left ?? node.right;\n      // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n      if (child == null)\n        return null;\n      // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n      else\n        node = child;\n    } else {\n      // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n      TreeNode? temp = node.right;\n      while (temp!.left != null) {\n        temp = temp.left;\n      }\n      node.right = removeHelper(node.right, temp.val);\n      node.val = temp.val;\n    }\n  }\n  updateHeight(node); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n  node = rotate(node);\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  return node;\n}\n
avl_tree.rs
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfn remove(&self, val: i32) {\n    Self::remove_helper(self.root.clone(), val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfn remove_helper(node: OptionTreeNodeRc, val: i32) -> OptionTreeNodeRc {\n    match node {\n        Some(mut node) => {\n            /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n            if val < node.borrow().val {\n                let left = node.borrow().left.clone();\n                node.borrow_mut().left = Self::remove_helper(left, val);\n            } else if val > node.borrow().val {\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, val);\n            } else if node.borrow().left.is_none() || node.borrow().right.is_none() {\n                let child = if node.borrow().left.is_some() {\n                    node.borrow().left.clone()\n                } else {\n                    node.borrow().right.clone()\n                };\n                match child {\n                    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n                    None => {\n                        return None;\n                    }\n                    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n                    Some(child) => node = child,\n                }\n            } else {\n                // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n                let mut temp = node.borrow().right.clone().unwrap();\n                loop {\n                    let temp_left = temp.borrow().left.clone();\n                    if temp_left.is_none() {\n                        break;\n                    }\n                    temp = temp_left.unwrap();\n                }\n                let right = node.borrow().right.clone();\n                node.borrow_mut().right = Self::remove_helper(right, temp.borrow().val);\n                node.borrow_mut().val = temp.borrow().val;\n            }\n            Self::update_height(Some(node.clone())); // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n\n            /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n            node = Self::rotate(Some(node)).unwrap();\n            // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n            Some(node)\n        }\n        None => None,\n    }\n}\n
avl_tree.c
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\n// \u0418\u0437-\u0437\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f stdio.h \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e remove\nvoid removeItem(AVLTree *tree, int val) {\n    TreeNode *root = removeHelper(tree->root, val);\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f) */\nTreeNode *removeHelper(TreeNode *node, int val) {\n    TreeNode *child, *grandChild;\n    if (node == NULL) {\n        return NULL;\n    }\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (val < node->val) {\n        node->left = removeHelper(node->left, val);\n    } else if (val > node->val) {\n        node->right = removeHelper(node->right, val);\n    } else {\n        if (node->left == NULL || node->right == NULL) {\n            child = node->left;\n            if (node->right != NULL) {\n                child = node->right;\n            }\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child == NULL) {\n                return NULL;\n            } else {\n                // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n                node = child;\n            }\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            TreeNode *temp = node->right;\n            while (temp->left != NULL) {\n                temp = temp->left;\n            }\n            int tempVal = temp->val;\n            node->right = removeHelper(node->right, temp->val);\n            node->val = tempVal;\n        }\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    updateHeight(node);\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node);\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node;\n}\n
avl_tree.kt
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfun remove(_val: Int) {\n    root = removeHelper(root, _val)\n}\n\n/* \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) */\nfun removeHelper(n: TreeNode?, _val: Int): TreeNode? {\n    var node = n ?: return null\n    /* 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e */\n    if (_val < node._val)\n        node.left = removeHelper(node.left, _val)\n    else if (_val > node._val)\n        node.right = removeHelper(node.right, _val)\n    else {\n        if (node.left == null || node.right == null) {\n            val child = if (node.left != null)\n                node.left\n            else\n                node.right\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            if (child == null)\n                return null\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n            else\n                node = child\n        } else {\n            // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n            var temp = node.right\n            while (temp!!.left != null) {\n                temp = temp.left\n            }\n            node.right = removeHelper(node.right, temp._val)\n            node._val = temp._val\n        }\n    }\n    updateHeight(node) // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n    /* 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e */\n    node = rotate(node)\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n    return node\n}\n

```ruby title=\"avl_tree.rb\"

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e ###\ndef rotate(node)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  balance_factor = balance_factor(node)\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return right_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0431\u0445\u043e\u0434\u0430\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return left_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  node\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) ###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    return node\n  end\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  rotate(node)\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n

=begin File: avl_tree.rb Created Time: 2024-04-17 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## AVL-\u0434\u0435\u0440\u0435\u0432\u043e ### class AVLTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 -1, \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 0\n  return node.height unless node.nil?\n\n  -1\nend\n\n# ## \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430 ###\ndef update_height(node)\n  # \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 + 1\n  node.height = [height(node.left), height(node.right)].max + 1\nend\n\n# ## \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 ###\ndef balance_factor(node)\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u043f\u0443\u0441\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u0435\u043d 0\n  return 0 if node.nil?\n\n  # \u041a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 = \u0432\u044b\u0441\u043e\u0442\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 - \u0432\u044b\u0441\u043e\u0442\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430\n  height(node.left) - height(node.right)\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef right_rotate(node)\n  child = node.left\n  grand_child = child.right\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.right = node\n  node.left = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043b\u0435\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f ###\ndef left_rotate(node)\n  child = node.right\n  grand_child = child.left\n  # \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 node \u0432\u043e\u043a\u0440\u0443\u0433 child\n  child.left = node\n  node.right = grand_child\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  update_height(child)\n  # \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f\n  child\nend\n\n# ## \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e ###\ndef rotate(node)\n  # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u044d\u0444\u0444\u0438\u0446\u0438\u0435\u043d\u0442 \u0431\u0430\u043b\u0430\u043d\u0441\u0430 \u0443\u0437\u043b\u0430 node\n  balance_factor = balance_factor(node)\n  # \u041e\u0431\u043e\u0439\u0442\u0438 \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  if balance_factor > 1\n    if balance_factor(node.left) >= 0\n      # \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return right_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043f\u0440\u0430\u0432\u043e\u0435\n      node.left = left_rotate(node.left)\n      return right_rotate(node)\n    end\n  # \u041f\u0440\u0430\u0432\u043e\u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043e\u0431\u0445\u043e\u0434\u0430\n  elsif balance_factor < -1\n    if balance_factor(node.right) <= 0\n      # \u041b\u0435\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\n      return left_rotate(node)\n    else\n      # \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043b\u0435\u0432\u043e\u0435\n      node.right = right_rotate(node.right)\n      return left_rotate(node)\n    end\n  end\n  # \u0414\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443\n  node\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(val)\n  @root = insert_helper(@root, val)\nend\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) ###\ndef insert_helper(node, val)\n  return TreeNode.new(val) if node.nil?\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b\n  if val < node.val\n    node.left = insert_helper(node.left, val)\n  elsif val > node.val\n    node.right = insert_helper(node.right, val)\n  else\n    # \u041f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u043d\u0435 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    return node\n  end\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  rotate(node)\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 ###\ndef remove(val)\n  @root = remove_helper(@root, val)\nend\n\n# ## \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 (\u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434) ###\ndef remove_helper(node, val)\n  return if node.nil?\n  # 1. \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0438 \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e\n  if val < node.val\n    node.left = remove_helper(node.left, val)\n  elsif val > node.val\n    node.right = remove_helper(node.right, val)\n  else\n    if node.left.nil? || node.right.nil?\n      child = node.left || node.right\n      # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n      return if child.nil?\n      # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 1, \u0443\u0434\u0430\u043b\u0438\u0442\u044c node \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e\n      node = child\n    else\n      # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2, \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u0443\u0437\u0435\u043b \u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b\n      temp = node.right\n      while !temp.left.nil?\n        temp = temp.left\n      end\n      node.right = remove_helper(node.right, temp.val)\n      node.val = temp.val\n    end\n  end\n  # \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0432\u044b\u0441\u043e\u0442\u0443 \u0443\u0437\u043b\u0430\n  update_height(node)\n  # 2. \u0412\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0441\u043d\u043e\u0432\u0430 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  rotate(node)\nend\n```\n
"},{"location":"chapter_tree/avl_tree/#3_1","title":"3. \u00a0 \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430","text":"

\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u0437\u043b\u0430 \u0432 AVL-\u0434\u0435\u0440\u0435\u0432\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0434\u0435\u0441\u044c \u043e\u043d\u0430 \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e \u043d\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f.

"},{"location":"chapter_tree/avl_tree/#754-avl-","title":"7.5.4 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f AVL-\u0434\u0435\u0440\u0435\u0432\u0430","text":""},{"location":"chapter_tree/binary_search_tree/","title":"7.4 \u00a0 \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-16, \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 (binary search tree) \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c.

  1. \u0414\u043b\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 \u043c\u0435\u043d\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0430 \u0432\u0441\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430.
  2. \u041b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0442\u0430\u043a\u0436\u0435 \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0442\u043e\u0436\u0435 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u044e\u0442 \u0443\u0441\u043b\u043e\u0432\u0438\u044e 1. .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-16 \u00a0 \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430

"},{"location":"chapter_tree/binary_search_tree/#741","title":"7.4.1 \u00a0 \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0430","text":"

\u041c\u044b \u0438\u043d\u043a\u0430\u043f\u0441\u0443\u043b\u0438\u0440\u0443\u0435\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u043a\u043b\u0430\u0441\u0441 BinarySearchTree \u0438 \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e-\u0447\u043b\u0435\u043d root , \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0434\u0435\u0440\u0435\u0432\u0430.

"},{"location":"chapter_tree/binary_search_tree/#1","title":"1. \u00a0 \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430","text":"

\u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0446\u0435\u043b\u0435\u0432\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 num \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u043e\u0438\u0441\u043a, \u043e\u043f\u0438\u0440\u0430\u044f\u0441\u044c \u043d\u0430 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435, \u043c\u044b \u043e\u0431\u044a\u044f\u0432\u043b\u044f\u0435\u043c \u0443\u0437\u0435\u043b cur , \u0441\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u0435\u0440\u0435\u0432\u0430 root \u0438 \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f cur.val \u0438 num .

<1><2><3><4>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-17 \u00a0 \u041f\u0440\u0438\u043c\u0435\u0440 \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u0437\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430

\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043e \u0442\u043e\u043c\u0443 \u0436\u0435 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0443, \u0447\u0442\u043e \u0438 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a: \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043e\u043d\u0430 \u043e\u0442\u0431\u0440\u0430\u0441\u044b\u0432\u0430\u0435\u0442 \u043f\u043e\u043b\u043e\u0432\u0438\u043d\u0443 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432. \u0427\u0438\u0441\u043b\u043e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0435 \u043f\u0440\u0435\u0432\u043e\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u044b\u0441\u043e\u0442\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0430 \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043e, \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \\(O(\\log n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
def search(self, num: int) -> TreeNode | None:\n    \"\"\"\u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430\"\"\"\n    cur = self._root\n    # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while cur is not None:\n        # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur.val < num:\n            cur = cur.right\n        # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        elif cur.val > num:\n            cur = cur.left\n        # \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else:\n            break\n    return cur\n
binary_search_tree.cpp
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nTreeNode *search(int num) {\n    TreeNode *cur = root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != nullptr) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur->val < num)\n            cur = cur->right;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur->val > num)\n            cur = cur->left;\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else\n            break;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.java
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nTreeNode search(int num) {\n    TreeNode cur = root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num)\n            cur = cur.right;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else\n            break;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.cs
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nTreeNode? Search(int num) {\n    TreeNode? cur = root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur =\n            cur.right;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur.val > num)\n            cur = cur.left;\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else\n            break;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.go
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nfunc (bst *binarySearchTree) search(num int) *TreeNode {\n    node := bst.root\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    for node != nil {\n        if node.Val.(int) < num {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            node = node.Right\n        } else if node.Val.(int) > num {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            node = node.Left\n        } else {\n            // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return node\n}\n
binary_search_tree.swift
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nfunc search(num: Int) -> TreeNode? {\n    var cur = root\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while cur != nil {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if cur!.val > num {\n            cur = cur?.left\n        }\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else {\n            break\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur\n}\n
binary_search_tree.js
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nsearch(num) {\n    let cur = this.root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur.val > num) cur = cur.left;\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else break;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.ts
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nsearch(num: number): TreeNode | null {\n    let cur = this.root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur.val > num) cur = cur.left;\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else break;\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.dart
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nTreeNode? search(int _num) {\n  TreeNode? cur = _root;\n  // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while (cur != null) {\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else if (cur.val > _num)\n      cur = cur.left;\n    // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    else\n      break;\n  }\n  // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  return cur;\n}\n
binary_search_tree.rs
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\npub fn search(&self, num: i32) -> OptionTreeNodeRc {\n    let mut cur = self.root.clone();\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Greater => cur = node.borrow().right.clone(),\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Less => cur = node.borrow().left.clone(),\n            // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            Ordering::Equal => break,\n        }\n    }\n\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    cur\n}\n
binary_search_tree.c
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nTreeNode *search(BinarySearchTree *bst, int num) {\n    TreeNode *cur = bst->root;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != NULL) {\n        if (cur->val < num) {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            cur = cur->right;\n        } else if (cur->val > num) {\n            // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            cur = cur->left;\n        } else {\n            // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            break;\n        }\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur;\n}\n
binary_search_tree.kt
/* \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 */\nfun search(num: Int): TreeNode? {\n    var cur = root\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        cur = if (cur._val < num)\n            cur.right\n        // \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else if (cur._val > num)\n            cur.left\n        // \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        else\n            break\n    }\n    // \u0412\u0435\u0440\u043d\u0443\u0442\u044c \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    return cur\n}\n

```ruby title=\"binary_search_tree.rb\"

=begin File: binary_search_tree.rb Created Time: 2024-04-18 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 ### class BinarySearchTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 ###\ndef search(num)\n  cur = @root\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while !cur.nil?\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    elsif cur.val > num\n      cur = cur.left\n    # \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    else\n      break\n    end\n  end\n\n  cur\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_tree/binary_search_tree/#2","title":"2. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430","text":"

\u041f\u0443\u0441\u0442\u044c \u0434\u0430\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442 num , \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0443\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \"\u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e < \u043a\u043e\u0440\u0435\u043d\u044c < \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\", \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.

  1. \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438: \u043a\u0430\u043a \u0438 \u0432 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u043e\u0438\u0441\u043a\u0430, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u043e\u0442 \u043a\u043e\u0440\u043d\u044f, \u043c\u044b \u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043f\u0443\u0441\u043a\u0430\u0435\u043c\u0441\u044f \u0432\u043d\u0438\u0437 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0442\u0435\u043a\u0443\u0449\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0443\u0437\u043b\u0430 \u0438 num , \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u043c \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b (\u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0435 \u0434\u043e\u0439\u0434\u0435\u043c \u0434\u043e None ).
  2. \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b \u0432 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u0443\u044e \u043f\u043e\u0437\u0438\u0446\u0438\u044e: \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u0437\u0435\u043b num \u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430 \u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e None .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-18 \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430

\u0412 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u043e\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0432\u0430 \u043c\u043e\u043c\u0435\u043d\u0442\u0430.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
def insert(self, num: int):\n    \"\"\"\u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if self._root is None:\n        self._root = TreeNode(num)\n        return\n    # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if cur.val == num:\n            return\n        pre = cur\n        # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur.val < num:\n            cur = cur.right\n        # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else:\n            cur = cur.left\n    # \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    node = TreeNode(num)\n    if pre.val < num:\n        pre.right = node\n    else:\n        pre.left = node\n
binary_search_tree.cpp
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (root == nullptr) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode *cur = root, *pre = nullptr;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != nullptr) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur->val == num)\n            return;\n        pre = cur;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur->val < num)\n            cur = cur->right;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur->left;\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    TreeNode *node = new TreeNode(num);\n    if (pre->val < num)\n        pre->right = node;\n    else\n        pre->left = node;\n}\n
binary_search_tree.java
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode cur = root, pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num)\n            cur = cur.right;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur.left;\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    TreeNode node = new TreeNode(num);\n    if (pre.val < num)\n        pre.right = node;\n    else\n        pre.left = node;\n}\n
binary_search_tree.cs
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid Insert(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (root == null) {\n        root = new TreeNode(num);\n        return;\n    }\n    TreeNode? cur = root, pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur.val == num)\n            return;\n        pre = cur;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num)\n            cur = cur.right;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur.left;\n    }\n\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    TreeNode node = new(num);\n    if (pre != null) {\n        if (pre.val < num)\n            pre.right = node;\n        else\n            pre.left = node;\n    }\n}\n
binary_search_tree.go
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfunc (bst *binarySearchTree) insert(num int) {\n    cur := bst.root\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if cur == nil {\n        bst.root = NewTreeNode(num)\n        return\n    }\n    // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0443\u0437\u043b\u0430, \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u043e\u043c\u0443\n    var pre *TreeNode = nil\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    for cur != nil {\n        if cur.Val == num {\n            return\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            cur = cur.Right\n        } else {\n            cur = cur.Left\n        }\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    node := NewTreeNode(num)\n    if pre.Val.(int) < num {\n        pre.Right = node\n    } else {\n        pre.Left = node\n    }\n}\n
binary_search_tree.swift
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfunc insert(num: Int) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if root == nil {\n        root = TreeNode(x: num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while cur != nil {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if cur!.val == num {\n            return\n        }\n        pre = cur\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    let node = TreeNode(x: num)\n    if pre!.val < num {\n        pre?.right = node\n    } else {\n        pre?.left = node\n    }\n}\n
binary_search_tree.js
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\ninsert(num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur = this.root,\n        pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur.val === num) return;\n        pre = cur;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else cur = cur.left;\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    const node = new TreeNode(num);\n    if (pre.val < num) pre.right = node;\n    else pre.left = node;\n}\n
binary_search_tree.ts
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\ninsert(num: number): void {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (this.root === null) {\n        this.root = new TreeNode(num);\n        return;\n    }\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur.val === num) return;\n        pre = cur;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else cur = cur.left;\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    const node = new TreeNode(num);\n    if (pre!.val < num) pre!.right = node;\n    else pre!.left = node;\n}\n
binary_search_tree.dart
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(int _num) {\n  // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  if (_root == null) {\n    _root = TreeNode(_num);\n    return;\n  }\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while (cur != null) {\n    // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur.val == _num) return;\n    pre = cur;\n    // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else\n      cur = cur.left;\n  }\n  // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n  TreeNode? node = TreeNode(_num);\n  if (pre!.val < _num)\n    pre.right = node;\n  else\n    pre.left = node;\n}\n
binary_search_tree.rs
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\npub fn insert(&mut self, num: i32) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if self.root.is_none() {\n        self.root = Some(TreeNode::new(num));\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n            Ordering::Equal => return,\n            // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    let pre = pre.unwrap();\n    let node = Some(TreeNode::new(num));\n    if num > pre.borrow().val {\n        pre.borrow_mut().right = node;\n    } else {\n        pre.borrow_mut().left = node;\n    }\n}\n
binary_search_tree.c
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nvoid insert(BinarySearchTree *bst, int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (bst->root == NULL) {\n        bst->root = newTreeNode(num);\n        return;\n    }\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != NULL) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur->val == num) {\n            return;\n        }\n        pre = cur;\n        if (cur->val < num) {\n            // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            cur = cur->right;\n        } else {\n            // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            cur = cur->left;\n        }\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    TreeNode *node = newTreeNode(num);\n    if (pre->val < num) {\n        pre->right = node;\n    } else {\n        pre->left = node;\n    }\n}\n
binary_search_tree.kt
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 */\nfun insert(num: Int) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    if (root == null) {\n        root = TreeNode(num)\n        return\n    }\n    var cur = root\n    var pre: TreeNode? = null\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n        if (cur._val == num)\n            return\n        pre = cur\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        cur = if (cur._val < num)\n            cur.right\n        // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur.left\n    }\n    // \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n    val node = TreeNode(num)\n    if (pre?._val!! < num)\n        pre.right = node\n    else\n        pre.left = node\n}\n

```ruby title=\"binary_search_tree.rb\"

=begin File: binary_search_tree.rb Created Time: 2024-04-18 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 ### class BinarySearchTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 ###\ndef search(num)\n  cur = @root\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while !cur.nil?\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    elsif cur.val > num\n      cur = cur.left\n    # \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    else\n      break\n    end\n  end\n\n  cur\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(num)\n  # \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    return if cur.val == num\n\n    pre = cur\n    # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else\n      cur = cur.left\n    end\n  end\n\n  # \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

\u041a\u0430\u043a \u0438 \u043f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430, \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(\\log n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438.

"},{"location":"chapter_tree/binary_search_tree/#3","title":"3. \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430","text":"

\u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b, \u0430 \u0437\u0430\u0442\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e. \u041a\u0430\u043a \u0438 \u043f\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0435, \u043f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430: \"\u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e < \u043a\u043e\u0440\u0435\u043d\u044c < \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\". \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0447\u0438\u0441\u043b\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0443 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0441\u043e \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e 0, 1 \u0438 2, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0437\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f.

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-19, \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438\u0441\u0442\u043e\u043c \u0438 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0443\u0434\u0430\u043b\u0435\u043d \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-19 \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 (\u0441\u0442\u0435\u043f\u0435\u043d\u044c 0)

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-20, \u043a\u043e\u0433\u0434\u0430 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \\(1\\) , \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u0435\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u0443\u0437\u043b\u043e\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-20 \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 (\u0441\u0442\u0435\u043f\u0435\u043d\u044c 1)

\u041a\u043e\u0433\u0434\u0430 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0440\u0430\u0432\u043d\u0430 \\(2\\) , \u043c\u044b \u0443\u0436\u0435 \u043d\u0435 \u043c\u043e\u0436\u0435\u043c \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0435\u0433\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u0438 \u0434\u043e\u043b\u0436\u043d\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u0437\u0430\u043c\u0435\u043d\u044b \u0434\u0440\u0443\u0433\u043e\u0439 \u0443\u0437\u0435\u043b. \u0427\u0442\u043e\u0431\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \"\u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e \\(<\\) \u043a\u043e\u0440\u0435\u043d\u044c \\(<\\) \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\", \u044d\u0442\u0438\u043c \u0443\u0437\u043b\u043e\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u0438\u043b\u0438 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430.

\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u043c\u044b \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435. \u0422\u043e\u0433\u0434\u0430 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a.

  1. \u041d\u0430\u0439\u0442\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \"\u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430\" \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u0435\u0433\u043e \u043a\u0430\u043a tmp .
  2. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp \u0438\u0437 \u0434\u0435\u0440\u0435\u0432\u0430.
<1><2><3><4>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-21 \u00a0 \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 (\u0441\u0442\u0435\u043f\u0435\u043d\u044c 2)

\u041e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u0442\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(\\log n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0433\u0434\u0435 \u043f\u043e\u0438\u0441\u043a \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u0442\u043e\u0438\u0442 \\(O(\\log n)\\) , \u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0442\u0430\u043a\u0436\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \\(O(\\log n)\\) . \u041f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0434\u0430 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_search_tree.py
def remove(self, num: int):\n    \"\"\"\u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\"\"\"\n    # \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if self._root is None:\n        return\n    # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    cur, pre = self._root, None\n    while cur is not None:\n        # \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if cur.val == num:\n            break\n        pre = cur\n        # \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur.val < num:\n            cur = cur.right\n        # \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else:\n            cur = cur.left\n    # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if cur is None:\n        return\n\n    # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if cur.left is None or cur.right is None:\n        # \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        child = cur.left or cur.right\n        # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if cur != self._root:\n            if pre.left == cur:\n                pre.left = child\n            else:\n                pre.right = child\n        else:\n            # \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            self._root = child\n    # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else:\n        # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        tmp: TreeNode = cur.right\n        while tmp.left is not None:\n            tmp = tmp.left\n        # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        self.remove(tmp.val)\n        # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.val = tmp.val\n
binary_search_tree.cpp
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (root == nullptr)\n        return;\n    TreeNode *cur = root, *pre = nullptr;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != nullptr) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur->val == num)\n            break;\n        pre = cur;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur->val < num)\n            cur = cur->right;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur->left;\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur == nullptr)\n        return;\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur->left == nullptr || cur->right == nullptr) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = nullptr / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        TreeNode *child = cur->left != nullptr ? cur->left : cur->right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur != root) {\n            if (pre->left == cur)\n                pre->left = child;\n            else\n                pre->right = child;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            root = child;\n        }\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        delete cur;\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        TreeNode *tmp = cur->right;\n        while (tmp->left != nullptr) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        remove(tmp->val);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur->val = tmpVal;\n    }\n}\n
binary_search_tree.java
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (root == null)\n        return;\n    TreeNode cur = root, pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num)\n            cur = cur.right;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur.left;\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur == null)\n        return;\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur.left == null || cur.right == null) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        TreeNode child = cur.left != null ? cur.left : cur.right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur != root) {\n            if (pre.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            root = child;\n        }\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        TreeNode tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        remove(tmp.val);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.val = tmp.val;\n    }\n}\n
binary_search_tree.cs
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid Remove(int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (root == null)\n        return;\n    TreeNode? cur = root, pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur.val == num)\n            break;\n        pre = cur;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num)\n            cur = cur.right;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur = cur.left;\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur == null)\n        return;\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur.left == null || cur.right == null) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        TreeNode? child = cur.left ?? cur.right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur != root) {\n            if (pre!.left == cur)\n                pre.left = child;\n            else\n                pre.right = child;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            root = child;\n        }\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        TreeNode? tmp = cur.right;\n        while (tmp.left != null) {\n            tmp = tmp.left;\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        Remove(tmp.val!.Value);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.val = tmp.val;\n    }\n}\n
binary_search_tree.go
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfunc (bst *binarySearchTree) remove(num int) {\n    cur := bst.root\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if cur == nil {\n        return\n    }\n    // \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0443\u0437\u043b\u0430, \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u043c\u0443\n    var pre *TreeNode = nil\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    for cur != nil {\n        if cur.Val == num {\n            break\n        }\n        pre = cur\n        if cur.Val.(int) < num {\n            // \u0423\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435\n            cur = cur.Right\n        } else {\n            // \u0423\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435\n            cur = cur.Left\n        }\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if cur == nil {\n        return\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u043e 0 \u0438\u043b\u0438 1\n    if cur.Left == nil || cur.Right == nil {\n        var child *TreeNode = nil\n        // \u0418\u0437\u0432\u043b\u0435\u0447\u044c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        if cur.Left != nil {\n            child = cur.Left\n        } else {\n            child = cur.Right\n        }\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if cur != bst.root {\n            if pre.Left == cur {\n                pre.Left = child\n            } else {\n                pre.Right = child\n            }\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            bst.root = child\n        }\n        // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u043e 2\n    } else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0441\u043b\u0435 cur \u0443\u0437\u0435\u043b \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435 \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n        tmp := cur.Right\n        for tmp.Left != nil {\n            tmp = tmp.Left\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        bst.remove(tmp.Val.(int))\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.Val = tmp.Val\n    }\n}\n
binary_search_tree.swift
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfunc remove(num: Int) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if root == nil {\n        return\n    }\n    var cur = root\n    var pre: TreeNode?\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while cur != nil {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if cur!.val == num {\n            break\n        }\n        pre = cur\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if cur!.val < num {\n            cur = cur?.right\n        }\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else {\n            cur = cur?.left\n        }\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if cur == nil {\n        return\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if cur?.left == nil || cur?.right == nil {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        let child = cur?.left ?? cur?.right\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if cur !== root {\n            if pre?.left === cur {\n                pre?.left = child\n            } else {\n                pre?.right = child\n            }\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            root = child\n        }\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        var tmp = cur?.right\n        while tmp?.left != nil {\n            tmp = tmp?.left\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        remove(num: tmp!.val)\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur?.val = tmp!.val\n    }\n}\n
binary_search_tree.js
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nremove(num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (this.root === null) return;\n    let cur = this.root,\n        pre = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur.val === num) break;\n        pre = cur;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else cur = cur.left;\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur === null) return;\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur.left === null || cur.right === null) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        const child = cur.left !== null ? cur.left : cur.right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur !== this.root) {\n            if (pre.left === cur) pre.left = child;\n            else pre.right = child;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            this.root = child;\n        }\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        let tmp = cur.right;\n        while (tmp.left !== null) {\n            tmp = tmp.left;\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        this.remove(tmp.val);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.val = tmp.val;\n    }\n}\n
binary_search_tree.ts
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nremove(num: number): void {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (this.root === null) return;\n    let cur: TreeNode | null = this.root,\n        pre: TreeNode | null = null;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur !== null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur.val === num) break;\n        pre = cur;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        if (cur.val < num) cur = cur.right;\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else cur = cur.left;\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur === null) return;\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur.left === null || cur.right === null) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        const child: TreeNode | null =\n            cur.left !== null ? cur.left : cur.right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur !== this.root) {\n            if (pre!.left === cur) pre!.left = child;\n            else pre!.right = child;\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            this.root = child;\n        }\n    }\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        let tmp: TreeNode | null = cur.right;\n        while (tmp!.left !== null) {\n            tmp = tmp!.left;\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        this.remove(tmp!.val);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur.val = tmp!.val;\n    }\n}\n
binary_search_tree.dart
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nvoid remove(int _num) {\n  // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n  if (_root == null) return;\n  TreeNode? cur = _root;\n  TreeNode? pre = null;\n  // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while (cur != null) {\n    // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    if (cur.val == _num) break;\n    pre = cur;\n    // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if (cur.val < _num)\n      cur = cur.right;\n    // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else\n      cur = cur.left;\n  }\n  // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u043d\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n  if (cur == null) return;\n  // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n  if (cur.left == null || cur.right == null) {\n    // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    TreeNode? child = cur.left ?? cur.right;\n    // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n    if (cur != _root) {\n      if (pre!.left == cur)\n        pre.left = child;\n      else\n        pre.right = child;\n    } else {\n      // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n      _root = child;\n    }\n  } else {\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n    TreeNode? tmp = cur.right;\n    while (tmp!.left != null) {\n      tmp = tmp.left;\n    }\n    // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n    remove(tmp.val);\n    // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n    cur.val = tmp.val;\n  }\n}\n
binary_search_tree.rs
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\npub fn remove(&mut self, num: i32) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if self.root.is_none() {\n        return;\n    }\n    let mut cur = self.root.clone();\n    let mut pre = None;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while let Some(node) = cur.clone() {\n        match num.cmp(&node.borrow().val) {\n            // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n            Ordering::Equal => break,\n            // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Greater => {\n                pre = cur.clone();\n                cur = node.borrow().right.clone();\n            }\n            // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n            Ordering::Less => {\n                pre = cur.clone();\n                cur = node.borrow().left.clone();\n            }\n        }\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if cur.is_none() {\n        return;\n    }\n    let cur = cur.unwrap();\n    let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());\n    match (left_child.clone(), right_child.clone()) {\n        // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n        (None, None) | (Some(_), None) | (None, Some(_)) => {\n            // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = nullptr / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n            let child = left_child.or(right_child);\n            let pre = pre.unwrap();\n            // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n            if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {\n                let left = pre.borrow().left.clone();\n                if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {\n                    pre.borrow_mut().left = child;\n                } else {\n                    pre.borrow_mut().right = child;\n                }\n            } else {\n                // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n                self.root = child;\n            }\n        }\n        // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n        (Some(_), Some(_)) => {\n            // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n            let mut tmp = cur.borrow().right.clone();\n            while let Some(node) = tmp.clone() {\n                if node.borrow().left.is_some() {\n                    tmp = node.borrow().left.clone();\n                } else {\n                    break;\n                }\n            }\n            let tmp_val = tmp.unwrap().borrow().val;\n            // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n            self.remove(tmp_val);\n            // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n            cur.borrow_mut().val = tmp_val;\n        }\n    }\n}\n
binary_search_tree.c
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\n// \u0418\u0437-\u0437\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f stdio.h \u0437\u0434\u0435\u0441\u044c \u043d\u0435\u043b\u044c\u0437\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e remove\nvoid removeItem(BinarySearchTree *bst, int num) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (bst->root == NULL)\n        return;\n    TreeNode *cur = bst->root, *pre = NULL;\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != NULL) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur->val == num)\n            break;\n        pre = cur;\n        if (cur->val < num) {\n            // \u0423\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 root\n            cur = cur->right;\n        } else {\n            // \u0423\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 root\n            cur = cur->left;\n        }\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur == NULL)\n        return;\n    // \u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0435\u0441\u0442\u044c \u043b\u0438 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0443\u0437\u043b\u044b \u0443 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0433\u043e \u0443\u0437\u043b\u0430\n    if (cur->left == NULL || cur->right == NULL) {\n        /* \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1 */\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = nullptr / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        TreeNode *child = cur->left != NULL ? cur->left : cur->right;\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (pre->left == cur) {\n            pre->left = child;\n        } else {\n            pre->right = child;\n        }\n        // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\n        free(cur);\n    } else {\n        /* \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2 */\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        TreeNode *tmp = cur->right;\n        while (tmp->left != NULL) {\n            tmp = tmp->left;\n        }\n        int tmpVal = tmp->val;\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        removeItem(bst, tmp->val);\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur->val = tmpVal;\n    }\n}\n
binary_search_tree.kt
/* \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 */\nfun remove(num: Int) {\n    // \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (root == null)\n        return\n    var cur = root\n    var pre: TreeNode? = null\n    // \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    while (cur != null) {\n        // \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n        if (cur._val == num)\n            break\n        pre = cur\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        cur = if (cur._val < num)\n            cur.right\n        // \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n        else\n            cur.left\n    }\n    // \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    if (cur == null)\n        return\n    // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n    if (cur.left == null || cur.right == null) {\n        // \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n        val child = if (cur.left != null)\n            cur.left\n        else\n            cur.right\n        // \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n        if (cur != root) {\n            if (pre!!.left == cur)\n                pre.left = child\n            else\n                pre.right = child\n        } else {\n            // \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n            root = child\n        }\n        // \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n    } else {\n        // \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n        var tmp = cur.right\n        while (tmp!!.left != null) {\n            tmp = tmp.left\n        }\n        // \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n        remove(tmp._val)\n        // \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n        cur._val = tmp._val\n    }\n}\n

```ruby title=\"binary_search_tree.rb\"

=begin File: binary_search_tree.rb Created Time: 2024-04-18 Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com) =end

require_relative '../utils/tree_node' require_relative '../utils/print_util'

# ## \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 ### class BinarySearchTree # ## \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 ### def initialize # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0443\u0441\u0442\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e @root = nil end

# ## \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\ndef get_root\n  @root\nend\n\n# ## \u041f\u043e\u0438\u0441\u043a \u0443\u0437\u043b\u0430 ###\ndef search(num)\n  cur = @root\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  while !cur.nil?\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u0426\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    elsif cur.val > num\n      cur = cur.left\n    # \u041d\u0430\u0439\u0442\u0438 \u0446\u0435\u043b\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    else\n      break\n    end\n  end\n\n  cur\nend\n\n# ## \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 ###\ndef insert(num)\n  # \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  if @root.nil?\n    @root = TreeNode.new(num)\n    return\n  end\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u041d\u0430\u0439\u0442\u0438 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0443\u0437\u0435\u043b \u0438 \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n    return if cur.val == num\n\n    pre = cur\n    # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u041f\u043e\u0437\u0438\u0446\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else\n      cur = cur.left\n    end\n  end\n\n  # \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430\n  node = TreeNode.new(num)\n  if pre.val < num\n    pre.right = node\n  else\n    pre.left = node\n  end\nend\n\n# ## \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 ###\ndef remove(num)\n  # \u0415\u0441\u043b\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0443\u0441\u0442\u043e, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n  return if @root.nil?\n\n  # \u0418\u0441\u043a\u0430\u0442\u044c \u0432 \u0446\u0438\u043a\u043b\u0435 \u0438 \u0432\u044b\u0439\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0430 \u0437\u0430 \u043b\u0438\u0441\u0442\u043e\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  cur, pre = @root, nil\n  while !cur.nil?\n    # \u041d\u0430\u0439\u0442\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0438 \u0432\u044b\u0439\u0442\u0438 \u0438\u0437 \u0446\u0438\u043a\u043b\u0430\n    break if cur.val == num\n\n    pre = cur\n    # \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0440\u0430\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    if cur.val < num\n      cur = cur.right\n    # \u0423\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043b\u0435\u0432\u043e\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0435 cur\n    else\n      cur = cur.left\n    end\n  end\n  # \u0415\u0441\u043b\u0438 \u0443\u0437\u0435\u043b \u0434\u043b\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442, \u0441\u0440\u0430\u0437\u0443 \u0432\u0435\u0440\u043d\u0443\u0442\u044c\n  return if cur.nil?\n\n  # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 \u0438\u043b\u0438 1\n  if cur.left.nil? || cur.right.nil?\n    # \u041a\u043e\u0433\u0434\u0430 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 0 / 1, child = null / \u044d\u0442\u043e\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\n    child = cur.left || cur.right\n    # \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b cur\n    if cur != @root\n      if pre.left == cur\n        pre.left = child\n      else\n        pre.right = child\n      end\n    else\n      # \u0415\u0441\u043b\u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u044b\u0439 \u0443\u0437\u0435\u043b \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c, \u0437\u0430\u043d\u043e\u0432\u043e \u043d\u0430\u0437\u043d\u0430\u0447\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n      @root = child\n    end\n  # \u0427\u0438\u0441\u043b\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 = 2\n  else\n    # \u041f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u043f\u043e\u0441\u043b\u0435 cur \u0432 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435\n    tmp = cur.right\n    while !tmp.left.nil?\n      tmp = tmp.left\n    end\n    # \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b tmp\n    remove(tmp.val)\n    # \u041f\u0435\u0440\u0435\u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c cur \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c tmp\n    cur.val = tmp.val\n  end\nend\n```\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_tree/binary_search_tree/#4","title":"4. \u00a0 \u0423\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-22, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u043e\u0440\u044f\u0434\u043a\u0443 \"\u043b\u0435\u0432\u043e \\(\\rightarrow\\) \u043a\u043e\u0440\u0435\u043d\u044c \\(\\rightarrow\\) \u043f\u0440\u0430\u0432\u043e\", \u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u044f\u0435\u0442 \u0441\u043e\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \"\u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \\(<\\) \u043a\u043e\u0440\u0435\u043d\u044c \\(<\\) \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b\".

\u042d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043f\u0440\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c \u043e\u0431\u0445\u043e\u0434\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0443\u0437\u0435\u043b, \u0438 \u043e\u0442\u0441\u044e\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0432\u0430\u0436\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e: \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0435\u0439.

\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u044d\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u044e\u0449\u0435\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432\u0441\u0435\u0433\u043e \u0437\u0430 \\(O(n)\\) \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0431\u0435\u0437 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438, \u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-22 \u00a0 \u041f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430

"},{"location":"chapter_tree/binary_search_tree/#742","title":"7.4.2 \u00a0 \u042d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430","text":"

\u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u0435 \u043b\u0438\u0431\u043e \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u043b\u0438\u0431\u043e \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430. \u0418\u0437 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0438\u0436\u0435 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u043c\u0435\u0435\u0442 \u043b\u043e\u0433\u0430\u0440\u0438\u0444\u043c\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043f\u043e\u0440\u044f\u0434\u043e\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u0430 \u0438 \u0432\u044b\u0441\u043e\u043a\u0430. \u0422\u043e\u043b\u044c\u043a\u043e \u0432 \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f\u0445 \u0441 \u043e\u0447\u0435\u043d\u044c \u0447\u0430\u0441\u0442\u044b\u043c\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0430\u043c\u0438 \u0438 \u0440\u0435\u0434\u043a\u0438\u043c\u0438 \u043f\u043e\u0438\u0441\u043a\u0430\u043c\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u0435\u0435, \u0447\u0435\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 7-2 \u00a0 \u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430

\u041d\u0435\u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u041f\u043e\u0438\u0441\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(\\log n)\\) \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(1)\\) \\(O(\\log n)\\) \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \\(O(n)\\) \\(O(\\log n)\\)

\u0412 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \"\u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c\", \u0438 \u0442\u043e\u0433\u0434\u0430 \u043b\u044e\u0431\u043e\u0439 \u0443\u0437\u0435\u043b \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0437\u0430 \\(\\log n\\) \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439.

\u041e\u0434\u043d\u0430\u043a\u043e \u0435\u0441\u043b\u0438 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0438 \u0443\u0434\u0430\u043b\u044f\u0442\u044c \u0443\u0437\u043b\u044b, \u043e\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0440\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a, \u043a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-23. \u0422\u043e\u0433\u0434\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u0442\u043e\u0436\u0435 \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0434\u043e \\(O(n)\\) .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-23 \u00a0 \u0414\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430

"},{"location":"chapter_tree/binary_search_tree/#743","title":"7.4.3 \u00a0 \u0422\u0438\u043f\u0438\u0447\u043d\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430","text":""},{"location":"chapter_tree/binary_tree/","title":"7.1 \u00a0 \u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e","text":"

\u0414\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e (binary tree) - \u044d\u0442\u043e \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0430\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043f\u043e\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \"\u043f\u0440\u0435\u0434\u043a\u0430\u043c\u0438\" \u0438 \"\u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438\" \u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u044e\u0449\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0443 \"\u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0432\u043e\u0435\". \u041f\u043e\u0434\u043e\u0431\u043d\u043e \u0441\u0432\u044f\u0437\u043d\u043e\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u0443, \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0435\u0434\u0438\u043d\u0438\u0446\u0435\u0439 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0443\u0437\u0435\u043b; \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 \u0438 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430.

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby
class TreeNode:\n    \"\"\"\u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430\"\"\"\n    def __init__(self, val: int):\n        self.val: int = val                # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        self.left: TreeNode | None = None  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n        self.right: TreeNode | None = None # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nstruct TreeNode {\n    int val;          // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    TreeNode *left;   // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    TreeNode *right;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n};\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    int val;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    TreeNode left;   // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    TreeNode right;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    TreeNode(int x) { val = x; }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode(int? x) {\n    public int? val = x;    // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    public TreeNode? left;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    public TreeNode? right; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\ntype TreeNode struct {\n    Val   int\n    Left  *TreeNode\n    Right *TreeNode\n}\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nfunc NewTreeNode(v int) *TreeNode {\n    return &TreeNode{\n        Left:  nil, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n        Right: nil, // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n        Val:   v,   // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    var val: Int // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    var left: TreeNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    var right: TreeNode? // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n\n    init(x: Int) {\n        val = x\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    left; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    right; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    constructor(val, left, right) {\n        this.val = val === undefined ? 0 : val;\n        this.left = left === undefined ? null : left;\n        this.right = right === undefined ? null : right;\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n    val: number;\n    left: TreeNode | null;\n    right: TreeNode | null;\n\n    constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n        this.val = val === undefined ? 0 : val; // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        this.left = left === undefined ? null : left; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n        this.right = right === undefined ? null : right; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    }\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode {\n  int val;         // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  TreeNode? left;  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n  TreeNode? right; // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n  TreeNode(this.val, [this.left, this.right]);\n}\n
use std::rc::Rc;\nuse std::cell::RefCell;\n\n/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nstruct TreeNode {\n    val: i32,                               // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    left: Option<Rc<RefCell<TreeNode>>>,    // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    right: Option<Rc<RefCell<TreeNode>>>,   // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n}\n\nimpl TreeNode {\n    /* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\n    fn new(val: i32) -> Rc<RefCell<Self>> {\n        Rc::new(RefCell::new(Self {\n            val,\n            left: None,\n            right: None\n        }))\n    }\n}\n
/* \u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\ntypedef struct TreeNode {\n    int val;                // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    int height;             // \u0412\u044b\u0441\u043e\u0442\u0430 \u0443\u0437\u043b\u0430\n    struct TreeNode *left;  // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    struct TreeNode *right; // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n} TreeNode;\n\n/* \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 */\nTreeNode *newTreeNode(int val) {\n    TreeNode *node;\n\n    node = (TreeNode *)malloc(sizeof(TreeNode));\n    node->val = val;\n    node->height = 0;\n    node->left = NULL;\n    node->right = NULL;\n    return node;\n}\n
/* \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\nclass TreeNode(val _val: Int) {  // \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    val left: TreeNode? = null   // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n    val right: TreeNode? = null  // \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n}\n
### \u041a\u043b\u0430\u0441\u0441 \u0443\u0437\u043b\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 ###\nclass TreeNode\n  attr_accessor :val    # \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n  attr_accessor :left   # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n  attr_accessor :right  # \u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430\n\n  def initialize(val)\n    @val = val\n  end\nend\n

\u041a\u0430\u0436\u0434\u044b\u0439 \u0443\u0437\u0435\u043b \u0438\u043c\u0435\u0435\u0442 \u0434\u0432\u0435 \u0441\u0441\u044b\u043b\u043a\u0438 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 (left-child node) \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 (right-child node); \u0434\u0430\u043d\u043d\u044b\u0439 \u0443\u0437\u0435\u043b \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c \u0443\u0437\u043b\u043e\u043c (parent node) \u0434\u043b\u044f \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432. \u0415\u0441\u043b\u0438 \u0437\u0430\u0434\u0430\u043d \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443\u0437\u0435\u043b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0442\u043e \u0434\u0435\u0440\u0435\u0432\u043e, \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0435\u0433\u043e \u043b\u0435\u0432\u044b\u043c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c \u0443\u0437\u043b\u043e\u043c \u0438 \u0432\u0441\u0435\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438 \u043d\u0438\u0436\u0435 \u043d\u0435\u0433\u043e, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043b\u0435\u0432\u044b\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\u043c (left subtree) \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430; \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e (right subtree).

\u0412 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435, \u043a\u0440\u043e\u043c\u0435 \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0443\u0437\u043b\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0443\u0437\u043b\u044b \u0438 \u043d\u0435\u043f\u0443\u0441\u0442\u044b\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f. \u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-1, \u0435\u0441\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0442\u044c \"\u0443\u0437\u0435\u043b 2\" \u043a\u0430\u043a \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439, \u0442\u043e \u0435\u0433\u043e \u043b\u0435\u0432\u044b\u043c \u0438 \u043f\u0440\u0430\u0432\u044b\u043c \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u043c\u0438 \u0443\u0437\u043b\u0430\u043c\u0438 \u0431\u0443\u0434\u0443\u0442 \"\u0443\u0437\u0435\u043b 4\" \u0438 \"\u0443\u0437\u0435\u043b 5\"; \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e - \u044d\u0442\u043e \"\u0443\u0437\u0435\u043b 4 \u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0438\u0436\u0435 \u043d\u0435\u0433\u043e\", \u0430 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e - \u044d\u0442\u043e \"\u0443\u0437\u0435\u043b 5 \u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0438\u0436\u0435 \u043d\u0435\u0433\u043e\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-1 \u00a0 \u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0443\u0437\u0435\u043b, \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0443\u0437\u043b\u044b \u0438 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f

"},{"location":"chapter_tree/binary_tree/#711","title":"7.1.1 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-2.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-2 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0435\u0440\u043c\u0438\u043d\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u043e\u0434 \"\u0432\u044b\u0441\u043e\u0442\u043e\u0439\" \u0438 \"\u0433\u043b\u0443\u0431\u0438\u043d\u043e\u0439\" \u043f\u043e\u043d\u0438\u043c\u0430\u044e\u0442 \"\u0447\u0438\u0441\u043b\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u0431\u0435\u0440\", \u043d\u043e \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u0438\u043b\u0438 \u0443\u0447\u0435\u0431\u043d\u0438\u043a\u0430\u0445 \u0438\u0445 \u043c\u043e\u0433\u0443\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0442\u044c \u043a\u0430\u043a \"\u0447\u0438\u0441\u043b\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432\". \u0412 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438 \u0432\u044b\u0441\u043e\u0442\u0443, \u0438 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043d\u0443\u0436\u043d\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c \u043d\u0430 1 .

"},{"location":"chapter_tree/binary_tree/#712","title":"7.1.2 \u00a0 \u0411\u0430\u0437\u043e\u0432\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":""},{"location":"chapter_tree/binary_tree/#1","title":"1. \u00a0 \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u041a\u0430\u043a \u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0443\u0437\u043b\u044b, \u0430 \u0437\u0430\u0442\u0435\u043c \u043c\u0435\u0436\u0434\u0443 \u043d\u0438\u043c\u0438 \u0441\u0442\u0440\u043e\u044f\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0438 (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0438).

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nn1 = TreeNode(val=1)\nn2 = TreeNode(val=2)\nn3 = TreeNode(val=3)\nn4 = TreeNode(val=4)\nn5 = TreeNode(val=5)\n# \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.cpp
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nTreeNode* n1 = new TreeNode(1);\nTreeNode* n2 = new TreeNode(2);\nTreeNode* n3 = new TreeNode(3);\nTreeNode* n4 = new TreeNode(4);\nTreeNode* n5 = new TreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.java
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.cs
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nTreeNode n1 = new(1);\nTreeNode n2 = new(2);\nTreeNode n3 = new(3);\nTreeNode n4 = new(4);\nTreeNode n5 = new(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.go
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nn1 := NewTreeNode(1)\nn2 := NewTreeNode(2)\nn3 := NewTreeNode(3)\nn4 := NewTreeNode(4)\nn5 := NewTreeNode(5)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.Left = n2\nn1.Right = n3\nn2.Left = n4\nn2.Right = n5\n
binary_tree.swift
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nlet n1 = TreeNode(x: 1)\nlet n2 = TreeNode(x: 2)\nlet n3 = TreeNode(x: 3)\nlet n4 = TreeNode(x: 4)\nlet n5 = TreeNode(x: 5)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.js
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.ts
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nlet n1 = new TreeNode(1),\n    n2 = new TreeNode(2),\n    n3 = new TreeNode(3),\n    n4 = new TreeNode(4),\n    n5 = new TreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.dart
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nTreeNode n1 = new TreeNode(1);\nTreeNode n2 = new TreeNode(2);\nTreeNode n3 = new TreeNode(3);\nTreeNode n4 = new TreeNode(4);\nTreeNode n5 = new TreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2;\nn1.right = n3;\nn2.left = n4;\nn2.right = n5;\n
binary_tree.rs
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nlet n1 = TreeNode::new(1);\nlet n2 = TreeNode::new(2);\nlet n3 = TreeNode::new(3);\nlet n4 = TreeNode::new(4);\nlet n5 = TreeNode::new(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.borrow_mut().left = Some(n2.clone());\nn1.borrow_mut().right = Some(n3);\nn2.borrow_mut().left = Some(n4);\nn2.borrow_mut().right = Some(n5);\n
binary_tree.c
/* \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 */\n// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nTreeNode *n1 = newTreeNode(1);\nTreeNode *n2 = newTreeNode(2);\nTreeNode *n3 = newTreeNode(3);\nTreeNode *n4 = newTreeNode(4);\nTreeNode *n5 = newTreeNode(5);\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1->left = n2;\nn1->right = n3;\nn2->left = n4;\nn2->right = n5;\n
binary_tree.kt
// \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nval n1 = TreeNode(1)\nval n2 = TreeNode(2)\nval n3 = TreeNode(3)\nval n4 = TreeNode(4)\nval n5 = TreeNode(5)\n// \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
binary_tree.rb
# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430\n# \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0443\u0437\u043b\u043e\u0432\nn1 = TreeNode.new(1)\nn2 = TreeNode.new(2)\nn3 = TreeNode.new(3)\nn4 = TreeNode.new(4)\nn5 = TreeNode.new(5)\n# \u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0441\u0441\u044b\u043b\u043e\u043a (\u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439) \u043c\u0435\u0436\u0434\u0443 \u0443\u0437\u043b\u0430\u043c\u0438\nn1.left = n2\nn1.right = n3\nn2.left = n4\nn2.right = n5\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%D0%9A%D0%BB%D0%B0%D1%81%D1%81%20%D1%83%D0%B7%D0%BB%D0%B0%20%D0%B4%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%BE%D0%B3%D0%BE%20%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%B0%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%D0%97%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%B0%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D0%BB%D0%B5%D0%B2%D1%8B%D0%B9%20%D0%B4%D0%BE%D1%87%D0%B5%D1%80%D0%BD%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D0%BF%D1%80%D0%B0%D0%B2%D1%8B%D0%B9%20%D0%B4%D0%BE%D1%87%D0%B5%D1%80%D0%BD%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%B4%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%BE%D0%B5%20%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%83%D0%B7%D0%B5%D0%BB%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%D0%9F%D0%BE%D1%81%D1%82%D1%80%D0%BE%D0%B8%D1%82%D1%8C%20%D1%81%D1%81%D1%8B%D0%BB%D0%BA%D0%B8%20%D0%BC%D0%B5%D0%B6%D0%B4%D1%83%20%D1%83%D0%B7%D0%BB%D0%B0%D0%BC%D0%B8%20%28%D1%83%D0%BA%D0%B0%D0%B7%D0%B0%D1%82%D0%B5%D0%BB%D0%B8%29%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

"},{"location":"chapter_tree/binary_tree/#2","title":"2. \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432","text":"

\u041a\u0430\u043a \u0438 \u0432 \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0441\u043f\u0438\u0441\u043a\u0435, \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u0435\u0439. \u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-3 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043f\u0440\u0438\u043c\u0435\u0440.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-3 \u00a0 \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree.py
# \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432\np = TreeNode(0)\n# \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = p\np.left = n2\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2\n
binary_tree.cpp
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nTreeNode* P = new TreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1->left = P;\nP->left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1->left = n2;\n// \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\ndelete P;\n
binary_tree.java
TreeNode P = new TreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P;\nP.left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2;\n
binary_tree.cs
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nTreeNode P = new(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P;\nP.left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2;\n
binary_tree.go
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\np := NewTreeNode(0)\nn1.Left = p\np.Left = n2\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.Left = n2\n
binary_tree.swift
let P = TreeNode(x: 0)\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P\nP.left = n2\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2\n
binary_tree.js
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nlet P = new TreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P;\nP.left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2;\n
binary_tree.ts
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nconst P = new TreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P;\nP.left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2;\n
binary_tree.dart
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nTreeNode P = new TreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P;\nP.left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2;\n
binary_tree.rs
let p = TreeNode::new(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.borrow_mut().left = Some(p.clone());\np.borrow_mut().left = Some(n2.clone());\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b p\nn1.borrow_mut().left = Some(n2);\n
binary_tree.c
/* \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 */\nTreeNode *P = newTreeNode(0);\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1->left = P;\nP->left = n2;\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1->left = n2;\n// \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c\nfree(P);\n
binary_tree.kt
val P = TreeNode(0)\n// \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b P \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = P\nP.left = n2\n// \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b P\nn1.left = n2\n
binary_tree.rb
# \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432\n_p = TreeNode.new(0)\n# \u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0443\u0437\u0435\u043b _p \u043c\u0435\u0436\u0434\u0443 n1 -> n2\nn1.left = _p\n_p.left = n2\n# \u0423\u0434\u0430\u043b\u0438\u0442\u044c \u0443\u0437\u0435\u043b\nn1.left = n2\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f

https://pythontutor.com/render.html#code=class%20TreeNode%3A%0A%20%20%20%20%22%22%22%D0%9A%D0%BB%D0%B0%D1%81%D1%81%20%D1%83%D0%B7%D0%BB%D0%B0%20%D0%B4%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%BE%D0%B3%D0%BE%20%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%B0%22%22%22%0A%20%20%20%20def%20__init__%28self%2C%20val%3A%20int%29%3A%0A%20%20%20%20%20%20%20%20self.val%3A%20int%20%3D%20val%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%23%20%D0%97%D0%BD%D0%B0%D1%87%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%B0%0A%20%20%20%20%20%20%20%20self.left%3A%20TreeNode%20%7C%20None%20%3D%20None%20%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D0%BB%D0%B5%D0%B2%D1%8B%D0%B9%20%D0%B4%D0%BE%D1%87%D0%B5%D1%80%D0%BD%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%20%20%20%20%20%20%20%20self.right%3A%20TreeNode%20%7C%20None%20%3D%20None%20%23%20%D0%A1%D1%81%D1%8B%D0%BB%D0%BA%D0%B0%20%D0%BD%D0%B0%20%D0%BF%D1%80%D0%B0%D0%B2%D1%8B%D0%B9%20%D0%B4%D0%BE%D1%87%D0%B5%D1%80%D0%BD%D0%B8%D0%B9%20%D1%83%D0%B7%D0%B5%D0%BB%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D0%B4%D0%B2%D0%BE%D0%B8%D1%87%D0%BD%D0%BE%D0%B5%20%D0%B4%D0%B5%D1%80%D0%B5%D0%B2%D0%BE%0A%20%20%20%20%23%20%D0%98%D0%BD%D0%B8%D1%86%D0%B8%D0%B0%D0%BB%D0%B8%D0%B7%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%20%D1%83%D0%B7%D0%B5%D0%BB%0A%20%20%20%20n1%20%3D%20TreeNode%28val%3D1%29%0A%20%20%20%20n2%20%3D%20TreeNode%28val%3D2%29%0A%20%20%20%20n3%20%3D%20TreeNode%28val%3D3%29%0A%20%20%20%20n4%20%3D%20TreeNode%28val%3D4%29%0A%20%20%20%20n5%20%3D%20TreeNode%28val%3D5%29%0A%20%20%20%20%23%20%D0%9F%D0%BE%D1%81%D1%82%D1%80%D0%BE%D0%B8%D1%82%D1%8C%20%D1%81%D1%81%D1%8B%D0%BB%D0%BA%D0%B8%20%D0%BC%D0%B5%D0%B6%D0%B4%D1%83%20%D1%83%D0%B7%D0%BB%D0%B0%D0%BC%D0%B8%20%28%D1%83%D0%BA%D0%B0%D0%B7%D0%B0%D1%82%D0%B5%D0%BB%D0%B8%29%0A%20%20%20%20n1.left%20%3D%20n2%0A%20%20%20%20n1.right%20%3D%20n3%0A%20%20%20%20n2.left%20%3D%20n4%0A%20%20%20%20n2.right%20%3D%20n5%0A%0A%20%20%20%20%23%20%D0%92%D1%81%D1%82%D0%B0%D0%B2%D0%BA%D0%B0%20%D0%B8%20%D1%83%D0%B4%D0%B0%D0%BB%D0%B5%D0%BD%D0%B8%D0%B5%20%D1%83%D0%B7%D0%BB%D0%BE%D0%B2%0A%20%20%20%20p%20%3D%20TreeNode%280%29%0A%20%20%20%20%23%20%D0%92%D1%81%D1%82%D0%B0%D0%B2%D0%B8%D1%82%D1%8C%20%D1%83%D0%B7%D0%B5%D0%BB%20P%20%D0%BC%D0%B5%D0%B6%D0%B4%D1%83%20n1%20-%3E%20n2%0A%20%20%20%20n1.left%20%3D%20p%0A%20%20%20%20p.left%20%3D%20n2%0A%20%20%20%20%23%20%D0%A3%D0%B4%D0%B0%D0%BB%D0%B8%D1%82%D1%8C%20%D1%83%D0%B7%D0%B5%D0%BB%20P%0A%20%20%20%20n1.left%20%3D%20n2&cumulative=false&curInstr=37&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0432\u0441\u0442\u0430\u0432\u043a\u0430 \u0443\u0437\u043b\u0430 \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0438\u0441\u0445\u043e\u0434\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u0435\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c\u0438. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u044b\u0447\u043d\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0447\u0430\u0441\u0442\u044c\u044e \u0431\u043e\u043b\u0435\u0435 \u043a\u0440\u0443\u043f\u043d\u043e\u0433\u043e \u043d\u0430\u0431\u043e\u0440\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043e\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u043e\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435.

"},{"location":"chapter_tree/binary_tree/#713","title":"7.1.3 \u00a0 \u0420\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432","text":""},{"location":"chapter_tree/binary_tree/#1_1","title":"1. \u00a0 \u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-4, \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e (perfect binary tree) \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u043d\u0430 \u0432\u0441\u0435\u0445 \u0443\u0440\u043e\u0432\u043d\u044f\u0445. \u0412 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u0430 \\(0\\) , \u0430 \u0443 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0440\u0430\u0432\u043d\u0430 \\(2\\) ; \u0435\u0441\u043b\u0438 \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0440\u0430\u0432\u043d\u0430 \\(h\\) , \u0442\u043e \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u043e \\(2^{h+1} - 1\\) , \u0447\u0442\u043e \u043e\u0431\u0440\u0430\u0437\u0443\u0435\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u0443\u044e \u044d\u043a\u0441\u043f\u043e\u043d\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u0443\u044e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0438 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0449\u0435\u0435\u0441\u044f \u0432 \u043f\u0440\u0438\u0440\u043e\u0434\u0435 \u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043a\u043b\u0435\u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u043b\u0435\u043d\u0438\u044f.

Tip

\u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0432 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u043e\u044f\u0437\u044b\u0447\u043d\u043e\u043c \u0441\u043e\u043e\u0431\u0449\u0435\u0441\u0442\u0432\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0447\u0430\u0441\u0442\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u043d\u044b\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-4 \u00a0 \u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e

"},{"location":"chapter_tree/binary_tree/#2_1","title":"2. \u00a0 \u041f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-5, \u043f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e (complete binary tree) \u0434\u043e\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u043d\u0435\u043f\u043e\u043b\u043d\u043e\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u043d\u0438\u0436\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435, \u043f\u0440\u0438\u0447\u0435\u043c \u0443\u0437\u043b\u044b \u044d\u0442\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e. \u041e\u0431\u0440\u0430\u0442\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435: \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0442\u043e\u0436\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u043d\u044b\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-5 \u00a0 \u041f\u043e\u043b\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e

"},{"location":"chapter_tree/binary_tree/#3","title":"3. \u00a0 \u0421\u0442\u0440\u043e\u0433\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-6, \u0441\u0442\u0440\u043e\u0433\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e (full binary tree) \u0438\u043c\u0435\u0435\u0442 \u0443 \u0432\u0441\u0435\u0445 \u043d\u0435\u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u0440\u043e\u0432\u043d\u043e \u0434\u0432\u0443\u0445 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-6 \u00a0 \u0421\u0442\u0440\u043e\u0433\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e

"},{"location":"chapter_tree/binary_tree/#4","title":"4. \u00a0 \u0421\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-7, \u0432 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 (balanced binary tree) \u0434\u043b\u044f \u043b\u044e\u0431\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043d\u043e\u0441\u0442\u0438 \u0432\u044b\u0441\u043e\u0442 \u043b\u0435\u0432\u043e\u0433\u043e \u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u043d\u0435 \u043f\u0440\u0435\u0432\u044b\u0448\u0430\u0435\u0442 1 .

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-7 \u00a0 \u0421\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e

"},{"location":"chapter_tree/binary_tree/#714","title":"7.1.4 \u00a0 \u0412\u044b\u0440\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-8 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u044b \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u0432\u044b\u0440\u043e\u0436\u0434\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430. \u041a\u043e\u0433\u0434\u0430 \u043a\u0430\u0436\u0434\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d \u0443\u0437\u043b\u0430\u043c\u0438, \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \"\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e\"; \u043a\u043e\u0433\u0434\u0430 \u0436\u0435 \u0432\u0441\u0435 \u0443\u0437\u043b\u044b \u0441\u043c\u0435\u0449\u0430\u044e\u0442\u0441\u044f \u043a \u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435, \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0432\u044b\u0440\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \"\u0441\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-8 \u00a0 \u041b\u0443\u0447\u0448\u0438\u0439 \u0438 \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 7-1, \u0432 \u043b\u0443\u0447\u0448\u0435\u043c \u0438 \u0445\u0443\u0434\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0447\u0438\u0441\u043b\u043e \u043b\u0438\u0441\u0442\u043e\u0432\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432, \u0432\u044b\u0441\u043e\u0442\u0430 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0445\u0430\u0440\u0430\u043a\u0442\u0435\u0440\u0438\u0441\u0442\u0438\u043a\u0438 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0434\u043e\u0441\u0442\u0438\u0433\u0430\u044e\u0442 \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c\u0430 \u0438\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430.

\u0422\u0430\u0431\u043b\u0438\u0446\u0430 7-1 \u00a0 \u041b\u0443\u0447\u0448\u0438\u0439 \u0438 \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430

\u0418\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0427\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \\(i\\) \\(2^{i-1}\\) \\(1\\) \u0427\u0438\u0441\u043b\u043e \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0443 \u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u044b\u0441\u043e\u0442\u044b \\(h\\) \\(2^h\\) \\(1\\) \u041e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0443 \u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u044b\u0441\u043e\u0442\u044b \\(h\\) \\(2^{h+1} - 1\\) \\(h + 1\\) \u0412\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \\(n\\) \u0443\u0437\u043b\u0430\u043c\u0438 \\(\\log_2 (n+1) - 1\\) \\(n - 1\\)"},{"location":"chapter_tree/binary_tree_traversal/","title":"7.2 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430","text":"

\u0421 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0440\u0430\u0437\u043d\u043e\u0432\u0438\u0434\u043d\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0432\u044f\u0437\u0435\u0439, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u043e\u0431\u0445\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0443\u0437\u043b\u0430\u043c \u043f\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f\u043c. \u041e\u0434\u043d\u0430\u043a\u043e \u0434\u0435\u0440\u0435\u0432\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u043b\u0438\u043d\u0435\u0439\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442, \u0435\u0433\u043e \u043e\u0431\u0445\u043e\u0434 \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u043e\u0431\u0445\u043e\u0434 \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430, \u0438 \u0434\u043b\u044f \u043d\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b.

\u041a \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u0430\u043c \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043e\u0431\u0445\u043e\u0434 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c, \u043f\u0440\u044f\u043c\u043e\u0439 \u043e\u0431\u0445\u043e\u0434, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434.

"},{"location":"chapter_tree/binary_tree_traversal/#721","title":"7.2.1 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c","text":"

\u041a\u0430\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-9, \u043e\u0431\u0445\u043e\u0434 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c (level-order traversal) \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0432\u0435\u0440\u0445\u0443 \u0432\u043d\u0438\u0437 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u0438 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u043f\u043e\u0441\u0435\u0449\u0430\u0435\u0442 \u0443\u0437\u043b\u044b \u0441\u043b\u0435\u0432\u0430 \u043d\u0430\u043f\u0440\u0430\u0432\u043e.

\u041f\u043e \u0441\u0432\u043e\u0435\u0439 \u0441\u0443\u0442\u0438 \u043e\u0431\u0445\u043e\u0434 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a \u043e\u0431\u0445\u043e\u0434\u0443 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 (breadth-first traversal), \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 (breadth-first search, BFS); \u043e\u043d \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u0434\u0435\u044e \"\u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c\u0441\u044f \u0441\u043b\u043e\u0439 \u0437\u0430 \u0441\u043b\u043e\u0435\u043c \u043d\u0430\u0440\u0443\u0436\u0443\".

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-9 \u00a0 \u041e\u0431\u0445\u043e\u0434 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c

"},{"location":"chapter_tree/binary_tree_traversal/#1","title":"1. \u00a0 \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \"\u043e\u0447\u0435\u0440\u0435\u0434\u0438\". \u041e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \"\u043f\u0435\u0440\u0432\u044b\u043c \u043f\u0440\u0438\u0448\u0435\u043b - \u043f\u0435\u0440\u0432\u044b\u043c \u0432\u044b\u0448\u0435\u043b\", \u0430 \u043e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043f\u043e\u0434\u0447\u0438\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \"\u043f\u0440\u043e\u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0443\u0440\u043e\u0432\u043d\u044f\u043c\", \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0437\u0430 \u043d\u0438\u043c\u0438 \u0438\u0434\u0435\u044f \u0441\u043e\u0433\u043b\u0430\u0441\u043e\u0432\u0430\u043d\u0430. \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d \u043d\u0438\u0436\u0435:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_bfs.py
def level_order(root: TreeNode | None) -> list[int]:\n    \"\"\"\u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443\"\"\"\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    queue: deque[TreeNode] = deque()\n    queue.append(root)\n    # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    res = []\n    while queue:\n        node: TreeNode = queue.popleft()  # \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        res.append(node.val)  # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if node.left is not None:\n            queue.append(node.left)  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if node.right is not None:\n            queue.append(node.right)  # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    return res\n
binary_tree_bfs.cpp
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nvector<int> levelOrder(TreeNode *root) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    queue<TreeNode *> queue;\n    queue.push(root);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    vector<int> vec;\n    while (!queue.empty()) {\n        TreeNode *node = queue.front();\n        queue.pop();              // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        vec.push_back(node->val); // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node->left != nullptr)\n            queue.push(node->left); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if (node->right != nullptr)\n            queue.push(node->right); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    }\n    return vec;\n}\n
binary_tree_bfs.java
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nList<Integer> levelOrder(TreeNode root) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    Queue<TreeNode> queue = new LinkedList<>();\n    queue.add(root);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<Integer> list = new ArrayList<>();\n    while (!queue.isEmpty()) {\n        TreeNode node = queue.poll(); // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.add(node.val);           // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node.left != null)\n            queue.offer(node.left);   // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if (node.right != null)\n            queue.offer(node.right);  // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    }\n    return list;\n}\n
binary_tree_bfs.cs
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nList<int> LevelOrder(TreeNode root) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    Queue<TreeNode> queue = new();\n    queue.Enqueue(root);\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    List<int> list = [];\n    while (queue.Count != 0) {\n        TreeNode node = queue.Dequeue(); // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.Add(node.val!.Value);       // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node.left != null)\n            queue.Enqueue(node.left);    // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if (node.right != null)\n            queue.Enqueue(node.right);   // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    }\n    return list;\n}\n
binary_tree_bfs.go
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfunc levelOrder(root *TreeNode) []any {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    queue := list.New()\n    queue.PushBack(root)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0440\u0435\u0437 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    nums := make([]any, 0)\n    for queue.Len() > 0 {\n        // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        node := queue.Remove(queue.Front()).(*TreeNode)\n        // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        nums = append(nums, node.Val)\n        if node.Left != nil {\n            // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n            queue.PushBack(node.Left)\n        }\n        if node.Right != nil {\n            // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n            queue.PushBack(node.Right)\n        }\n    }\n    return nums\n}\n
binary_tree_bfs.swift
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfunc levelOrder(root: TreeNode) -> [Int] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    var queue: [TreeNode] = [root]\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    var list: [Int] = []\n    while !queue.isEmpty {\n        let node = queue.removeFirst() // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.append(node.val) // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if let left = node.left {\n            queue.append(left) // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        }\n        if let right = node.right {\n            queue.append(right) // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        }\n    }\n    return list\n}\n
binary_tree_bfs.js
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfunction levelOrder(root) {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    const queue = [root];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    const list = [];\n    while (queue.length) {\n        let node = queue.shift(); // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.push(node.val); // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node.left) queue.push(node.left); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if (node.right) queue.push(node.right); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    }\n    return list;\n}\n
binary_tree_bfs.ts
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfunction levelOrder(root: TreeNode | null): number[] {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    const queue = [root];\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    const list: number[] = [];\n    while (queue.length) {\n        let node = queue.shift() as TreeNode; // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.push(node.val); // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node.left) {\n            queue.push(node.left); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        }\n        if (node.right) {\n            queue.push(node.right); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        }\n    }\n    return list;\n}\n
binary_tree_bfs.dart
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nList<int> levelOrder(TreeNode? root) {\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  Queue<TreeNode?> queue = Queue();\n  queue.add(root);\n  // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n  List<int> res = [];\n  while (queue.isNotEmpty) {\n    TreeNode? node = queue.removeFirst(); // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    res.add(node!.val); // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    if (node.left != null) queue.add(node.left); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    if (node.right != null) queue.add(node.right); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n  }\n  return res;\n}\n
binary_tree_bfs.rs
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    let mut que = VecDeque::new();\n    que.push_back(root.clone());\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    let mut vec = Vec::new();\n\n    while let Some(node) = que.pop_front() {\n        // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        vec.push(node.borrow().val); // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if let Some(left) = node.borrow().left.as_ref() {\n            que.push_back(left.clone()); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        }\n        if let Some(right) = node.borrow().right.as_ref() {\n            que.push_back(right.clone()); // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        };\n    }\n    vec\n}\n
binary_tree_bfs.c
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nint *levelOrder(TreeNode *root, int *size) {\n    /* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    int front, rear;\n    int index, *arr;\n    TreeNode *node;\n    TreeNode **queue;\n\n    /* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u044c */\n    queue = (TreeNode **)malloc(sizeof(TreeNode *) * MAX_SIZE);\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    front = 0, rear = 0;\n    // \u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    queue[rear++] = root;\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    /* \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 */\n    arr = (int *)malloc(sizeof(int) * MAX_SIZE);\n    // \u0423\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c \u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\n    index = 0;\n    while (front < rear) {\n        // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        node = queue[front++];\n        // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        arr[index++] = node->val;\n        if (node->left != NULL) {\n            // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n            queue[rear++] = node->left;\n        }\n        if (node->right != NULL) {\n            // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n            queue[rear++] = node->right;\n        }\n    }\n    // \u041e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0434\u043b\u0438\u043d\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    *size = index;\n    arr = realloc(arr, sizeof(int) * (*size));\n\n    // \u041e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0442\u044c \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\n    free(queue);\n    return arr;\n}\n
binary_tree_bfs.kt
/* \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 */\nfun levelOrder(root: TreeNode?): MutableList<Int> {\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n    val queue = LinkedList<TreeNode?>()\n    queue.add(root)\n    // \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n    val list = mutableListOf<Int>()\n    while (queue.isNotEmpty()) {\n        val node = queue.poll()      // \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n        list.add(node?._val!!)       // \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n        if (node.left != null)\n            queue.offer(node.left)   // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n        if (node.right != null)\n            queue.offer(node.right)  // \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    }\n    return list\n}\n
binary_tree_bfs.rb
=begin\nFile: binary_tree_bfs.rb\nCreated Time: 2024-04-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041e\u0431\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 ###\ndef level_order(root)\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b\n  queue = [root]\n  # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043f\u0438\u0441\u043e\u043a \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u0445\u043e\u0434\u0430\n  res = []\n  while !queue.empty?\n    node = queue.shift # \u0418\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438\n    res << node.val # \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u0430\n    queue << node.left unless node.left.nil? # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043b\u0435\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n    queue << node.right unless node.right.nil? # \u041f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u044b\u0439 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0439 \u0443\u0437\u0435\u043b \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\n  end\n  res\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

"},{"location":"chapter_tree/binary_tree_traversal/#2","title":"2. \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":""},{"location":"chapter_tree/binary_tree_traversal/#722","title":"7.2.2 \u00a0 \u041f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u044b","text":"

\u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u044b \u043e\u0442\u043d\u043e\u0441\u044f\u0442\u0441\u044f \u043a \u043e\u0431\u0445\u043e\u0434\u0443 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 (depth-first traversal), \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u043e\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u043e\u043c \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 (depth-first search, DFS); \u043e\u043d \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442 \u0438\u0434\u0435\u044e \"\u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0438\u0434\u0442\u0438 \u0434\u043e \u043a\u043e\u043d\u0446\u0430, \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u043a\u0430\u0442\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0442\u044c\".

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 7-10 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0440\u0438\u043d\u0446\u0438\u043f \u0440\u0430\u0431\u043e\u0442\u044b \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443. \u041e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0442\u043e, \u043a\u0430\u043a \u0431\u0443\u0434\u0442\u043e \u043c\u044b \u043e\u0431\u0445\u043e\u0434\u0438\u043c \u0432\u0441\u044e \u0434\u0432\u043e\u0438\u0447\u043d\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 \u043f\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u043c\u0443 \u043a\u043e\u043d\u0442\u0443\u0440\u0443 , \u0438 \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0443\u0437\u043b\u0430 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c \u0442\u0440\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u044f\u043c\u043e\u043c\u0443, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u043e\u043c\u0443 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0430\u043c.

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-10 \u00a0 \u041f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u044b \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430

"},{"location":"chapter_tree/binary_tree_traversal/#1_1","title":"1. \u00a0 \u041a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438","text":"

\u041f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043e\u0431\u044b\u0447\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e:

PythonC++JavaC#GoSwiftJSTSDartRustCKotlinRuby binary_tree_dfs.py
def pre_order(root: TreeNode | None):\n    \"\"\"\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n    if root is None:\n        return\n    # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    res.append(root.val)\n    pre_order(root=root.left)\n    pre_order(root=root.right)\n\ndef in_order(root: TreeNode | None):\n    \"\"\"\u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n    if root is None:\n        return\n    # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    in_order(root=root.left)\n    res.append(root.val)\n    in_order(root=root.right)\n\ndef post_order(root: TreeNode | None):\n    \"\"\"\u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\"\"\"\n    if root is None:\n        return\n    # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    post_order(root=root.left)\n    post_order(root=root.right)\n    res.append(root.val)\n
binary_tree_dfs.cpp
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid preOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    vec.push_back(root->val);\n    preOrder(root->left);\n    preOrder(root->right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid inOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root->left);\n    vec.push_back(root->val);\n    inOrder(root->right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid postOrder(TreeNode *root) {\n    if (root == nullptr)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root->left);\n    postOrder(root->right);\n    vec.push_back(root->val);\n}\n
binary_tree_dfs.java
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid preOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.add(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid inOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root.left);\n    list.add(root.val);\n    inOrder(root.right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid postOrder(TreeNode root) {\n    if (root == null)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root.left);\n    postOrder(root.right);\n    list.add(root.val);\n}\n
binary_tree_dfs.cs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid PreOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.Add(root.val!.Value);\n    PreOrder(root.left);\n    PreOrder(root.right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid InOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    InOrder(root.left);\n    list.Add(root.val!.Value);\n    InOrder(root.right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid PostOrder(TreeNode? root) {\n    if (root == null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    PostOrder(root.left);\n    PostOrder(root.right);\n    list.Add(root.val!.Value);\n}\n
binary_tree_dfs.go
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc preOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    nums = append(nums, node.Val)\n    preOrder(node.Left)\n    preOrder(node.Right)\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc inOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(node.Left)\n    nums = append(nums, node.Val)\n    inOrder(node.Right)\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc postOrder(node *TreeNode) {\n    if node == nil {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(node.Left)\n    postOrder(node.Right)\n    nums = append(nums, node.Val)\n}\n
binary_tree_dfs.swift
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc preOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.append(root.val)\n    preOrder(root: root.left)\n    preOrder(root: root.right)\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc inOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root: root.left)\n    list.append(root.val)\n    inOrder(root: root.right)\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunc postOrder(root: TreeNode?) {\n    guard let root = root else {\n        return\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root: root.left)\n    postOrder(root: root.right)\n    list.append(root.val)\n}\n
binary_tree_dfs.js
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction preOrder(root) {\n    if (root === null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction inOrder(root) {\n    if (root === null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction postOrder(root) {\n    if (root === null) return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
binary_tree_dfs.ts
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction preOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.push(root.val);\n    preOrder(root.left);\n    preOrder(root.right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction inOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root.left);\n    list.push(root.val);\n    inOrder(root.right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfunction postOrder(root: TreeNode | null): void {\n    if (root === null) {\n        return;\n    }\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root.left);\n    postOrder(root.right);\n    list.push(root.val);\n}\n
binary_tree_dfs.dart
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid preOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  list.add(node.val);\n  preOrder(node.left);\n  preOrder(node.right);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid inOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  inOrder(node.left);\n  list.add(node.val);\n  inOrder(node.right);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid postOrder(TreeNode? node) {\n  if (node == null) return;\n  // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n  postOrder(node.left);\n  postOrder(node.right);\n  list.add(node.val);\n}\n
binary_tree_dfs.rs
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfn pre_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n            let node = node.borrow();\n            res.push(node.val);\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfn in_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            res.push(node.val);\n            dfs(node.right.as_ref(), res);\n        }\n    }\n    dfs(root, &mut result);\n\n    result\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfn post_order(root: Option<&Rc<RefCell<TreeNode>>>) -> Vec<i32> {\n    let mut result = vec![];\n\n    fn dfs(root: Option<&Rc<RefCell<TreeNode>>>, res: &mut Vec<i32>) {\n        if let Some(node) = root {\n            // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n            let node = node.borrow();\n            dfs(node.left.as_ref(), res);\n            dfs(node.right.as_ref(), res);\n            res.push(node.val);\n        }\n    }\n\n    dfs(root, &mut result);\n\n    result\n}\n
binary_tree_dfs.c
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid preOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    arr[(*size)++] = root->val;\n    preOrder(root->left, size);\n    preOrder(root->right, size);\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid inOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root->left, size);\n    arr[(*size)++] = root->val;\n    inOrder(root->right, size);\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nvoid postOrder(TreeNode *root, int *size) {\n    if (root == NULL)\n        return;\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root->left, size);\n    postOrder(root->right, size);\n    arr[(*size)++] = root->val;\n}\n
binary_tree_dfs.kt
/* \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfun preOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    list.add(root._val)\n    preOrder(root.left)\n    preOrder(root.right)\n}\n\n/* \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfun inOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n    inOrder(root.left)\n    list.add(root._val)\n    inOrder(root.right)\n}\n\n/* \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 */\nfun postOrder(root: TreeNode?) {\n    if (root == null) return\n    // \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n    postOrder(root.left)\n    postOrder(root.right)\n    list.add(root._val)\n}\n
binary_tree_dfs.rb
=begin\nFile: binary_tree_dfs.rb\nCreated Time: 2024-04-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef pre_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n=begin\nFile: binary_tree_dfs.rb\nCreated Time: 2024-04-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef pre_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n# ## \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef in_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n=begin\nFile: binary_tree_dfs.rb\nCreated Time: 2024-04-18\nAuthor: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)\n=end\n\nrequire_relative '../utils/tree_node'\nrequire_relative '../utils/print_util'\n\n# ## \u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef pre_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043a\u043e\u0440\u0435\u043d\u044c -> \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  $res << root.val\n  pre_order(root.left)\n  pre_order(root.right)\nend\n\n# ## \u0421\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef in_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\n  in_order(root.left)\n  $res << root.val\n  in_order(root.right)\nend\n\n# ## \u041e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434 ###\ndef post_order(root)\n  return if root.nil?\n\n  # \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430: \u043b\u0435\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e -> \u043a\u043e\u0440\u0435\u043d\u044c\n  post_order(root.left)\n  post_order(root.right)\n  $res << root.val\nend\n
\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430

\u0412\u043e \u0432\u0435\u0441\u044c \u044d\u043a\u0440\u0430\u043d >

Tip

\u041f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0438 \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e; \u0437\u0430\u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043c\u043e\u0433\u0443\u0442 \u0438\u0437\u0443\u0447\u0438\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e.

\u041d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0430\u0445 \u043d\u0438\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0435 \u043f\u0440\u043e\u0442\u0438\u0432\u043e\u043f\u043e\u043b\u043e\u0436\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438: \"\u0432\u0445\u043e\u0434 \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\" \u0438 \"\u0432\u043e\u0437\u0432\u0440\u0430\u0442\".

  1. \"\u0412\u0445\u043e\u0434 \u0432 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u044e\" \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0437\u0430\u043f\u0443\u0441\u043a \u043d\u043e\u0432\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438; \u0432 \u044d\u0442\u043e\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c\u0443 \u0443\u0437\u043b\u0443.
  2. \"\u0412\u043e\u0437\u0432\u0440\u0430\u0442\" \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0442 \u043d\u0430\u0437\u0430\u0434, \u0442\u043e \u0435\u0441\u0442\u044c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0443\u0437\u0435\u043b \u0443\u0436\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d.
<1><2><3><4><5><6><7><8><9><10><11>

\u0420\u0438\u0441\u0443\u043d\u043e\u043a 7-11 \u00a0 \u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043e\u0431\u0445\u043e\u0434\u0430

"},{"location":"chapter_tree/binary_tree_traversal/#2_1","title":"2. \u00a0 \u0410\u043d\u0430\u043b\u0438\u0437 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438","text":""},{"location":"chapter_tree/summary/","title":"7.6 \u00a0 \u041a\u0440\u0430\u0442\u043a\u0438\u0435 \u0438\u0442\u043e\u0433\u0438","text":""},{"location":"chapter_tree/summary/#1","title":"1. \u00a0 \u041e\u0441\u043d\u043e\u0432\u043d\u044b\u0435 \u043c\u043e\u043c\u0435\u043d\u0442\u044b","text":""},{"location":"chapter_tree/summary/#2-q-a","title":"2. \u00a0 Q & A","text":"

Q: \u0414\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0435\u0433\u043e \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0443\u0437\u043b\u0430, \u0432\u044b\u0441\u043e\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u043a\u043e\u0440\u043d\u044f \u043e\u0431\u0435 \u0440\u0430\u0432\u043d\u044b \\(0\\) ?

\u0414\u0430, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u044b\u0441\u043e\u0442\u0430 \u0438 \u0433\u043b\u0443\u0431\u0438\u043d\u0430 \u043e\u0431\u044b\u0447\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u0430\u043a \"\u0447\u0438\u0441\u043b\u043e \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0440\u0435\u0431\u0435\u0440\".

Q: \u0412\u0441\u0442\u0430\u0432\u043a\u0430 \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0441\u043e\u0441\u0442\u0430\u0432\u0435 \u043d\u0430\u0431\u043e\u0440\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0427\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u044d\u0442\u043e\u0442 \"\u043d\u0430\u0431\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439\"? \u041c\u043e\u0436\u043d\u043e \u043b\u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u044d\u0442\u043e \u043a\u0430\u043a \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0443 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u0440\u0435\u0441\u0443\u0440\u0441\u0430?

\u0412\u043e\u0437\u044c\u043c\u0435\u043c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430: \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u0434\u0435\u043b\u0438\u0442\u0441\u044f \u043d\u0430 \u0442\u0440\u0438 \u0441\u043b\u0443\u0447\u0430\u044f, \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u044d\u0442\u0438\u0445 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0448\u0430\u0433\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0443\u0437\u043b\u0430\u043c\u0438.

Q: \u041f\u043e\u0447\u0435\u043c\u0443 \u0443 DFS \u0434\u043b\u044f \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0435\u0441\u0442\u044c \u0442\u0440\u0438 \u043f\u043e\u0440\u044f\u0434\u043a\u0430: \u043f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439? \u0414\u043b\u044f \u0447\u0435\u0433\u043e \u043e\u043d\u0438 \u043d\u0443\u0436\u043d\u044b?

\u041f\u043e\u0434\u043e\u0431\u043d\u043e \u043f\u0440\u044f\u043c\u043e\u043c\u0443 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c\u0443 \u043e\u0431\u0445\u043e\u0434\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043f\u0440\u044f\u043c\u043e\u0439, \u0441\u0438\u043c\u043c\u0435\u0442\u0440\u0438\u0447\u043d\u044b\u0439 \u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043e\u0431\u0445\u043e\u0434\u044b - \u044d\u0442\u043e \u0442\u0440\u0438 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u043c \u0434\u0435\u0440\u0435\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430, \u0433\u0434\u0435 \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043b\u0435\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 < \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0440\u043d\u044f < \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0434\u043e\u0447\u0435\u0440\u043d\u0435\u0433\u043e \u0443\u0437\u043b\u0430 , \u0435\u0441\u043b\u0438 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u0434\u0435\u0440\u0435\u0432\u043e \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u043e\u043c \"\u043b\u0435\u0432\u043e \\(\\rightarrow\\) \u043a\u043e\u0440\u0435\u043d\u044c \\(\\rightarrow\\) \u043f\u0440\u0430\u0432\u043e\", \u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0443\u043f\u043e\u0440\u044f\u0434\u043e\u0447\u0435\u043d\u043d\u0430\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0443\u0437\u043b\u043e\u0432.

Q: \u041f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u0435\u0436\u0434\u0443 node , child \u0438 grand_child . \u0410 \u0441\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 node \u0438 \u0435\u0433\u043e \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u043c \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c \u0440\u0430\u0437\u0432\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0442\u044c? \u041f\u043e\u0441\u043b\u0435 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043e\u043d\u0430 \u0432\u0435\u0434\u044c \u043d\u0435 \u043e\u0431\u043e\u0440\u0432\u0435\u0442\u0441\u044f?

\u041d\u0430 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438. \u0412 \u043f\u0440\u0430\u0432\u043e\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 right_rotate(root) \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u0435\u043d\u044c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430, \u0430 \u0437\u0430\u0442\u0435\u043c \u0447\u0435\u0440\u0435\u0437 return child \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u0435\u043d\u044c \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u043b\u0435 \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f. \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043d\u043e\u0432\u044b\u043c \u043a\u043e\u0440\u043d\u0435\u043c \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u0438 \u0435\u0433\u043e \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u0435\u043c \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043d\u0435 \u0432\u0445\u043e\u0434\u0438\u0442 \u0432 \u043e\u0431\u044f\u0437\u0430\u043d\u043d\u043e\u0441\u0442\u0438 \u0441\u0430\u043c\u043e\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f.

Q: \u0412 C++ \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u0435\u043b\u044f\u0442\u0441\u044f \u043d\u0430 private \u0438 public . \u041a\u0430\u043a\u0430\u044f \u043b\u043e\u0433\u0438\u043a\u0430 \u0441\u0442\u043e\u0438\u0442 \u0437\u0430 \u044d\u0442\u0438\u043c? \u041f\u043e\u0447\u0435\u043c\u0443 height() \u0438 updateHeight() \u043f\u043e\u043c\u0435\u0449\u0430\u044e\u0442 \u0432 \u0440\u0430\u0437\u043d\u044b\u0435 \u043e\u0431\u043b\u0430\u0441\u0442\u0438 \u0432\u0438\u0434\u0438\u043c\u043e\u0441\u0442\u0438?

\u0413\u043b\u0430\u0432\u043d\u044b\u0439 \u043a\u0440\u0438\u0442\u0435\u0440\u0438\u0439 - \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u043c\u0435\u0442\u043e\u0434 \u043d\u0443\u0436\u0435\u043d \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043b\u0430\u0441\u0441\u0430, \u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u0430\u043a private . \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432 updateHeight() \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u043c \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430: \u044d\u0442\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d \u0438\u0437 \u0448\u0430\u0433\u043e\u0432 \u0432\u043d\u0443\u0442\u0440\u0438 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u0438\u043b\u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f. \u0410 height() \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f \u0432\u044b\u0441\u043e\u0442\u044b \u0443\u0437\u043b\u0430, \u043f\u043e\u0434\u043e\u0431\u043d\u043e vector.size() , \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c public .

Q: \u041a\u0430\u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u0438\u0437 \u043d\u0430\u0431\u043e\u0440\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445? \u0412\u0430\u0436\u0435\u043d \u043b\u0438 \u0432\u044b\u0431\u043e\u0440 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430?

\u0414\u0430, \u0432\u0430\u0436\u0435\u043d. \u0421\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u0430 \u0443\u0436\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 build_tree() \u0432 \u043a\u043e\u0434\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043e\u0438\u0441\u043a\u0430. \u0427\u0442\u043e \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0431\u043e\u0440\u0430 \u043a\u043e\u0440\u043d\u044f, \u043e\u0431\u044b\u0447\u043d\u043e \u0432\u0445\u043e\u0434\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u044e\u0442, \u0431\u0435\u0440\u0443\u0442 \u0441\u0440\u0435\u0434\u043d\u0438\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043a\u0430\u043a \u043a\u043e\u0440\u0435\u043d\u044c, \u0430 \u0437\u0430\u0442\u0435\u043c \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0441\u0442\u0440\u043e\u044f\u0442 \u043b\u0435\u0432\u043e\u0435 \u0438 \u043f\u0440\u0430\u0432\u043e\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u044f. \u042d\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432 \u043d\u0430\u0438\u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0431\u0430\u043b\u0430\u043d\u0441 \u0434\u0435\u0440\u0435\u0432\u0430.

Q: \u041d\u0443\u0436\u043d\u043e \u043b\u0438 \u0432 Java \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c equals() \u0434\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a?

\u0412 Java \u0434\u043b\u044f \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 == \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0442\u044c, \u0440\u0430\u0432\u043d\u044b \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u0432\u0443\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445. \u0414\u043b\u044f \u0441\u0441\u044b\u043b\u043e\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u043b\u043e\u0433\u0438\u043a\u0430 \u0443 \u044d\u0442\u0438\u0445 \u0434\u0432\u0443\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0443\u0436\u0435 \u0440\u0430\u0437\u043d\u0430\u044f.

\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u0442\u043e \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c equals() . \u041d\u043e \u0441\u0442\u0440\u043e\u043a\u0438, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u043a String a = \"hi\"; String b = \"hi\"; , \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u043c \u043f\u0443\u043b\u0435 \u043a\u043e\u043d\u0441\u0442\u0430\u043d\u0442 \u0438 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043d\u0430 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 a == b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0434\u0430\u0442\u044c \u0438\u0441\u0442\u0438\u043d\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e.

Q: \u0414\u043e \u0434\u043e\u0441\u0442\u0438\u0436\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0438\u0436\u043d\u0435\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u043f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0440\u0430\u0432\u043d\u043e \\(2^h\\) ?

\u0414\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f \u043f\u043e\u043b\u043d\u043e\u0433\u043e \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u044b\u0441\u043e\u0442\u044b \\(h = 2\\) \u043e\u0431\u0449\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0432\u043d\u043e \\(n = 7\\) , \u0430 \u0447\u0438\u0441\u043b\u043e \u0443\u0437\u043b\u043e\u0432 \u043d\u0430 \u043d\u0438\u0436\u043d\u0435\u043c \u0443\u0440\u043e\u0432\u043d\u0435 \u0440\u0430\u0432\u043d\u043e \\(4 = 2^h = (n + 1) / 2\\) .

"}]}