Skip to content

Instantly share code, notes, and snippets.

View betterkenly's full-sized avatar

Kenly Hui betterkenly

  • San Francisco
View GitHub Profile
var isSubsequence = function(s, t) {
s = s.split('');
t = t.split('');
var curr = 0;
var temp = 0;
for (var i = 0; i < s.length; i++) {
var temp2 = temp;
for (var j = temp2; j < t.length; j++) {
if (t[j] === s[i]) {
temp = j
var maxEnvelopes = function(envelopes) {
var result = [];
var sorted0 = envelopes.sort((a,b) => {
return a[0] - b[0];
});
var sorted1 = envelopes.sort((a,b) => {
return a[1] - b[1];
});
for (var i = 0; i < sorted0.length; i++) {
@betterkenly
betterkenly / knapsack.js
Created August 9, 2017 23:32 — forked from danwoods/knapsack.js
Knapsack algorithm in JavaScript
//Knapsack algorithm
//==================
// wikipedia: [Knapsack (0/1)](http://en.wikipedia.org/wiki/Knapsack_problem#0.2F1_Knapsack_Problem)
// Given a set `[{weight:Number, benefit:Number}]` and a capacity,
// find the maximum value possible while keeping the weight below
// or equal to the capacity
// **params**:
// `capacity` : Number,
// `items` : [{w:Number, b:Number}]
// **returns**:
/*
A Thief has a knapsack that can hold X lbs of stolen goods
Each stolen good is worth a certain amount of cash, but
the stolen good also weighs a certain weight. This means that
the thief has to pick an optimal combination of items!
The Thief can't pick the same item twice.
What is the maximum worth of goods that the thief can steal?
*/
//1
let num = 0;
let reverse = (num) => {
if (num < 0) {
let num1 = num.toString().split('').slice(1).reverse().join('');
return Number(num1) * -1;
} else {
let num1 = num.toString().split('').reverse().join('');
// for desktop (notes: @ab_test_class is a variable to determine the style and this is how i do it in front-end, and the values are // v1,v2,v3,v4 and you can refer the styles below)
<% if @ab_aff_calc %>
<% unless is_mobile? %>
<div id="vu-get-preapproved" class="col-xxs-12 <%= @ab_test_class %>-vu-get-preapproved">
<div class="<%= @ab_test_class %>-vu-get-preapproved-text">Veterans: Get Preapproved for a $0 down VA Loan</div>
</div>
<% end %>
<% end %>