Skip to content

Commit d4dd90b

Browse files
committedMay 26, 2023
Linked List(61, 86, 146)
1 parent fc25d9b commit d4dd90b

File tree

4 files changed

+207
-0
lines changed

4 files changed

+207
-0
lines changed
 

‎README.md

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
| 55 | Jump Game | [Ruby](./algorithms/ruby/0055-jump-game.rb) | Medium |
4646
| 58 | Length of Last Word | [Ruby](./algorithms/ruby/0058-length-of-last-word.rb) | Easy |
4747
| 59 | Spiral Matrix II | [Ruby](./algorithms/ruby/0059-spiral-matrix-ii.rb) | Medium |
48+
| 61 | Rotate List | [Ruby](./algorithms/ruby/0061-rotate-list.rb) | Medium |
4849
| 62 | Unique Paths | [Ruby](./algorithms/ruby/0062-unique-paths.rb) | Medium |
4950
| 63 | Unique Paths II | [Ruby](./algorithms/ruby/0063-unique-paths-ii.rb) | Medium |
5051
| 64 | Minimum Path Sum | [Ruby](./algorithms/ruby/0064-minimum-path-sum.rb) | Medium |
@@ -60,6 +61,7 @@
6061
| 76 | Minimum Window Substring | [Ruby](./algorithms/ruby/0076-minimum-window-substring.rb) | Hard |
6162
| 80 | Remove Duplicates from Sorted Array II | [Ruby](./algorithms/ruby/0080-remove-duplicates-from-sorted-array-ii.rb) | Medium |
6263
| 82 | Remove Duplicates from Sorted List II | [Ruby](./algorithms/ruby/0082-remove-duplicates-from-sorted-list-ii.rb) | Medium |
64+
| 86 | Partition List | [Ruby](./algorithms/ruby/0086-partition-list.rb) | Medium |
6365
| 87 | Scramble String | [Ruby](./algorithms/ruby/0087-scramble-string.rb) | Hard |
6466
| 88 | Merge Sorted Array | [Ruby](./algorithms/ruby/0088-merge-sorted-array.rb) | Easy |
6567
| 91 | Decode Ways | [Ruby](./algorithms/ruby/0091-decode-ways.rb) | Medium |
@@ -92,6 +94,7 @@
9294
| 139 | Word Break | [Ruby](./algorithms/ruby/0139-word-break.rb) | Medium |
9395
| 141 | Linked List Cycle | [Ruby](./algorithms/ruby/0141-linked-list-cycle.rb) | Easy |
9496
| 142 | Linked List Cycle II | [Ruby](./algorithms/ruby/0142-linked-list-cycle-ii.rb) | Medium |
97+
| 146 | LRU Cache | [Ruby](./algorithms/ruby/0146-lru-cache.rb) | Medium |
9598
| 148 | Sort List | [Ruby](./algorithms/ruby/0148-sort-list.rb) | Medium |
9699
| 149 | Max Points on a Line | [Ruby](./algorithms/ruby/0149-max-points-on-a-line.rb) | Hard |
97100
| 150 | Evaluate Reverse Polish Notation | [Ruby](./algorithms/ruby/0150-evaluate-reverse-polish-notation.rb) | Medium |

‎algorithms/ruby/0061-rotate-list.rb

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# frozen_string_literal: true
2+
3+
# 61. Rotate List
4+
# https://leetcode.com/problems/rotate-list
5+
# Medium
6+
7+
=begin
8+
Given the head of a linked list, rotate the list to the right by k places.
9+
10+
Example 1:
11+
Input: head = [1,2,3,4,5], k = 2
12+
Output: [4,5,1,2,3]
13+
14+
Example 2:
15+
Input: head = [0,1,2], k = 4
16+
Output: [2,0,1]
17+
18+
Constraints:
19+
* The number of nodes in the list is in the range [0, 500].
20+
* -100 <= Node.val <= 100
21+
* 0 <= k <= 2 * 109
22+
=end
23+
24+
# Definition for singly-linked list.
25+
# class ListNode
26+
# attr_accessor :val, :next
27+
# def initialize(val = 0, _next = nil)
28+
# @val = val
29+
# @next = _next
30+
# end
31+
# end
32+
# @param {ListNode} head
33+
# @param {Integer} k
34+
# @return {ListNode}
35+
def rotate_right(head, k)
36+
return head if head.nil? || head.next.nil? || k == 0
37+
38+
# Calculate the length of the linked list
39+
length = 1
40+
old_tail = head
41+
while old_tail.next
42+
old_tail = old_tail.next
43+
length += 1
44+
end
45+
46+
# Update k to be the effective number of rotations needed
47+
k %= length
48+
return head if k == 0
49+
50+
# Find the new tail node (length - k - 1) and the new head node (length - k)
51+
new_tail = head
52+
(length - k - 1).times { new_tail = new_tail.next }
53+
new_head = new_tail.next
54+
55+
# Update the next pointers of the new tail and the old tail
56+
new_tail.next = nil
57+
old_tail.next = head
58+
59+
new_head
60+
end
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# frozen_string_literal: true
2+
3+
# 86. Partition List
4+
# https://leetcode.com/problems/partition-list
5+
# Medium
6+
7+
=begin
8+
Given the head of a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
9+
10+
You should preserve the original relative order of the nodes in each of the two partitions.
11+
12+
Example 1:
13+
Input: head = [1,4,3,2,5,2], x = 3
14+
Output: [1,2,2,4,3,5]
15+
16+
Example 2:
17+
Input: head = [2,1], x = 2
18+
Output: [1,2]
19+
20+
Constraints:
21+
* The number of nodes in the list is in the range [0, 200].
22+
* -100 <= Node.val <= 100
23+
* -200 <= x <= 200
24+
=end
25+
26+
# Definition for singly-linked list.
27+
# class ListNode
28+
# attr_accessor :val, :next
29+
# def initialize(val = 0, _next = nil)
30+
# @val = val
31+
# @next = _next
32+
# end
33+
# end
34+
# @param {ListNode} head
35+
# @param {Integer} x
36+
# @return {ListNode}
37+
def partition(head, x)
38+
# Create dummy nodes for before and after lists
39+
before = ListNode.new(0)
40+
after = ListNode.new(0)
41+
42+
# Initialize pointers for before and after lists
43+
before_ptr = before
44+
after_ptr = after
45+
46+
# Iterate through the linked list
47+
while head
48+
if head.val < x
49+
# Append the node to the before list
50+
before_ptr.next = head
51+
before_ptr = before_ptr.next
52+
else
53+
# Append the node to the after list
54+
after_ptr.next = head
55+
after_ptr = after_ptr.next
56+
end
57+
58+
# Move to the next node in the original list
59+
head = head.next
60+
end
61+
62+
# Connect the before list with the after list
63+
before_ptr.next = after.next
64+
65+
# Set the last node of the after list to nil
66+
after_ptr.next = nil
67+
68+
# Return the new head of the partitioned list
69+
before.next
70+
end

‎algorithms/ruby/0146-lru-cache.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# 146. LRU Cache
4+
# https://leetcode.com/problems/lru-cache
5+
# Medium
6+
7+
=begin
8+
Design a data structure that follows the constraints of a Least Recently Used (LRU) cache.
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+
The functions get and put must each run in O(1) average time complexity.
16+
17+
Example 1:
18+
Input
19+
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
20+
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
21+
Output
22+
[null, null, null, 1, null, -1, null, -1, 3, 4]
23+
Explanation
24+
LRUCache lRUCache = new LRUCache(2);
25+
lRUCache.put(1, 1); // cache is {1=1}
26+
lRUCache.put(2, 2); // cache is {1=1, 2=2}
27+
lRUCache.get(1); // return 1
28+
lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3}
29+
lRUCache.get(2); // returns -1 (not found)
30+
lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3}
31+
lRUCache.get(1); // return -1 (not found)
32+
lRUCache.get(3); // return 3
33+
lRUCache.get(4); // return 4
34+
35+
Constraints:
36+
1 <= capacity <= 3000
37+
0 <= key <= 104
38+
0 <= value <= 105
39+
At most 2 * 105 calls will be made to get and put.
40+
=end
41+
42+
class LRUCache
43+
# Initialize your data structure here
44+
# @param {Integer} capacity
45+
def initialize(capacity)
46+
@capacity = capacity
47+
@elems = {}
48+
end
49+
50+
# @param {Integer} key
51+
# @return {Integer}
52+
def get(key)
53+
val = @elems.delete key
54+
if val
55+
@elems[key] = val
56+
else
57+
-1
58+
end
59+
end
60+
61+
# @param {Integer} key
62+
# @param {Integer} value
63+
# @return {Void}
64+
def put(key, value)
65+
@elems.delete key
66+
@elems[key] = value
67+
@elems.delete @elems.first.first if @elems.size > @capacity
68+
end
69+
end
70+
71+
# Your LRUCache object will be instantiated and called as such:
72+
# obj = LRUCache.new(capacity)
73+
# param_1 = obj.get(key)
74+
# obj.put(key, value)

0 commit comments

Comments
 (0)