This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode 51 N queen | |
def solve_n_queens(n) | |
@n = n | |
@solutions = [] | |
board = Array.new(n) { Array.new(n, '.') } | |
place_queens(board, 0) | |
@solutions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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| |
NewerOlder