Kev daws teeb meem los ntawm kev xam phaj Google hauv JavaScript: 4 txoj hauv kev sib txawv

Kev daws teeb meem los ntawm kev xam phaj Google hauv JavaScript: 4 txoj hauv kev sib txawv

Thaum kuv tab tom kawm txog kev ua tau zoo ntawm algorithms, kuv tuaj hla qhov no Nov yog ib daim vis dis aus ntawm kev sib tham hauv Google.. Nws tsis tsuas yog muab lub tswv yim ntawm yuav ua li cas kev xam phaj ntawm cov tuam txhab thev naus laus zis loj, tab sis kuj tso cai rau koj kom nkag siab tias yuav daws teeb meem algorithmic li cas thiaj li ua tau.

Cov kab lus no yog ib hom kev sib txuas rau hauv video. Hauv nws kuv muab cov lus qhia rau txhua qhov kev daws teeb meem, ntxiv rau kuv tus kheej version ntawm kev daws teeb meem hauv JavaScript. Cov nuances ntawm txhua algorithm kuj tau tham txog.

Peb nco qab: rau txhua tus neeg nyeem Habr - 10 ruble luv nqi thaum tso npe rau hauv ib chav kawm Skillbox siv Habr promo code.

Skillbox pom zoo: Cov chav kawm siv tau "Mobile Developer PRO".

Nqe lus ntawm qhov teeb meem

Peb muab ib qho kev txiav txim array thiab tus nqi tshwj xeeb. Tom qab ntawd nws raug nug kom tsim ib txoj haujlwm uas rov qab muaj tseeb lossis tsis tseeb nyob ntawm seb tus lej ntawm ob tus lej hauv qhov array tuaj yeem sib npaug ntawm tus nqi muab.

Hauv lwm lo lus, puas muaj ob tus lej hauv qhov array, x thiab y, uas thaum ntxiv ua ke sib npaug ntawm tus nqi teev?

Piv txwv A

Yog tias peb muab ib qho array [1, 2, 4, 9] thiab tus nqi yog 8, qhov kev ua haujlwm yuav rov qab tsis tseeb vim tsis muaj ob tus lej hauv array tuaj yeem ntxiv txog 8.

Piv txwv B

Tab sis yog tias nws yog ib qho array [1, 2, 4, 4] thiab tus nqi yog 8, txoj haujlwm yuav tsum rov qab muaj tseeb vim 4 + 4 = 8.

Txoj Kev 1: Lub zog loj

Sijhawm complexity: O(NΒ²).
Qhov chaw complexity: O(1).

Lub ntsiab lus pom tseeb tshaj plaws yog siv ib khub ntawm nested loops.

const findSum = (arr, val) => {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length; j++) {
      if (i !== j && arr[i] + arr[j] === val) {
        return true;
      };
    };
  };
  return false;
};

Qhov kev daws teeb meem no tsis muaj txiaj ntsig vim tias nws tshawb xyuas txhua qhov ua tau ntawm ob lub ntsiab lus hauv array thiab tseem muab piv rau txhua khub ntawm qhov ntsuas ob zaug. (Piv txwv li, thaum i = 1 thiab j = 2 yeej zoo ib yam li kev sib piv nrog i = 2 thiab j = 1, tab sis hauv cov tshuaj no peb sim ob qho kev xaiv).

Vim tias peb cov kev daws teeb meem siv ib khub ntawm nested rau loops, nws yog quadratic nrog O (NΒ²) lub sij hawm complexity.


Txoj Kev 2: Kev Tshawb Nrhiav Binary

Sijhawm complexity: O(Nlog(N)).
Qhov chaw complexity: O (1)
.

Txij li thaum cov arrays raug txiav txim, peb tuaj yeem tshawb nrhiav kev daws teeb meem siv kev tshawb nrhiav binary. Qhov no yog qhov ua tau zoo tshaj plaws algorithm rau xaj arrays. Binary nrhiav nws tus kheej muaj lub sijhawm ua haujlwm ntawm O(log(N)). Txawm li cas los xij, koj tseem yuav tsum tau siv lub voj voog txhawm rau txheeb xyuas txhua lub ntsiab lus tiv thaiv tag nrho lwm cov txiaj ntsig.

Nov yog qhov kev daws teeb meem zoo li cas. Txhawm rau ua kom pom tseeb, peb siv cov haujlwm cais los tswj binary tshawb nrhiav. Thiab tseem lub removeIndex() muaj nuj nqi, uas rov qab version ntawm lub array rho tawm qhov muab index.

const findSum = (arr, val) => {
  for (let i = 0; i < arr.length; i++){
    if (binarySearch(removeIndex(arr, i), val - arr[i])) {
      return true;
    }
  };
  return false;
};
 
const removeIndex = (arr, i) => {
  return arr.slice(0, i).concat(arr.slice(i + 1, arr.length));
};
 
const binarySearch = (arr, val) => {
  let start = 0;
  let end = arr.length - 1;
  let pivot = Math.floor(arr.length / 2); 
  while (start < end) {
    if (val < arr[pivot]) {
      end = pivot - 1;
    } else if (val > arr[pivot]) {
      start = pivot + 1;
    };
    pivot = Math.floor((start + end) / 2);
    if (arr[pivot] === val) {
      return true;
    }
  };
  return false;
};

Lub algorithm pib los ntawm index [0]. Nws ces tsim ib tug version ntawm cov array tsis suav cov thawj qhov Performance index thiab siv binary tshawb nrhiav seb puas muaj ib qho ntawm cov nqi ntxiv tuaj yeem ntxiv rau cov array los tsim cov txiaj ntsig xav tau. Qhov kev txiav txim no tau ua ib zaug rau txhua lub hauv paus.

Lub rau voj nws tus kheej yuav muaj lub sij hawm linear complexity ntawm O (N), tab sis nyob rau hauv lub voj peb ua ib tug binary search, uas muab ib tug tag nrho lub sij hawm complexity ntawm O (Nlog (N)). Qhov kev daws teeb meem no zoo dua li yav dhau los, tab sis tseem muaj chaw rau kev txhim kho.


Solution 3: Linear time

Lub sij hawm complexity: O (N).
Qhov chaw complexity: O(1).

Tam sim no peb yuav daws qhov teeb meem, nco ntsoov tias cov array raug txheeb xyuas. Txoj kev daws teeb meem yog coj ob tus lej: ib qho ntawm qhov pib thiab ib qho kawg. Yog tias qhov tshwm sim txawv ntawm qhov xav tau, ces hloov cov ntsiab lus pib thiab xaus.

Thaum kawg peb yuav ntsib tus nqi uas xav tau thiab rov qab muaj tseeb, lossis cov ntsiab lus pib thiab xaus yuav sib sau ua ke thiab rov qab tsis tseeb.

const findSum = (arr, val) => {
  let start = 0;
  let end = arr.length - 1;
  while (start < end) {
    let sum = arr[start] + arr[end];
    if (sum > val) {
      end -= 1;
    } else if (sum < val) {
      start += 1;
    } else {
      return true;
    };
  };
  return false;
};


Tam sim no txhua yam zoo, qhov kev daws teeb meem zoo li zoo. Tab sis leej twg tuaj yeem lav tias qhov array raug txiav txim?

Yog li cas?

Thaum xub thawj siab ib muag, peb tuaj yeem tau yooj yim xaj cov array ua ntej thiab tom qab ntawd siv cov tshuaj saum toj no. Tab sis qhov no yuav cuam tshuam li cas rau lub sijhawm ua tiav?

Qhov zoo tshaj plaws algorithm yog quicksort nrog lub sij hawm complexity O (Nlog (N)). Yog tias peb siv nws hauv peb qhov kev daws teeb meem, nws yuav hloov nws qhov kev ua tau zoo ntawm O (N) mus rau O (Nlog (N)). Puas muaj peev xwm nrhiav tau ib qho kev daws teeb meem nrog ib qho array uas tsis tau txiav txim?

4 Kev daws

Lub sij hawm complexity: O (N).
Qhov chaw complexity: O (N).

Yog lawm, muaj ib txoj hauv kev daws teeb meem; ua qhov no, peb yuav tsum tsim cov array tshiab uas muaj cov npe sib tw uas peb tab tom nrhiav. Kev lag luam tawm ntawm no yog siv ntau lub cim xeeb: nws yog tib txoj kev daws teeb meem hauv daim ntawv nrog qhov chaw tsis yooj yim dua O (1).

Yog tias thawj tus nqi ntawm qhov muab array yog 1 thiab tus nqi tshawb nrhiav yog 8, peb tuaj yeem ntxiv tus nqi 7 rau "kev tshawb nrhiav qhov tseem ceeb" array.

Tom qab ntawd, thaum peb ua txhua yam ntawm cov array, peb tuaj yeem tshawb xyuas qhov array ntawm "kev tshawb nrhiav qhov tseem ceeb" thiab pom tias ib qho ntawm lawv sib npaug rau peb tus nqi. Yog tias muaj, rov qab muaj tseeb.

const findSum = (arr, val) => {
  let searchValues = [val - arr[0]];
  for (let i = 1; i < arr.length; i++) {
    let searchVal = val - arr[i];
    if (searchValues.includes(arr[i])) {
      return true;
    } else {
      searchValues.push(searchVal);
    }
  };
  return false;
};

Lub hauv paus ntawm kev daws yog ib lub voj voog, uas, raws li peb pom saum toj no, muaj lub sij hawm linear complexity ntawm O (N).

Qhov thib ob qhov tseem ceeb ntawm peb txoj haujlwm yog Array.prototype.include(), JavaScript txoj kev uas yuav rov qab muaj tseeb lossis tsis tseeb nyob ntawm seb qhov array muaj tus nqi muab.

Txhawm rau txheeb xyuas lub sijhawm nyuaj ntawm Array.prototype.includes(), peb tuaj yeem saib cov polyfill muab los ntawm MDN (thiab sau rau hauv JavaScript) lossis siv ib txoj hauv kev hauv qhov chaws ntawm lub cav JavaScript xws li Google V8 (C++).

// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
  Object.defineProperty(Array.prototype, 'includes', {
    value: function(valueToFind, fromIndex) {
 
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }
 
      // 1. Let O be ? ToObject(this value).
      var o = Object(this);
 
      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;
 
      // 3. If len is 0, return false.
      if (len === 0) {
        return false;
      }
 
      // 4. Let n be ? ToInteger(fromIndex).
      //    (If fromIndex is undefined, this step produces the value 0.)
      var n = fromIndex | 0;
 
      // 5. If n β‰₯ 0, then
      //  a. Let k be n.
      // 6. Else n < 0,
      //  a. Let k be len + n.
      //  b. If k < 0, let k be 0.
      var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
 
      function sameValueZero(x, y) {
        return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
      }
 
      // 7. Repeat, while k < len
      while (k < len) {
        // a. Let elementK be the result of ? Get(O, ! ToString(k)).
        // b. If SameValueZero(valueToFind, elementK) is true, return true.
        if (sameValueZero(o[k], valueToFind)) {
          return true;
        }
        // c. Increase k by 1.
        k++;
      }
 
      // 8. Return false
      return false;
    }
  });
}

Ntawm no yog ib feem ntawm Array.prototype.include() yog lub voj voog nyob rau hauv kauj ruam 7 uas (yuav luag) traverses tag nrho ntev ntawm cov muab array. Qhov no txhais tau hais tias nws lub sij hawm complexity kuj linear. Zoo, txij li nws ib txwm yog ib kauj ruam tom qab peb lub ntsiab array, lub sij hawm complexity yog O (N + (N - 1)). Siv Big O Notation, peb ua kom yooj yim rau O (N) - vim nws yog N uas muaj kev cuam tshuam loj tshaj plaws thaum nce qhov loj me.

Hais txog spatial complexity, xav tau ib qho ntxiv array uas nws ntev tsom iav qhov qub array ( rho tawm ib qho, yog, tab sis qhov ntawd tuaj yeem tsis quav ntsej), ua rau O (N) spatial complexity. Zoo, nce kev siv lub cim xeeb kom ua tau zoo tshaj plaws ntawm cov algorithm.


Kuv vam tias koj yuav pom cov ntawv tseem ceeb ntxiv rau koj qhov kev sib tham video. Nws qhia tau hais tias qhov teeb meem yooj yim tuaj yeem daws tau los ntawm ntau txoj hauv kev nrog ntau cov peev txheej siv (lub sijhawm, nco).

Skillbox pom zoo:

Tau qhov twg los: www.hab.com

Ntxiv ib saib