Skip to content

Commit 9f4737d

Browse files
committed
63, 97, 120, 123, 188, 221, 300
1 parent 6a3744a commit 9f4737d

8 files changed

+454
-0
lines changed

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
| 58 | Length of Last Word | [Ruby](./algorithms/ruby/0058-length-of-last-word.rb) | Easy |
4545
| 59 | Spiral Matrix II | [Ruby](./algorithms/ruby/0059-spiral-matrix-ii.rb) | Medium |
4646
| 62 | Unique Paths | [Ruby](./algorithms/ruby/0062-unique-paths.rb) | Medium |
47+
| 63 | Unique Paths II | [Ruby](./algorithms/ruby/0063-unique-paths-ii.rb) | Medium |
4748
| 64 | Minimum Path Sum | [Ruby](./algorithms/ruby/0064-minimum-path-sum.rb) | Medium |
4849
| 66 | Plus One | [Ruby](./algorithms/ruby/0066-plus-one.rb) | Easy |
4950
| 67 | Add Binary | [Ruby](./algorithms/ruby/0067-add-binary.rb) [Python3](./algorithms/python3/0067-add-binary.py) | Easy |
@@ -59,6 +60,7 @@
5960
| 87 | Scramble String | [Ruby](./algorithms/ruby/0087-scramble-string.rb) | Hard |
6061
| 88 | Merge Sorted Array | [Ruby](./algorithms/ruby/0088-merge-sorted-array.rb) | Easy |
6162
| 91 | Decode Ways | [Ruby](./algorithms/ruby/0091-decode-ways.rb) | Medium |
63+
| 97 | Interleaving String | [Ruby](./algorithms/ruby/0097-interleaving-string.rb) | Medium |
6264
| 98 | Validate Binary Search Tree | [Ruby](./algorithms/ruby/0098-validate-binary-search-tree.rb) | Medium |
6365
| 100 | Same Tree | [Ruby](./algorithms/ruby/0100-same-tree.rb) | Easy |
6466
| 101 | Symmetric Tree | [Ruby](./algorithms/ruby/0101-symmetric-tree.rb) | Easy |
@@ -70,8 +72,10 @@
7072
| 109 | Convert Sorted List to Binary Search Tree | [Ruby](./algorithms/ruby/0109-convert-sorted-list-to-binary-search-tree.rb) | Medium |
7173
| 110 | Balanced Binary Tree | [Ruby](./algorithms/ruby/0110-balanced-binary-tree.rb) | Easy |
7274
| 113 | Path Sum II | [Ruby](./algorithms/ruby/0113-path-sum-ii.rb) | Medium |
75+
| 120 | Triangle | [Ruby](./algorithms/ruby/0120-triangle.rb) | Medium |
7376
| 121 | Best Time to Buy and Sell Stock | [Ruby](./algorithms/ruby/0121-best-time-to-buy-and-sell-stock.rb) | Easy |
7477
| 122 | Best Time to Buy and Sell Stock II | [Ruby](./algorithms/ruby/0122-best-time-to-buy-and-sell-stock-ii.rb) | Medium |
78+
| 123 | Best Time to Buy and Sell Stock III | [Ruby](./algorithms/ruby/0123-best-time-to-buy-and-sell-stock-iii.rb) | Hard |
7579
| 125 | Valid Palindrome | [Ruby](./algorithms/ruby/0125-valid-palindrome.rb) | Easy |
7680
| 128 | Longest Consecutive Sequence | [Ruby](./algorithms/ruby/0128-longest-consecutive-sequence.rb) | Medium |
7781
| 129 | Sum Root to Leaf Numbers | [Ruby](./algorithms/ruby/0129-sum-root-to-leaf-numbers.rb) | Medium |
@@ -89,6 +93,7 @@
8993
| 167 | Two Sum II - Input Array Is Sorted | [Ruby](./algorithms/ruby/0167-two-sum-ii-input-array-is-sorted.rb) | Medium |
9094
| 169 | Majority Element | [Ruby](./algorithms/ruby/0169-majority-element.rb) | Easy |
9195
| 173 | Binary Search Tree Iterator | [Ruby](./algorithms/ruby/0173-binary-search-tree-iterato.rb) | Medium |
96+
| 188 | Best Time to Buy and Sell Stock IV | [Ruby](./algorithms/ruby/0188-best-time-to-buy-and-sell-stock-iv.rb) | Hard |
9297
| 189 | Rotate Array | [Ruby](./algorithms/ruby/0189-rotate-array.rb) | Medium |
9398
| 198 | House Robber | [Ruby](./algorithms/ruby/0198-house-robber.rb) | Medium |
9499
| 199 | Binary Tree Right Side View | [Ruby](./algorithms/ruby/0199-binary-tree-right-side-view.rb) | Medium |
@@ -103,6 +108,7 @@
103108
| 215 | Kth Largest Element in an Array | [Ruby](./algorithms/ruby/0215-kth-largest-element-in-an-array.rb) | Medium |
104109
| 216 | Combination Sum III | [Ruby](./algorithms/ruby/0216-combination-sum-iii.rb) | Medium |
105110
| 219 | Contains Duplicate II | [Ruby](./algorithms/ruby/0219-contains-duplicate-ii.rb) | Easy |
111+
| 221 | Maximal Square | [Ruby](./algorithms/ruby/0221-maximal-square.rb) | Medium |
106112
| 224 | Basic Calculator | [Ruby](./algorithms/ruby/0224-basic-calculator.rb) | Hard |
107113
| 226 | Invert Binary Tree | [Ruby](./algorithms/ruby/0226-invert-binary-tree.rb) | Easy |
108114
| 227 | Basic Calculator II | [Ruby](./algorithms/ruby/0227-basic-calculator-ii.rb) | Medium |
@@ -121,6 +127,7 @@
121127
| 289 | Game of Life | [Ruby](./algorithms/ruby/0289-game-of-life.rb) | Medium |
122128
| 290 | Word Pattern | [Ruby](./algorithms/ruby/0290-word-pattern.rb) | Easy |
123129
| 299 | Bulls and Cows | [Ruby](./algorithms/ruby/0299-bulls-and-cows.rb) [Python3](./algorithms/python3/0299-bulls-and-cows.py) | Medium |
130+
| 300 | Longest Increasing Subsequence | [Ruby](./algorithms/ruby/0300-longest-increasing-subsequence.rb) | Medium |
124131
| 319 | Bulb Switcher | [Ruby](./algorithms/ruby/0319-bulb-switcher.rb) | Medium |
125132
| 322 | Coin Change | [Ruby](./algorithms/ruby/0322-coin-change.rb) | Medium |
126133
| 328 | Odd Even Linked List | [Ruby](./algorithms/ruby/0328-odd-even-linked-list.rb) | Medium |
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# 63. Unique Paths II
4+
# https://leetcode.com/problems/unique-paths-ii
5+
# Medium
6+
7+
=begin
8+
You are given an m x n integer array grid. There is a robot initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
9+
10+
An obstacle and space are marked as 1 or 0 respectively in grid. A path that the robot takes cannot include any square that is an obstacle.
11+
12+
Return the number of possible unique paths that the robot can take to reach the bottom-right corner.
13+
14+
The testcases are generated so that the answer will be less than or equal to 2 * 109.
15+
16+
Example 1:
17+
Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
18+
Output: 2
19+
Explanation: There is one obstacle in the middle of the 3x3 grid above.
20+
There are two ways to reach the bottom-right corner:
21+
1. Right -> Right -> Down -> Down
22+
2. Down -> Down -> Right -> Right
23+
24+
Example 2:
25+
Input: obstacleGrid = [[0,1],[0,0]]
26+
Output: 1
27+
28+
Constraints:
29+
m == obstacleGrid.length
30+
n == obstacleGrid[i].length
31+
1 <= m, n <= 100
32+
obstacleGrid[i][j] is 0 or 1.
33+
=end
34+
35+
# @param {Integer[][]} obstacle_grid
36+
# @return {Integer}
37+
# @param {Integer[][]} obstacle_grid
38+
# @return {Integer}
39+
def unique_paths_with_obstacles(obstacle_grid)
40+
hight = obstacle_grid.count
41+
width = obstacle_grid[0].count
42+
43+
return 0 if hight * width == 0 || obstacle_grid[0][0] == 1
44+
45+
obstacle_grid[0][0] = 1
46+
47+
(1...hight).each do |y|
48+
obstacle_grid[y][0] = obstacle_grid[y - 1][0] * (1 - obstacle_grid[y][0])
49+
end
50+
51+
(1...width).each do |x|
52+
obstacle_grid[0][x] = obstacle_grid[0][x - 1] * (1 - obstacle_grid[0][x])
53+
end
54+
55+
(1...hight).each do |y|
56+
(1...width).each do |x|
57+
obstacle_grid[y][x] = (obstacle_grid[y][x - 1] + obstacle_grid[y - 1][x]) * (1 - obstacle_grid[y][x])
58+
end
59+
end
60+
61+
obstacle_grid[hight - 1][width - 1]
62+
end
63+
64+
# ********************#
65+
# TEST #
66+
# ********************#
67+
68+
require "test/unit"
69+
class Test_unique_paths_with_obstacles < Test::Unit::TestCase
70+
def test_
71+
assert_equal 2, unique_paths_with_obstacles([[0, 0, 0], [0, 1, 0], [0, 0, 0]])
72+
assert_equal 1, unique_paths_with_obstacles([[0, 1], [0, 0]])
73+
end
74+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# 97. Interleaving String
4+
# https://leetcode.com/problems/interleaving-string
5+
# Medium
6+
7+
=begin
8+
Given strings s1, s2, and s3, find whether s3 is formed by an interleaving of s1 and s2.
9+
10+
An interleaving of two strings s and t is a configuration where s and t are divided into n and m substrings respectively, such that:
11+
12+
s = s1 + s2 + ... + sn
13+
t = t1 + t2 + ... + tm
14+
|n - m| <= 1
15+
The interleaving is s1 + t1 + s2 + t2 + s3 + t3 + ... or t1 + s1 + t2 + s2 + t3 + s3 + ...
16+
Note: a + b is the concatenation of strings a and b.
17+
18+
Example 1:
19+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac"
20+
Output: true
21+
Explanation: One way to obtain s3 is:
22+
Split s1 into s1 = "aa" + "bc" + "c", and s2 into s2 = "dbbc" + "a".
23+
Interleaving the two splits, we get "aa" + "dbbc" + "bc" + "a" + "c" = "aadbbcbcac".
24+
Since s3 can be obtained by interleaving s1 and s2, we return true.
25+
26+
Example 2:
27+
Input: s1 = "aabcc", s2 = "dbbca", s3 = "aadbbbaccc"
28+
Output: false
29+
Explanation: Notice how it is impossible to interleave s2 with any other string to obtain s3.
30+
31+
Example 3:
32+
Input: s1 = "", s2 = "", s3 = ""
33+
Output: true
34+
35+
Constraints:
36+
0 <= s1.length, s2.length <= 100
37+
0 <= s3.length <= 200
38+
s1, s2, and s3 consist of lowercase English letters.
39+
=end
40+
41+
# @param {String} s1
42+
# @param {String} s2
43+
# @param {String} s3
44+
# @return {Boolean}
45+
def is_interleave(s1, s2, s3)
46+
return false if s1.length + s2.length != s3.length
47+
48+
row = [true] * (s2.length + 1)
49+
(1...row.length).each do |j|
50+
row[j] = row[j - 1] && s2[j - 1] == s3[j - 1]
51+
end
52+
53+
s1.length.times do |i|
54+
row[0] = row[0] && s1[i] == s3[i]
55+
(1...row.length).each do |j|
56+
row[j] = (row[j] && s1[i] == s3[i + j]) || (row[j - 1] && s2[j - 1] == s3[i + j])
57+
end
58+
end
59+
60+
row[-1]
61+
end
62+
63+
# ********************#
64+
# TEST #
65+
# ********************#
66+
67+
require "test/unit"
68+
class Test_is_interleave < Test::Unit::TestCase
69+
def test_
70+
assert_equal true, is_interleave("aabcc", "dbbca", "aadbbcbcac")
71+
assert_equal false, is_interleave("aabcc", "dbbca", "aadbbbaccc")
72+
assert_equal true, is_interleave("", "", "")
73+
end
74+
end

algorithms/ruby/0120-triangle.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# frozen_string_literal: true
2+
3+
# 120. Triangle
4+
# https://leetcode.com/problems/triangle
5+
# Medium
6+
7+
=begin
8+
Given a triangle array, return the minimum path sum from top to bottom.
9+
10+
For each step, you may move to an adjacent number of the row below. More formally, if you are on index i on the current row, you may move to either index i or index i + 1 on the next row.
11+
12+
Example 1:
13+
Input: triangle = [[2],[3,4],[6,5,7],[4,1,8,3]]
14+
Output: 11
15+
Explanation: The triangle looks like:
16+
2
17+
3 4
18+
6 5 7
19+
4 1 8 3
20+
The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above).
21+
22+
Example 2:
23+
Input: triangle = [[-10]]
24+
Output: -10
25+
26+
Constraints:
27+
1 <= triangle.length <= 200
28+
triangle[0].length == 1
29+
triangle[i].length == triangle[i - 1].length + 1
30+
-104 <= triangle[i][j] <= 104
31+
=end
32+
33+
# @param {Integer[][]} triangle
34+
# @return {Integer}
35+
def minimum_total(triangle)
36+
cost, last_cost = nil, nil
37+
triangle.size.times do |i|
38+
cost = triangle[triangle.size - i - 1]
39+
cost.size.times { |j| cost[j] += [last_cost[j], last_cost[j + 1]].min } if i > 0
40+
last_cost = cost
41+
end
42+
cost.first
43+
end
44+
45+
# **************** #
46+
# TEST #
47+
# **************** #
48+
49+
require "test/unit"
50+
class Test_minimum_total < Test::Unit::TestCase
51+
def test_
52+
assert_equal 11, minimum_total([[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]])
53+
assert_equal(-10, minimum_total([[-10]]))
54+
end
55+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# frozen_string_literal: true
2+
3+
# 123. Best Time to Buy and Sell Stock III
4+
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii
5+
# Hard
6+
7+
=begin
8+
You are given an array prices where prices[i] is the price of a given stock on the ith day.
9+
10+
Find the maximum profit you can achieve. You may complete at most two transactions.
11+
12+
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
14+
Example 1:
15+
Input: prices = [3,3,5,0,0,3,1,4]
16+
Output: 6
17+
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
18+
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
19+
20+
Example 2:
21+
Input: prices = [1,2,3,4,5]
22+
Output: 4
23+
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
24+
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are engaging multiple transactions at the same time. You must sell before buying again.
25+
26+
Example 3:
27+
Input: prices = [7,6,4,3,1]
28+
Output: 0
29+
Explanation: In this case, no transaction is done, i.e. max profit = 0.
30+
31+
Constraints:
32+
1 <= prices.length <= 105
33+
0 <= prices[i] <= 105
34+
=end
35+
36+
# @param {Integer[]} prices
37+
# @return {Integer}
38+
def max_profit(prices)
39+
cost1 = cost2 = 100000
40+
profit1 = profit2 = temp = 0
41+
prices.each do |price|
42+
cost1 = price if cost1 > price
43+
profit1 = temp if profit1 < temp = price - cost1
44+
cost2 = temp if cost2 > temp = price - profit1
45+
profit2 = temp if profit2 < temp = price - cost2
46+
end
47+
profit2
48+
end
49+
50+
# **************** #
51+
# TEST #
52+
# **************** #
53+
54+
require "test/unit"
55+
class Test_max_profit < Test::Unit::TestCase
56+
def test_
57+
assert_equal 6, max_profit([3, 3, 5, 0, 0, 3, 1, 4])
58+
assert_equal 4, max_profit([1, 2, 3, 4, 5])
59+
assert_equal 0, max_profit([7, 6, 4, 3, 1])
60+
end
61+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
# 188. Best Time to Buy and Sell Stock IV
4+
# https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv
5+
# Hard
6+
7+
=begin
8+
You are given an integer array prices where prices[i] is the price of a given stock on the ith day, and an integer k.
9+
10+
Find the maximum profit you can achieve. You may complete at most k transactions: i.e. you may buy at most k times and sell at most k times.
11+
12+
Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).
13+
14+
Example 1:
15+
Input: k = 2, prices = [2,4,1]
16+
Output: 2
17+
Explanation: Buy on day 1 (price = 2) and sell on day 2 (price = 4), profit = 4-2 = 2.
18+
19+
Example 2:
20+
Input: k = 2, prices = [3,2,6,5,0,3]
21+
Output: 7
22+
Explanation: Buy on day 2 (price = 2) and sell on day 3 (price = 6), profit = 6-2 = 4. Then buy on day 5 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
23+
24+
Constraints:
25+
1 <= k <= 100
26+
1 <= prices.length <= 1000
27+
0 <= prices[i] <= 1000
28+
=end
29+
30+
# @param {Integer} k
31+
# @param {Integer[]} prices
32+
# @return {Integer}
33+
def max_profit(k, prices)
34+
return 0 if prices.count < 2
35+
36+
n = prices.count
37+
if k > n / 2
38+
differences = (1..prices.count - 1).reduce([]) { |ar, i| ar << (prices[i] - prices[i - 1]) }
39+
return differences.select { |num| num > 0 }.reduce(:+) || 0
40+
end
41+
42+
d = Array.new(k + 1) { Array.new(n + 1) { 0 } }
43+
(1..k).each do |i|
44+
tmp_max = d[i - 1][0] - prices[0]
45+
46+
(1..n - 1).each do |j|
47+
d[i][j] = [ d[i][j - 1], prices[j] + tmp_max ].max
48+
tmp_max = [ tmp_max, d[i - 1][j] - prices[j] ].max
49+
end
50+
end
51+
52+
d[k][n - 1]
53+
end
54+
55+
# **************** #
56+
# TEST #
57+
# **************** #
58+
59+
require "test/unit"
60+
class Test_max_profit < Test::Unit::TestCase
61+
def test_
62+
assert_equal 2, max_profit(2, [2, 4, 1])
63+
assert_equal 7, max_profit(2, [3, 2, 6, 5, 0, 3])
64+
end
65+
end

0 commit comments

Comments
 (0)