Title: #1622 - Fancy Sequence - LeetCode JavaScript Solutions
Open Graph Title: #1622 - Fancy Sequence - LeetCode JavaScript Solutions
X Title: #1622 - Fancy Sequence - LeetCode JavaScript Solutions
Description: Write an API that generates fancy sequences using the append, addAll, and multAll operations. Implement the Fancy class: Fancy() Initialize...
Open Graph Description: Write an API that generates fancy sequences using the append, addAll, and multAll operations. Implement the Fancy class: Fancy() Initialize...
X Description: Write an API that generates fancy sequences using the append, addAll, and multAll operations. Implement the Fancy class: Fancy() Initialize...
Keywords:
Opengraph URL: https://leetcodejavascript.com/solutions/fancy-sequence
Domain: leetcodejavascript.com
{"@context":"https://schema.org","@type":"Code","url":"https://leetcodejavascript.com/solutions/fancy-sequence","name":"#1622 - Fancy Sequence - LeetCode JavaScript Solutions","description":"Write an API that generates fancy sequences using the append, addAll, and multAll operations. Implement the Fancy class: Fancy() Initialize...","programmingLanguage":"JavaScript","codeRepository":"https://github.com/JoshCrozier/leetcode-javascript","codeSampleType":"JavaScript","text":"var Fancy = function() {\n this.sequence = [];\n this.additive = 0n;\n this.multiplicative = 1n;\n this.MOD = 1000000007n;\n};\n\n/**\n * @param {number} val\n * @return {void}\n */\nFancy.prototype.append = function(val) {\n const valBig = BigInt(val);\n const inverse = this.modInverse(this.multiplicative);\n const adjustedVal = ((valBig - this.additive + this.MOD) * inverse) % this.MOD;\n this.sequence.push(adjustedVal);\n};\n\n/**\n * @param {number} inc\n * @return {void}\n */\nFancy.prototype.addAll = function(inc) {\n this.additive = (this.additive + BigInt(inc)) % this.MOD;\n};\n\n/**\n * @param {number} m\n * @return {void}\n */\nFancy.prototype.multAll = function(m) {\n const mBig = BigInt(m);\n this.additive = (this.additive * mBig) % this.MOD;\n this.multiplicative = (this.multiplicative * mBig) % this.MOD;\n};\n\n/**\n * @param {number} idx\n * @return {number}\n */\nFancy.prototype.getIndex = function(idx) {\n if (idx >= this.sequence.length) return -1;\n return Number((this.sequence[idx] * this.multiplicative + this.additive) % this.MOD);\n};\n\n/**\n * @param {number} a\n * @return {number}\n */\nFancy.prototype.modInverse = function(a) {\n let m = this.MOD;\n const m0 = m;\n let x = 1n;\n let y = 0n;\n\n a = a % m;\n while (a > 1n) {\n const q = a / m;\n [a, m] = [m, a % m];\n [x, y] = [y, x - q * y];\n }\n\n return x < 0n ? x + m0 : x;\n};","keywords":"LeetCode 1622, #1622 - Fancy Sequence, Hard, JavaScript solution","learningResourceType":"Code","isAccessibleForFree":true,"educationalLevel":"intermediate","interactivityType":"mixed"}
| author | LeetCodeJavascript.com |
| og:type | article |
| og:image | https://leetcodejavascript.com/og-image.jpg |
| og:site_name | LeetCode JavaScript Solutions |
| twitter:card | summary_large_image |
| twitter:url | https://leetcodejavascript.com/solutions/fancy-sequence |
| twitter:image | https://leetcodejavascript.com/og-image.jpg |
| twitter:creator | @joshcrozier |
| theme-color | #1f2937 |
Links:
| LeetCodeJavascript.com | https://leetcodejavascript.com/ |
| Star on GitHub | https://github.com/JoshCrozier/leetcode-javascript |
| Back to all solutions | https://leetcodejavascript.com |
| View on LeetCode | https://leetcode.com/problems/fancy-sequence/ |
| View on GitHub | https://github.com/JoshCrozier/leetcode-javascript/blob/master/solutions/1622-fancy-sequence.js |
| Math | https://leetcodejavascript.com/tags/math |
| Design | https://leetcodejavascript.com/tags/design |
| Segment Tree | https://leetcodejavascript.com/tags/segment-tree |
| Josh Crozier | https://joshcrozier.com |
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow