Skip to content

Commit 78c73c6

Browse files
author
Aman Kumar
committed
add heap
1 parent 57041bb commit 78c73c6

32 files changed

+829
-2
lines changed

.DS_Store

10 KB
Binary file not shown.

dp/job_scheduling_max_profit.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# @param {Integer[]} start_time
2+
# @param {Integer[]} end_time
3+
# @param {Integer[]} profit
4+
# @return {Integer}
5+
Job = Struct.new(:start, :end, :profit, :pos)
6+
def job_scheduling(start_time, end_time, profit)
7+
return if start_time.length.zero?
8+
return profit[0] if start_time.length == 1
9+
10+
jobs = []
11+
for i in (0...start_time.length)
12+
jobs << Job.new(start_time[i], end_time[i], profit[i], i+1)
13+
end
14+
jobs.sort_by!(&:profit).reverse!
15+
schedule = Array.new(end_time.max)
16+
17+
profit = 0
18+
19+
for i in (0...jobs.length)
20+
available = true
21+
current_job = jobs[i]
22+
for j in (jobs[i].start...jobs[i].end)
23+
if schedule[j]
24+
available = false
25+
break
26+
end
27+
end
28+
next unless available
29+
for j in (jobs[i].start...jobs[i].end)
30+
schedule[j] = true
31+
end
32+
33+
profit += jobs[i].profit
34+
35+
end
36+
profit
37+
end

greedy/fractional_knapsack.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Given weights and values of n items, we need to put these items in a knapsack of capacity W to get the maximum total value in the knapsack.
2+
3+
class Item
4+
attr_reader :weight, :value, :pos
5+
6+
def initialize(wt, val, pos)
7+
@weight = wt
8+
@value = val
9+
@pos = pos
10+
end
11+
12+
def val_to_wt
13+
@value / @weight
14+
end
15+
16+
def to_s
17+
"wt: #{@weight}, val: #{@value}, val_to_wt: #{val_to_wt} pos: #{@pos}"
18+
end
19+
end
20+
21+
def get_max(wt, val, capacity)
22+
items = []
23+
(0...wt.length).each do |i|
24+
items << Item.new(wt[i], val[i], i + 1)
25+
end
26+
# sort as per decreasing order of val_to_wt
27+
items.sort! { |a, b| b.val_to_wt <=> a.val_to_wt }
28+
curr_capacity = 0
29+
total_value = 0
30+
items.each do |item|
31+
if item.weight <= capacity - curr_capacity
32+
curr_capacity += item.weight
33+
total_value += item.value
34+
else
35+
# include fractional weight
36+
fraction = (capacity - curr_capacity) / item.weight.to_f
37+
total_value += (fraction * item.value).to_i
38+
curr_capacity += (fraction * item.weight)
39+
break
40+
end
41+
end
42+
total_value
43+
end
44+
45+
wt = [10, 40, 20, 30]
46+
val = [60, 40, 100, 120]
47+
capacity = 50
48+
49+
puts get_max(wt, val, capacity)
File renamed without changes.

greedy/job_sequencing_max_profit.rb

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Given an array of jobs where every job has a deadline and associated profit if the job is finished before the deadline.
2+
# It is also given that every job takes a single unit of time, so the minimum possible deadline for any job is 1.
3+
# How to maximize total profit if only one job can be scheduled at a time.
4+
# Input: Four Jobs with following
5+
# deadlines and profits
6+
# JobID Deadline Profit
7+
# a 4 20
8+
# b 1 10
9+
# c 1 40
10+
# d 1 30
11+
# Output: Following is maximum
12+
# profit sequence of jobs
13+
# c, a
14+
15+
# function to schedule the jobs take 2
16+
# arguments array and no of jobs to schedule
17+
def job_sequencing(arr, max_deadline)
18+
return if arr.empty?
19+
20+
length = arr.length
21+
22+
# sort array by decending profit
23+
arr.sort! { |a, b| b[2] <=> a[2] }.to_s
24+
25+
# true denotes time_slot is empty
26+
time_slots = Array.new(max_deadline) { |_a| true }
27+
output = []
28+
profit = 0
29+
filled_slots = 0
30+
arr.each_with_index do |job, _index|
31+
min_deadline = [max_deadline, job[1]].min
32+
33+
# as index start from 0, and min deadline can be 1. We decremented 1
34+
min_deadline -= 1
35+
36+
# try to accomodate job at deadline or before
37+
while min_deadline >= 0
38+
# empty slot found
39+
if time_slots[min_deadline]
40+
time_slots[min_deadline] = false
41+
profit += job[2]
42+
output[min_deadline] = job[0]
43+
filled_slots += 1
44+
break
45+
end
46+
min_deadline -= 1
47+
end
48+
break if filled_slots == max_deadline
49+
end
50+
output
51+
end
52+
53+
# Driver COde
54+
# # Job Array
55+
# With job name, deadline, profit
56+
arr = [['a', 2, 100],
57+
['b', 1, 19],
58+
['c', 2, 27],
59+
['d', 1, 25],
60+
['e', 3, 15]]
61+
62+
print('Following is maximum profit sequence of jobs')
63+
64+
# Function Call
65+
puts job_sequencing(arr, 3).to_s
66+
67+
arr = [['j1', 2, 60],
68+
['j2', 1, 100],
69+
['j3', 3, 20],
70+
['j4', 2, 40],
71+
['j5', 1, 20]]
72+
73+
puts job_sequencing(arr, 3).to_s

greedy/max_meetings_in_a_room.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# There is one meeting room in a firm.
2+
# There are N meetings in the form of (S[i], F[i])
3+
# where S[i] is the start time of meeting i and F[i] is finish time of meeting i.
4+
# The task is to find the maximum number of meetings that can be accommodated in the meeting room.
5+
# Print all meeting numbers
6+
7+
# class Meeting
8+
# attr_accessor :start, :ending, :pos
9+
# def initialize(start, ending, pos)
10+
# @start = start
11+
# @end = ending
12+
# @pos = pos
13+
# end
14+
# end
15+
#
16+
Meeting = Struct.new(:start, :end, :pos)
17+
18+
def max_meeting(start, finish)
19+
length = start.length
20+
meetings = []
21+
(0...length).each do |i|
22+
meetings << Meeting.new(start[i], finish[i], i + 1)
23+
end
24+
25+
meetings.sort_by!(&:end)
26+
27+
output = []
28+
output << meetings.shift
29+
meetings.each do |meeting|
30+
output << meeting if output.last.end <= meeting.start
31+
end
32+
output.map { |e| e.pos }
33+
end
34+
# Starting time
35+
s = [1, 3, 0, 5, 8, 5]
36+
37+
# Finish time
38+
f = [2, 4, 6, 7, 9, 9]
39+
40+
puts max_meeting(s, f).to_s
41+
42+
# max_meeting([75250, 50074, 43659, 8931, 11273, 27545, 50879, 77924],
43+
# [112960, 114515, 81825, 93424, 54316, 35533, 73383, 160252])

greedy/minimum_number_of_coins.rb

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Given a value V, if we want to make a change for V Rs, and we have an infinite supply of each of the denominations in Indian currency,
2+
# i.e., we have an infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes,
3+
# what is the minimum number of coins and/or notes needed to make the change?
4+
5+
# Input: V = 70
6+
# Output: 2
7+
# We need a 50 Rs note and a 20 Rs note.
8+
#
9+
# Input: V = 121
10+
# Output: 3
11+
# We need a 100 Rs note, a 20 Rs note and a 1 Rs coin.
12+
13+
def coin_change(coins, amount)
14+
return 0 if amount.zero?
15+
16+
output = 0
17+
remaining = amount
18+
coins.sort! { |a, b| b <=> a }
19+
coins.each do |coin|
20+
num = remaining / coin
21+
next if num.zero?
22+
23+
remaining -= (coin * num)
24+
output += num
25+
return output if remaining.zero?
26+
end
27+
-1
28+
end
29+
30+
# coins = [1, 2, 5]
31+
deno = [1, 2, 5, 10, 20, 50,
32+
100, 500, 1000]
33+
amount = 11
34+
35+
puts coin_change(deno, amount)

greedy/water_connection_problem.rb

Whitespace-only changes.

heap/build_heap_from_array.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# build max heap from array
2+
3+
def heapify_max(arr, index)
4+
return arr if arr.empty?
5+
6+
largest = index
7+
left = (index * 2) + 1
8+
right = (index * 2) + 2
9+
10+
# check if left exist, and value of left is greater then root
11+
largest = left if left < arr.length && arr[left] > arr[largest]
12+
largest = right if right < arr.length && arr[right] > arr[largest]
13+
14+
# swap if largest is not root index
15+
if largest != index
16+
arr[largest], arr[index] = arr[index], arr[largest]
17+
18+
# Recursively heapify the affected sub-tree
19+
heapify_max(arr, largest)
20+
end
21+
end
22+
23+
def heapify_min(arr, index)
24+
smallest = index
25+
left = 2 * index + 1
26+
right = 2 * index + 2
27+
length = arr.length
28+
29+
smallest = left if left < length && arr[left] < arr[smallest]
30+
smallest = right if right < length && arr[right] < arr[smallest]
31+
32+
if smallest != index
33+
arr[smallest], arr[index] = arr[index], arr[smallest]
34+
heapify_min(arr, smallest)
35+
end
36+
end
37+
38+
def build_heap(arr)
39+
return arr if arr.empty?
40+
41+
# Find 1st non leaf
42+
length = arr.length
43+
start_index = length / 2 - 1
44+
start_index.downto(0).each do |i|
45+
heapify_max(arr, i)
46+
end
47+
end
48+
49+
arr = [1, 3, 5, 4, 6, 13,
50+
10, 9, 8, 15, 17]
51+
build_heap(arr)
52+
53+
puts arr.to_s

0 commit comments

Comments
 (0)