Skip to content

kamilrybacki/RustyLeetcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

70 Commits
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode problems in Rust

This repository contains my solutions to LeetCode problems in Rust.

Environment

All problems are solved in VS Code using the LeetCode extension.

Results

# Title Solution Difficulty Speed score Memory score Comments
1 Two Sum Rust Easy 23.84 % 68.85 % Two pointers: one at $i$ and other at $j>i$
9 Palindrome Number Rust Easy 69.35 % 17.69 % Split into digits and converge towards center
13 Roman to Integer Rust Easy 100 % 16.15 % Use mapping for combinations 🤷
14 Longest Common Prefix Rust Easy 100 % 55.24 % Filter remaining strings for containing characters from first one
20 Valid Parentheses Rust Easy 50.15 % 19.94 % Stack
21 Merge Two Sorted Lists Rust Easy 6.97% 35.95% Make list of ordered nodes and collapse it from the end
26 Remove Duplicates from Sorted Array Rust Easy 100 % 78.6 % Make list with uniques and then replace first $n$ entries
27 Remove Element Rust Easy 79.48 % 46.72 % Use two pointers (from start and end)
35 Search Insert Position Rust Easy 78.53 % 66.75 % Check when difference with target changes signs
58 Length of Last Word Rust Easy 100 % 40.44 % Reset counter on whitespace, except for the last character
66 Plus One Rust Easy 79.3 % 73.02 % Split number into digits and do carry overs for digits preceeding urrent index
67 Add Binary Rust Easy 26.51 % 26.05 % Similar to the 66
69 Sqrt(x) Rust Easy 100 % 74.03 % Newton's method for calculating square root
70 Climbing Stairs Rust Easy 100 % 37.6 % Recurrence @ $n-1$ and $n-2$ for $n>1$ (for $n\in\lbrace1,2\rbrace$ return $n$) + memo with HashMap
83 Remove Duplicates from Sorted List Rust Easy 9.48 % 88.79 % List with unique entries and then wrap the values from end
88 Merge Sorted Array Rust Easy 83.65 % 47.4 % Three pointers (for $n$, $m$ and $n+m$)
94 Binary Tree Inorder Traversal Rust Easy 100 % 38.52 % Recursion while enforcing the left-root-right order + clone the nodes accessed as refs and borrowed from RefCells
100 Same Tree Rust Easy 100 % 91.67 % Recursion with match that covers cases for node_1 and node_2 (etc. (None,None) ) that does triple boolean expression comaring lefts, roots and rights
101 Symmetric Tree Rust Easy 100 % 29.84 % Check if (L,R) is (None,None) or (Some,Some) with equal L and R's ($L.R=R.L \land L.L=R.R$)
104 Maximum Depth of Binary Tree Rust Easy 100 % 13.27 % Create vector of ints with depths for each branches and stop at None with pushing the current_depth
108 Convert Sorted Array to Binary Search Tree Rust Easy 72.86 % 37.14 % Recursion, with values split at middle index for left and right branches + calculating middle index w.r.t. to evenness of nums length (substract 1 if even)
110 Balanced Binary Tree Rust Easy 100% 16.98% Height for branch is 1 + max of $H(l)$ and $H(r)$, their differences must be <= 1 AND the $l$ and $r$ must also be balanced (recursion)
111 Minimum Depth of Binary Tree Rust Easy 58.24 % 6.59 % Recursion with running counter for branch, if two branches are present - choose the minimum counter value, if at None return counter - 1
112 Path Sum Rust Easy 65.6 % 28.8 % At each node check if there are available paths on left and/or right. If one is available, go down that path only with recursion, if both - check both and return OR of their respective recursion calls. If there are no paths left, check if sum == target (there is nowhere left to go). At each step increment sum by val
118 Pascal's Triangle Rust Easy 84.02 % 58.9 % Use previous row entries from 0 to len - 1 to calculate inner numbers of next row, append 1s to its start and end. First row is always [1], second one is always [1,1]. Use running row counter.
119 Pascal's Triangle II Rust Easy 100 % 47.78 % Same as 118, but only return the last row
121 Best Time to Buy and Sell Stock Rust Easy 89.95 % 68.92 % Keep track of minimum price and maximum profit (greedy algorithm)
125 Valid Palindrome Rust Easy 100 % 34.21 % Iterate over characters forwards and backwards, while skipping non-alphanumeric characters (while also advancing index), and compare their lowercase ASCII representations
136 Single Number Rust Easy 81.45 % 62.16 % Iterate and keep track on seen numbers, if a number is seen twice - remove it from cache - the result is the remaining entry (there is also a trick with XOR operator): result ^= number
141 Linked List Cycle - Easy NaN NaN NOT AVAILABLE IN RUST (due to difficulties with multi-pointers in Rust i.e. borrowing rules)
144 Binary Tree Preorder Traversal Rust Easy 100 % 53.97 % Recursion with root -> left -> right order (the usual None/Some letf-right schtick)
145 Binary Tree Postorder Traversal Rust Easy 100 % 45.28 % Recursion with left -> right -> root order (just change the order in the previous problem of appending values 🤷)
160 Intersection of Two Linked Lists - Easy NaN NaN NOT AVAILABLE IN RUST (due to difficulties with multi-pointers in Rust i.e. borrowing rules)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages