Skip to content

Instantly share code, notes, and snippets.

View allenhwkim's full-sized avatar

Allen Kim allenhwkim

View GitHub Profile
/**
given two non-empty arrays A and B consisting of N integers, returns the number of fish that will stay alive.
For example, given the arrays shown above, the function should return 2, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [1..100,000];
each element of array A is an integer within the range [0..1,000,000,000];
each element of array B is an integer that can have one of the following values: 0, 1;
@allenhwkim
allenhwkim / NumberOfDiscIntersections.js
Last active July 5, 2022 03:16
NumberOfDiscIntersections
function solution(A) { // [1,5,2,1,4,0]
var starts = []; // disk start points
var ends = []; // disk end points
for (var i=0; i<A.length; i++) {
starts.push(i - A[i]); // [-1, -4, 0, 2, 0, 5]
ends.push(i + A[i]); // [1, 6, 4, 4, 8, 5]
}
starts.sort( (a,b) => a-b) // [ -4, -1, 0, 0, 2, 5 ]
ends.sort( (a,b) => a-b) // [ 1, 4, 4, 5, 6, 8 ]
function solution(A) {
if (A.length < 3) return 0;
A = A.sort( (a,b) => a - b)
for (var i=0; i< A.length -2; i++) {
const [p, q, r] = A.slice(i, i+3)
if ( (p + q) > r && (p+r) > q && (q+r) > p)
return 1
}
function solution(A) {
n = A.length;
maxProduct = Number.MIN_SAFE_INTEGER
A = A.sort( (a,b) => a-b) // <--- Important
maxProduct = Math.max(maxProduct, A[0] * A[1] * A[n-1]);
maxProduct = Math.max(maxProduct, A[n-3] * A[n-2] * A[n-1]);
return maxProduct;
}
/**
given a non-empty array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
For example, given array A such that:
A[0] = 4
A[1] = 2
A[2] = 2
A[3] = 5
A[4] = 1
@allenhwkim
allenhwkim / Codility Training - frog-river-one.js
Last active July 3, 2022 21:09
Codility Developer Training
/**
given a non-empty array A consisting of N integers and integer X, returns the earliest time when the frog can jump to the other side of the river.
If the frog is never able to jump to the other side of the river, the function should return −1.
For example, given X = 5 and array A such that:
[1, 3, 1, 4, 2, 3, 5, 4]
the function should return 6, as explained above.
function solution(N) {
const binString = N.toString(2);
const matches = binString.replace(/[0]+$/, '').match(/0+/g);
return (matches || []).reduce( (acc, cur) => Math.max(acc, cur.length), 0);
}
@allenhwkim
allenhwkim / release.yml
Last active May 18, 2022 20:43
semantic-release auto publish
name: Release
"on":
push:
branches:
- main
jobs:
release:
name: release
runs-on: ubuntu-latest
@allenhwkim
allenhwkim / restrict-keyboard-input.js
Created May 16, 2022 20:41
restrict Keyboard Input
$('#my-input').addEventListener('keydown', restrictKeyboardInput);
$('#my-input').addEventListener('paste', restrictKeyboardInput);
function restrictKeyboardInput(event) {
if (event.type === 'keydown' && !event.key.match(/^[a-zA-Z0-9-_\ .]$/)) {
if (event.keyCode === 8 || event.keyCode === 46 || event.keyCode === 9) {
// Allow backspace, delete and Tab key
} else if (event.keyCode >= 37 && event.keyCode <= 40) {
// Allow left, right, up and down arrows
} else {
@allenhwkim
allenhwkim / lazy-loading.module.js
Created February 4, 2022 22:15
AngularJs Lazy Loading
angular.module('lazyLoading',[])
.component('childAngularComponent', ChildAngularComponent)
.component('childChildAngularComponent', ChildChildAngularComponent);
var ChildAngularComponent = {
template: `
<div>
<h2>I'm Child Angular Component</h2>
<p>form.value: {{$ctrl.form.value}}</p>