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[]} arr | |
# @param {Integer} a | |
# @param {Integer} b | |
# @param {Integer} c | |
# @return {Integer} | |
def count_good_triplets(arr, a, b, c) | |
count = 0 | |
size = arr.size | |
for i in (0..size-3) | |
for j in (i+1..size-2) |
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 singly-linked list. | |
# class ListNode | |
# attr_accessor :val, :next | |
# def initialize(val = 0, _next = nil) | |
# @val = val | |
# @next = _next | |
# end | |
# end | |
# @param {ListNode} head | |
# @return {Integer} |
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} | |
def balanced_string_split(s) | |
counter = 0 | |
balance = 0 | |
characters = s.chars | |
characters.each do |c| | |
balance += 1 if c == 'L' | |
balance -= 1 if c == 'R' | |
counter += 1 if balance == 0 |
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 create_target_array(nums, index) | |
result = [] | |
i = 0 | |
while result.size < nums.size | |
result.insert(index[i], nums[i]) | |
i += 1 | |
end | |
result | |
end |
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 decompress_rl_elist(nums) | |
result = [] | |
for index in (0..nums.size-2) do | |
item = nums[index] | |
if index.even? | |
item.times do | |
result << nums[index+1] | |
end |
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} n | |
# @return {Integer} | |
def subtract_product_and_sum(n) | |
product = 1 | |
sum = 0 | |
while n > 0 | |
sum += n % 10 | |
product *= n % 10 | |
n = n / 10 | |
end |
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) | |
result = [] | |
first = 0 | |
second = n | |
index = 0 | |
while result.size < 2*n | |
result[index] = nums[first] |
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) | |
nums.each_with_index do |item, index| | |
if index == 0 | |
next | |
end | |
nums[index] = nums[index] + nums[index-1] | |
end | |
end |
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 | |
# @param {Integer[]} indices | |
# @return {String} | |
def restore_string(s, indices) | |
result = '' * indices.size | |
indices.each_with_index do |item, index| | |
result[item] = s[index] | |
end | |
result | |
end |
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
Node* Tree::generateFromTraversal(int *inorder, int *preorder, int inStart, int inEnd) { | |
// Reference: https://algorithms.tutorialhorizon.com/make-a-binary-tree-from-given-inorder-and-preorder-traveral/ | |
static int preIndex = 0; | |
if (inStart > inEnd){ | |
return nullptr; | |
} | |
Node* node = new Node(preorder[preIndex++]); | |