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 {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 |
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://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 |
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
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 |
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
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] | |
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
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++ { |
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
package main | |
import ( | |
"fmt" | |
"image" | |
"image/png" | |
"log" | |
"math" | |
"os" |
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
package main | |
import ( | |
"fmt" | |
"image" | |
"image/png" | |
"log" | |
"os" | |
"github.com/Arafatk/glot" |
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
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++ { |
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
#!/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 |
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
#include <stdlib.h> | |
#include <math.h> | |
#include <curses.h> | |
#include <cmath> | |
#include <algorithm> | |
#include <iostream> | |
#include <time.h> | |
// функция, которая получает и выводит на пачеть | |
// *ptr - указатель на нулевой элемент |
NewerOlder