Skip to content

Commit

Permalink
Initial commit with solutions.
Browse files Browse the repository at this point in the history
  • Loading branch information
rboyd committed Feb 27, 2019
0 parents commit 5c9b04d
Show file tree
Hide file tree
Showing 29 changed files with 769 additions and 0 deletions.
32 changes: 32 additions & 0 deletions easy/count_and_say.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution:
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
ret = "1"
for i in range(n):
latest = ""
if i < 1:
next
else:
c = ret[0]
count = 0
for j in range(len(ret)):
print "i: " + str(i) + " latest: '" + latest + "'"
if ret[j] == c:
print "count (" + str(count) + ") += 1"
count += 1
else:
# convert 'count' integer to string
latest = latest + str(count) + c
print "else: " + latest
c = ret[j]
count = 1
latest = latest + str(count) + c
ret = latest
return ret

if __name__ == "__main__":
s = Solution()
print(s.countAndSay(5))
29 changes: 29 additions & 0 deletions easy/count_and_say.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution:
def countAndSay(self, n):
"""
:type n: int
:rtype: str
"""
ret = "1"
for i in range(n):
latest = ""
if i < 2:
next
else:
c = ret[0]
latest = ""
count = 0
for j in range(len(ret)):
if ret[j] == c and j < len(ret) - 1:
count += 1
else:
# convert 'count' integer to string
latest = latest + str(count) + c
c = ret[j]
count = 1
ret = latest
return ret

if __name__ == "__main__":
s = Solution()
print(s.countAndSay(1))
15 changes: 15 additions & 0 deletions easy/palindrome.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False
y = x
accum = 0
while y > 0:
j = y % 10
y = int(y / 10)
accum = (accum * 10) + j
return accum == x
21 changes: 21 additions & 0 deletions easy/remove-duplicates/sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
start = -Float::INFINITY
(0..nums.size-1).each do |i|
swapped = false
(i..nums.size-1).each do |j|
if nums[j] > start then
start = nums[i] = nums[j]
swapped = true
break
end
end
return i unless swapped
end
return nums.size
end


remove_duplicates([0,0,1,1,1,2,2,3,3,4])
remove_duplicates([1,1,2])
32 changes: 32 additions & 0 deletions easy/remove-duplicates/sub.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @param {Integer[]} nums
# @return {Integer}
def remove_duplicates(nums)
print nums
(0..nums.size-1).each do |i|
if nums[i].nil? then
j = i
loop do
j += 1
if !nums[j].nil?
nums[i] = nums[j]
break
end
end
return i
end

j = i
loop do
j += 1
break if j == nums.size-1
break if nums[j] > nums[i]
nums[j] = nil if nums[j] == nums[i]
end

end
return nums.size
end


remove_duplicates([0,0,1,1,1,2,2,3,3,4])
#remove_duplicates([1,1,2]) # => 1
17 changes: 17 additions & 0 deletions easy/rotate-array/sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/

def rotate(nums, k)
k.times do
tmp = nums[nums.size-1]
(nums.size-1).downto(1).each do |i|
nums[i] = nums[i-1]
end
nums[0] = tmp
end
end


nums = [1,2,3,4,5,6]
rotate(nums, 2)
print nums

27 changes: 27 additions & 0 deletions easy/valid-parentheses/sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @param {String} s
# @return {Boolean}
def is_valid(s)
st = []
s.split('').each do |c|
case c
when '('
st << ')'
when '['
st << ']'
when '{'
st << '}'
else
if c == st[-1]
st.pop
else
return false
end
end
end
st.empty?
end

is_valid("[")
# is_valid("(]")
# is_valid("()[]{}")
# is_valid("{[]}")
27 changes: 27 additions & 0 deletions easy/valid-parentheses/sub.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# @param {String} s
# @return {Boolean}
def is_valid(s)
st = [] # => []
s.split('').each do |c| # => ["(", ")"]
case c # => "("
when '('
st << ')' # => [")"]
break
when '['
st << ']'
next
when '{'
st << '}'
next
else
if c == st[-1]
st.pop!
else
return false
end
end
end # => nil
return true if st.empty? # => false
end # => :is_valid

is_valid("()") # => nil
32 changes: 32 additions & 0 deletions google/k_empty_slots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# @param {Integer[]} flowers
# @param {Integer} k
# @return {Integer}
def k_empty_slots(flowers, k)
(0..flowers.size-k-2).each do |left| # => 0..6
right = left + k + 1 # => 3, 4
found = true # => true, true
(left+1..right-1).each do |middle| # => 1..2, 2..3
found = false if flowers[middle].between?(flowers[left], flowers[right]) # => nil, false, nil, nil
end # => 1..2, 2..3

puts [left, right] if found # => nil, nil
puts [flowers[left], flowers[right]] if found # => nil, nil
puts [flowers[left], flowers[right]].max if found # => nil, nil
return [flowers[left], flowers[right]].max if found # => false, true
end
return -1
end # => :k_empty_slots

#k_empty_slots([1,3,2], 1) # => 2
#k_empty_slots([1,2,3], 1) # => -1
#k_empty_slots([1,2,3,4,5,7,6], 1)
k_empty_slots([6,5,8,9,7,1,10,2,3,4],2) # => 7

puts (5..3) # => nil

# >> 1
# >> 4
# >> 5
# >> 7
# >> 7
# >> 5..3
40 changes: 40 additions & 0 deletions hard/#valid_num.rb#
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

def is_number(s)
return true if /^\s*\+\.\d+/ =~ s
return false if /\..*\./ =~ s
return false if /^\s*\d+e\d+\.\d+$/ =~ s
return true if /^\s*\.\d+\s*$/ =~ s
return false if (/^\s*\.\s*$/ =~ s) or (/^\s*$/ =~ s) or (/^\s*\.+.*\s*$/ =~ s) or
(/^\s*e\d+\s*$/ =~ s)

0 == (/^\s*(?!e)(?!\+\.)(?!\-\-)(?!\-\+)([+-]\d)?\d*\.?(\.\d)?\d*([e]\d)?(e[+-]\d+)?\d*\.?(\d*)?\s*$/ =~ s)
end


/^\s*(?!e)(?!-)(?![-+])([+-]\d)?\d*(\.\d)?\d*+([e]\d)?(e[+-]\d+)?\d*\.?(\d*)?\s*$/ =~ 'te1'

is_number("te1")
is_number("..2")
is_number('+.8')
is_number('-1.')
is_number(".1")
is_number("2e10")
is_number(" -90e3 " )
is_number(" 1e" )
is_number(" 6e-1" )
is_number(" 99e2.5 " )
is_number("53.5e93" )
is_number("95a54e53" )
is_number("0")
is_number(" 0.1 ")
is_number("abc" )
is_number("1 a" )
is_number("e3" )
is_number(" --6 " )
is_number("-+3" )


/^\.\..*$/ =~ "..2"

/^\s*(?!\-\+)([+-]\d)?\d*\.?(\.\d)?\d*([e]\d)?(e[+-]\d+)?\d*\.?(\d*)?\s*$/ =~ "-1."
/^(?!\+\.)/ =~ '+.8'
18 changes: 18 additions & 0 deletions hard/median-of-two-sorted-arrays/sub.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@



find combined length, if odd then we're looking for the median of the set
if even then we're looking for the sum / 2 of the two middle numbers







# @param {Integer[]} nums1
# @param {Integer[]} nums2
# @return {Float}
def find_median_sorted_arrays(nums1, nums2)

end
29 changes: 29 additions & 0 deletions hard/valid_num.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

/^[+-]?\d+[\.]0$/ =~ "123.0" # => 0

/[+-]?(\d+[e?]|\d+[e?]\d*)([.]\d*)?/ =~ "123" # => nil

def is_number(s)

/^[+-]?\d+[\.]0$/ =~ s # => nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, ...

# /[+-]?(\d+[e?]|\d+[e?]\d*)([.]\d*)?/ =~ s # => nil, nil, nil, nil, 0, 1, 1, nil, 1, 1, ...
end # => :is_number


is_number("2e10") # => 0
is_number(" -90e3 " ) # => 1
is_number(" 1e" ) # => 1
is_number(" 6e-1" ) # => 1
is_number(" 99e2.5 " ) # => 1
is_number("53.5e93" ) # => 3
is_number("95a54e53" ) # => 3


is_number("0") # => nil
is_number(" 0.1 ") # => nil
is_number("abc" ) # => nil
is_number("1 a" ) # => nil
is_number("e3" ) # => nil
is_number(" --6 " ) # => nil
is_number("-+3" ) # => nil
3 changes: 3 additions & 0 deletions hard/valid_num.rb~
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def is_number(s)
/[+-]?(\d+[e?]|\d+[e?]\d*)([.]\d*)?/ =~ s
end
19 changes: 19 additions & 0 deletions medium/939/medium_area_rectangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution(object):
def minAreaRect(self, points):
columns = collections.defaultdict(list)
for x, y in points:
columns[x].append(y)
lastx = {}
ans = float('inf')
for x in sorted(columns):
column = columns[x]
column.sort()
for j, y2 in enumerate(column):
for i in range(j):
y1 = column[i]
if (y1, y2) in lastx:
ans = min(ans, (x - lastx[y1,y2]) * (y2 - y1))
lastx[y1, y2] = x
return ans if ans < float('inf') else 0

#
Empty file.
Loading

0 comments on commit 5c9b04d

Please sign in to comment.