摘要:原題目題目有一個隊列,排到的人,再次排到隊尾,并將自己變成雙倍。個人也覺得減法更一點
原題目
Sheldon, Leonard, Penny, Rajesh and Howard are in the queue for a "Double Cola" drink vending machine; there are no other people in the queue. The first one in the queue (Sheldon) buys a can, drinks it and doubles! The resulting two Sheldons go to the end of the queue. Then the next in the queue (Leonard) buys a can, drinks it and gets to the end of the queue as two Leonards, and so on.
For example, Penny drinks the third can of cola and the queue will look like this:
Rajesh, Howard, Sheldon, Sheldon, Leonard, Leonard, Penny, Penny
Write a program that will return the name of a man who will drink the n-th cola.
Note that in the very beginning the queue looks like that:
Sheldon, Leonard, Penny, Rajesh, Howard
題目: 有一個隊列Sheldon, Leonard, Penny, Rajesh, Howard,排到的人,再次排到隊尾,并將自己變成雙倍。若給了一個隊列names,排到的第r個人是誰?(題目比較繞,不知道該怎么描述?)
// 初始隊列, 第一輪 ["Sheldon", "Leonard", "Penny", "Rajesh", "Howard"] // Sheldon排過隊之后,重新到隊尾,并將自己復(fù)制了一份 ["Leonard", "Penny", "Rajesh", "Howard", "Sheldon", "Sheldon"] // ... // 第二輪,隊列如下 ["Sheldon", "Sheldon", "Leonard", "Leonard", "Penny", "Penny", "Rajesh", "Rajesh", "Howard", "Howard"] // 第三輪,隊列如下 ["Sheldon", "Sheldon", "Sheldon", "Sheldon", "Leonard", "Leonard", "Leonard", "Leonard", "Penny", "Penny", "Penny", "Penny", "Rajesh", "Rajesh", "Rajesh", "Rajesh", "Howard", "Howard", "Howard", "Howard"]My Solution
隊列每輪過后,隊列中的人數(shù)將變?yōu)樵瓉淼亩丁H舫跏缄犃械拈L度為len,第r人位于第m輪
第一輪:n=1,sum = len
第二輪:n=2,sum = len + len * 2
第三輪:n=4,sum = len + len * 2 + len * 4
...
第m輪:n=2^m, sum = len + len * 2 + …… + len * n
則,第r個人在第m輪的排名為:x = r + len * n - sum
則,第r個人的名字在初始隊列中排名為t(數(shù)組下標為t-1),則:(t-1) * n < x <= t * n
所以,t = Math.ceil( x / n )
最后,代碼整理如下:
function whoIsNext(names, r){ var len = names.length; var sum = names.length; var n = 1; while(sum < r) { n *= 2; sum += len * n; } return names[Math.ceil((r + len * n - sum) / n) - 1] }Best Practices / Clever
function whoIsNext(names, r) { var l = names.length; while (r >= l) { r -= l; l *= 2; } return names[Math.ceil(names.length * r / l)-1]; }對比
兩個方法一個用加法,一個用減法,得出最后一輪的人數(shù)。個人也覺得 減法 更 clever 一點 ?
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/88866.html
摘要:裝飾者模式是動態(tài)地將責任附加到對象上。然后我們在子類計算價格的時候加上父類中計算好的配料的價格。結(jié)果可樂加冰可樂加冰加糖在的類庫中就有很多實際應(yīng)用到了裝飾模式,比如就可以用來裝飾,提供更加強大的功能。 裝飾者模式是動態(tài)地將責任附加到對象上。若要擴展功能,裝飾者提供了比繼承更有彈性的替代方案。 假設(shè)我們有一個需求,是給一家飲料店做一個計算各種飲料價格的功能。聽起來很簡單,我們創(chuàng)建一個抽象...
摘要:是一個基于和的服務(wù)器端和瀏覽器端的的前后端全棧應(yīng)用框架。是的組件,并且會進行數(shù)據(jù)初始化不但可以支持的數(shù)據(jù)初始化,還可以合并和的,使用同一個,和的無縫結(jié)合。 koa-cola是一個基于koa和react的服務(wù)器端SSR(server side render)和瀏覽器端的SPA(single page application)的web前后端全棧應(yīng)用框架。 koa-cola使用typescr...
摘要:原題目題目找出一個數(shù)值該數(shù)值將以字符串的形式傳入中最大的五位數(shù)。如果數(shù)字的位數(shù)小于,則直接返回該數(shù)值如果數(shù)字的位數(shù)不小于六位,則依次截取連續(xù)的位數(shù),求取最大值對比中使用了遞歸。 原題目 In the following 6 digit number:28391091 is the greatest sequence of 2 digits. In the following 10 di...
摘要:若提供比較函數(shù)返回值返回值不變返回值交換位置升序排列后,再利用反序?qū)⒆址D(zhuǎn)換為可選參數(shù),表示進制。規(guī)定使用,但是并不是所有的瀏覽器都遵循這個規(guī)定。因此,永遠都要明確給出參數(shù)的值。若傳入的字符串中含有非數(shù)字字符,將返回。 原題目 Your task is to make a function that can take any non-negative integer as a ar...
摘要:原題目題目有一個不少于四個元素的數(shù)組,計算其中兩個最小值的和。對比我寫的方法比較常規(guī),中采用了的解構(gòu)賦值和箭頭函數(shù) 原題目 Create a function that returns the sum of the two lowest positive numbers given an array of minimum 4 integers. No floats or empty ...
閱讀 3143·2021-09-22 15:50
閱讀 3343·2021-09-10 10:51
閱讀 3170·2019-08-29 17:10
閱讀 2934·2019-08-26 12:14
閱讀 1849·2019-08-26 12:00
閱讀 970·2019-08-26 11:44
閱讀 665·2019-08-26 11:44
閱讀 2833·2019-08-26 11:41