{"id":1397,"date":"2020-08-14T16:33:09","date_gmt":"2020-08-14T16:33:09","guid":{"rendered":"https:\/\/lvboard.infostore.in.ua\/?p=1397"},"modified":"2020-08-14T16:33:09","modified_gmt":"2020-08-14T16:33:09","slug":"%d1%80%d0%b5%d1%88%d0%b5%d0%bd%d0%b8%d0%b5-%d0%bd%d0%b5%d1%81%d0%ba%d0%be%d0%bb%d1%8c%d0%ba%d0%b8%d1%85-%d0%b7%d0%b0%d0%b4%d0%b0%d1%87-%d0%be%d1%82-amazon-%d0%bd%d0%b0-%d0%bf%d1%80%d0%b8%d0%bc%d0%b5","status":"publish","type":"post","link":"https:\/\/lvboard.infostore.in.ua\/?p=1397","title":{"rendered":"\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447 \u043e\u0442 Amazon \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 JavaScript"},"content":{"rendered":"\n<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438&nbsp;<a href=\"https:\/\/medium.com\/javascript-in-plain-english\/amazon-coding-interview-questions-187eaf91f194\">\u00abAmazon Coding Interview Questions\u00bb<\/a>&nbsp;\u0430\u0432\u0442\u043e\u0440\u0430 Trung Anh Dang.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0430\u0432\u0442\u043e\u0440 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e (\u0442\u0440\u0438, \u0435\u0441\u043b\u0438 \u0431\u044b\u0442\u044c \u0442\u043e\u0447\u043d\u0435\u0435) \u0437\u0430\u0434\u0430\u0447 \u043e\u0442 Amazon (\u043a\u0430\u043a \u043e\u043d \u0443\u0442\u0432\u0435\u0440\u0436\u0434\u0430\u0435\u0442) \u0438 \u0441\u0432\u043e\u0438 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0440\u0435\u0448\u0435\u043d\u0438\u0439.<\/p>\n\n\n\n<p>\u041f\u043e\u0441\u043b\u0435 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0438, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0439\u0442\u0435 \u0440\u0435\u0448\u0438\u0442\u044c \u0435\u0435 \u0441\u0430\u043c\u043e\u0441\u0442\u043e\u044f\u0442\u0435\u043b\u044c\u043d\u043e. \u0417\u0430\u0442\u0435\u043c \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u0435 \u0441\u0432\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0441 \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u0438\u043c. \u041c\u043e\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u043e\u043a\u0430\u0437\u0430\u043b\u0438\u0441\u044c \u0431\u043b\u0438\u0437\u043a\u0438\u043c\u0438 \u043a \u0430\u0432\u0442\u043e\u0440\u0441\u043a\u0438\u043c, \u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0445\u0443\u0436\u0435. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u043f\u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0432\u0430\u0448\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043b\u0443\u0447\u0448\u0435, \u043f\u0440\u043e\u0448\u0443 \u043f\u043e\u0434\u0435\u043b\u0438\u0442\u044c\u0441\u044f \u0438\u043c \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n\n\n\n<p>\u0418\u0442\u0430\u043a, \u043f\u043e\u0435\u0445\u0430\u043b\u0438.<\/p>\n\n\n\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u2116 1<\/h3>\n\n\n\n<p>\u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0434\u043b\u0438\u043d \u0441\u0435\u0440\u0438\u0439 \u0438\u043b\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432 (run-length encoding) \u2014 \u044d\u0442\u043e \u0431\u044b\u0441\u0442\u0440\u044b\u0439 \u0438 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a. \u0421\u0443\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0437\u0430\u043c\u0435\u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0445\u0441\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 (\u0441\u0435\u0440\u0438\u0438) \u043d\u0430 \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b \u0438 \u0447\u0438\u0441\u043b\u043e \u0435\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432. \u0421\u0435\u0440\u0438\u0435\u0439 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0430\u044f \u0438\u0437 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u041f\u0440\u0438 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u0442\u0440\u043e\u043a\u0430 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0438\u0445 \u0441\u0435\u0440\u0438\u044e, \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u0441\u0430\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0449\u0438\u0439\u0441\u044f \u0441\u0438\u043c\u0432\u043e\u043b \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0435\u0433\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432.<\/p>\n\n\n\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0441\u0442\u0440\u043e\u043a\u0430&nbsp;<code>\"AAAABBBCCDAA\"<\/code>&nbsp;\u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a&nbsp;<code>\"4A3B2C1D2A\"<\/code>.<\/p>\n\n\n\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0439\u0442\u0435 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432. \u0421\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0431\u0443\u043a\u0432, \u043d\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0438\u0441\u0435\u043b. \u0421\u0442\u0440\u043e\u043a\u0430 \u0434\u043b\u044f \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0439.<\/p>\n\n\n\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n\n\n\n<h4>\u041a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438<\/h4>\n\n\n\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const encoding_string = function(string) {\n    if(string.length === 0) return ''\n    \n    let currChar = string.charAt(0)\n    let count = 1\n    let encoding = ''\n    \n    for(let i = 1; i &lt; string.length; i++) {\n        const char = string.charAt(i)\n        if(char === currChar) count++\n        else {\n            encoding += count + currChar\n            \n            \/\/ \u0441\u0431\u0440\u043e\u0441\n            count = 1\n            currChar = char\n        }\n    }\n    \n    encoding += count + currChar\n    \n    return encoding\n}\n<\/code><\/pre>\n\n\n\n<h4>\u0414\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438<\/h4>\n\n\n\n<p>\u041d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \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:<\/p>\n\n\n\n<p>1. \u0424\u0443\u043d\u043a\u0446\u0438\u044e, \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u044e\u0449\u0443\u044e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u0438\u043c\u0432\u043e\u043b \u0447\u0438\u0441\u043b\u043e\u043c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const isNumber = function(str) {\n    return \/^\\d+$\/.test(str)\n}\n<\/code><\/pre>\n\n\n\n<p>2. \u0424\u0443\u043d\u043a\u0446\u0438\u044e, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const addCountAmount = function(string, char, count) {\n    for(let i = 1; i &lt;= count; i++) {\n        string += char\n    }\n    \n    return string\n}\n<\/code><\/pre>\n\n\n\n<p>\u0412\u043e\u0442 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const decoding_string = function(string) {\n    if(string.length === 0) return ''\n    let currCount = 0\n    let i = 0\n    let decoding = ''\n    \n    while(i &lt; string.length) {\n        const char = string.charAt(i)\n        if(isNumber(char)) {\n            const num = +char\n            currCount = currCount * 10 + num\n        } else {\n            decoding = addCountAmount(decoding, char, currCount)\n            currCount = 0\n        }\n        \n        i++\n    }\n    \n    return decoding\n}\n<\/code><\/pre>\n\n\n\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<p>1. \u0421\u0442\u0440\u043e\u043a\u0430&nbsp;<code>\"AAAABBBCCDAA\"<\/code>&nbsp;\u0434\u043e\u043b\u0436\u043d\u0430 \u0431\u044b\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0430 \u0432&nbsp;<code>\"4A3B2C1D2A\"<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(encoding_string(\"AAAABBBCCDAA\")) \/\/ 4A3B2C1D2A\n<\/code><\/pre>\n\n\n\n<p>2. \u041e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(decoding_string(\"4A3B2C1D2A\")) \/\/ AAAABBBCCDAA\n<\/code><\/pre>\n\n\n\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u2116 2<\/h3>\n\n\n\n<p>\u0415\u0441\u0442\u044c \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0430 \u0441 N \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e\u043c \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 \u043e\u0434\u043d\u0443 \u0438\u043b\u0438 \u0434\u0432\u0435 \u0441\u0442\u0443\u043f\u0435\u043d\u0438. \u0414\u0430\u043d\u043e N. \u041d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0443\u044e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435. \u041f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u0440\u0435\u043e\u0434\u043e\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439 \u043d\u0435 \u0432\u0430\u0436\u0435\u043d.<\/p>\n\n\n\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 N = 4, \u0442\u043e \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 5 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">    1, 1, 1, 1\n    2, 1, 1\n    1, 2, 1\n    1, 1, 2\n    2, 2\n<\/pre>\n\n\n\n<p>\u0427\u0442\u043e \u0435\u0441\u043b\u0438 \u0443\u0431\u0440\u0430\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439? \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 X = {1, 3, 5}, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0430 1, 3 \u0438\u043b\u0438 5 \u0441\u0442\u0443\u043f\u0435\u043d\u0435\u0439 \u0437\u0430 \u0440\u0430\u0437.<\/p>\n\n\n\n<h3>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h3>\n\n\n\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 (\u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435\u043c):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const climb_stairs_1 = function(stairs) {\n    if(stairs === 1) return 1\n    if(stairs === 2) return 2\n\n    let prev = 1\n    let curr = 2\n    for(let i = 2; i &lt; stairs; i++) {\n        const sum = prev + curr\n        prev = curr\n        curr = sum\n    }\n    return curr\n}\n<\/code><\/pre>\n\n\n\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0430\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u0434\u043d\u044f\u0442\u044c\u0441\u044f \u043f\u043e \u043b\u0435\u0441\u0442\u043d\u0438\u0446\u0435 (\u0431\u0435\u0437 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const climb_stairs_2 = function(stairs, nums) {\n    const dp = Array(stairs + 1).fill(0)\n\n    for(let i = 1; i &lt;= stairs; i++) {\n        \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043e\u0431\u0449\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e f(i - x), \u0433\u0434\u0435 x - \u043a\u0430\u0436\u0434\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 nums\n        let total = 0\n        for(let j = 0; j &lt; nums.length; j++) {\n            const num = nums&#91;j]\n\n            \/\/ \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u0435 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\n            if(i - num > 0) total += dp&#91;i - num]\n        }\n        dp&#91;i] += total\n\n        \/\/ \u0435\u0441\u043b\u0438 i \u0438\u043c\u0435\u0435\u0442\u0441\u044f \u0432 nums, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\n        if(nums.indexOf(i) !== -1) dp&#91;i] += 1\n    }\n    \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0447\u0438\u0441\u043b\u043e \u0432 dp\n    return dp&#91;dp.length - 1]\n}\n<\/code><\/pre>\n\n\n\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(climb_stairs_1(4)) \/\/ 5\n\nconsole.log(climb_stairs_2(4, &#91;1, 3, 5])) \/\/ 3\n<\/code><\/pre>\n\n\n\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u2116 3<\/h3>\n\n\n\n<p>\u0414\u0430\u043d\u043e \u0446\u0435\u043b\u043e\u0435 \u0447\u0438\u0441\u043b\u043e K \u0438 \u0441\u0442\u0440\u043e\u043a\u0430 S. \u041d\u0443\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0434\u043b\u0438\u043d\u0443 \u0441\u0430\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 K \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432.<\/p>\n\n\n\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u0430\u043d\u0430&nbsp;<code>s = \"abcba\"<\/code>&nbsp;\u0438&nbsp;<code>k = 2<\/code>. \u0421\u0430\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u0441 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 K \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f&nbsp;<code>\"bcb\"<\/code>.<\/p>\n\n\n\n<h4>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/h4>\n\n\n\n<p>\u0412\u043e\u0442 \u043c\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const k_longest_substring = function(string, k) {\n    let l = 0\n    let r = 0\n    const charCount = new Map()\n    let longestSubstring = ''\n\n    while(r &lt; string.length) {\n        const char = string.charAt(r)\n\n        \/\/ \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0441\u0447\u0435\u0442\u0447\u0438\u043a\n        if(charCount.has(char)) {\n            charCount.set(char, charCount.get(char) + 1)\n        } else {\n            charCount.set(char, 1)\n        }\n\n        \/\/ \u0440\u0430\u0437\u043c\u0435\u0440 charCount \u0440\u0430\u0432\u0435\u043d k\n        \/\/ \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u043c \u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u0432\u043b\u0435\u0432\u043e \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 k \u0440\u0430\u0437\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432\n        if(charCount.size > k) {\n            while(charCount.size > k &amp;&amp; l &lt; r) {\n                const leftChar = string.charAt(l)\n                const count = charCount.get(leftChar)\n\n                if(count === 1) charCount.delete(leftChar)\n                else charCount.set(leftChar, count - 1)\n\n                l++\n            }\n        }\n\n        const substring = string.substring(l, r + 1)\n        longestSubstring = substring.length > longestSubstring.length ? substring : longestSubstring\n        r++\n    }\n    return longestSubstring\n}\n<\/code><\/pre>\n\n\n\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(k_longest_substring(\"abcba\", 2)) \/\/ bcb\n<\/code><\/pre>\n\n\n\n<p>\u041b\u0435\u0433\u043a\u043e, \u043d\u0435 \u043f\u0440\u0430\u0432\u0434\u0430 \u043b\u0438?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0414\u043e\u0431\u0440\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0441\u0443\u0442\u043e\u043a. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0432\u0430\u0448\u0435\u043c\u0443 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u044e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u0441\u0442\u0430\u0442\u044c\u0438&nbsp;\u00abAmazon Coding Interview Questions\u00bb&nbsp;\u0430\u0432\u0442\u043e\u0440\u0430 Trung Anh Dang.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[30],"tags":[95],"_links":{"self":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1397"}],"collection":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1397"}],"version-history":[{"count":1,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1397\/revisions"}],"predecessor-version":[{"id":1398,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=\/wp\/v2\/posts\/1397\/revisions\/1398"}],"wp:attachment":[{"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lvboard.infostore.in.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}