Skip to content

Commit e79cb8d

Browse files
authored
Merge pull request #44 from remy727/20230324
205, 392, 1466
2 parents 5ee4798 + f6ac8cd commit e79cb8d

File tree

4 files changed

+203
-0
lines changed

4 files changed

+203
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,14 @@
4141
| 121 | Best Time to Buy and Sell Stock | [Ruby](./algorithms/ruby/0121-best-time-to-buy-and-sell-stock.rb) | Easy |
4242
| 129 | Sum Root to Leaf Numbers | [Ruby](./algorithms/ruby/0129-sum-root-to-leaf-numbers.rb) | Medium |
4343
| 142 | Linked List Cycle II | [Ruby](./algorithms/ruby/0142-linked-list-cycle-ii.rb) | Medium |
44+
| 205 | Isomorphic Strings | [Ruby](./algorithms/ruby/0205-isomorphic-strings.rb) | Easy |
4445
| 208 | Implement Trie (Prefix Tree) | [Ruby](./algorithms/ruby/0208-implement-trie-prefix-tree.rb) | Medium |
4546
| 211 | Design Add and Search Words Data Structure | [Ruby](./algorithms/ruby/0211-design-add-and-search-words-data-structure.rb) | Medium |
4647
| 226 | Invert Binary Tree | [Ruby](./algorithms/ruby/0226-invert-binary-tree.rb) | Easy |
4748
| 344 | Reverse String | [Ruby](./algorithms/ruby/0344-reverse-string.rb) | Easy |
4849
| 382 | Linked List Random Node | [Ruby](./algorithms/ruby/0382-linked-list-random-node.rb) | Medium |
4950
| 387 | First Unique Character in a String | [Ruby](./algorithms/ruby/0387-first-unique-character-in-a-string.rb) | Easy |
51+
| 392 | Is Subsequence | [Ruby](./algorithms/ruby/0392-is-subsequence.rb) | Easy |
5052
| 427 | Construct Quad Tree | [Ruby](./algorithms/ruby/0427-construct-quad-tree.rb) | Medium |
5153
| 438 | Find All Anagrams in a String | [Ruby](./algorithms/ruby/0438-find-all-anagrams-in-a-string.rb) | Medium |
5254
| 443 | String Compression | [Ruby](./algorithms/ruby/0443-string-compression.rb) [Python3](./algorithms/python3/0443-string-compression.py) | Medium |
@@ -75,6 +77,7 @@
7577
| 1162 | As Far from Land as Possible | [Ruby](./algorithms/ruby/1162-as-far-from-land-as-possible.rb) | Medium |
7678
| 1319 | Number of Operations to Make Network Connected | [Ruby](./algorithms/ruby/1319-number-of-operations-to-make-network-connected.rb) | Medium |
7779
| 1345 | Jump Game IV | [Ruby](./algorithms/ruby/1345-jump-game-iv.rb) | Hard |
80+
| 1466 | Reorder Routes to Make All Paths Lead to the City Zero | [Ruby](./algorithms/ruby/1466-eorder-routes-to-make-all-paths-lead-to-the-city-zero.rb) | Medium |
7881
| 1470 | Shuffle the Array | [Ruby](./algorithms/ruby/1470-shuffle-the-array.rb) | Easy |
7982
| 1472 | Design Browser History | [Ruby](./algorithms/ruby/1472-design-browser-history.rb) | Medium |
8083
| 1480 | Running Sum of 1d Array | [Ruby](./algorithms/ruby/1480-running-sum-of-1d-array.rb) | Easy |
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
# 205. Isomorphic Strings
4+
# https://leetcode.com/problems/isomorphic-strings
5+
6+
=begin
7+
8+
Given two strings s and t, determine if they are isomorphic.
9+
10+
Two strings s and t are isomorphic if the characters in s can be replaced to get t.
11+
12+
All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character, but a character may map to itself.
13+
14+
### Example 1:
15+
16+
Input: s = "egg", t = "add"
17+
Output: true
18+
19+
### Example 2:
20+
21+
Input: s = "foo", t = "bar"
22+
Output: false
23+
24+
### Example 3:
25+
26+
Input: s = "paper", t = "title"
27+
Output: true
28+
29+
### Constraints:
30+
31+
* 1 <= s.length <= 5 * 104
32+
* t.length == s.length
33+
* s and t consist of any valid ascii character.
34+
35+
=end
36+
37+
# Runtime 258 ms
38+
# Memory 211.3 MB
39+
# @param {String} s
40+
# @param {String} t
41+
# @return {Boolean}
42+
def is_isomorphic(s, t)
43+
ms = {}
44+
mt = {}
45+
n = s.length
46+
i = 0
47+
48+
while i < n && ms[s[i]] == mt[t[i]]
49+
ms[s[i]] = mt[t[i]] = i + 1
50+
i += 1
51+
end
52+
53+
i == n
54+
end
55+
56+
# **************** #
57+
# TEST #
58+
# **************** #
59+
60+
require "test/unit"
61+
class Test_is_isomorphic < Test::Unit::TestCase
62+
def test_
63+
assert_equal true, is_isomorphic("egg", "add")
64+
assert_equal false, is_isomorphic("foo", "bar")
65+
assert_equal true, is_isomorphic("paper", "title")
66+
end
67+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
# 392. Is Subsequence
4+
# https://leetcode.com/problems/is-subsequence
5+
6+
=begin
7+
8+
Given two strings s and t, return true if s is a subsequence of t, or false otherwise.
9+
10+
A subsequence of a string is a new string that is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (i.e., "ace" is a subsequence of "abcde" while "aec" is not).
11+
12+
### Example 1:
13+
14+
Input: s = "abc", t = "ahbgdc"
15+
Output: true
16+
17+
### Example 2:
18+
19+
Input: s = "axc", t = "ahbgdc"
20+
Output: false
21+
22+
### Constraints:
23+
24+
* 0 <= s.length <= 100
25+
* 0 <= t.length <= 104
26+
* s and t consist only of lowercase English letters.
27+
28+
=end
29+
30+
# Runtime 86 ms
31+
# Memory 212.1 MB
32+
# @param {String} s
33+
# @param {String} t
34+
# @return {Boolean}
35+
def is_subsequence(s, t)
36+
return true if s.size == 0
37+
return false if s.size > t.size
38+
39+
sp = 0
40+
t.chars.each do |char|
41+
if s[sp] == char
42+
sp += 1
43+
return true if sp == s.size
44+
end
45+
end
46+
47+
false
48+
end
49+
50+
# **************** #
51+
# TEST #
52+
# **************** #
53+
54+
require "test/unit"
55+
class Test_is_subsequence < Test::Unit::TestCase
56+
def test_
57+
assert_equal true, is_subsequence("abc", "ahbgdc")
58+
assert_equal false, is_subsequence("axc", "ahbgdc")
59+
end
60+
end
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
# 1466. Reorder Routes to Make All Paths Lead to the City Zero
4+
# https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero
5+
6+
=begin
7+
8+
There are n cities numbered from 0 to n - 1 and n - 1 roads such that there is only one way to travel between two different cities (this network form a tree). Last year, The ministry of transport decided to orient the roads in one direction because they are too narrow.
9+
10+
Roads are represented by connections where connections[i] = [ai, bi] represents a road from city ai to city bi.
11+
12+
This year, there will be a big event in the capital (city 0), and many people want to travel to this city.
13+
14+
Your task consists of reorienting some roads such that each city can visit the city 0. Return the minimum number of edges changed.
15+
16+
It's guaranteed that each city can reach city 0 after reorder.
17+
18+
### Example 1:
19+
Input: n = 6, connections = [[0,1],[1,3],[2,3],[4,0],[4,5]]
20+
Output: 3
21+
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
22+
23+
### Example 2:
24+
Input: n = 5, connections = [[1,0],[1,2],[3,2],[3,4]]
25+
Output: 2
26+
Explanation: Change the direction of edges show in red such that each node can reach the node 0 (capital).
27+
28+
### Example 3:
29+
Input: n = 3, connections = [[1,0],[2,0]]
30+
Output: 0
31+
32+
=end
33+
34+
# Runtime 396 ms
35+
# Memory 234 MB
36+
# @param {Integer} n
37+
# @param {Integer[][]} connections
38+
# @return {Integer}
39+
def min_reorder(n, connections)
40+
al = {}
41+
connections.each do |c|
42+
al[c[0]] = al[c[0]].to_a + [c[1]]
43+
al[c[1]] = al[c[1]].to_a + [-c[0]]
44+
end
45+
46+
dfs(al, [], 0)
47+
end
48+
49+
def dfs(al, visited, from)
50+
change = 0
51+
visited[from] = true
52+
53+
al[from].each do |to|
54+
unless visited[to.abs]
55+
change += dfs(al, visited, to.abs) + (to > 0 ? 1 : 0)
56+
end
57+
end
58+
59+
change
60+
end
61+
62+
# **************** #
63+
# TEST #
64+
# **************** #
65+
66+
require "test/unit"
67+
class Test_min_reorder < Test::Unit::TestCase
68+
def test_
69+
assert_equal 3, min_reorder(6, [[0, 1], [1, 3], [2, 3], [4, 0], [4, 5]])
70+
assert_equal 2, min_reorder(5, [[1, 0], [1, 2], [3, 2], [3, 4]])
71+
assert_equal 0, min_reorder(3, [[1, 0], [2, 0]])
72+
end
73+
end

0 commit comments

Comments
 (0)