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
| # https://superuser.com/a/1434648 | |
| Add-Type -AssemblyName System.Runtime.WindowsRuntime | |
| $asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | ? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0] | |
| Function Await($WinRtTask, $ResultType) { | |
| $asTask = $asTaskGeneric.MakeGenericMethod($ResultType) | |
| $netTask = $asTask.Invoke($null, @($WinRtTask)) | |
| $netTask.Wait(-1) | Out-Null | |
| $netTask.Result |
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
| /* A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, | |
| the sequence A, C, G, T, G, T, C, A, A, A, A, T, C, G has many palindromic subsequences, including | |
| A, C, G, C, A and A, A, A, A (on the other hand, the subsequence A, C, T is not palindromic). Devise | |
| an O(n*n)-time algorithm that takes a sequence x[1 . . . n] and returns the (length of the) longest | |
| palindromic subsequence. */ | |
| const palindrome = (arr, i=0, j) => { | |
| // Solved using recursion O(2^n). | |
| j = j === undefined ? arr.length - 1 : j; |
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
| // Given a supply of coins of specific denomination, determine if you can make change for the target value. | |
| // https://www.velvetreactor.com/posts/dpv-6-17-change-making/ | |
| const set = []; | |
| const change = (items, value, n) => { | |
| // Solved using recursion O(2^n). | |
| n = n === undefined ? items.length : n; | |
| if (value === 0) { | |
| // We've succeeded in matching the value! | |
| return 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
| // Solved using recursion. O(2^n) | |
| const knapsack = (items, max, n) => { | |
| n = n === undefined ? items.length : n; | |
| if (n === 0 || max === 0) { | |
| // We ran out of values or achieved a max weight. | |
| return 0; | |
| } | |
| else { | |
| if (items[n - 1].weight > 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
| /* | |
| Split an array of values into two equal sums. | |
| Solution: | |
| 1. Calculate the sum of the array. | |
| 2. If the sum is odd, return false. | |
| 3. If the sum is even, divide the sum in half, and find a subset of array equal to half. | |
| */ | |
| const canPartition = (arr, n, sum) => { |
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
| /* | |
| Yuckdonald's is opening restaurants along a strip of highway. | |
| You are given the distances from the starting point of the possible locations of the highways | |
| and the profits at each of these locations. | |
| You can only have one restaurant at each location. | |
| Each location must be at least (k = min) miles apart. | |
| P[i] = maximum profit at location i | |
| P[i] = max(j<i) { P[j] + (di-dj < k ? 0 : P[i]), P[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[]} nums | |
| * @return {number} | |
| */ | |
| var maxSubArray = function(nums) { | |
| // Start with the max sum being the first value. | |
| const S = [nums[0]]; | |
| // Store the first value so that we can output the substring of values. | |
| const V = [[nums[0]]]; | |
| let maxIndex = 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
| /* | |
| Find the longest common subsequence (non-consecutive characters) betweenen two strings. | |
| L(i,j) = length of LCS in X1..Xi and Y1..Yj | |
| X = BCDBCDA | |
| Y = ABECBA | |
| Output = BCBA | |
| if Xi == Yj: L(i,j) = 1 + L(i-1, j-1) | |
| else: L(i,j) = max { L(i-1, j), L(i, j-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 lis = arr => { | |
| // Calculate the longest increasing subsequence length in an array. | |
| // A subsequence is array of items that can be obtained by removing other elements. | |
| const L = []; // Store the LIS length for each i-index in the array. | |
| let maxIndex = 0; // Maintain the max length index. | |
| for (let i=0; i<arr.length; i++) { | |
| // We start with the LIS of a[i] = 1. | |
| L[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 fib = n => { | |
| // Populate the first two fibonacci values. | |
| const F = [0, 1]; | |
| for (let i=2; i<=n; i++) { | |
| F[i] = F[i-1] + F[i-2]; | |
| } | |
| return F[n]; | |
| }; |