Created
July 8, 2023 07:06
-
-
Save kkuprikov/30eab66f72e287c91c3e1453677fb554 to your computer and use it in GitHub Desktop.
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
def foobar n=100 | |
(1..n).each do |i| | |
output = "#{'Hi' if i % 3 == 0}#{'By' if i % 5 == 0}" | |
output = i.to_s if output.empty? | |
puts output | |
end | |
end | |
def swapcase str | |
str.chars.map{ |c| c == c.downcase ? c.upcase : c.downcase }.join | |
# Or just str.swapcase | |
end | |
# Let me know if you use DP on a daily basis! | |
# https://leetcode.com/problems/word-break/solutions/3454380/python3-dp-bottom-up-with-step-by-step-explanation/ | |
# https://leetcode.com/problems/triangle/solutions/3646355/from-brute-force-to-dp-with-o-n-space-in-ruby/ | |
# This one I've actually coded myself some time ago: | |
def max_subarray nums | |
return 0 if nums.empty? | |
dp = nums.map(&:clone) | |
result = dp[0] | |
(1..nums.size - 1).each do |i| | |
dp[i] = [nums[i], nums[i] + dp[i - 1]].max | |
result = [result, dp[i]].max | |
end | |
result | |
end | |
# Last task relatively looks like a TSP: https://en.wikipedia.org/wiki/Travelling_salesman_problem |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment