Skip to content

Instantly share code, notes, and snippets.

View varenya's full-sized avatar

Varenya Thyagaraj varenya

  • Equal Experts
  • Bangalore
View GitHub Profile
function validatePizza(layers: string[], rules: Rule[]): Rule | boolean {
const indexMap = layers.reduce((acc, curr, index) => {
return acc.set(curr, index);
}, new Map<string, number>());
for (let rule of rules) {
const [ing1, ing2] = rule;
if ((indexMap.get(ing1) || 0) < (indexMap.get(ing2) || 0)) {
continue;
}
return rule;
function majorityElement(elements: number[]): number {
let m: number | undefined = undefined;
let c = 0;
for (let element of elements) {
if (c === 0) {
m = element;
c = 1;
} else if (m === element) {
c += 1;
} else {
function maxSubArraySum(input: number[]): number {
let maxSum = input[0];
let maxEnding = input[0];
for (let i = 1; i < input.length; i++) {
maxEnding = Math.max(maxEnding + input[i], input[i]);
maxSum = Math.max(maxSum, maxEnding);
}
return maxSum;
}
type Grid = number[][];
type Point = { x: number; y: number };
function zoom(grid: number[][], k: number): number[][] {
const rowSize = grid.length;
const colSize = grid[0].length;
const zoomedGrid = Array.from({ length: rowSize * k }, () =>
Array(colSize * k),
);
for (let i = 0; i < rowSize; i++) {
function moveNums(nums: number[], n: number): number[] {
let i = 0,
last = nums.length - 1;
while (i < last) {
if (nums[i] === n) {
nums.splice(i, 1);
let last = nums.length;
nums[last] = n;
}
i++;
type Day =
| "Sunday"
| "Monday"
| "Tuesday"
| "Wednesday"
| "Thursday"
| "Friday"
| "Saturday"
| "Sunday";
const vowelsRegex = /a|e|i|o|u/gi;
function flippedy(input: string): string {
const [first, ...rest] = input.split(" ");
const firstWordVowelCount = countVowels(first);
const flippedWords = rest.map((word) => {
const vowelCount = countVowels(word);
return firstWordVowelCount === vowelCount ? reverse(word) : word;
});
return [first, ...flippedWords].join(" ");
const KONAMI_CODE = "UUDDLRLRBA";
const kRegex =
/(?<up>.){2}(?<down>.){2}(?<left>.){1}(?<right>.){1}(\3){1}(\4){1}(.){1}(.){1}/gi;
function konamiCode(input: string): { [key: string]: string } {
const matches = input.match(kRegex);
const result: { [key: string]: string } = {};
if (matches && matches.length > 0) {
const match = matches[0];
for (let i = 0; i < KONAMI_CODE.length; i++) {
result[match[i]] = KONAMI_CODE[i];
const bears = [
{ name: "Baloo", hunger: 6 },
{ name: "Yogi", hunger: 9 },
{ name: "Paddington", hunger: 4 },
{ name: "Winnie", hunger: 10 },
{ name: "Chicago", hunger: 20 },
];
function hungryBears(bears: { name: string; hunger: number }[]): Array<string> {
const average =
function maxScoreWithOneReset(input: number[]): number {
const increasedPoints = [];
for (let i = 1; i < input.length; i++) {
if (input[i - 1] < input[i]) {
increasedPoints.push(i - 1);
}
}
let maxSum = 0;
for (const incIndex of increasedPoints) {
const currSum = sum(input.slice(incIndex + 1));