π¨βπ»
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} arr | |
* @return {number} | |
*/ | |
var numOfSubarrays = function(arr) { | |
const MOD = 10**9 + 7; | |
let n = arr.length; | |
let count = 0; | |
let prefixSum = 0; | |
let oddCount = 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[][]} edges | |
* @param {number} bob | |
* @param {number[]} amount | |
* @return {number} | |
*/ | |
var mostProfitablePath = function(edges, bob, amount) { | |
const n = amount.length; | |
const graph = new Map(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {number[]} preorder |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {string} traversal |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string[]} nums | |
* @return {string} | |
*/ | |
var findDifferentBinaryString = function(nums) { | |
const n = nums.length; | |
const numsSet = new Set(nums); | |
// Generate all possible binary strings of length n | |
for (let i = 0; i < 2 ** n; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} n | |
* @param {number} k | |
* @return {string} | |
*/ | |
var getHappyString = function(n, k) { | |
// Helper function to perform backtracking and generate all happy string | |
function backtrack(current, result) { | |
// If the current string has reached the desired length, add it to the result list | |
if (current.length === n) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} pattern | |
* @return {string} | |
*/ | |
var smallestNumber = function(pattern) { | |
const n = pattern.length; | |
const result = []; | |
const stack = []; | |
for (let i = 0; i <= n; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {string} tiles | |
* @return {number} | |
*/ | |
var numTilePossibilities = function(tiles) { | |
// Helper function for backtracking | |
function backtrack(path, remaining) { | |
// If the current path is not empty, add it to the set of unique sequences | |
if (path.length > 0) { | |
uniqueSequences.add(path.join('')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number} n | |
* @return {number[]} | |
*/ | |
var constructDistancedSequence = function(n) { | |
// Initialize the sequence array with 0s and an array to track placed numbers | |
const seq = new Array(n * 2 - 1).fill(0), isPlaced = new Array(n + 1).fill(false); | |
// Mark index 0 as placed (not used in actual sequence) | |
isPlaced[0] = true; |