Skip to content

Instantly share code, notes, and snippets.

View codertcet111's full-sized avatar
💭
Consistency brings happiness

codertcet111

💭
Consistency brings happiness
View GitHub Profile
@codertcet111
codertcet111 / leetcode_42_trapping_rain_water.rb
Created November 6, 2024 15:20
leetcode 42 trapping rain water
=begin
42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining.
Example 1:
Input: height = [0,1,0,2,1,0,1,3,2,1,2,1]
Output: 6
Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped.
Example 2:
@codertcet111
codertcet111 / leetcode_347_top_k_frequent_element.rb
Created October 23, 2024 16:49
347. Leetcode top K frequent elements
=begin
Leetcode 347. Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
Example 1:
Input: nums = [1,1,1,2,2,3], k = 2
@codertcet111
codertcet111 / leetcode_49_group_anagrams_with_3_solution.rb
Created October 23, 2024 16:19
49. Leetcode group anagrams problem with 3 approaches
=begin
Leetcode 49. Group Anagrams
Given an array of strings strs, group the
anagrams
together. You can return the answer in any order.
Example 1:
Input: strs = ["eat","tea","tan","ate","nat","bat"]
@codertcet111
codertcet111 / leetcode_53_maximum_subarray.rb
Created April 4, 2024 04:31
Leetcode 53 Maximum Subarray
Leetcode 53 Maximum Subarray
# @param {Integer[]} nums
# @return {Integer}
def max_sub_array(nums)
max = nums[0]
sum = 0
nums.each_with_index do |ele, i|
sum += ele
if sum > max
@codertcet111
codertcet111 / leetcode_51_n_queen.rb
Created April 4, 2024 03:57
Leetcode 51 N queen
Leetcode 51 N queen
def solve_n_queens(n)
@n = n
@solutions = []
board = Array.new(n) { Array.new(n, '.') }
place_queens(board, 0)
@solutions
@codertcet111
codertcet111 / leetcode_49_group_anagrams.rb
Created April 4, 2024 03:56
Leetcode 49 Group Anagram
Leetcode 49 Group Anagram
# @param {String[]} strs
# @return {String[][]}
def group_anagrams(strs)
sum_ascii_hash = {}
strs.each_with_index do |str, i|
sorted_aa = str.chars.sort.join
sum_ascii_hash["#{sorted_aa}"] ? sum_ascii_hash["#{sorted_aa}"] << i : sum_ascii_hash["#{sorted_aa}"] = [i]
@codertcet111
codertcet111 / leetcode_48_rotate_image.rb
Created April 4, 2024 03:55
Leetcode 48 Rotate image
Leetcode 48 Rotate image
# @param {Integer[][]} matrix
# @return {Void} Do not return anything, modify matrix in-place instead.
def rotate(matrix)
# Just change row's to column, like [1,2,3] 1st row will become last column
# Then 2nd row [4,5,6] will become 2nd column and so on
#ignore: matrix.transpose.reverse
n = matrix.length
(0...n).each do |i|
@codertcet111
codertcet111 / leetcode_45_jump_game.rb
Created April 4, 2024 03:54
Leetcode 45 Jump game
Leetcode 45 Jump game
# @param {Integer[]} nums
# @return {Integer}
def jump(nums)
# Explanation:
# Example: [6,4,3,1,1,5,4,3,2,3,1,9,7,4,3,2,1,1,6,2,1]
# for each ele in loop
# for current ele, check the next possible window with ele sized elements
@codertcet111
codertcet111 / leetcode_38_count_and_say.rb
Created March 30, 2024 11:26
Leetcode 38: count and say
Leetcode 38: count and say
# @param {Integer} n
# @return {String}
def count_and_say(n)
return "1" if n == 1
str = count_and_say(n-1)
str = str.split("").map {|ch| ch.to_i}
#str.tally.map{|k,v| "#{v}#{k}" }.join("")
len = str.length
@codertcet111
codertcet111 / leetcode_18_four_sum.rb
Last active March 30, 2024 05:59
Leetcode 18: 4 sum
Leetcode 18: 4 sum hard
# def four_sum(nums, target)
# nums.sort!
# set = Set.new
# output = []
# (0..nums.length - 4).each do |i|
# (i + 1..nums.length - 3).each do |j|