Skip to content

Instantly share code, notes, and snippets.

View alvinlau's full-sized avatar

Alvin Lau alvinlau

View GitHub Profile
@alvinlau
alvinlau / simplify-path.rb
Last active October 23, 2023 17:12
71. Simplify Path
# https://leetcode.com/problems/simplify-path/description/
# 71. Simplify Path
# Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.
# In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.
# The canonical path should have the following format:
@alvinlau
alvinlau / coin-change.rb
Last active October 20, 2023 01:55
322. Coin Change
# https://leetcode.com/problems/coin-change/description/
# 322. Coin Change
# You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money.
# Return the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1.
# You may assume that you have an infinite number of each kind of coin.
@alvinlau
alvinlau / beautiful-towers-i.rb
Created October 18, 2023 04:53
2865. Beautiful Towers I
# https://leetcode.com/problems/beautiful-towers-i/
# 2865. Beautiful Towers I
# You are given a 0-indexed array maxHeights of n integers.
# You are tasked with building n towers in the coordinate line. The ith tower is built at coordinate i and has a height of heights[i].
# A configuration of towers is beautiful if the following conditions hold:
@alvinlau
alvinlau / design-circular-queue.rb
Created October 16, 2023 21:26
622. Design Circular Queue
# https://leetcode.com/problems/design-circular-queue/description/
# 622. Design Circular Queue
# Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle, and the last position is connected back to the first position to make a circle. It is also called "Ring Buffer".
# One of the benefits of the circular queue is that we can make use of the spaces in front of the queue. In a normal queue, once the queue becomes full, we cannot insert the next element even if there is a space in front of the queue. But using the circular queue, we can use the space to store new values.
# Implement the MyCircularQueue class:
@alvinlau
alvinlau / employee-importance.rb
Last active October 14, 2023 01:33
690. Employee Importance
# https://leetcode.com/problems/employee-importance/
# 690. Employee Importance
# You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.
# You are given an array of employees employees where:
# employees[i].id is the ID of the ith employee.
# employees[i].importance is the importance value of the ith employee.
@alvinlau
alvinlau / construct-binary-tree-from-preorder-and-inorder-traversal.rb
Last active October 11, 2023 01:45
105. Construct Binary Tree from Preorder and Inorder Traversal
# https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
# Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.
# Example 1:
# Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
# Output: [3,9,20,null,null,15,7]
@alvinlau
alvinlau / partition-list.rb
Created October 8, 2023 10:16
86. Partition List
# https://leetcode.com/problems/partition-list/
# 86. Partition List
# 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.
# You should preserve the original relative order of the nodes in each of the two partitions.
@alvinlau
alvinlau / multiply-strings.rb
Last active October 5, 2023 12:29
43. Multiply Strings
# https://leetcode.com/problems/multiply-strings/
# 43. Multiply String
# Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string.
# Note: You must not use any built-in BigInteger library or convert the inputs to integer directly.
# Example 1:
@alvinlau
alvinlau / swap-nodes-in-pairs.rb
Created August 9, 2022 06:48
24. Swap Nodes in Pairs
# https://leetcode.com/problems/swap-nodes-in-pairs/
# Runtime: 81 ms, faster than 92.68% of Ruby online submissions for Swap Nodes in Pairs.
# Memory Usage: 211.1 MB, less than 18.70% of Ruby online submissions for Swap Nodes in Pairs.
# 24. Swap Nodes in Pairs
# Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)
@alvinlau
alvinlau / 3sum-closest.rb
Created August 9, 2022 06:26
16. 3Sum Closest
# https://leetcode.com/problems/3sum-closest/
# 16. 3Sum Closest
# Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
# Return the sum of the three integers.
# You may assume that each input would have exactly one solution.