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
| // | |
| // 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) { |
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. | |
| * 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) |
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
| 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++; | |
| } |
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
| 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; | |
| } |
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
| 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); |
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
| /* 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) { |
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
| 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; |
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
| const largestAltitude = (gain: number[]): number => { | |
| let max = 0; | |
| let altitude = 0; | |
| gain.forEach(current => { | |
| altitude += current; | |
| max = Math.max(max, altitude); | |
| }); | |
| return max; |
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
| 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]]) |
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
| public class Solution { | |
| protected class Data | |
| { | |
| public List<int> positions; | |
| public int rank; | |
| public Data() | |
| { | |
| positions = new List<int>(); | |
| rank = 0; |