Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
//
// Recursive solution.
// Base case: Check if either list is empty, if so, just return the other list.
// Otherwise, take whichever node is smaller and recurse with the rest of the list.
//
var mergeTwoListsR = function(list1, list2) {
let node;
// If either list is empty, return the other list.
if (!list1) {
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
const sortArrayByParity = nums => {
let left = 0;
let right = nums.length - 1;
// While the left pointer is less than the right pointer.
while (left < right) {
// Scroll the left pointer to the first odd number to swap out.
while (nums[left] % 2 === 0 && left < nums.length) {
left++;
}
public class Solution {
public int CanBeTypedWords(string text, string brokenLetters) {
int validWords = 0;
// Build a hash of broken letters.
Dictionary<char, bool> broken = new Dictionary<char, bool>();
foreach (char ch in brokenLetters)
{
broken[ch] = true;
}
const minFlips = (a: number, b: number, c: number): number => {
let flips = 0;
// Convert the decimal values to binary strings.
let a2 = (a >>> 0).toString(2);
let b2 = (b >>> 0).toString(2);
let c2 = (c >>> 0).toString(2);
// Determine the longest length and pad the values.
const n = Math.max(a2.length, b2.length, c2.length);
/* Using dual pointers. O(n) */
const sortedSquares = nums => {
let start = 0;
let end = nums.length - 1;
let i, j;
let index = nums.length - 1;
const result = [];
// Check the left and right values.
while (start <= end) {
const kthDistinct = (arr, k) => {
let result = '';
const hash = {};
const distinct = {};
// Create list of distinct strings.
for (let i=0; i<arr.length; i++) {
if (!hash[arr[i]]) {
hash[arr[i]] = 1;
distinct[arr[i]] = 1;
const largestAltitude = (gain: number[]): number => {
let max = 0;
let altitude = 0;
gain.forEach(current => {
altitude += current;
max = Math.max(max, altitude);
});
return max;
class Solution:
def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int:
good = []
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if abs(arr[i] - arr[j]) <= a:
for k in range(j+1, len(arr)):
if abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c:
good.append([arr[i], arr[j], arr[k]])
public class Solution {
protected class Data
{
public List<int> positions;
public int rank;
public Data()
{
positions = new List<int>();
rank = 0;