This file contains 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
const compareByVersionNumber = (a, b) => { | |
longest = (a.length > b.length) ? a.length : b.length | |
for(let i = 0; i < longest; i += 1) { | |
if (!a[i]) { | |
return -1 | |
} else if (!b[i]) { | |
return 1 | |
} | |
versionA = Number(a[i]) | |
versionB = Number(b[i]) |
This file contains 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
# One transaction | |
# @param {Integer[]} prices | |
# @return {Integer} | |
def max_profit(prices) | |
max_profit = 0 | |
min_price = prices[0] | |
i = 1 | |
while i < prices.length do | |
if prices[i] < min_price |
This file contains 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 longest_palindrome(s) | |
letter_count = | |
s.chars.each_with_object(Hash.new(0)) { |c, acc| acc[c] += 1 } | |
result = | |
letter_count | |
.each_with_object({"odd" => false, "length" => 0}) do |(letter, count), h| | |
if count.even? | |
h["length"] += count | |
else |
This file contains 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
# Definition for a binary tree node. | |
# class TreeNode | |
# attr_accessor :val, :left, :right | |
# def initialize(val = 0, left = nil, right = nil) | |
# @val = val | |
# @left = left | |
# @right = right | |
# end | |
# end | |
def path_sum(root, target) |
This file contains 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} row_index | |
# @return {Integer[]} | |
def get_row(row_index) | |
return [1] if row_index.zero? | |
table = Array.new(row_index + 1) { 0 } | |
table[0] = 1 | |
1.upto(row_index) do |i| | |
i.downto(1) do |j| |
This file contains 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 {String} s | |
# @return {String} | |
def longest_palindrome(s) | |
return s if s.length <= 1 | |
longest = "" | |
for i in 0...(s.length - 1) | |
odd = check_from_center(s, i, i) | |
even = check_from_center(s, i, i + 1) | |
This file contains 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 length_of_longest_substring(s) | |
cache = Hash.new | |
start = 0 | |
len = 0 | |
for (c, i) in s.chars.each_with_index | |
if cache[c] && cache[c] >= start | |
start = cache[c] + 1 | |
end | |
len = [len, i-start+1].max | |
cache[c] = i |
This file contains 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 | |
# @param {Integer} n | |
# @return {Integer[]} | |
def shuffle(nums, n) | |
# list = [] | |
# i = 0 | |
# while i < n | |
# list << nums[i] | |
# list << nums[n + i] | |
# i += 1 |
This file contains 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 {String} s | |
# @return {Integer} | |
OFFSET = "A".ord - 1 | |
def title_to_number(s) | |
# s.bytes.reverse.inject([0, 0]) do |(place, value), c| | |
# place_value = (c - OFFSET) * (26 ** place) | |
# [place + 1, value + place_value] | |
# end | |
# .last |
This file contains 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 running_sum(nums) | |
# sum_so_far = 0 | |
# nums.map do |n| | |
# sum_so_far += n | |
# sum_so_far | |
# end | |
# nums | |