This file contains 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
# A left rotation operation on an array shifts each of the array's elements unit to the left. | |
# For Example: if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2]. | |
# Given an array of a integers and a number, d, perform d left rotations on the array. | |
#Return the updated array to be printed as a single line of space-separated integers. | |
def rotLeft(a, d): | |
aLen = len(a) | |
if (aLen == d) : return a |
This file contains 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
const arrayManipulation = (n, queries) => { | |
const zeroArr = []; | |
let max = 0; | |
for (let i = 0; i < n; i++) { | |
zeroArr.push(0); | |
} | |
queries.forEach((query) => { | |
zeroArr[query[0] - 1] += query[2]; |
This file contains 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
const repeatedString = (s, n) => { | |
let strLen = s.length; | |
if (strLen === 1 && s === strToCheck) return n; | |
if (strLen === 1 && s != strToCheck) return 0; | |
const rem = n % strLen; | |
const quotient = Math.floor(n/strLen); | |
let count = 0; |
This file contains 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
/** | |
* Emma is playing a new mobile game that starts with consecutively numbered clouds. | |
* Some of the clouds are thunderheads and others are cumulus. She can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. | |
* She must avoid the thunderheads. | |
* Determine the minimum number of jumps it will take Emma to jump from her starting postion to the last cloud. It is always possible to win the game. | |
* For each game, Emma will get an array of clouds numbered 0 if they are safe or 1 if they must be avoided. | |
* For example, c = [0, 1, 0, 0, 0, 1, 0] indexed from 0...6. The number on each cloud is its index in the list so she must avoid the clouds at indexes 1 and 5. | |
* She could follow the following two paths: 0->2->4->6 or 0->2->3->4->6. The first path takes 3 jumps while the second takes 4. | |
*/ | |
public class JumpingOnClouds { |
This file contains 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
/** | |
* Given a hiker's sequence of up and down steps during his last hike, find and print the number of valleys he walked through. | |
* For example, if the hiker's path is s = [DDUUUUDD], he first enters a valley units deep. Then he climbs out an up onto a mountain units high. Finally, he returns to sea level and ends his hike. | |
*/ | |
public class CountingValleys { | |
static int countingValleys(int n, String s) { | |
char[] chars = s.toCharArray(); | |
int sum = 0; | |
int valleys = 0; | |
int prevSum; |
This file contains 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
const ds2 = { | |
unit: ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'], | |
tens: ['twenty', 'thirty', 'fourty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] | |
}; | |
const isUnit = (num) => num % 10 === parseInt(num); | |
const isTeen = (num) => num[0] === '1' && num.length > 1; | |
const isTens = (num) => num[0] > 1 && num.length > 1; | |
const getNumStrArr2 = (num) => { | |
const parts = num.toString().match(/.{1,2}/g); | |
const resultArr = []; |
This file contains 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
const ds = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']; | |
const getNumStringArr = (num) => { | |
const numArr = num.toString().split(''); | |
const resultArr = []; | |
for (let i = 0; i < numArr.length; i++) { | |
const m = parseInt(numArr[i]); | |
resultArr.push(ds[m]); | |
} | |
return resultArr; | |
} |
This file contains 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
public static int diagonalDifference(List<List<Integer>> arr) { | |
int primarySum = 0; | |
int secondarySum = 0; | |
for (int i = 0; i < arr.size(); i++) { | |
primarySum += arr.get(i).get(j); | |
secondarySum += arr.get(i).get(arr.size() - j - 1); | |
} | |
return Math.abs(primarySum - secondarySum); |
This file contains 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
const findFirstMissingPositiveInteger = input => { | |
const sortedInput = input.sort((a, b) => { | |
if (a < b) return -1; | |
if (a > b) return 1; | |
return 0; | |
}); | |
const nthInput = input.length - 1; | |
if (sortedInput[nthInput] <= 0) return 1; |
This file contains 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
/** | |
Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. | |
For example, given the following Node class | |
class Node: | |
def __init__(self, val, left=None, right=None): | |
self.val = val | |
self.left = left | |
self.right = right |
NewerOlder