Skip to content

Instantly share code, notes, and snippets.

View okovalov's full-sized avatar
🏠
Working from home

Oleksandr Kovalov okovalov

🏠
Working from home
View GitHub Profile
/**
Find the minimum value in an array of strings using a loop. For example, for this array:
a=['my','name','is','john','doe'];
**/
const sortByLengthOnly = arr => arr.sort( (a, b) => a.length - b.length)
const sortByLengthAndValue = arr => arr.sort( (a, b) => {
--
-- There is a `t` table with 3 fields:
-- `uid` - user ID
-- `dt` - date and time of message
-- `s` - text of the message
-- Write an SQL query to retrieve date and text of the last message for all users.
-- schema
CREATE TABLE `user_message_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
@okovalov
okovalov / .eslintrc.js
Created March 3, 2019 21:33 — forked from nkbt/.eslintrc.js
Strict ESLint config for React, ES6 (based on Airbnb Code style)
{
"env": {
"browser": true,
"node": true,
"es6": true
},
"plugins": ["react"],
"ecmaFeatures": {
const maxProfit = arr => {
let bestBuyPrice = 1000000
let maxProfit = -1
for(let i = 0; i < arr.length - 1; i++) {
const currentBuyPrice = arr[i]
const nextSellPrice = arr[i+1]
if (currentBuyPrice < bestBuyPrice) bestBuyPrice = currentBuyPrice
/**
* n = 20
*
* test merge: 0.517ms
* test bubble: 0.204ms
*
* n = 100
*
* test merge: 3.295ms
* test bubble: 1.395ms
const mergeSort = arr => {
if (arr.length < 2) return arr
const middleIdx = Math.floor(arr.length / 2)
let left = arr.splice(0, middleIdx)
let right = arr.splice(0)
return merge(mergeSort(left), mergeSort(right))
}
const bubbleSort = arr => {
const { length } = arr
for (let i = 0 ; i < length; i++) {
for (let j = 0; j < length - i - 1; j++) {
if (arr[j] > arr[j+1]) {
const temp = arr[j]
arr[j] = arr[j + 1]
arr[j+1] = temp
}
const sieveOfErathosphenes = num => {
const arr = [false, false]
for (let idx = 2; idx < num; idx ++) {
arr[idx] = true
}
for (let i = 2; i < Math.sqrt(num); i++) {
for (let j = 2; j * i <= num; j++) {
arr[i * j] = false
const binarySearch = (arr, key) => {
const midIdx = Math.floor(arr.length / 2)
const midElement = arr[midIdx]
if (midElement === key) return true
if (arr.length === 1) return false
if (midElement > key) {
return binarySearch(arr.splice(0, midIdx), key)
}
/**
* Fibonacci
*
* 1, 1, 2, 3, 5, 8, 13, 21, 34
*
* fibonacci = O(n^2) - exponential
* fibMemo = O(n) - linear
*/
/** O(n^2) **/