Skip to content

Commit 198870a

Browse files
committed
grind 75 problems
1 parent 11d4e5c commit 198870a

7 files changed

+211
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
153153

154154
1. [Print Next Greatest Element for each element of array](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/arrays/NextGreatestElement.rb)
155155
2. [Backspace String Compare](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/backspace_string_compare.rb)
156+
3. [Evaluate Reverse Polish Notation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/evaluate_reverse_polish_notation.rb)
156157

157158
<a name="linked_list"/>
158159

@@ -162,7 +163,7 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
162163
3. [Linked List Cycle](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/linked_list_cycle.rb)
163164
4. [Merge Two Sorted Lists](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/merge_sorted_lists.rb)
164165
5. [Palindrome Linked List](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/palindrome_linked_list.rb)
165-
166+
6. [Remove nth node from end in list](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/remove_nth_node_from_end_of_list.rb)
166167

167168
<a name="bitAlgo"/>
168169

@@ -274,6 +275,12 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
274275
34. [Reverse Bits](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/reverse_bits.rb)
275276
35. [Meeting Rooms](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/meeting_rooms.rb)
276277
36. [Roman to Integer](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/roman_to_integer.rb)
278+
37. [Merge Intervals](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/merge_intervals.rb)
279+
38. [Insert Intervals](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/insert_intervals.rb)
280+
39. [Product of Array Except self](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/product_of_array_except_self.rb)
281+
40. [Sort Colors](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/sort_colors.rb)
282+
41. [Evaluate Reverse Polish Notation](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/evaluate_reverse_polish_notation.rb)
283+
42. [Remove nth node from end in list](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/remove_nth_node_from_end_of_list.rb)
277284

278285
<a name="striver"/>
279286

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
=begin
2+
You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation.
3+
Evaluate the expression. Return an integer that represents the value of the expression.
4+
Note that:
5+
6+
1. The valid operators are '+', '-', '*', and '/'.
7+
2. Each operand may be an integer or another expression.
8+
3. The division between two integers always truncates toward zero.(IMP: 6/-132 = -1 in ruby/python not 0 so modify / accordingly )
9+
4. There will not be any division by zero.
10+
5. The input represents a valid arithmetic expression in a reverse polish notation.
11+
6. The answer and all the intermediate calculations can be represented in a 32-bit integer.
12+
=end
13+
14+
# Solution
15+
def eval_rpn(tokens)
16+
operations = {
17+
"+" => ->(a,b) {a+b},
18+
"-" => ->(a,b) {a-b},
19+
"*" => ->(a,b) {a*b},
20+
# "/" => lambda (a,b) { b.zero? ? raise ArgumentError: a/b} (if 4th confition was not given)
21+
"/" => ->(a,b) {a*b<0 ? -1*(a.abs/b.abs): a/b } # To satisfy 3rd condition we divide absolute values
22+
}
23+
op_stack = []
24+
tokens.each do |token|
25+
#raise ArgumentError if (op_stack.empty? or op_stack.length==1 ) and operations.has_key?(token) (if 5th confition was not given)
26+
if operations.has_key?(token)
27+
b = op_stack.pop
28+
a = op_stack.pop
29+
result = operations[token].(a,b)
30+
p [a,b,result]
31+
op_stack.push(result)
32+
else
33+
op_stack.push(token.to_i)
34+
end
35+
end
36+
op_stack[-1]
37+
end
38+

leetcode/grind75/insert_intervals.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
=begin
2+
You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval
3+
and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.
4+
Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals
5+
(merge overlapping intervals if necessary).
6+
7+
Return intervals after the insertion.
8+
9+
Note that you don't need to modify intervals in-place. You can make a new array and return it.
10+
11+
=end
12+
13+
# Solution
14+
def insert(intervals, new_interval)
15+
#intervals = intervals.sort{ |a,b| a[0]<=>b[0]} (if intervals are not in sorted order then sort first)
16+
curr_interval=new_interval
17+
merged_intervals = []
18+
for i in 0...intervals.length
19+
if new_interval[0]>intervals[i][1]
20+
merged_intervals.push(intervals[i])
21+
next
22+
end
23+
if is_overlapping(intervals[i],curr_interval)
24+
curr_interval[0] = [intervals[i][0],curr_interval[0]].min
25+
curr_interval[1] = [intervals[i][1],curr_interval[1]].max
26+
next
27+
end
28+
merged_intervals.push(curr_interval)
29+
curr_interval = intervals[i]
30+
end
31+
merged_intervals.push(curr_interval)
32+
merged_intervals
33+
end
34+
35+
def is_overlapping(given_intv,curr_intv)
36+
# either interval 1's end time lies between interval 2 or vice-versa
37+
(given_intv[1]>=curr_intv[0] and given_intv[1]<=curr_intv[1]) or \
38+
(given_intv[0]<=curr_intv[1] and given_intv[1]>=curr_intv[1])
39+
end
40+

leetcode/grind75/merge_intervals.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
=begin
2+
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals,
3+
and return an array of the non-overlapping intervals that cover all the intervals in the input.
4+
=end
5+
6+
#Solution 1 (use last pushed intervals to check overlap)
7+
def merge(intervals)
8+
intervals = intervals.sort{ |a,b| a[0]<=>b[0]}
9+
merged_intervals = [intervals[0]]
10+
for i in 1...intervals.length
11+
if is_overlapping(merged_intervals[-1],intervals[i])
12+
merged_intervals[-1][1] = [merged_intervals[-1][1],intervals[i][1]].max
13+
next
14+
end
15+
merged_intervals.push(intervals[i])
16+
end
17+
merged_intervals
18+
end
19+
20+
def is_overlapping(intv1,intv2)
21+
intv1[1]>=intv2[0]
22+
end
23+
24+
#Solution 2
25+
def merge(intervals)
26+
intervals = intervals.sort{ |a,b| a[0]<=>b[0]}
27+
merged_intervals = []
28+
curr_interval = intervals[0]
29+
for i in 1...intervals.length
30+
if is_overlapping(curr_interval,intervals[i])
31+
curr_interval[1] = [curr_interval[1],intervals[i][1]].max
32+
next
33+
end
34+
merged_intervals.push(curr_interval)
35+
curr_interval = intervals[i]
36+
end
37+
merged_intervals.push(curr_interval)
38+
merged_intervals
39+
end
40+
41+
def is_overlapping(intv1,intv2)
42+
intv1[1]>=intv2[0]
43+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=begin
2+
3+
=end
4+
5+
#Solution
6+
#Approach: I first pass populate result array with product of all
7+
# leftside elements then do the same for right side in second pass
8+
def product_except_self(nums)
9+
n = nums.length
10+
product_arr = [1]*n
11+
left_product = 1
12+
for i in 0...n
13+
product_arr[i]=left_product
14+
left_product*=nums[i]
15+
end
16+
right_product = 1
17+
(n-1).downto(0) do |i|
18+
product_arr[i]*=right_product
19+
right_product*=nums[i]
20+
end
21+
product_arr
22+
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=begin
2+
Given the head of a linked list, remove the nth node from the end of the list and return its head.
3+
=end
4+
5+
#Solution 1 (First find length and then remove l-n+1 node, O(n) requires 2 passes)
6+
7+
#Solution 1 (fast and slow pointer approach)
8+
#Approach: Give a head start to a pointer by n steps then move other pointer till head_start reaches end of list
9+
10+
def remove_nth_from_end(head, n)
11+
return [] if head.nil?
12+
head_start = head
13+
follower = head
14+
prev_node = nil
15+
count = 0
16+
while count<n
17+
head_start = head_start.next
18+
count+=1
19+
end
20+
while head_start
21+
prev_node = follower
22+
follower = follower.next
23+
head_start = head_start.next
24+
end
25+
if prev_node.nil?
26+
head = follower.next
27+
else
28+
prev_node.next = follower.next
29+
end
30+
head
31+
end
32+

leetcode/grind75/sort_colors.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=begin
2+
Given an array a with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent,
3+
with the colors in the order red, white, and blue.
4+
We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
5+
6+
You must solve this problem without using the library's sort function.
7+
=end
8+
9+
#Solution 1 (count the number of 0,1 and 2 and rewrite, need constant extra space but 2 paases)
10+
11+
#Solution 2(Dutch Nation Flag Algo)
12+
#Approach: 3 pointers, left and right move according to value encountered at mid
13+
def sort_colors(a)
14+
left,middle,right = 0,0,a.length-1
15+
while middle<=right
16+
if a[middle]==0
17+
a[left],a[middle]=a[middle],a[left]
18+
left+=1
19+
middle+=1
20+
elsif a[middle]==1
21+
middle+=1
22+
else
23+
a[right],a[middle]=a[middle],a[right]
24+
right-=1
25+
end
26+
end
27+
a
28+
end

0 commit comments

Comments
 (0)