Skip to content

Commit 08a3324

Browse files
committed
Adds various algorithm implementations including prefix sum, sliding window, and sorting algorithms
1 parent db8612b commit 08a3324

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1439
-0
lines changed

src/code/ruby/array/prefix_sum.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This method computes the prefix sum of an array.
2+
# It returns a new array where each element at index i is the sum of elements from index 0 to i-1 in the original array.
3+
# # For example, given the array [1, 2, 3], the prefix sum array will be [1, 3, 6].
4+
def prefix_sum(arr)
5+
n = arr.length
6+
prefix = [arr[0]]
7+
8+
(1...n).each do |i|
9+
prefix << (prefix.last + arr[i])
10+
end
11+
12+
prefix
13+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def fn(arr)
2+
n = arr.length
3+
window = 0
4+
left = 0
5+
result = 0
6+
7+
(0...n).each do |right|
8+
# TODO: add arr[right] to window
9+
10+
while WINDOW_CONDITION_BROKEN
11+
# TODO: remove arr[left] from window
12+
left += 1
13+
end
14+
15+
# TODO: update result
16+
end
17+
18+
result
19+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def fn(strs)
2+
# array of strings/chrs
3+
ans = []
4+
5+
strs.each do |char|
6+
ans << char
7+
end
8+
9+
ans.join('')
10+
end
11+
12+
# simple
13+
def fn2(strs)
14+
strs.join
15+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def fn(arr)
2+
ans = 0
3+
left = 0
4+
right = arr.length - 1
5+
6+
while left < right
7+
# TODO: logic with left and right
8+
if CONDITION
9+
left += 1
10+
else
11+
right -= 1
12+
end
13+
end
14+
15+
ans
16+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
def fn(arr1, arr2)
2+
i, j = 0, 0
3+
ans = 0
4+
5+
while i < arr1.length && j < arr2.length
6+
# TODO: Logic
7+
if CONDITION
8+
i += 1
9+
else
10+
j += 1
11+
end
12+
end
13+
14+
while i < arr1.length
15+
# TODO: Logic
16+
i += 1
17+
end
18+
19+
while j < arr2.length
20+
# TODO: Logic
21+
j += 1
22+
end
23+
24+
ans
25+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def backtrack(curr, OTHER_ARGUMENTS...)
2+
if(BASE_CASE)
3+
# TODO: modify answer
4+
return
5+
end
6+
7+
ans = 0
8+
9+
INPUTS.each do |i|
10+
# TODO: modify current state
11+
ans += backtrack(curr, OTHER_ARGUMENTS...)
12+
# TODO: undo modification of current state
13+
end
14+
15+
ans
16+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def fn(arr, target)
2+
left = 0
3+
right = arr.length - 1
4+
5+
while left <= right
6+
mid = (left + right) / 2
7+
8+
if arr[mid] == target
9+
# TODO: logic
10+
return
11+
elsif arr[mid] > target
12+
right = mid - 1
13+
else
14+
left = mid + 1
15+
end
16+
end
17+
18+
left
19+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def fn(arr, target)
2+
left = 0
3+
right = len(arr)
4+
5+
while left < right
6+
mid = (left + right) / 2
7+
8+
if arr[mid] >= target
9+
right = mid
10+
else
11+
left = mid + 1
12+
end
13+
end
14+
15+
left
16+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def fn(arr, target)
2+
left = 0
3+
right = len(arr)
4+
5+
while left < right
6+
mid = (left + right) / 2
7+
8+
if arr[mid] > target
9+
right = mid
10+
else
11+
left = mid + 1
12+
end
13+
end
14+
15+
left
16+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
def fn(arr)
2+
left = MINIMUM_POSSIBLE_ANSWER
3+
right = MAXIMUM_POSSIBLE_ANSWER
4+
5+
while left <= right
6+
mid = (left + right) / 2 # integer division
7+
8+
if check(mid)
9+
left = mid + 1
10+
else
11+
right = mid - 1
12+
end
13+
14+
end
15+
16+
right
17+
end
18+
19+
def check(x)
20+
BOOLEAN
21+
end

0 commit comments

Comments
 (0)