Skip to content

Instantly share code, notes, and snippets.

View mAlishera's full-sized avatar

Ekaterina mAlishera

  • Berlin, Germany
View GitHub Profile
@mAlishera
mAlishera / max_sub_array.rb
Created April 3, 2020 17:52
LC 30 days challenge - DAY 3
# @param {Integer[]} nums
# @return {Integer}
def max_sub_array(nums)
max = nums[0]
for i in 1...nums.size do
nums[i] += nums[i-1] if nums[i-1] > 0
max = nums[i] if nums[i] > max
end
max
@mAlishera
mAlishera / happy_number.rb
Created April 2, 2020 13:38
LC 30 days challenge - DAY2
# https://leetcode.com/problems/happy-number/
def is_happy(n)
repeats = []
loop do
n = n.to_s.split('').inject(0) { |sum,x| sum + x.to_i*x.to_i }
return true if n == 1
return false if repeats.include?(n)
repeats << n
end
@mAlishera
mAlishera / daily_stock_profit.rb
Last active March 29, 2020 09:22
find max profit (min loss) to buy and sell within one day stocks prices (Kadane's algo)
def find_buy_sell_stock_prices(array)
return if !array || !array[1]
c_buy, g_sell, g_profit, c_profit = array[0], array[1], (array[1] - array[0]), 0
i = 1
while i < array.size
c_profit = array[i] - c_buy
if c_profit > g_profit
@mAlishera
mAlishera / 53_2.go
Last active January 16, 2020 15:54
4 ms 3.3 MB
func maxSubArray(nums []int) int {
if len(nums) <= 0 {
return 0
}
max := nums[0]
for i := 1; i < len(nums); i++ {
if nums[i-1] > 0 {
nums[i] += nums[i-1]
@mAlishera
mAlishera / 53_1.go
Created January 16, 2020 15:44
4 ms 4.3 MB
func maxSubArray(nums []int) int {
if len(nums) <= 0 {
return 0
}
arr := []int{}
sum := 0
max := nums[0]
for i := 0; i < len(nums); i++ {
package main
import (
"fmt"
"image"
"image/png"
"log"
"math"
"os"
package main
import (
"fmt"
"image"
"image/png"
"log"
"os"
"github.com/Arafatk/glot"
@mAlishera
mAlishera / damerau_levenshtein.go
Created March 3, 2019 21:57
Proper Damerau-Levenshtein edit distance algorithm in Go
package damerau_levenshtein
// O(|s| x |t|)
// a and b are zero-indexed, not one-indexed
func Distance(s, t string) int {
var i, j, cost int
m, n := len(s), len(t)
d := make([][]int, m+1)
for i = 0; i <= m; i++ {
@mAlishera
mAlishera / damerau_levenshtein_distance.rb
Created March 3, 2019 21:34 — forked from dhruvasagar/damerau_levenshtein_distance.rb
Damerau-Levenshtein distance for ruby in C
#!/usr/bin/env ruby1.9
# encoding: UTF-8
require 'rubygems'
require 'inline'
require 'time'
class DamerauLevenshtein
def distance(str1, str2, block_size=2, max_distance=10)
res = distance_utf(str1.unpack("U*"), str2.unpack("U*"), block_size, max_distance)
(res > max_distance) ? nil : res
@mAlishera
mAlishera / laba_13_04_17.cpp
Created April 20, 2017 12:26
1 - сдвиг массива влево до минимального и потом вправо до максимального, 2 - сортировка матрицы по столбцам
#include <stdlib.h>
#include <math.h>
#include <curses.h>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <time.h>
// функция, которая получает и выводит на пачеть
// *ptr - указатель на нулевой элемент