Skip to content

Commit 6d3d8ca

Browse files
authored
Added tasks 142-338
1 parent 77087d3 commit 6d3d8ca

File tree

32 files changed

+2352
-1
lines changed

32 files changed

+2352
-1
lines changed

README.md

Lines changed: 95 additions & 0 deletions
Large diffs are not rendered by default.

src/main/ruby/g0001_0100/s0079_word_search/readme.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def traverse(board, word, idx, i, j)
7777

7878
board[i][j] = '#'
7979

80-
# res = false
8180
res = (traverse(board, word, idx + 1, i, j + 1) ||
8281
traverse(board, word, idx + 1, i, j - 1) ||
8382
traverse(board, word, idx + 1, i + 1, j) ||
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 141\. Linked List Cycle
5+
6+
Easy
7+
8+
Given `head`, the head of a linked list, determine if the linked list has a cycle in it.
9+
10+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to. **Note that `pos` is not passed as a parameter**.
11+
12+
Return `true` _if there is a cycle in the linked list_. Otherwise, return `false`.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
17+
18+
**Input:** head = [3,2,0,-4], pos = 1
19+
20+
**Output:** true
21+
22+
**Explanation:** There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)
27+
28+
**Input:** head = [1,2], pos = 0
29+
30+
**Output:** true
31+
32+
**Explanation:** There is a cycle in the linked list, where the tail connects to the 0th node.
33+
34+
**Example 3:**
35+
36+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)
37+
38+
**Input:** head = [1], pos = -1
39+
40+
**Output:** false
41+
42+
**Explanation:** There is no cycle in the linked list.
43+
44+
**Constraints:**
45+
46+
* The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
47+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
48+
* `pos` is `-1` or a **valid index** in the linked-list.
49+
50+
**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
51+
52+
## Solution
53+
54+
```ruby
55+
# @param {ListNode} head
56+
# @return {Boolean}
57+
def hasCycle(head)
58+
return false if head.nil?
59+
60+
fast = head.next
61+
slow = head
62+
63+
while fast && fast.next
64+
return true if fast == slow
65+
66+
fast = fast.next.next
67+
slow = slow.next
68+
end
69+
70+
false
71+
end
72+
```
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 142\. Linked List Cycle II
5+
6+
Medium
7+
8+
Given the `head` of a linked list, return _the node where the cycle begins. If there is no cycle, return_ `null`.
9+
10+
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next` pointer. Internally, `pos` is used to denote the index of the node that tail's `next` pointer is connected to (**0-indexed**). It is `-1` if there is no cycle. **Note that** `pos` **is not passed as a parameter**.
11+
12+
**Do not modify** the linked list.
13+
14+
**Example 1:**
15+
16+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist.png)
17+
18+
**Input:** head = [3,2,0,-4], pos = 1
19+
20+
**Output:** tail connects to node index 1
21+
22+
**Explanation:** There is a cycle in the linked list, where tail connects to the second node.
23+
24+
**Example 2:**
25+
26+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test2.png)
27+
28+
**Input:** head = [1,2], pos = 0
29+
30+
**Output:** tail connects to node index 0
31+
32+
**Explanation:** There is a cycle in the linked list, where tail connects to the first node.
33+
34+
**Example 3:**
35+
36+
![](https://assets.leetcode.com/uploads/2018/12/07/circularlinkedlist_test3.png)
37+
38+
**Input:** head = [1], pos = -1
39+
40+
**Output:** no cycle
41+
42+
**Explanation:** There is no cycle in the linked list.
43+
44+
**Constraints:**
45+
46+
* The number of the nodes in the list is in the range <code>[0, 10<sup>4</sup>]</code>.
47+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
48+
* `pos` is `-1` or a **valid index** in the linked-list.
49+
50+
**Follow up:** Can you solve it using `O(1)` (i.e. constant) memory?
51+
52+
## Solution
53+
54+
```ruby
55+
# Definition for singly-linked list.
56+
# class ListNode
57+
# attr_accessor :val, :next
58+
# def initialize(val)
59+
# @val = val
60+
# @next = nil
61+
# end
62+
# end
63+
64+
# @param {ListNode} head
65+
# @return {ListNode}
66+
def detectCycle(head)
67+
return nil if head.nil? || head.next.nil?
68+
69+
slow = head
70+
fast = head
71+
72+
while fast && fast.next
73+
fast = fast.next.next
74+
slow = slow.next
75+
76+
# Intersected inside the loop.
77+
break if slow == fast
78+
end
79+
80+
return nil if fast.nil? || fast.next.nil?
81+
82+
slow = head
83+
while slow != fast
84+
slow = slow.next
85+
fast = fast.next
86+
end
87+
88+
slow
89+
end
90+
```
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 146\. LRU Cache
5+
6+
Medium
7+
8+
Design a data structure that follows the constraints of a **[Least Recently Used (LRU) cache](https://en.wikipedia.org/wiki/Cache_replacement_policies#LRU)**.
9+
10+
Implement the `LRUCache` class:
11+
12+
* `LRUCache(int capacity)` Initialize the LRU cache with **positive** size `capacity`.
13+
* `int get(int key)` Return the value of the `key` if the key exists, otherwise return `-1`.
14+
* `void put(int key, int value)` Update the value of the `key` if the `key` exists. Otherwise, add the `key-value` pair to the cache. If the number of keys exceeds the `capacity` from this operation, **evict** the least recently used key.
15+
16+
The functions `get` and `put` must each run in `O(1)` average time complexity.
17+
18+
**Example 1:**
19+
20+
**Input** ["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
21+
22+
**Output:** [null, null, null, 1, null, -1, null, -1, 3, 4]
23+
24+
**Explanation:**
25+
26+
LRUCache lRUCache = new LRUCache(2);
27+
lRUCache.put(1, 1); // cache is {1=1}
28+
lRUCache.put(2, 2); // cache is {1=1, 2=2}
29+
lRUCache.get(1); // return 1
30+
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
31+
lRUCache.get(2); // returns -1 (not found)
32+
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
33+
lRUCache.get(1); // return -1 (not found)
34+
lRUCache.get(3); // return 3
35+
lRUCache.get(4); // return 4
36+
37+
**Constraints:**
38+
39+
* `1 <= capacity <= 3000`
40+
* <code>0 <= key <= 10<sup>4</sup></code>
41+
* <code>0 <= value <= 10<sup>5</sup></code>
42+
* At most 2<code> * 10<sup>5</sup></code> calls will be made to `get` and `put`.
43+
44+
## Solution
45+
46+
```ruby
47+
class LRUCache
48+
class LruCacheNode
49+
attr_accessor :key, :value, :prev, :next
50+
51+
def initialize(k, v)
52+
@key = k
53+
@value = v
54+
end
55+
end
56+
57+
=begin
58+
:type capacity: Integer
59+
=end
60+
def initialize(cap)
61+
@capacity = cap
62+
@cache_map = {}
63+
# insert here
64+
@head = nil
65+
# remove here
66+
@tail = nil
67+
end
68+
69+
=begin
70+
:type key: Integer
71+
:rtype: Integer
72+
=end
73+
def get(key)
74+
val_node = @cache_map[key]
75+
return -1 if val_node.nil?
76+
77+
move_to_head(val_node)
78+
val_node.value
79+
end
80+
81+
=begin
82+
:type key: Integer
83+
:type value: Integer
84+
:rtype: Void
85+
=end
86+
def put(key, value)
87+
val_node = @cache_map[key]
88+
89+
if val_node
90+
val_node.value = value
91+
move_to_head(val_node)
92+
else
93+
if @cache_map.size < @capacity
94+
if @cache_map.empty?
95+
node = LruCacheNode.new(key, value)
96+
@cache_map[key] = node
97+
@head = node
98+
@tail = node
99+
else
100+
node = LruCacheNode.new(key, value)
101+
@cache_map[key] = node
102+
node.next = @head
103+
@head.prev = node
104+
@head = node
105+
end
106+
else
107+
# remove from tail
108+
last = @tail
109+
@tail = last.prev
110+
@tail.next = nil unless @tail.nil?
111+
@cache_map.delete(last.key)
112+
@head = nil if @cache_map.empty?
113+
# Call recursively
114+
put(key, value)
115+
end
116+
end
117+
end
118+
119+
private
120+
121+
def move_to_head(node)
122+
return if node == @head
123+
124+
@tail = node.prev if node == @tail
125+
prev = node.prev
126+
nex = node.next
127+
prev.next = nex
128+
nex.prev = prev unless nex.nil?
129+
node.prev = nil
130+
node.next = @head
131+
@head.prev = node
132+
@head = node
133+
end
134+
end
135+
136+
# Your LRUCache object will be instantiated and called as such:
137+
# obj = LRUCache.new(capacity)
138+
# param_1 = obj.get(key)
139+
# obj.put(key, value)
140+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Ruby/LeetCode-in-Ruby?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork)
3+
4+
## 148\. Sort List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, return _the list after sorting it in **ascending order**_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_1.jpg)
13+
14+
**Input:** head = [4,2,1,3]
15+
16+
**Output:** [1,2,3,4]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/09/14/sort_list_2.jpg)
21+
22+
**Input:** head = [-1,5,3,4,0]
23+
24+
**Output:** [-1,0,3,4,5]
25+
26+
**Example 3:**
27+
28+
**Input:** head = []
29+
30+
**Output:** []
31+
32+
**Constraints:**
33+
34+
* The number of nodes in the list is in the range <code>[0, 5 * 10<sup>4</sup>]</code>.
35+
* <code>-10<sup>5</sup> <= Node.val <= 10<sup>5</sup></code>
36+
37+
**Follow up:** Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?
38+
39+
## Solution
40+
41+
```ruby
42+
require_relative '../../com_github_leetcode/list_node'
43+
44+
# Definition for singly-linked list.
45+
# class ListNode
46+
# attr_accessor :val, :next
47+
# def initialize(val = 0, _next = nil)
48+
# @val = val
49+
# @next = _next
50+
# end
51+
# end
52+
# @param {ListNode} head
53+
# @return {ListNode}
54+
def sort_list(head)
55+
list = []
56+
new_head = ListNode.new
57+
58+
while head
59+
list << head.val
60+
head = head.next
61+
end
62+
63+
curr = new_head
64+
list.sort.each do |v|
65+
curr.next = ListNode.new(v)
66+
curr = curr.next
67+
end
68+
69+
new_head.next
70+
end
71+
```

0 commit comments

Comments
 (0)