Skip to content

Commit 6120795

Browse files
committed
more leetcode questions
1 parent 1f714b5 commit 6120795

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

Diff for: README.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
175175
5. [Find missing number in array(Approach 2)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/arrays/FindMissingNumber.rb)
176176
6. [Find two repeating elements in given array(Approach 4)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/arrays/FindTwoRepeatingElem.rb)
177177
7. [Add Binary](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/add_binary.rb)
178+
8. [Counting Bits](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/counting_bits.rb)
178179

179180
<a name="maths"/>
180181

@@ -194,7 +195,7 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
194195
#### Binary Trees
195196
1. [Invert Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/invert_binary_tree.rb)
196197
2. [Height of Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/max_depth_of_binary_tree.rb)
197-
198+
3. [Same Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/same_tree.rb)
198199

199200
<a name="bst"/>
200201

@@ -264,6 +265,9 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
264265
26. [Add Binary](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/add_binary.rb)
265266
27. [Max Depth of Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/max_depth_of_binary_tree.rb)
266267
28. [Sorted Array to BST](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/sorted_array_to_bst.rb)
268+
29. [Maximum Subarray Sum](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/maximum_subarray_sum.rb)
269+
30. [Same Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/same_tree.rb)
270+
31. [Counting Bits](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/counting_bits.rb)
267271

268272
<a name="striver"/>
269273

Diff for: leetcode/grind75/counting_bits.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
=begin
2+
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n),
3+
ans[i] is the number of 1's in the binary representation of i.
4+
=end
5+
6+
#Solution 1 (O(n log n))
7+
def count_bits(n)
8+
ans = []
9+
for i in 0..n
10+
num_of_set_bits = 0
11+
while i>0
12+
pow = Math.log(i,2).to_int
13+
i-=(2**pow)
14+
num_of_set_bits+=1
15+
end
16+
ans.push(num_of_set_bits)
17+
end
18+
ans
19+
end
20+
21+
# Solution 2 (O(n))
22+
# Approach: Left shift or right shift doesn't change num of bits in even numbers it only halves the number
23+
def count_bits(n)
24+
bits_count = [0]*(n+1)
25+
return bits_count unless n>0
26+
bits_count[1]=1
27+
for i in 2..n
28+
bits_count[i] = bits_count[i/2]
29+
bits_count[i]+=1 unless i%2==0
30+
end
31+
bits_count
32+
end
33+
34+
#Solution 3 (O(n))
35+
def count_bits(n)
36+
bits_count = [0]*(n+1)
37+
return bits_count unless n>0
38+
bits_count[1]=1
39+
for i in 2..n
40+
# replaced division with right shift and condition with bitwise and op with 1
41+
bits_count[i] = bits_count[i>>1]+(i&1)
42+
end
43+
bits_count
44+
end
45+

Diff for: leetcode/grind75/maximum_subarray_sum.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=begin
2+
Given an integer array nums,
3+
find the subarray with the largest sum, and return its sum.
4+
=end
5+
6+
# Solution 1 (Kadane's Algorithm)
7+
def max_sub_array(nums)
8+
max_sum = nums[0]
9+
curr_sum = 0
10+
nums.each do |num|
11+
curr_sum=0 if curr_sum<0
12+
curr_sum+=num
13+
max_sum = curr_sum if curr_sum>max_sum
14+
end
15+
max_sum
16+
end
17+
18+
# Solution 2 (Using Dynamic Programming)
19+
#TO DO

Diff for: leetcode/grind75/same_tree.rb

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=begin
2+
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
3+
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
4+
=end
5+
6+
# Solution (using recursion)
7+
8+
# Definition for a binary tree node.
9+
# class TreeNode
10+
# attr_accessor :val, :left, :right
11+
# def initialize(val = 0, left = nil, right = nil)
12+
# @val = val
13+
# @left = left
14+
# @right = right
15+
# end
16+
# end
17+
18+
def is_same_tree(p, q)
19+
return true if p.nil? and q.nil?
20+
return false if p.nil? or q.nil? or (p.val!=q.val)
21+
is_same_tree(p.left,q.left) and is_same_tree(p.right,q.right)
22+
end

0 commit comments

Comments
 (0)