├── 1. Two Sum (Leetcode) ├── main.cpp ├── main.py └── main2.py ├── 1002. Find Common Characters (Leetcode) └── main.py ├── 1009. Complement of Base 10 Integer (Leetcode) └── main.py ├── 1017. Convert to Base -2 (Leetcode) └── main.py ├── 1030. Matrix Cells in Distance Order (Leetcode) └── main.py ├── 104. Maximum Depth of Binary Tree (Leetcode) └── main.py ├── 1050. Actors and Directors Who Cooperated At Least Three Times (Leetcode) └── main.py ├── 1051. Height Checker (Leetcode) └── main.py ├── 1099. Two Sum Less Than K (Leetcode) └── main.py ├── 1108. Defanging an IP Address (Leetcode) └── main.py ├── 1109. Corporate Flight Bookings └── main.py ├── 1119. Remove Vowels from a String └── main.py ├── 1137. N-th Tribonacci Number (Leetcode) └── main.py ├── 1148. Article Views I (Leetcode) └── main.py ├── 1154. Day of the Year (Leetcode) └── main.py ├── 118. Pascal's Triangle (Leetcode) └── main.py ├── 1185. Day of the Week (Leetcode) └── main.py ├── 1189. Maximum Number of Balloons (Leetcode) └── main.py ├── 119. Pascal's Triangle II (Leetcode) └── main.py ├── 12. Integer to Roman (Leetcode) └── main.py ├── 1206. Design Skiplist └── main.py ├── 1207. Unique Number of Occurrences └── main.py ├── 1232. Check If It Is a Straight Line (Leetcode) └── main.py ├── 1244. Design A Leaderboard (Leetcode) └── main.py ├── 125. Valid Palindrome (Leetcode) └── main.py ├── 1281. Subtract the Product and Sum of Digits of an Integer (Leetcode) └── main.py ├── 1290. Convert Binary Number in a Linked List to Integer (Leetcode) └── main.py ├── 1295. Find Numbers with Even Number of Digits (Leetcode) └── main.py ├── 13. Roman to Integer (Leetcode) └── main.py ├── 1303. Find the Team Size (Leetcode) └── main.py ├── 1304. Find N Unique Integers Sum up to Zero (Leetcode) └── main.py ├── 1317. Convert Integer to the Sum of Two No-Zero Integers (Leetcode) └── main.py ├── 1342. Number of Steps to Reduce a Number to Zero (Leetcode) └── main.py ├── 1344. Angle Between Hands of a Clock (Leetcode) └── main.py ├── 1346. Check If N and Its Double Exist (Leetcode) └── main.py ├── 1350. Students With Invalid Departments (Leetcode) └── main.py ├── 1351. Count Negative Numbers in a Sorted Matrix (Leetcode) └── main.py ├── 1357. Apply Discount Every n Orders (Leetcode) └── main.py ├── 136. Single Number (Leetcode) └── main.py ├── 1360. Number of Days Between Two Dates └── main.py ├── 1365. How Many Numbers Are Smaller Than the Current Number (Leetcode) └── main.py ├── 137. Single Number II (Leetcode) └── main.py ├── 1374. Generate a String With Characters That Have Odd Counts (Leetcode) └── main.py ├── 1378. Replace Employee ID With The Unique Identifier (Leetcode) └── main.py ├── 1381. Design a Stack With Increment Operation └── main.py ├── 1390. Four Divisors (Leetcode) └── main.py ├── 1398. Customers Who Bought Products A and B but Not C (Leetcode) └── main.py ├── 1399. Count Largest Group (Leetcode) └── main.py ├── 141. Linked List Cycle (Leetcode) └── main.py ├── 142. Linked List Cycle II (Leetcode) └── main.py ├── 1431. Kids With the Greatest Number of Candies └── main.py ├── 1436. Destination City └── main.py ├── 144. Binary Tree Preorder Traversal (Leetcode) └── main.py ├── 1445. Apples & Oranges (Leetcode) └── main.py ├── 1446. Consecutive Characters (Leetcode) └── main.py ├── 145. Binary Tree Postorder Traversal (Leetcode) └── main.py ├── 1451. Rearrange Words in a Sentence (Leetcode) └── main.py ├── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (Leetcode) └── main.py ├── 1464. Maximum Product of Two Elements in an Array (Leetcode) └── main.py ├── 147. Insertion Sort List (Leetcode) └── main.py ├── 1470. Shuffle the Array (Leetcode) └── main.py ├── 1472. Design Browser History (Leetcode) └── main.py ├── 148. Sort List (Leetcode) └── main.py ├── 1480. Running Sum of 1d Array (Leetcode) └── main.py ├── 1486. XOR Operation in an Array (Leetcode) └── main.py ├── 1491. Average Salary Excluding the Minimum and Maximum Salary (Leetcode) └── main.py ├── 1492. The kth Factor of n (Leetcode) └── main.py ├── 1502. Can Make Arithmetic Progression From Sequence (Leetcode) └── main.py ├── 1507. Reformat Date (Leetcode) └── main.py ├── 151. Reverse Words in a String (Leetcode) ├── main.py └── main2.py ├── 1512. Number of Good Pairs (Leetcode) └── main.py ├── 1517. Find Users With Valid E-Mails (Leetcode) └── main.py ├── 1523. Count Odd Numbers in an Interval Range (Leetcode) └── main.py ├── 1527. Patients With a Condition (Leetcode) └── main.py ├── 1528. Shuffle String (Leetcode) └── main.py ├── 1534. Count Good Triplets (Leetcode) └── main.py ├── 1539. Kth Missing Positive Number (Leetcode) └── main.py ├── 155. Min Stack └── main.py ├── 1550. Three Consecutive Odds (Leetcode) └── main.py ├── 1570. Dot Product of Two Sparse Vectors └── main.py ├── 1571. Warehouse Manager (Leetcode) └── main.py ├── 1572. Matrix Diagonal Sum (Leetcode) └── main.py ├── 1582. Special Positions in a Binary Matrix (Leetcode) └── main.py ├── 1598. Crawler Log Folder └── main.py ├── 160. Intersection of Two Linked Lists (Leetcode) └── main.py ├── 1603. Design Parking System └── main.py ├── 1633. Percentage of Users Attended a Contest └── main.py ├── 1636. Sort Array by Increasing Frequency └── main.py ├── 164. Maximum Gap (Leetcode) └── main.py ├── 1641. Count Sorted Vowel Strings (Leetcode) └── main.py ├── 1652. Defuse the Bomb (Leetcode) └── main.py ├── 1662. Check If Two String Arrays are Equivalent (Leetcode) └── main.py ├── 1667. Fix Names in a Table (Leetcode) └── main.py ├── 167. Two Sum II - Input Array Is Sorted └── main.py ├── 1672. Richest Customer Wealth (Leetcode) └── main.py ├── 1683. Invalid Tweets (Leetcode) └── main.py ├── 1684. Count the Number of Consistent Strings (Leetcode) └── main.py ├── 1688. Count of Matches in Tournament (Leetcode) └── main.py ├── 169. Majority Element (Leetcode) └── main.py ├── 17. Letter Combinations of a Phone Number (Leetcode) └── main.py ├── 170. Two Sum III - Data structure design (Leetcode) └── main.py ├── 1704. Determine if String Halves Are Alike (Leetcode) └── main.py ├── 171. Excel Sheet Column Number (Leetcode) └── main.py ├── 1716. Calculate Money in Leetcode Bank (Leetcode) └── main.py ├── 1721. Swapping Nodes in a Linked List └── main.py ├── 1732. Find the Highest Altitude └── main.py ├── 1748. Sum of Unique Elements (Leetcode) └── main.py ├── 1756. Design Most Recently Used Queue └── main.py ├── 1757. Recyclable and Low Fat Products (Leetcode) └── main.py ├── 176. Second Highest Salary (Leetcode) └── main.py ├── 1768. Merge Strings Alternately └── main.py ├── 1773. Count Items Matching a Rule (Leetcode) └── main.py ├── 1779. Find Nearest Point That Has the Same X or Y Coordinate (Leetcode) └── main.py ├── 1780. Check if Number is a Sum of Powers of Three (Leetcode) └── main.py ├── 1812. Determine Color of a Chessboard Square (Leetcode) └── main.py ├── 1816. Truncate Sentence (Leetcode) └── main.py ├── 182. Duplicate Emails (Leetcode) └── main.py ├── 1821. Find Customers With Positive Revenue this Year └── main.py ├── 1822. Sign of the Product of an Array (Leetcode) ├── main.py ├── main1.py └── main2.py ├── 183. Customers Who Never Order (Leetcode) └── main.py ├── 1832. Check if the Sentence Is Pangram (Leetcode) └── main.py ├── 1837. Sum of Digits in Base K (Leetcode) └── main.py ├── 184. Department Highest Salary (Leetcode) └── main.py ├── 1873. Calculate Special Bonus (Leetcode) └── main.py ├── 189. Rotate Array (Leetcode) └── main.py ├── 19. Remove Nth Node From End of List (Leetcode) └── main.py ├── 1907. Count Salary Categories (Leetcode) └── main.py ├── 191. Number of 1 Bits (Leetcode) └── main.py ├── 1913. Maximum Product Difference Between Two Pairs (Leetcode) └── main.py ├── 1920. Build Array from Permutation (Leetcode) └── main.py ├── 1929. Concatenation of Array (Leetcode) └── main.py ├── 1935. Maximum Number of Words You Can Type (Leetcode) └── main.py ├── 1941. Check if All Characters Have Equal Number of Occurrences (Leetcode) └── main.py ├── 1952. Three Divisors (Leetcode) └── main.py ├── 1957. Delete Characters to Make Fancy String (Leetcode) └── main.py ├── 1965. Employees With Missing Information (Leetcode) └── main.py ├── 1967. Number of Strings That Appear as Substrings in Word (Leetcode) └── main.py ├── 1979. Find Greatest Common Divisor of Array (Leetcode) └── main.py ├── 1995. Count Special Quadruplets (Leetcode) └── main.py ├── 2. Add Two Numbers (Leetcode) └── main.py ├── 2000. Reverse Prefix of Word (Leetcode) └── main.py ├── 2006. Count Number of Pairs With Absolute Difference K (Leetcode) └── main.py ├── 2011. Final Value of Variable After Performing Operations (Leetcode) └── main.py ├── 202. Happy Number └── main.py ├── 203. Remove Linked List Elements (Leetcode) └── main.py ├── 2032. Two Out of Three (Leetcode) └── main.py ├── 204. Count Primes (Leetcode) ├── main.py └── main1.py ├── 2043. Simple Bank System └── main.py ├── 2053. Kth Distinct String in an Array (Leetcode) └── main.py ├── 2057. Smallest Index With Equal Value (Leetcode) └── main.py ├── 206. Reverse Linked List (Leetcode) └── main.py ├── 2073. Time Needed to Buy Tickets └── main.py ├── 2074. Reverse Nodes in Even Length Groups (Leetcode) └── main.py ├── 2085. Count Common Words With One Occurrence (Leetcode) └── main.py ├── 2089. Find Target Indices After Sorting Array (Leetcode) └── main.py ├── 2095. Delete the Middle Node of a Linked List (Leetcode) └── main.py ├── 21. Merge Two Sorted Lists (Leetcode) └── main.py ├── 2114. Maximum Number of Words Found in Sentences (Leetcode) └── main.py ├── 2119. A Number After a Double Reversal (Leetcode) ├── main.py └── main2.py ├── 2126. Destroying Asteroids └── main.py ├── 2129. Capitalize the Title (Leetcode) └── main.py ├── 2130. Maximum Twin Sum of a Linked List └── main.py ├── 2154. Keep Multiplying Found Values by Two (Leetcode) └── main.py ├── 2169. Count Operations to Obtain Zero (Leetcode) └── main.py ├── 217. Contains Duplicate (Leetcode) └── main.py ├── 2176. Count Equal and Divisible Pairs in an Array (Leetcode) └── main.py ├── 2180. Count Integers With Even Digit Sum (Leetcode) └── main.py ├── 2181. Merge Nodes in Between Zeros (Leetcode) └── main.py ├── 2190. Most Frequent Number Following Key In an Array (Leetcode) └── main.py ├── 2194. Cells in a Range on an Excel Sheet (Leetcode) └── main.py ├── 2206. Divide Array Into Equal Pairs (Leetcode) └── main.py ├── 2215. Find the Difference of Two Arrays (Leetcode) └── main.py ├── 222. Count Complete Tree Nodes (Leetcode) └── main.py ├── 2221. Find Triangular Sum of an Array (Leetcode) └── main.py ├── 2224. Minimum Number of Operations to Convert Time (Leetcode) └── main.py ├── 2235. Add Two Integers (Leetcode) └── main.py ├── 225. Implement Stack using Queues └── main.py ├── 2256. Minimum Average Difference (Leetcode) └── main.py ├── 226. Invert Binary Tree (Leetcode) └── main.py ├── 2278. Percentage of Letter in String (Leetcode) └── main.py ├── 2288. Apply Discount to Prices (Leetcode) └── main.py ├── 229. Majority Element II (Leetcode) └── main.py ├── 23. Merge k Sorted Lists (Leetcode) └── main.py ├── 2309. Greatest English Letter in Upper and Lower Case (Leetcode) └── main.py ├── 231. Power of Two (Leetcode) └── main.py ├── 232. Implement Queue using Stacks └── main.py ├── 2325. Decode the Message (Leetcode) └── main.py ├── 2339. All the Matches of the League └── main.py ├── 234. Palindrome Linked List (Leetcode) └── main.py ├── 2351. First Letter to Appear Twice (Leetcode) └── main.py ├── 2356. Number of Unique Subjects Taught by Each Teacher (Leetcode) └── main.py ├── 2363. Merge Similar Items (Leetcode) └── main.py ├── 2367. Number of Arithmetic Triplets (Leetcode) └── main.py ├── 237. Delete Node in a Linked List (Leetcode) └── main.py ├── 238. Product of Array Except Self (Leetcode) └── main.py ├── 2390. Removing Stars From a String └── main.py ├── 2396. Strictly Palindromic Number (Leetcode) └── main.py ├── 240. Search a 2D Matrix II (Leetcode) └── main.py ├── 2404. Most Frequent Even Element └── main.py ├── 2413. Smallest Even Multiple (Leetcode) └── main.py ├── 242.Valid Anagram (Leetcode) └── main.py ├── 2423. Remove Letter To Equalize Frequency (Leetcode) └── main.py ├── 2427. Number of Common Factors (Leetcode) └── main.py ├── 2455. Average Value of Even Numbers That Are Divisible by Three (Leetcode) ├── main.py └── main1.py ├── 2465. Number of Distinct Averages (Leetcode) └── main.py ├── 2469. Convert the Temperature (Leetcode) └── main.py ├── 2475. Number of Unequal Triplets in Array (Leetcode) └── main.py ├── 2481. Minimum Cuts to Divide a Circle (Leetcode) └── main.py ├── 2485. Find the Pivot Integer └── main.py ├── 2490. Circular Sentence └── main.py ├── 2500. Delete Greatest Value in Each Row (Leetcode) └── main.py ├── 2520. Count the Digits That Divide a Number (Leetcode) └── main.py ├── 2529. Maximum Count of Positive Integer and Negative Integer (Leetcode) └── main.py ├── 2535. Difference Between Element Sum and Digit Sum of an Array (Leetcode) └── main.py ├── 2540. Minimum Common Value (Leetcode) └── main.py ├── 2544. Alternating Digit Sum (Leetcode) └── main.py ├── 2578. Split With Minimum Sum (Leetcode) └── main.py ├── 258. Add Digits (Leetcode) └── main.py ├── 258. Add Digits └── main.py ├── 2582. Pass the Pillow (Leetcode) └── main.py ├── 2586. Count the Number of Vowel Strings in Range (Leetcode) └── main.py ├── 2595. Number of Even and Odd Bits (Leetcode) └── main.py ├── 26. Remove Duplicates from Sorted Array (Leetcode) └── main.py ├── 260. Single Number III (Leetcode) └── main.py ├── 2614. Prime In Diagonal (Leetcode) └── main.py ├── 263. Ugly Number └── main.py ├── 2651. Calculate Delayed Arrival Time (Leetcode) └── main.py ├── 2652. Sum Multiples (Leetcode) └── main.py ├── 2671. Frequency Tracker (Leetcode) └── main.py ├── 2678. Number of Senior Citizens (Leetcode) └── main.py ├── 2679. Sum in a Matrix (Leetcode) └── main.py ├── 268. Missing Number (Leetcode) └── main.py ├── 27. Remove Element (Leetcode) └── main.py ├── 270. Closest Binary Search Tree Value (Leetcode) └── main.py ├── 2706. Buy Two Chocolates (Leetcode) └── main.py ├── 2710. Remove Trailing Zeros From a String (Leetcode) └── main.py ├── 273. Integer to English Words (Leetcode) └── main.py ├── 2733. Neither Minimum nor Maximum (Leetcode) └── main.py ├── 2739. Total Distance Traveled (Leetcode) └── main.py ├── 274. H-Index (Leetcode) └── main.py ├── 275. H-Index II (Leetcode) └── main.py ├── 2769. Find the Maximum Achievable Number (Leetcode) └── main.py ├── 2778. Sum of Squares of Special Elements (Leetcode) └── main.py ├── 2788. Split Strings by Separator (Leetcode) └── main.py ├── 2798. Number of Employees Who Met the Target (Leetcode) └── main.py ├── 28. Find the Index of the First Occurrence in a String (Leetcode) └── main.py ├── 2807. Insert Greatest Common Divisors in Linked List (Leetcode) └── main.py ├── 281. Zigzag Iterator └── main.py ├── 2816. Double a Number Represented as a Linked List (Leetcode) └── main.py ├── 2824. Count Pairs Whose Sum is Less than Target (Leetcode) └── main.py ├── 2828. Check if a String Is an Acronym of Words (Leetcode) └── main.py ├── 283. Move Zeroes (Leetcode) └── main.py ├── 2864. Maximum Odd Binary Number └── main.py ├── 287. Find the Duplicate Number (Leetcode) └── main.py ├── 2873. Maximum Value of an Ordered Triplet I (Leetcode) └── main.py ├── 2877. Create a DataFrame from List (Leetcode) └── main.py ├── 2878. Get the Size of a DataFrame (Leetcode) └── main.py ├── 2879. Display the First Three Rows (Leetcode) └── main.py ├── 2880. Select Data (Leetcode) └── main.py ├── 2881. Create a New Column (Leetcode) └── main.py ├── 2882. Drop Duplicate Rows (Leetcode) └── main.py ├── 2883. Drop Missing Data (Leetcode) └── main.py ├── 2884. Modify Columns (Leetcode) └── main.py ├── 2885. Rename Columns (Leetcode) └── main.py ├── 2886. Change Data Type (Leetcode) └── main.py ├── 2887. Fill Missing Data (Leetcode) └── main.py ├── 2888. Reshape Data: Concatenate (Leetcode) └── main.py ├── 2891. Method Chaining (Leetcode) └── main.py ├── 2894. Divisible and Non-divisible Sums Difference (Leetcode) └── main.py ├── 2903. Find Indices With Index and Value Difference I (Leetcode) └── main.py ├── 2908. Minimum Sum of Mountain Triplets I (Leetcode) └── main.py ├── 2942. Find Words Containing Character (Leetcode) └── main.py ├── 295. Find Median from Data Stream └── main.py ├── 2951. Find the Peaks (Leetcode) └── main.py ├── 2965. Find Missing and Repeated Values (Leetcode) └── main.py ├── 2974. Minimum Number Game (Leetcode) └── main.py ├── 2980. Check if Bitwise OR Has Trailing Zeros (Leetcode) └── main.py ├── 2985. Calculate Compressed Mean (Leetcode) └── main.py ├── 2989. Class Performance (Leetcode) └── main.py ├── 3019. Number of Changing Keys (Leetcode) └── main.py ├── 3024. Type of Triangle (Leetcode) └── main.py ├── 3028. Ant on the Boundary (Leetcode) └── main.py ├── 303. Range Sum Query - Immutable └── main.py ├── 3033. Modify the Matrix (Leetcode) └── main.py ├── 3046. Split the Array (Leetcode) └── main.py ├── 3051. Find Candidates for Data Scientist Position └── main.py ├── 3053. Classifying Triangles by Lengths (Leetcode) └── main.py ├── 3062. Winner of the Linked List Game └── main.py ├── 3063. Linked List Frequency └── main.py ├── 3065. Minimum Operations to Exceed Threshold Value I (Leetcode) └── main.py ├── 3079. Find the Sum of Encrypted Integers (Leetcode) └── main.py ├── 3099. Harshad Number (Leetcode) └── main.py ├── 3103. Find Trending Hashtags II (Leetcode) └── main.py ├── 3110. Score of a String (Leetcode) └── main.py ├── 3115. Maximum Prime Difference └── main.py ├── 3136. Valid Word (Leetcode) └── main.py ├── 3150. Invalid Tweets II (Leetcode) └── main.py ├── 3151. Special Array I (Leetcode) ├── main.py └── main1.py ├── 3158. Find the XOR of Numbers Which Appear Twice (Leetcode) └── main.py ├── 3162. Find the Number of Good Pairs I (Leetcode) └── main.py ├── 3168. Minimum Number of Chairs in a Waiting Room (Leetcode) └── main.py ├── 3178. Find the Child Who Has the Ball After K Seconds (Leetcode) └── main.py ├── 3190. Find Minimum Operations to Make All Elements Divisible by Three (Leetcode) └── main.py ├── 3194. Minimum Average of Smallest and Largest Elements (Leetcode) └── main.py ├── 3210. Find the Encrypted String (Leetcode) └── main.py ├── 3232. Find if Digit Game Can Be Won (Leetcode) └── main.py ├── 3248. Snake in Matrix └── main.py ├── 326. Power of Three (Leetcode) └── main.py ├── 3263. Convert Doubly Linked List to Array I └── main.py ├── 3274. Check if Two Chessboard Squares Have the Same Color └── main.py ├── 328. Odd Even Linked List (Leetcode) └── main.py ├── 3280. Convert Date to Binary └── main.py ├── 3289. The Two Sneaky Numbers of Digitville └── main.py ├── 3294. Convert Doubly Linked List to Array II └── main.py ├── 3338. Second Highest Salary II (Leetcode) └── main.py ├── 3340. Check Balanced String (Leetcode) └── main.py ├── 3345. Smallest Divisible Digit Product I (Leetcode) └── main.py ├── 3368. First Letter Capitalization (Leetcode) └── main.py ├── 338. Counting Bits (Leetcode) └── main.py ├── 342. Power of Four (Leetcode) └── main.py ├── 344. Reverse String └── main.py ├── 346. Moving Average from Data Stream └── main.py ├── 347. Top K Frequent Elements └── main.py ├── 349. Intersection of Two Arrays (Leetcode) └── main.py ├── 350. Intersection of Two Arrays II (Leetcode) └── main.py ├── 3512. Minimum Operations to Make Array Sum Divisible by K (Leetcode) └── main.py ├── 3516. Find Closest Person (Leetcode) └── main.py ├── 36. Valid Sudoku (Leetcode) └── main.py ├── 367. Valid Perfect Square └── main.py ├── 369. Plus One Linked List └── main.py ├── 374. Guess Number Higher or Lower (Leetcode) └── main.py ├── 379. Design Phone Directory └── main.py ├── 384. Shuffle an Array └── main.py ├── 387. First Unique Character in a String └── main.py ├── 398. Random Pick Index (Leetcode) └── main.py ├── 412. Fizz Buzz (Leetcode) ├── main.cpp └── main.py ├── 414. Third Maximum Number (Leetcode) └── main.py ├── 415.Add Strings (Leetcode) └── main.py ├── 434. Number of Segments in a String └── main.py ├── 445. Add Two Numbers II (Leetcode) └── main.py ├── 448. Find All Numbers Disappeared in an Array (Leetcode) └── main.py ├── 451. Sort Characters By Frequency └── main.py ├── 453. Minimum Moves to Equal Array Elements (Leetcode) └── main.py ├── 459. Repeated Substring Pattern (Leetcode) └── main.py ├── 462. Minimum Moves to Equal Array Elements II (Leetcode) └── main.py ├── 476. Number Complement (Leetcode) └── main.py ├── 48. Rotate Image (Leetcode) └── main.py ├── 485. Max Consecutive Ones (Leetcode) └── main.py ├── 49. Group Anagrams (Leetcode) └── main.py ├── 50. Pow(x, n) (Leetcode) └── main.py ├── 501. Find Mode in Binary Search Tree (Leetcode) └── main.py ├── 504. Base 7 (Leetcode) └── main.py ├── 507. Perfect Number (Leetcode) └── main.py ├── 509. Fibonacci Number (Leetcode) └── main.py ├── 511. Game Play Analysis I └── main.py ├── 520. Detect Capital └── main.py ├── 530. Minimum Absolute Difference in BST (Leetcode) └── main.py ├── 54. Spiral Matrix (Leetcode) └── main.py ├── 557. Reverse Words in a String III (Leetcode) └── main.py ├── 562. Longest Line of Consecutive One in Matrix (Leetcode) └── main.py ├── 58. Length of Last Word (Leetcode) └── main.py ├── 584. Find Customer Referee (Leetcode) └── main.py ├── 586. Customer Placing the Largest Number of Orders └── main.py ├── 592. Fraction Addition and Subtraction (Leetcode) └── main.py ├── 593. Valid Square └── main.py ├── 595. Big Countries (Leetcode) └── main.py ├── 596. Classes More Than 5 Students (Leetcode) └── main.py ├── 599. Minimum Index Sum of Two Lists (Leetcode) └── main.py ├── 6. Zigzag Conversion (Leetcode) └── main.py ├── 60. Permutation Sequence (Leetcode) └── main.py ├── 607. Sales Person (Leetcode) └── main.py ├── 61. Rotate List (Leetcode) └── main.py ├── 622. Design Circular Queue (Leetcode) └── main.py ├── 627. Swap Salary (Leetcode) └── main.py ├── 640. Solve the Equation (Leetcode) └── main.py ├── 66. Plus One (Leetcode) └── main.py ├── 67. Add Binary (Leetcode) └── main.py ├── 682. Baseball Game (Leetcode) └── main.py ├── 7. Reverse Integer (Leetcode) └── main.py ├── 70. Climbing Stairs (Leetcode) └── main.py ├── 705. Design HashSet └── main.py ├── 706. Design HashMap └── main.py ├── 709. To Lower Case (Leetcode) └── main.py ├── 724. Find Pivot Index └── main.py ├── 728. Self Dividing Numbers (Leetcode) └── main.py ├── 73. Set Matrix Zeroes (Leetcode) └── main.py ├── 74. Search a 2D Matrix (Leetcode) └── main.py ├── 747. Largest Number At Least Twice of Others (Leetcode) └── main.py ├── 75. Sort Colors └── main.py ├── 771. Jewels and Stones (Leetcode) └── main.py ├── 783. Minimum Distance Between BST Nodes (Leetcode) └── main.py ├── 82. Remove Duplicates from Sorted List II (Leetcode) └── main.py ├── 83. Remove Duplicates from Sorted List (Leetcode) └── main.py ├── 832. Flipping an Image (Leetcode) └── main.py ├── 844. Backspace String Compare └── main.py ├── 876. Middle of the Linked List (Leetcode) └── main.py ├── 884. Uncommon Words from Two Sentences (Leetcode) └── main.py ├── 896. Monotonic Array (Leetcode) └── main.py ├── 9. Palindrome Number (Leetcode) └── main.py ├── 905. Sort Array By Parity (Leetcode) └── main.py ├── 92. Reverse Linked List II (Leetcode) └── main.py ├── 922. Sort Array By Parity II (Leetcode) └── main.py ├── 933. Number of Recent Calls └── main.py ├── 938. Range Sum of BST (Leetcode) └── main.py ├── 94. Binary Tree Inorder Traversal (Leetcode) └── main.py ├── 977. Squares of a Sorted Array (Leetcode) └── main.py └── 989. Add to Array-Form of Integer (Leetcode) └── main.py /1. Two Sum (Leetcode)/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector nums, int target) { 4 | int length = nums.size(); 5 | vector result; 6 | for (int i=0;i List[int]: 3 | results = [] 4 | for i in range(len(List)): 5 | for j in range(i,len(List)): 6 | if List[i] + List[j] == target: 7 | return [i,j] 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /1. Two Sum (Leetcode)/main2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, nums: List[int], target: int) -> List[int]: 3 | # This code finds the complement 4 | complements = {} 5 | 6 | for i, num in enumerate(nums): 7 | complement = target-num 8 | if complement in complements: 9 | return [complements[complement],i] 10 | else: 11 | complements[num] = i 12 | -------------------------------------------------------------------------------- /1002. Find Common Characters (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def commonChars(self, words: List[str]) -> List[str]: 3 | letters = [] 4 | word = words[0] 5 | chars = list(set(word)) 6 | result = [] 7 | for char in chars: 8 | possible = [] 9 | for word in words: 10 | if char not in word: 11 | break 12 | else: 13 | possible.append(word.count(char)) 14 | if len(possible) == len(words): 15 | for i in range(min(possible)): 16 | result.append(char) 17 | 18 | return result 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /1009. Complement of Base 10 Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bitwiseComplement(self, n: int) -> int: 3 | if n == 0: 4 | return 1 5 | bits = [] 6 | while n > 0: 7 | remainder = n % 2 8 | n = n // 2 9 | bits.append(remainder) 10 | total = 0 11 | for i in range(len(bits)): 12 | if bits[i] == 0: 13 | total += pow(2,i) 14 | return total 15 | -------------------------------------------------------------------------------- /1017. Convert to Base -2 (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def baseNeg2(self, n): 3 | if n == 0: 4 | return "0" 5 | bits = [] 6 | while n != 0: 7 | remainder = n % -2 8 | n = n // -2 9 | 10 | if remainder < 0: 11 | remainder += 2 12 | n += 1 13 | bits.append(str(remainder)) 14 | return "".join(bits[::-1]) 15 | 16 | 17 | -------------------------------------------------------------------------------- /1030. Matrix Cells in Distance Order (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def allCellsDistOrder(self, rows: int, cols: int, rCenter: int, cCenter: int) -> List[List[int]]: 3 | output = [] 4 | for i in range(rows): 5 | for j in range(cols): 6 | new_list = [i,j] 7 | output.append(new_list) 8 | return sorted(output,key=lambda x:(abs(x[0]-rCenter)+abs(x[1]-cCenter))) -------------------------------------------------------------------------------- /104. Maximum Depth of Binary Tree (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def maxDepth(self, root: Optional[TreeNode]) -> int: 9 | self.counts = [] 10 | self.traverse(root, level=1) 11 | 12 | return max(self.counts) 13 | 14 | def traverse(self, root, level=1): 15 | if not root: 16 | self.counts.append(level - 1) 17 | return 18 | 19 | print(level) 20 | self.traverse(root.left, level + 1) 21 | self.traverse(root.right, level + 1) 22 | 23 | -------------------------------------------------------------------------------- /1050. Actors and Directors Who Cooperated At Least Three Times (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def actors_and_directors(actor_director: pd.DataFrame) -> pd.DataFrame: 5 | count = {} 6 | for (index, row) in actor_director.iterrows(): 7 | pair = (row["actor_id"], row["director_id"]) 8 | if pair not in count: 9 | count[pair] = 1 10 | else: 11 | count[pair] += 1 12 | 13 | id1 = [] 14 | id2 = [] 15 | for (key, value) in count.items(): 16 | if value >= 3: 17 | id1.append(key[0]) 18 | id2.append(key[1]) 19 | return pd.DataFrame({"actor_id": id1, "director_id": id2}) -------------------------------------------------------------------------------- /1051. Height Checker (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def heightChecker(self, heights): 3 | count = 0 4 | all_expected = sorted(heights) 5 | for i in range(len(heights)): 6 | height = heights[i] 7 | expected = all_expected[i] 8 | if height != expected: 9 | count += 1 10 | return count 11 | -------------------------------------------------------------------------------- /1099. Two Sum Less Than K (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSumLessThanK(self, nums: List[int], k: int) -> int: 3 | results = [-1] 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | if nums[i]+ nums[j] < k: 7 | results.append(nums[i]+nums[j]) 8 | return max(results) 9 | -------------------------------------------------------------------------------- /1108. Defanging an IP Address (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def defangIPaddr(self, address: str) -> str: 3 | new_string = "" 4 | for char in address: 5 | if char == ".": 6 | new_string += "[.]" 7 | else: 8 | new_string += char 9 | return new_string 10 | -------------------------------------------------------------------------------- /1109. Corporate Flight Bookings/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]: 3 | #Prefix sum 4 | result = [0] * n 5 | 6 | for i, j, k in bookings: 7 | result[i - 1] += k 8 | if j == n: 9 | continue 10 | else: 11 | result[j] -= k 12 | 13 | for i in range(1, n): 14 | result[i] = result[i - 1] + result[i] 15 | 16 | return result 17 | -------------------------------------------------------------------------------- /1119. Remove Vowels from a String/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeVowels(self, s: str) -> str: 3 | return s.replace("a", "").replace("e", "").replace("i", "").replace("o", "").replace("u", "") 4 | -------------------------------------------------------------------------------- /1137. N-th Tribonacci Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def tribonacci(self, n: int) -> int: 3 | if n == 0: 4 | return 0 5 | a = 0 6 | b = 1 7 | c = 1 8 | for i in range(n-2): 9 | d = a+b+c 10 | a = b 11 | b = c 12 | c = d 13 | return c -------------------------------------------------------------------------------- /1148. Article Views I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def article_views(views: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | for (index,row) in views.iterrows(): 6 | if row["author_id"] == row["viewer_id"]: 7 | ids.append(row["author_id"]) 8 | ids = sorted(list(set(ids))) 9 | return pd.DataFrame({"id":ids}) -------------------------------------------------------------------------------- /118. Pascal's Triangle (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def generate(self, numRows): 3 | numbers=[] 4 | 5 | 6 | for i in range(numRows): 7 | items = [] 8 | for j in range(i+1): 9 | top = self.factorial(i) 10 | bottom = self.factorial(j) * self.factorial(i-j) 11 | items.append(int(top/bottom)) 12 | numbers.append(items) 13 | return numbers 14 | 15 | 16 | def factorial(self,n): 17 | if n == 0 or n == 1: 18 | return 1 19 | else: 20 | return n * self.factorial(n-1) 21 | 22 | 23 | -------------------------------------------------------------------------------- /1185. Day of the Week (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def dayOfTheWeek(self, day, month, year): 3 | months = [0,3,2,5,0,3,5,1,4,6,2,4] 4 | if month < 3: 5 | year -= 1 6 | remainder = (year + year//4 -year//100 + year//400 + months[month-1]+day)%7 7 | days = {0:"Sunday",1:"Monday",2:"Tuesday",3:"Wednesday",4:"Thursday",5:"Friday",6:"Saturday"} 8 | return days[remainder] 9 | 10 | 11 | -------------------------------------------------------------------------------- /1189. Maximum Number of Balloons (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxNumberOfBalloons(self, text): 3 | dictionary = {} 4 | for char in text: 5 | if char in "balloon": 6 | if char not in dictionary: 7 | dictionary[char] = 1 8 | else: 9 | dictionary[char] += 1 10 | 11 | for char in 'balloon': 12 | if char not in dictionary: 13 | return 0 14 | 15 | char = 'o' 16 | dictionary[char] = dictionary[char] // 2 17 | 18 | char = 'l' 19 | dictionary[char] = dictionary[char] // 2 20 | 21 | return min(list(dictionary.values())) -------------------------------------------------------------------------------- /119. Pascal's Triangle II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def getRow(self, rowIndex): 3 | numbers =[] 4 | count = 0 5 | for i in range(rowIndex+1): 6 | top = (self.factorial(rowIndex)) 7 | bottom = (self.factorial(i)) * (self.factorial(rowIndex-i)) 8 | numbers.append(int(top/bottom)) 9 | return numbers 10 | 11 | 12 | 13 | def factorial(self,n): 14 | if n == 0 or n==1: 15 | return 1 16 | else: 17 | return n * self.factorial(n-1) 18 | 19 | 20 | s = Solution() 21 | s.getRow(3) 22 | -------------------------------------------------------------------------------- /1206. Design Skiplist/main.py: -------------------------------------------------------------------------------- 1 | class Skiplist: 2 | 3 | def __init__(self): 4 | self.list = [] 5 | 6 | def search(self, target: int) -> bool: 7 | if target in self.list: 8 | return True 9 | return False 10 | 11 | def add(self, num: int) -> None: 12 | self.list.append(num) 13 | 14 | def erase(self, num: int) -> bool: 15 | if self.search(num): 16 | self.list.remove(num) 17 | return True 18 | return False 19 | 20 | # Your Skiplist object will be instantiated and called as such: 21 | # obj = Skiplist() 22 | # param_1 = obj.search(target) 23 | # obj.add(num) 24 | # param_3 = obj.erase(num) 25 | -------------------------------------------------------------------------------- /1207. Unique Number of Occurrences/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueOccurrences(self, arr: List[int]) -> bool: 3 | dictionary = {} 4 | for n in arr: 5 | if n not in dictionary: 6 | dictionary[n] = 1 7 | elif n in dictionary: 8 | dictionary[n] += 1 9 | 10 | values = [dictionary[n] for n in dictionary] 11 | 12 | set_form = set(values) 13 | 14 | if len(values) == len(set_form): 15 | return True 16 | else: 17 | return False 18 | 19 | -------------------------------------------------------------------------------- /1232. Check If It Is a Straight Line (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkStraightLine(self, coordinates: list[list[int]]): 3 | gradients = [] 4 | for i in range(0,len(coordinates)-1): 5 | y2 = coordinates[i+1][1] 6 | y1 = coordinates[i][1] 7 | x2 = coordinates[i+1][0] 8 | x1 = coordinates[i][0] 9 | try: 10 | gradient = (y2-y1)/(x2-x1) 11 | except ZeroDivisionError: 12 | if x2 == 0 and x1 ==0: 13 | gradient= "undefined" 14 | else: 15 | gradient = 0 16 | gradients.append(gradient) 17 | if i >= 1 and gradients.count(gradient) == 1: 18 | return False 19 | return True -------------------------------------------------------------------------------- /1244. Design A Leaderboard (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Leaderboard: 2 | 3 | def __init__(self): 4 | self.dictionary = {} 5 | 6 | def addScore(self, playerId: int, score: int) -> None: 7 | if playerId not in self.dictionary: 8 | self.dictionary[playerId] = score 9 | else: 10 | self.dictionary[playerId] += score 11 | 12 | def top(self, K: int) -> int: 13 | results = self.dictionary.values() 14 | 15 | t = sorted(results)[len(results) - K:] 16 | 17 | return sum(t) 18 | 19 | def reset(self, playerId: int) -> None: 20 | self.dictionary[playerId] = 0 21 | 22 | # Your Leaderboard object will be instantiated and called as such: 23 | # obj = Leaderboard() 24 | # obj.addScore(playerId,score) 25 | # param_2 = obj.top(K) 26 | # obj.reset(playerId) -------------------------------------------------------------------------------- /125. Valid Palindrome (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s: str) -> bool: 3 | string_list = [char.lower() for char in s if char.isalnum()] 4 | 5 | print(string_list) 6 | new_list = string_list[::-1] 7 | print(new_list) 8 | if new_list == string_list: 9 | return True 10 | else: 11 | return False -------------------------------------------------------------------------------- /1281. Subtract the Product and Sum of Digits of an Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def subtractProductAndSum(self, n: int) -> int: 3 | total = 0 4 | product = 1 5 | for i in range(len(str(n))): 6 | char = int(str(n)[i]) 7 | total += char 8 | product *= char 9 | return product-total -------------------------------------------------------------------------------- /1290. Convert Binary Number in a Linked List to Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def getDecimalValue(self, head: ListNode) -> int: 8 | node = head 9 | 10 | values = [] 11 | while node is not None: 12 | values.append(node.val) 13 | node = node.next 14 | 15 | values = values[::-1] 16 | total = sum([pow(2, i) for i in range(len(values)) if values[i] == 1]) 17 | return total 18 | -------------------------------------------------------------------------------- /1295. Find Numbers with Even Number of Digits (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findNumbers(self, nums): 3 | return len([num for num in nums if len(list(str(num)))%2 ==0]) 4 | -------------------------------------------------------------------------------- /1303. Find the Team Size (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def team_size(employee: pd.DataFrame) -> pd.DataFrame: 5 | dictionary1 = {} 6 | dictionary2 = {} 7 | 8 | for index, row in employee.iterrows(): 9 | team = row["team_id"] 10 | dictionary1[row["employee_id"]] = team 11 | 12 | if team not in dictionary2: 13 | dictionary2[team] = 1 14 | else: 15 | dictionary2[team] += 1 16 | ids = [] 17 | teams = [] 18 | for index, row in employee.iterrows(): 19 | ids.append(row["employee_id"]) 20 | team = dictionary1[row["employee_id"]] 21 | teams.append(dictionary2[team]) 22 | 23 | return pd.DataFrame({"employee_id": ids, "team_size": teams}) 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /1304. Find N Unique Integers Sum up to Zero (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumZero(self, n: int) -> list[int]: 3 | if n == 1: 4 | return [0] 5 | else: 6 | zero = [] 7 | if n % 2 ==0: 8 | for i in range(1,(n//2)+1): 9 | zero.append(i) 10 | zero.append(-i) 11 | else: 12 | zero.append(0) 13 | for i in range(1,(n//2)+1): 14 | zero.append(i) 15 | zero.append(-i) 16 | return zero 17 | 18 | -------------------------------------------------------------------------------- /1317. Convert Integer to the Sum of Two No-Zero Integers (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getNoZeroIntegers(self, n: int) -> List[int]: 3 | if n == 2: 4 | return [1,1] 5 | else: 6 | for i in range(1,n): 7 | n2 = n-i 8 | if "0" not in str(i) and "0" not in str(n2): 9 | return [i,n2] -------------------------------------------------------------------------------- /1342. Number of Steps to Reduce a Number to Zero (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfSteps(self, num: int) -> int: 3 | steps = 0 4 | while num >0: 5 | if num % 2 == 0: 6 | num = num//2 7 | steps +=1 8 | else: 9 | num -= 1 10 | steps +=1 11 | return steps -------------------------------------------------------------------------------- /1344. Angle Between Hands of a Clock (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def angleClock(self, hour: int, minutes: int) -> float: 3 | minutes_angle = minutes * 6 4 | hour_angle = (hour+(minutes/60))*30 5 | 6 | if hour_angle >= 360: 7 | hour_angle -= 360 8 | 9 | angle1 = abs(minutes_angle-hour_angle) 10 | angle2 = 360-option1 11 | 12 | #Given two numbers, hour and minutes, return the smaller angle (in degrees) formed between the hour and the minute hand. 13 | #The leetcode question is asking for the smaller angle 14 | return min(angle1,angle2) 15 | -------------------------------------------------------------------------------- /1346. Check If N and Its Double Exist (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfExist(self, arr: List[int]) -> bool: 3 | for i in range(0, len(arr)): 4 | for j in range(0, len(arr)): 5 | if i != j: 6 | if arr[i] == 2 * arr[j]: 7 | return True 8 | return False 9 | -------------------------------------------------------------------------------- /1350. Students With Invalid Departments (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # This is a sample Python script. 2 | 3 | # Press ⌃R to execute it or replace it with your code. 4 | # Press Double ⇧ to search everywhere for classes, files, tool windows, actions, and settings. 5 | 6 | 7 | def print_hi(name): 8 | # Use a breakpoint in the code line below to debug your script. 9 | print(f'Hi, {name}') # Press ⌘F8 to toggle the breakpoint. 10 | 11 | 12 | # Press the green button in the gutter to run the script. 13 | if __name__ == '__main__': 14 | print_hi('PyCharm') 15 | 16 | # See PyCharm help at https://www.jetbrains.com/help/pycharm/ 17 | -------------------------------------------------------------------------------- /1351. Count Negative Numbers in a Sorted Matrix (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countNegatives(self, grid): 3 | count = 0 4 | for i in range(len(grid)): 5 | for j in range(len(grid[i])): 6 | if grid[i][j] < 0: 7 | count += 1 8 | return count 9 | -------------------------------------------------------------------------------- /136. Single Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | dictionary = {} 4 | for num in nums: 5 | if num not in dictionary: 6 | dictionary[num] = 1 7 | else: 8 | dictionary.pop(num) 9 | for key in dictionary: 10 | return key 11 | -------------------------------------------------------------------------------- /1360. Number of Days Between Two Dates/main.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | 3 | 4 | class Solution: 5 | def daysBetweenDates(self, date1: str, date2: str) -> int: 6 | if date1 > date2: 7 | c = date1 8 | date1 = date2 9 | date2 = c 10 | 11 | date1 = date1.split("-") 12 | year1 = int(date1[0]) 13 | month1 = int(date1[1]) 14 | day1 = int(date1[2]) 15 | d1 = datetime.datetime(day=day1, month=month1, year=year1) 16 | 17 | date2 = date2.split("-") 18 | year2 = int(date2[0]) 19 | month2 = int(date2[1]) 20 | day2 = int(date2[2]) 21 | d2 = datetime.datetime(day=day2, month=month2, year=year2) 22 | 23 | days = 0 24 | while d1 < d2: 25 | one_day = datetime.timedelta(days=1) 26 | d1 += one_day 27 | days += 1 28 | 29 | return days 30 | 31 | -------------------------------------------------------------------------------- /1365. How Many Numbers Are Smaller Than the Current Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallerNumbersThanCurrent(self, nums): 3 | numbers = [] 4 | for num in nums: 5 | count = 0 6 | for n in nums: 7 | if num > n: 8 | count +=1 9 | numbers.append(count) 10 | return numbers -------------------------------------------------------------------------------- /137. Single Number II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def singleNumber(self, nums): 3 | dictionary = {} 4 | for num in nums: 5 | if num not in dictionary: 6 | dictionary[num] = 1 7 | else: 8 | dictionary[num] +=1 9 | for key in dictionary: 10 | if dictionary[key] ==1: 11 | return key 12 | -------------------------------------------------------------------------------- /1374. Generate a String With Characters That Have Odd Counts (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def generateTheString(self, n: int) -> str: 3 | # this checks if it is a power of 2 4 | if n == 2: 5 | return "xy" 6 | elif n > 0 and (n & (n - 1)) == 0: 7 | s1 = ((n//2)+1) * "a" 8 | s2 = ((n//2)-1) * "h" 9 | return s1 +s2 10 | elif n % 2 == 0 and n//2 % 2== 0: 11 | s1 = ((n//2)+1) * "a" 12 | s2 = ((n//2)-1) * "h" 13 | return s1 +s2 14 | elif n % 2 == 0: 15 | s1 = (n//2) * "a" 16 | s2 = (n//2) * "h" 17 | return s1+s2 18 | else: 19 | return n* "h" 20 | 21 | -------------------------------------------------------------------------------- /1378. Replace Employee ID With The Unique Identifier (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def replace_employee_id(employees: pd.DataFrame, employee_uni: pd.DataFrame) -> pd.DataFrame: 5 | dictionary = {} 6 | 7 | for index, row in employee_uni.iterrows(): 8 | dictionary[row['id']] = row['unique_id'] 9 | 10 | unique_ids = [] 11 | names = [] 12 | for index, row in employees.iterrows(): 13 | i = row['id'] 14 | if i not in dictionary: 15 | unique_ids.append(None) 16 | else: 17 | unique_ids.append(dictionary[i]) 18 | names.append(row['name']) 19 | 20 | return pd.DataFrame({"unique_id": unique_ids, 'name': names}) -------------------------------------------------------------------------------- /1381. Design a Stack With Increment Operation/main.py: -------------------------------------------------------------------------------- 1 | class CustomStack: 2 | 3 | def __init__(self, maxSize: int): 4 | self.list = [] 5 | self.size = maxSize 6 | 7 | def push(self, x: int) -> None: 8 | if len(self.list) < self.size: 9 | self.list.append(x) 10 | 11 | def pop(self) -> int: 12 | if len(self.list) == 0: 13 | return -1 14 | else: 15 | return self.list.pop() 16 | 17 | def increment(self, k: int, val: int) -> None: 18 | length = len(self.list) 19 | if length < k: 20 | for i in range(len(self.list)): 21 | self.list[i] += val 22 | else: 23 | for i in range(k): 24 | self.list[i] += val 25 | 26 | # Your CustomStack object will be instantiated and called as such: 27 | # obj = CustomStack(maxSize) 28 | # obj.push(x) -------------------------------------------------------------------------------- /1390. Four Divisors (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def categorizeBox(self, length: int, width: int, height: int, mass: int) -> str: 3 | categories = [] 4 | if length >= 10 ** 4 or width >= 10 ** 4 or height >= 10 ** 4 or (length * width * height) >= 10 ** 9: 5 | categories.append("Bulky") 6 | if mass >= 100: 7 | categories.append("Heavy") 8 | 9 | if len(categories) == 0: 10 | return "Neither" 11 | elif len(categories) == 1: 12 | if categories[0] == "Bulky": 13 | return "Bulky" 14 | else: 15 | return "Heavy" 16 | else: 17 | return "Both" 18 | -------------------------------------------------------------------------------- /1399. Count Largest Group (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countLargestGroup(self, n): 3 | dictionary = {} 4 | 5 | for i in range(1,n+1): 6 | total = sum([int(item) for item in list(str(i))]) 7 | if total not in dictionary: 8 | dictionary[total] = [i] 9 | else: 10 | dictionary[total].append(i) 11 | greatest = max([len(dictionary[key]) for key in dictionary]) 12 | count = 0 13 | for key in dictionary: 14 | if len(dictionary[key]) == greatest: 15 | count +=1 16 | return count 17 | 18 | -------------------------------------------------------------------------------- /141. Linked List Cycle (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def hasCycle(self, head: Optional[ListNode]) -> bool: 9 | if head is None: 10 | return 11 | values = [] 12 | node = head 13 | values.append(node.val) 14 | node = node.next 15 | while node is not None: 16 | value = node.val 17 | if value not in values: 18 | values.append(value) 19 | else: 20 | return True 21 | 22 | return False 23 | -------------------------------------------------------------------------------- /142. Linked List Cycle II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: 9 | nodes = [] 10 | 11 | node = head 12 | while node is not None: 13 | if node not in nodes: 14 | nodes.append(node) 15 | else: 16 | return node 17 | node = node.next 18 | 19 | return None 20 | -------------------------------------------------------------------------------- /1431. Kids With the Greatest Number of Candies/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: 3 | output = [] 4 | greatest = max(candies) 5 | for candy in candies: 6 | if (candy + extraCandies) >= greatest: 7 | output.append(True) 8 | else: 9 | output.append(False) 10 | 11 | return output 12 | 13 | 14 | -------------------------------------------------------------------------------- /1436. Destination City/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def destCity(self, paths: List[List[str]]) -> str: 3 | found = False 4 | city = paths[0][1] 5 | count = len(paths) 6 | while count <= len(paths): 7 | previous_city = city 8 | for path in paths: 9 | if path[0] == city: 10 | city = path[1] 11 | if previous_city == city: 12 | return city 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /144. Binary Tree Preorder Traversal (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def preorderTraversal(self, root): 9 | self.result = [] 10 | self.preorderTraversal1(root) 11 | 12 | return self.result 13 | 14 | def preorderTraversal1(self, root): 15 | if not root: 16 | return None 17 | 18 | self.result.append(root.val) 19 | self.preorderTraversal1(root.left) 20 | self.preorderTraversal1(root.right) 21 | -------------------------------------------------------------------------------- /1445. Apples & Oranges (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def apples_oranges(sales: pd.DataFrame) -> pd.DataFrame: 4 | dictionary = {} 5 | 6 | for index,row in sales.iterrows(): 7 | date = row["sale_date"] 8 | if date not in dictionary: 9 | dictionary[date] = row["sold_num"] 10 | else: 11 | dictionary[date] -= row["sold_num"] 12 | 13 | sales = [] 14 | dif = [] 15 | for key in dictionary: 16 | sales.append(key) 17 | dif.append(dictionary[key]) 18 | 19 | return pd.DataFrame({"sale_date":sales,"diff":dif}) -------------------------------------------------------------------------------- /1446. Consecutive Characters (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxPower(self, s): 3 | record = [1] 4 | count = 1 5 | char = s[0] 6 | for i in range(1,len(s)): 7 | new_char = s[i] 8 | if new_char == char: 9 | count +=1 10 | record.append(count) 11 | else: 12 | count = 1 13 | char = new_char 14 | return max(record) -------------------------------------------------------------------------------- /145. Binary Tree Postorder Traversal (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def postorderTraversal(self, root): 9 | self.result = [] 10 | self.postorderTraversal1(root) 11 | 12 | return self.result 13 | 14 | def postorderTraversal1(self, root): 15 | if not root: 16 | return None 17 | 18 | self.postorderTraversal1(root.left) 19 | self.postorderTraversal1(root.right) 20 | self.result.append(root.val) 21 | -------------------------------------------------------------------------------- /1451. Rearrange Words in a Sentence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def arrangeWords(self, text): 3 | lengths = {} 4 | sizes = [] 5 | words = text.split() 6 | for word in words: 7 | length = len(word) 8 | if length in lengths: 9 | lengths[length].append(word) 10 | else: 11 | lengths[length] = [word] 12 | sizes.append(length) 13 | output = [] 14 | sizes.sort() 15 | for length in sizes: 16 | length_words = lengths[length] 17 | for word in length_words: 18 | output.append(word.lower()) 19 | string = " ".join(output) 20 | first_char = string[0].upper() 21 | output = first_char + string[1:] 22 | return output 23 | 24 | -------------------------------------------------------------------------------- /1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPrefixOfWord(self, sentence, searchWord): 3 | words = sentence.split() 4 | for i in range(len(words)): 5 | if searchWord == words[i][0:len(searchWord)]: 6 | return i+1 7 | return -1 8 | -------------------------------------------------------------------------------- /1464. Maximum Product of Two Elements in an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxProduct(self, nums): 3 | n1 = max(nums) 4 | nums.remove(n1) 5 | n2 = max(nums) 6 | return (n1-1)*(n2-1) 7 | -------------------------------------------------------------------------------- /147. Insertion Sort List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | values = [] 9 | 10 | node = head 11 | while node is not None: 12 | values.append(node.val) 13 | node = node.next 14 | 15 | values = sorted(values) 16 | 17 | new_head = ListNode(0) 18 | node = new_head 19 | for value in values: 20 | new_node = ListNode(value) 21 | node.next = new_node 22 | node = node.next 23 | 24 | return new_head.next 25 | -------------------------------------------------------------------------------- /1470. Shuffle the Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def shuffle(self, nums, n): 3 | x = [] 4 | y = [] 5 | half = len(nums) / 2 6 | for i in range(len(nums)): 7 | if i > half - 1: 8 | y.append(nums[i]) 9 | else: 10 | x.append(nums[i]) 11 | 12 | output = [] 13 | for i in range(len(x)): 14 | output.append(x[i]) 15 | output.append(y[i]) 16 | return output -------------------------------------------------------------------------------- /148. Sort List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | values = [] 9 | node = head 10 | while node is not None: 11 | values.append(node.val) 12 | node = node.next 13 | 14 | values = sorted(values) 15 | node = head 16 | for value in values: 17 | node.val = value 18 | node = node.next 19 | 20 | return head -------------------------------------------------------------------------------- /1480. Running Sum of 1d Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def runningSum(self, nums: List[int]) -> List[int]: 3 | return [sum(nums[:i+1]) for i in range(len(nums))] 4 | -------------------------------------------------------------------------------- /1486. XOR Operation in an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def xorOperation(self, n, start): 3 | xor = start 4 | for i in range(1, n): 5 | amount = start + (2 * i) 6 | xor ^= amount 7 | return xor 8 | -------------------------------------------------------------------------------- /1491. Average Salary Excluding the Minimum and Maximum Salary (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def average(self, salary: List[int]) -> float: 3 | length = len(salary) - 2 4 | salary.remove(max(salary)) 5 | salary.remove(min(salary)) 6 | return sum(salary)/length -------------------------------------------------------------------------------- /1492. The kth Factor of n (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def kthFactor(self, n, k): 3 | factors = [] 4 | for i in range(1,n+1): 5 | if n % i == 0: 6 | factors.append(i) 7 | print(factors) 8 | if k > len(factors): 9 | return -1 10 | else: 11 | return factors[k-1] 12 | -------------------------------------------------------------------------------- /1502. Can Make Arithmetic Progression From Sequence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canMakeArithmeticProgression(self, arr): 3 | arr.sort() 4 | if len(arr) == 2: 5 | return True 6 | else: 7 | gradient = arr[1] - arr[0] 8 | for i in range(1,len(arr)-1): 9 | if (arr[i+1] - arr[i]) != gradient: 10 | return False 11 | return True 12 | -------------------------------------------------------------------------------- /1507. Reformat Date (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reformatDate(self, date: str) -> str: 3 | month_dictionary = {"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04", "May": "05", "Jun": "06", "Jul": "07", "Aug": "08", "Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12"} 4 | dates = date.split() 5 | year = dates[2] 6 | month = month_dictionary[dates[1]] 7 | day = int(dates[0].rstrip("th").rstrip("nd").rstrip("rd").rstrip("st")) 8 | if day < 10: 9 | day = f"0{day}" 10 | return f"{year}-{month}-{day}" 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /151. Reverse Words in a String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | words = s.strip().split()[::-1] 4 | return " ".join(words) 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /151. Reverse Words in a String (Leetcode)/main2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | return " ".join([word for word in s.split()][::-1]) 4 | 5 | -------------------------------------------------------------------------------- /1512. Number of Good Pairs (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numIdenticalPairs(self, nums: List[int]) -> int: 3 | count = 0 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | if nums[i] == nums[j]: 7 | count += 1 8 | return count -------------------------------------------------------------------------------- /1523. Count Odd Numbers in an Interval Range (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countOdds(self, low, high): 3 | if low % 2 ==0 and high % 2 ==0: 4 | return (high-low)/2 5 | elif low %2 == 1 and high %2 ==1: 6 | return (1+((high-low)/2)) 7 | else: 8 | return ((high-low+1)/2) 9 | -------------------------------------------------------------------------------- /1527. Patients With a Condition (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def find_patients(patients: pd.DataFrame) -> pd.DataFrame: 5 | ids = [] 6 | names = [] 7 | conditions = [] 8 | for (index, row) in patients.iterrows(): 9 | types = row["conditions"].split() 10 | if "DIAB1" in row["conditions"] and "SADIAB100" not in row["conditions"]: 11 | ids.append(row["patient_id"]) 12 | names.append(row["patient_name"]) 13 | conditions.append(row["conditions"]) 14 | 15 | return pd.DataFrame({"patient_id": ids, "patient_name": names, "conditions": conditions}) 16 | -------------------------------------------------------------------------------- /1528. Shuffle String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def restoreString(self, s, indices): 3 | string = "" 4 | for i in range(len(indices)): 5 | index = indices.index(i) 6 | string += s[index] 7 | return string 8 | -------------------------------------------------------------------------------- /1534. Count Good Triplets (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: 3 | count = 0 4 | for i in range(len(arr)): 5 | for j in range(i + 1, len(arr)): 6 | for k in range(i + 2, len(arr)): 7 | if k > j and j > i: 8 | if abs(arr[i] - arr[j]) <= a: 9 | if abs(arr[j] - arr[k]) <= b: 10 | if abs(arr[i] - arr[k]) <= c: 11 | count += 1 12 | return count 13 | -------------------------------------------------------------------------------- /1539. Kth Missing Positive Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthPositive(self, arr: List[int], k: int) -> int: 3 | missing = [] 4 | number = 1 5 | while len(missing) < k: 6 | if number not in arr: 7 | missing.append(number) 8 | number += 1 9 | return number-1 -------------------------------------------------------------------------------- /155. Min Stack/main.py: -------------------------------------------------------------------------------- 1 | class MinStack: 2 | 3 | def __init__(self): 4 | self.list = [] 5 | 6 | def push(self, val: int) -> None: 7 | self.list.append(val) 8 | 9 | def pop(self) -> None: 10 | return self.list.pop() 11 | 12 | def top(self) -> int: 13 | return self.list[-1] 14 | 15 | def getMin(self) -> int: 16 | return min(self.list) 17 | 18 | # Your MinStack object will be instantiated and called as such: 19 | # obj = MinStack() 20 | # obj.push(val) 21 | # obj.pop() 22 | # param_3 = obj.top() 23 | # param_4 = obj.getMin() 24 | -------------------------------------------------------------------------------- /1550. Three Consecutive Odds (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def threeConsecutiveOdds(self, arr): 3 | count = 0 4 | for item in arr: 5 | if item % 2 ==1: 6 | count += 1 7 | if count == 3: 8 | return True 9 | else: 10 | count = 0 11 | return False 12 | -------------------------------------------------------------------------------- /1570. Dot Product of Two Sparse Vectors/main.py: -------------------------------------------------------------------------------- 1 | class SparseVector: 2 | def __init__(self, nums: List[int]): 3 | self.nums = nums 4 | 5 | # Return the dotProduct of two sparse vectors 6 | def dotProduct(self, vec: 'SparseVector') -> int: 7 | total = 0 8 | for i in range(len(self.nums)): 9 | total += (self.nums[i]) * (vec.nums[i]) 10 | 11 | return total 12 | 13 | # Your SparseVector object will be instantiated and called as such: 14 | # v1 = SparseVector(nums1) 15 | # v2 = SparseVector(nums2) 16 | # ans = v1.dotProduct(v2) 17 | 18 | -------------------------------------------------------------------------------- /1571. Warehouse Manager (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def warehouse_manager(warehouse: pd.DataFrame, products: pd.DataFrame) -> pd.DataFrame: 5 | volumes = {} 6 | 7 | for index, row in products.iterrows(): 8 | name = row["product_id"] 9 | volume = row["Width"] * row["Length"] * row["Height"] 10 | volumes[name] = volume 11 | 12 | houses = {} 13 | for index, row in warehouse.iterrows(): 14 | amount = volumes[row["product_id"]] * row["units"] 15 | name = row["name"] 16 | if name not in houses: 17 | houses[name] = amount 18 | else: 19 | houses[name] += amount 20 | 21 | names = [] 22 | v = [] 23 | for house in houses: 24 | names.append(house) 25 | v.append(houses[house]) 26 | 27 | return pd.DataFrame({"warehouse_name": names, "volume": v}) 28 | 29 | -------------------------------------------------------------------------------- /1572. Matrix Diagonal Sum (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def diagonalSum(self, mat: List[List[int]]) -> int: 3 | total = 0 4 | values = set() 5 | for i in range(len(mat)): 6 | t1 = (i, 0 + i) 7 | values.add(t1) 8 | t2 = i, len(mat) - 1 - i 9 | values.add(t2) 10 | 11 | values = list(values) 12 | print(values) 13 | for value in values: 14 | total += mat[value[0]][value[1]] 15 | 16 | return total 17 | -------------------------------------------------------------------------------- /1582. Special Positions in a Binary Matrix (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numSpecial(self, mat): 3 | count = 0 4 | for i in range(len(mat)): 5 | for j in range(len(mat[i])): 6 | if mat[i][j] == 1: 7 | if mat[i].count(1) == 1: 8 | lc = 0 9 | for m in mat: 10 | if m[j] == 0: 11 | lc +=1 12 | if lc == len(mat)-1: 13 | count +=1 14 | return count 15 | 16 | -------------------------------------------------------------------------------- /1598. Crawler Log Folder/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, logs: List[str]) -> int: 3 | count = 0 4 | for log in logs: 5 | if log == "../": 6 | if count > 0: 7 | count -= 1 8 | elif log == "./": 9 | continue 10 | else: 11 | count += 1 12 | 13 | return count -------------------------------------------------------------------------------- /160. Intersection of Two Linked Lists (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]: 9 | a_values = [] 10 | b_values = [] 11 | 12 | node = headA 13 | while node is not None: 14 | a_values.append(node) 15 | node = node.next 16 | 17 | node = headB 18 | while node is not None: 19 | b_values.append(node) 20 | node = node.next 21 | 22 | for node in a_values: 23 | if node in b_values: 24 | return node 25 | 26 | return None 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /1603. Design Parking System/main.py: -------------------------------------------------------------------------------- 1 | class ParkingSystem: 2 | 3 | def __init__(self, big: int, medium: int, small: int): 4 | self.big = big 5 | self.medium = medium 6 | self.small = small 7 | 8 | def addCar(self, carType: int) -> bool: 9 | if carType == 1 and self.big > 0: 10 | self.big -= 1 11 | return True 12 | 13 | if carType == 2 and self.medium > 0: 14 | self.medium -= 1 15 | return True 16 | 17 | if carType == 3 and self.small > 0: 18 | self.small -= 1 19 | return True 20 | 21 | return False 22 | 23 | # Your ParkingSystem object will be instantiated and called as such: 24 | # obj = ParkingSystem(big, medium, small) 25 | # param_1 = obj.addCar(carType) -------------------------------------------------------------------------------- /1636. Sort Array by Increasing Frequency/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def frequencySort(self, nums: List[int]) -> List[int]: 3 | f = {} 4 | for num in nums: 5 | if num not in f: 6 | f[num] = 1 7 | else: 8 | f[num] += 1 9 | 10 | result = sorted(f, key=lambda x: (f[x], -x)) 11 | 12 | output = [] 13 | 14 | for number in result: 15 | amount = f[number] 16 | if amount > 1: 17 | for i in range(amount): 18 | output.append(number) 19 | else: 20 | output.append(number) 21 | 22 | return output 23 | -------------------------------------------------------------------------------- /164. Maximum Gap (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumGap(self, nums: List[int]) -> int: 3 | total = len(nums) 4 | if total == 1: 5 | return 0 6 | 7 | nums = sorted(nums) 8 | 9 | greatest = 0 10 | for i in range(total - 1): 11 | difference = abs(nums[i + 1] - nums[i]) 12 | if difference > greatest: 13 | greatest = difference 14 | 15 | return greatest 16 | -------------------------------------------------------------------------------- /1641. Count Sorted Vowel Strings (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countVowelStrings(self, n: int) -> int: 3 | if n == 1: 4 | return len(["a", "e", "i", "o", "u"]) 5 | 6 | values = ["a","e","i","o","u"] 7 | possible = ["a","e","i","o","u"] 8 | dictionary = {"a":1,"e":2,"i":3,'o':4,"u":5} 9 | 10 | for i in range(n-1): 11 | new_output = [] 12 | for value in values: 13 | for char in possible: 14 | if dictionary[char] >= dictionary[value]: 15 | new_output.append(f"{char}") 16 | 17 | values = new_output 18 | 19 | return len(values) 20 | 21 | -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool: 3 | return "".join(word1) == "".join(word2) -------------------------------------------------------------------------------- /1667. Fix Names in a Table (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def fix_names(users: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | for (index,row) in users.iterrows(): 7 | ids.append(row["user_id"]) 8 | name = row["name"] 9 | name = name[0].upper() + name[1:].lower() 10 | names.append(name) 11 | 12 | new_ids = sorted(ids) 13 | new_names = [names[ids.index(item)] for item in new_ids] 14 | return pd.DataFrame({"user_id":new_ids,"name":new_names}) -------------------------------------------------------------------------------- /167. Two Sum II - Input Array Is Sorted/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers: List[int], target: int) -> List[int]: 3 | new_numbers = [] 4 | for n in numbers: 5 | if n not in new_numbers: 6 | new_numbers.append(n) 7 | if new_numbers.count(n) == 1: 8 | new_numbers.append(n) 9 | 10 | for n in new_numbers: 11 | other_pair = target-n 12 | if other_pair in new_numbers: 13 | if other_pair == n: 14 | first_index = numbers.index(n) 15 | second_index = numbers.index(n,first_index+1) 16 | return [first_index+1,second_index+1] 17 | else: 18 | first_index = numbers.index(n) 19 | second_index = numbers.index(other_pair) 20 | return [first_index+1,second_index+1] 21 | -------------------------------------------------------------------------------- /1672. Richest Customer Wealth (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maximumWealth(self, accounts): 3 | greatest = 0 4 | for account in accounts: 5 | wealth = sum(account) 6 | if wealth > greatest: 7 | greatest = wealth 8 | return greatest -------------------------------------------------------------------------------- /1683. Invalid Tweets (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | for (index,row) in tweets.iterrows(): 6 | if len(row["content"]) >= 16: 7 | ids.append(row["tweet_id"]) 8 | return pd.DataFrame({"tweet_id":ids}) -------------------------------------------------------------------------------- /1684. Count the Number of Consistent Strings (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countConsistentStrings(self, allowed: str, words: List[str]) -> int: 3 | allowed = list(allowed) 4 | count = 0 5 | for word in words: 6 | if len(word) == len([1 for char in word if char in allowed]): 7 | count += 1 8 | return count -------------------------------------------------------------------------------- /1688. Count of Matches in Tournament (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfMatches(self, n: int) -> int: 3 | matches = 0 4 | while n != 1: 5 | if n % 2 == 0: 6 | matches += n//2 7 | n = n//2 8 | elif n %2 == 1: 9 | matches += (n-1)//2 10 | n = ((n-1)//2) + 1 11 | return matches 12 | -------------------------------------------------------------------------------- /169. Majority Element (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def majorityElement(self, nums: List[int]) -> int: 3 | dictionary = {} 4 | for item in nums: 5 | if item in dictionary: 6 | dictionary[item] += 1 7 | else: 8 | dictionary[item] = 1 9 | for key in dictionary: 10 | if dictionary[key] >= len(nums)/2: 11 | return key 12 | 13 | -------------------------------------------------------------------------------- /1704. Determine if String Halves Are Alike (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def halvesAreAlike(self, s: str) -> bool: 3 | if len(s) % 2 == 1: 4 | return False 5 | else: 6 | s = s.lower() 7 | vowels = ["a","e","i","o","u"] 8 | half = len(s)//2 9 | s1 = s[0:half] 10 | s2 = s[half:] 11 | s1_count = 0 12 | s2_count = 0 13 | for item in s1: 14 | if item in vowels: 15 | s1_count += 1 16 | for item in s2: 17 | if item in vowels: 18 | s2_count += 1 19 | if s1_count == s2_count: 20 | return True 21 | else: 22 | return False 23 | 24 | -------------------------------------------------------------------------------- /171. Excel Sheet Column Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def titleToNumber(self, columnTitle: str) -> int: 3 | dictionary = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5, 'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10,'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15, 'P': 16, 'Q': 17, 'R': 18, 'S': 19,'T': 20, 'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26} 4 | total = 0 5 | 6 | reversed = list(columnTitle)[::-1] 7 | for i in range(len(columnTitle)): 8 | total += dictionary[reversed[i]]*pow(26,i) 9 | return total 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /1716. Calculate Money in Leetcode Bank (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def totalMoney(self, n: int) -> int: 3 | total = 0 4 | for i in range(1, n + 1): 5 | if i % 7 == 0: 6 | total += (i // 7) + 6 7 | else: 8 | total += (i % 7) + (i // 7) 9 | return total 10 | -------------------------------------------------------------------------------- /1721. Swapping Nodes in a Linked List/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def swapNodes(self, head: Optional[ListNode], k: int) -> Optional[ListNode]: 8 | values = [] 9 | 10 | node = head 11 | while node is not None: 12 | values.append(node.val) 13 | node = node.next 14 | 15 | a = values[k - 1] 16 | b = values[-k] 17 | 18 | values[k - 1] = b 19 | values[-k] = a 20 | 21 | dummy_head = ListNode(0) 22 | node = dummy_head 23 | for i in range(len(values)): 24 | new_node = ListNode(values[i]) 25 | node.next = new_node 26 | node = node.next 27 | 28 | return dummy_head.next 29 | 30 | 31 | -------------------------------------------------------------------------------- /1732. Find the Highest Altitude/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestAltitude(self, gain: List[int]) -> int: 3 | altitudes = [0] 4 | 5 | for i in range(len(gain)): 6 | height = gain[i] 7 | length = len(altitudes) 8 | 9 | previous_height = altitudes[length - 1] 10 | 11 | new_altitude = height + previous_height 12 | 13 | altitudes.append(new_altitude) 14 | 15 | return max(altitudes) 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /1748. Sum of Unique Elements (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sumOfUnique(self, nums): 3 | distinct = [] 4 | not_distinct = [] 5 | for num in nums: 6 | if num not in distinct and num not in not_distinct: 7 | distinct.append(num) 8 | elif num in distinct: 9 | distinct.remove(num) 10 | not_distinct.append(num) 11 | if len(distinct) == 0: 12 | return 0 13 | else: 14 | return sum(distinct) 15 | 16 | -------------------------------------------------------------------------------- /1756. Design Most Recently Used Queue/main.py: -------------------------------------------------------------------------------- 1 | class MRUQueue: 2 | 3 | def __init__(self, n: int): 4 | self.nums = [i for i in range(1, n + 1)] 5 | 6 | def fetch(self, k: int) -> int: 7 | number = self.nums[k - 1] 8 | self.nums.pop(k - 1) 9 | self.nums.append(number) 10 | 11 | return number 12 | 13 | # Your MRUQueue object will be instantiated and called as such: 14 | # obj = MRUQueue(n) 15 | # param_1 = obj.fetch(k) 16 | -------------------------------------------------------------------------------- /1757. Recyclable and Low Fat Products (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def find_products(products: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | for (index,row) in products.iterrows(): 6 | if row["low_fats"] == "Y" and row["recyclable"] == "Y": 7 | ids.append(row['product_id']) 8 | return pd.DataFrame({"product_id":ids}) -------------------------------------------------------------------------------- /176. Second Highest Salary (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def second_highest_salary(employee: pd.DataFrame) -> pd.DataFrame: 5 | salaries = [] 6 | for index, row in employee.iterrows(): 7 | salary = row['salary'] 8 | if salary not in salaries: 9 | salaries.append(row['salary']) 10 | 11 | if len(salaries) <= 1: 12 | return pd.DataFrame({"SecondHighestSalary": [None]}) 13 | 14 | return pd.DataFrame({"SecondHighestSalary": [sorted(salaries)[-2]]}) -------------------------------------------------------------------------------- /1768. Merge Strings Alternately/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeAlternately(self, word1: str, word2: str) -> str: 3 | count1 = len(word1) 4 | count2 = len(word2) 5 | 6 | length = max(count1, count2) 7 | output = "" 8 | 9 | for i in range(length): 10 | if count1 != 0: 11 | output += word1[i] 12 | count1 -= 1 13 | 14 | if count2 != 0: 15 | output += word2[i] 16 | count2 -= 1 17 | 18 | return output 19 | -------------------------------------------------------------------------------- /1773. Count Items Matching a Rule (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countMatches(self, items, ruleKey, ruleValue): 3 | keys = {"type":0,"color":1,"name":2} 4 | index = keys[ruleKey] 5 | count = 0 6 | for item in items: 7 | if item[index] == ruleValue: 8 | count +=1 9 | return count -------------------------------------------------------------------------------- /1779. Find Nearest Point That Has the Same X or Y Coordinate (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def nearestValidPoint(self, x, y, points): 3 | distances = [] 4 | indices = [] 5 | for i in range(len(points)): 6 | if x == points[i][0] or y == points[i][1]: 7 | distance = abs(x-points[i][0])+abs(y-points[i][1]) 8 | distances.append(distance) 9 | indices.append(i) 10 | if len(distances) == 0: 11 | return -1 12 | else: 13 | smallest = min(distances) 14 | index = indices[distances.index(smallest)] 15 | return index -------------------------------------------------------------------------------- /1812. Determine Color of a Chessboard Square (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def squareIsWhite(self, coordinates): 3 | mapping = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7, "h": 8} 4 | first = mapping[coordinates[0]] 5 | second = int(coordinates[1]) 6 | if (first % 2) == (second%2): 7 | return False 8 | else: 9 | return True -------------------------------------------------------------------------------- /1816. Truncate Sentence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def truncateSentence(self, s: str, k: int) -> str: 3 | return " ".join(s.split()[:k]) -------------------------------------------------------------------------------- /182. Duplicate Emails (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def duplicate_emails(person: pd.DataFrame) -> pd.DataFrame: 4 | emails = {} 5 | for (index,row) in person.iterrows(): 6 | email = row["email"] 7 | if email not in emails: 8 | emails[email] = 1 9 | else: 10 | emails[email] += 1 11 | 12 | chosen = [] 13 | for email in emails: 14 | if emails[email] > 1: 15 | chosen.append(email) 16 | return pd.DataFrame({"Email":chosen}) -------------------------------------------------------------------------------- /1821. Find Customers With Positive Revenue this Year/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def find_customers(customers: pd.DataFrame) -> pd.DataFrame: 4 | values = [] 5 | for (index,row) in customers.iterrows(): 6 | if row["year"] == 2021 and row["revenue"] >0: 7 | values.append(row["customer_id"]) 8 | 9 | return pd.DataFrame({"customer_id":values}) 10 | -------------------------------------------------------------------------------- /1822. Sign of the Product of an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arraySign(self, nums: List[int]) -> int: 3 | if 0 in nums: 4 | return 0 5 | negative_numbers = 0 6 | for i in range(0,len(nums)): 7 | if nums[i] < 0: 8 | negative_numbers +=1 9 | if negative_numbers % 2 == 0: 10 | return 1 11 | else: 12 | return -1 -------------------------------------------------------------------------------- /1822. Sign of the Product of an Array (Leetcode)/main1.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arraySign(self, nums: List[int]) -> int: 3 | if 0 in nums: 4 | return 0 5 | product = nums[0] 6 | for i in range(1,len(nums)): 7 | product *= nums[i] 8 | if product >0: 9 | return 1 10 | else: 11 | return -1 -------------------------------------------------------------------------------- /1822. Sign of the Product of an Array (Leetcode)/main2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arraySign(self, nums: List[int]) -> int: 3 | product = nums[0] 4 | for i in range(1,len(nums)): 5 | product *= nums[i] 6 | if product >0: 7 | return 1 8 | elif product == 0: 9 | return 0 10 | else: 11 | return -1 12 | 13 | -------------------------------------------------------------------------------- /183. Customers Who Never Order (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def find_customers(customers: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame: 5 | ordered = [] 6 | for (index, row) in orders.iterrows(): 7 | ordered.append(row["customerId"]) 8 | 9 | names = [] 10 | for (index, row) in customers.iterrows(): 11 | if row["id"] not in ordered: 12 | names.append(row["name"]) 13 | print(names) 14 | return pd.DataFrame({"Customers": names}) -------------------------------------------------------------------------------- /1832. Check if the Sentence Is Pangram (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfPangram(self, sentence: str) -> bool: 3 | seen = set() 4 | for char in sentence: 5 | seen.add(char) 6 | if len(seen) == 26: 7 | return True 8 | else: 9 | return False 10 | -------------------------------------------------------------------------------- /1837. Sum of Digits in Base K (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sumBase(self, n, k): 3 | total = 0 4 | while n != 0: 5 | remainder = n %k 6 | n = n//k 7 | total += remainder 8 | return total 9 | 10 | -------------------------------------------------------------------------------- /1873. Calculate Special Bonus (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def calculate_special_bonus(employees: pd.DataFrame) -> pd.DataFrame: 5 | ids = [] 6 | names = [] 7 | bonus = [] 8 | 9 | for (index, row) in employees.iterrows(): 10 | name = row["name"] 11 | first_letter = name[0] 12 | if int(row["employee_id"]) % 2 == 1 and first_letter != "M": 13 | ids.append(row["employee_id"]) 14 | bonus.append(row["salary"]) 15 | else: 16 | ids.append(row["employee_id"]) 17 | bonus.append(0) 18 | 19 | new_ids = sorted(ids) 20 | new_bonuses = [bonus[ids.index(new)] for new in new_ids] 21 | 22 | return pd.DataFrame({"employee_id": new_ids, "bonus": new_bonuses}) -------------------------------------------------------------------------------- /189. Rotate Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rotate(self, nums: List[int], k: int) -> None: 3 | k = k % len(nums) 4 | 5 | for i in range(k): 6 | number = nums[-1] 7 | nums.pop() 8 | nums.insert(0, number) 9 | -------------------------------------------------------------------------------- /19. Remove Nth Node From End of List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]: 8 | values = [] 9 | node = head 10 | 11 | while node is not None: 12 | values.append(node.val) 13 | 14 | node = node.next 15 | 16 | skip = values[-n] 17 | 18 | node = head 19 | while node is not None: 20 | if node.next.val == skip: 21 | node = node.next.next 22 | else: 23 | node = node.next 24 | 25 | return head 26 | -------------------------------------------------------------------------------- /1907. Count Salary Categories (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def count_salary_categories(accounts: pd.DataFrame) -> pd.DataFrame: 4 | low = 0 5 | average = 0 6 | high =0 7 | for (index,row) in accounts.iterrows(): 8 | amount = row["income"] 9 | if amount < 20000: 10 | low += 1 11 | elif amount >= 20000 and amount <= 50000: 12 | average += 1 13 | else: 14 | high += 1 15 | return pd.DataFrame({"category":["High Salary","Low Salary","Average Salary"],"accounts_count":[high,low,average]}) 16 | -------------------------------------------------------------------------------- /191. Number of 1 Bits (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hammingWeight(self, n: int) -> int: 3 | count = 0 4 | while n >0: 5 | remainder = n % 2 6 | n = n//2 7 | if remainder == 1: 8 | count += 1 9 | return count 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /1913. Maximum Product Difference Between Two Pairs (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maxProductDifference(self, nums): 3 | nums.sort() 4 | return (nums[-1]*nums[-2])-(nums[0]*nums[1]) 5 | -------------------------------------------------------------------------------- /1920. Build Array from Permutation (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def buildArray(self, nums: List[int]) -> List[int]: 3 | return [nums[nums[i]] for i in range(len(nums))] -------------------------------------------------------------------------------- /1929. Concatenation of Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def getConcatenation(self, nums): 3 | return nums + nums 4 | -------------------------------------------------------------------------------- /1935. Maximum Number of Words You Can Type (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def canBeTypedWords(self, text, brokenLetters): 3 | words = text.split() 4 | letters = list(brokenLetters) 5 | countable = 0 6 | for word in words: 7 | count = 0 8 | for letter in letters: 9 | if letter not in word: 10 | count += 1 11 | if len(letters) == count: 12 | countable += 1 13 | 14 | return countable -------------------------------------------------------------------------------- /1941. Check if All Characters Have Equal Number of Occurrences (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def areOccurrencesEqual(self, s: str) -> bool: 3 | dictionary = {} 4 | for char in s: 5 | if char not in dictionary: 6 | dictionary[char] = 1 7 | else: 8 | dictionary[char] +=1 9 | 10 | results = [value for (key,value) in dictionary.items()] 11 | r1 = results[0] 12 | for result in results: 13 | if result != r1: 14 | return False 15 | return True 16 | -------------------------------------------------------------------------------- /1952. Three Divisors (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isThree(self, n: int) -> bool: 3 | return len([i for i in range(1,n+1) if n % i == 0]) == 3 -------------------------------------------------------------------------------- /1957. Delete Characters to Make Fancy String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def makeFancyString(self, s: str) -> str: 3 | output = '' 4 | char = s[0] 5 | for i in range(1, len(s)): 6 | new = s[i] 7 | if char[0] == new: 8 | if len(char) <= 2: 9 | char += char 10 | else: 11 | if len(char) >= 3: 12 | output += (2 * char[0]) 13 | char = new 14 | else: 15 | output += char 16 | char = new 17 | if len(char) >= 3: 18 | output += (2 * char[0]) 19 | char = new 20 | else: 21 | output += char 22 | 23 | return output 24 | 25 | -------------------------------------------------------------------------------- /1965. Employees With Missing Information (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def find_employees(employees: pd.DataFrame, salaries: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | for (index,row) in employees.iterrows(): 6 | ids.append(row["employee_id"]) 7 | for (index,row) in salaries.iterrows(): 8 | ids.append(row["employee_id"]) 9 | 10 | ids = sorted([item for item in ids if ids.count(item)== 1]) 11 | return pd.DataFrame({"employee_id":ids}) 12 | -------------------------------------------------------------------------------- /1967. Number of Strings That Appear as Substrings in Word (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numOfStrings(self, patterns, word): 3 | count = 0 4 | for pattern in patterns: 5 | if pattern in word: 6 | count += 1 7 | return count 8 | -------------------------------------------------------------------------------- /1979. Find Greatest Common Divisor of Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findGCD(self, nums: List[int]) -> int: 3 | smallest = min(nums) 4 | largest = max(nums) 5 | return max([i for i in range(1,largest+1) if largest % i == 0 and smallest % i == 0]) 6 | -------------------------------------------------------------------------------- /1995. Count Special Quadruplets (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countQuadruplets(self, nums: List[int]) -> int: 3 | count = 0 4 | for a in range(len(nums)): 5 | for b in range(a+1,len(nums)): 6 | for c in range(a+2,len(nums)): 7 | for d in range(a+3,len(nums)): 8 | if d > c and c > b and b > a: 9 | if (nums[a] + nums[b] + nums[c]) == nums[d]: 10 | count += 1 11 | return count 12 | -------------------------------------------------------------------------------- /2000. Reverse Prefix of Word (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reversePrefix(self, word: str, ch: str) -> str: 3 | if ch not in word: 4 | return word 5 | else: 6 | reverse_string = '' 7 | index = 0 8 | for i in range(len(word)): 9 | char = word[i] 10 | if char == ch: 11 | reverse_string += char 12 | index = i+1 13 | break 14 | else: 15 | reverse_string += char 16 | return reverse_string[::-1] + word[index:] 17 | -------------------------------------------------------------------------------- /2006. Count Number of Pairs With Absolute Difference K (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countKDifference(self, nums, k): 3 | count = 0 4 | for i in range(len(nums)): 5 | for j in range(i + 1, len(nums)): 6 | if abs(nums[i] - nums[j]) == k: 7 | count += 1 8 | return count 9 | -------------------------------------------------------------------------------- /2011. Final Value of Variable After Performing Operations (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def finalValueAfterOperations(self, operations): 3 | x = 0 4 | for operation in operations: 5 | if "+" in operation: 6 | x += 1 7 | else: 8 | x -= 1 9 | return x -------------------------------------------------------------------------------- /202. Happy Number/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isHappy(self, n: int) -> bool: 3 | count = 0 4 | while count < 15 and n != 1: 5 | number = list(str(n)) 6 | 7 | n = sum([pow(int(n), 2) for n in number]) 8 | count += 1 9 | 10 | if count == 15: 11 | return False 12 | else: 13 | return True 14 | -------------------------------------------------------------------------------- /203. Remove Linked List Elements (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]: 8 | values = [] 9 | node = head 10 | 11 | while node is not None: 12 | values.append(node.val) 13 | node = node.next 14 | 15 | values = [v for v in values if v!=val] 16 | 17 | new_head = ListNode(0) 18 | node = new_head 19 | index = 0 20 | while index != len(values): 21 | new_node = ListNode(values[index]) 22 | node.next = new_node 23 | index += 1 24 | node = node.next 25 | 26 | return new_head.next 27 | -------------------------------------------------------------------------------- /2032. Two Out of Three (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]: 3 | output = [] 4 | for item in nums1: 5 | if item in nums2 or item in nums3: 6 | output.append(item) 7 | 8 | for item in nums2: 9 | if item in nums1 or item in nums3: 10 | output.append(item) 11 | return list(set(output)) -------------------------------------------------------------------------------- /204. Count Primes (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPrimes(self, n: int) -> int: 3 | if n < 2: 4 | return 0 5 | prime_list = [True for i in range(n)] 6 | prime_list[0],prime_list[1] = False,False 7 | 8 | for i in range(2,int(n**0.5)+1): 9 | if prime_list[i] == True: 10 | for j in range(i*i,n,i): 11 | prime_list[j] = False 12 | return sum(prime_list) 13 | 14 | -------------------------------------------------------------------------------- /204. Count Primes (Leetcode)/main1.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPrimes(self, n: int) -> int: 3 | count = 0 4 | if n <2: 5 | return 0 6 | else: 7 | for i in range(2,n): 8 | if i == 2: 9 | count += 1 10 | elif i % 2 == 0: 11 | continue 12 | else: 13 | is_prime = True 14 | for j in range(3,n,2): 15 | if i % j == 0 and i !=j: 16 | is_prime = False 17 | if is_prime: 18 | count += 1 19 | return count 20 | 21 | sol = Solution() 22 | x = sol.countPrimes(100000) 23 | print(x) 24 | -------------------------------------------------------------------------------- /2053. Kth Distinct String in an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def kthDistinct(self, arr, k): 3 | arr = [item for item in arr if arr.count(item)<2] 4 | if len(arr) < k: 5 | return "" 6 | else: 7 | return arr[k-1] -------------------------------------------------------------------------------- /2057. Smallest Index With Equal Value (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def smallestEqual(self, nums): 3 | index = -1 4 | for i in range(len(nums)): 5 | if i % 10 == nums[i]: 6 | return i 7 | return index 8 | -------------------------------------------------------------------------------- /206. Reverse Linked List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | values = [] 9 | node = head 10 | 11 | while node is not None: 12 | values.append(node.val) 13 | 14 | node = node.next 15 | 16 | values = values[::-1] 17 | i = 0 18 | node = head 19 | 20 | while node is not None: 21 | node.val = values[i] 22 | print(node.val) 23 | i += 1 24 | 25 | node = node.next 26 | 27 | return head 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2073. Time Needed to Buy Tickets/main.py: -------------------------------------------------------------------------------- 1 | 2 | class Queue: 3 | def __init__(self,tickets:list,k): 4 | self.time = 0 5 | self.tickets = tickets 6 | self.item = self.tickets[k] 7 | self.memory_address = id(tickets[k]) 8 | 9 | def isEmpty(self): 10 | if self.tickets: 11 | return False 12 | return True 13 | 14 | def enqueue(self,item): 15 | 16 | def dequeue(self): 17 | 18 | 19 | def calculate(self): 20 | 21 | while self.item != 0: 22 | 23 | 24 | 25 | 26 | 27 | class Solution: 28 | def timeRequiredToBuy(self, tickets: list[int], k: int) -> int: 29 | pass 30 | 31 | 32 | if __name__ == "__main__": 33 | 34 | 35 | -------------------------------------------------------------------------------- /2085. Count Common Words With One Occurrence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countWords(self, words1: List[str], words2: List[str]) -> int: 3 | count = 0 4 | for word in words1: 5 | if words1.count(word) == 1 and words2.count(word) == 1: 6 | count +=1 7 | return count 8 | -------------------------------------------------------------------------------- /2089. Find Target Indices After Sorting Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def targetIndices(self, nums: List[int], target: int) -> List[int]: 3 | array = sorted(nums) 4 | return [i for i in range(len(array)) if array[i] == target] 5 | -------------------------------------------------------------------------------- /2114. Maximum Number of Words Found in Sentences (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mostWordsFound(self, sentences: List[str]) -> int: 3 | return max([len(sentence.split()) for sentence in sentences]) -------------------------------------------------------------------------------- /2119. A Number After a Double Reversal (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSameAfterReversals(self, num: int) -> bool: 3 | return num == int(str(int(str(num)[::-1]))[::-1]) 4 | -------------------------------------------------------------------------------- /2119. A Number After a Double Reversal (Leetcode)/main2.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isSameAfterReversals(self, num: int) -> bool: 3 | if num == 0: 4 | return True 5 | if num % 10 == 0: 6 | return False 7 | else: 8 | return True 9 | -------------------------------------------------------------------------------- /2126. Destroying Asteroids/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def asteroidsDestroyed(self, mass: int, asteroids: List[int]) -> bool: 3 | asteroids = sorted(asteroids) 4 | 5 | for asteroid in asteroids: 6 | if mass < asteroid: 7 | return False 8 | else: 9 | mass += asteroid 10 | 11 | return True -------------------------------------------------------------------------------- /2129. Capitalize the Title (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def capitalizeTitle(self, title: str): 3 | output = [] 4 | words = title.split() 5 | 6 | for i in range(len(words)): 7 | string = words[i] 8 | if len(string) <3: 9 | output.append(string.lower()) 10 | else: 11 | output.append(string.title()) 12 | return " ".join(output) 13 | 14 | -------------------------------------------------------------------------------- /2130. Maximum Twin Sum of a Linked List/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def pairSum(self, head: Optional[ListNode]) -> int: 8 | values = [] 9 | node = head 10 | while node is not None: 11 | values.append(node.val) 12 | node = node.next 13 | 14 | greatest = 0 15 | for i in range(len(values) // 2): 16 | value1 = values[i] 17 | value2 = values[len(values) - i - 1] 18 | value = value1 + value2 19 | if value > greatest: 20 | greatest = value 21 | 22 | return greatest -------------------------------------------------------------------------------- /2154. Keep Multiplying Found Values by Two (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findFinalValue(self, nums: List[int], original: int) -> int: 3 | number = original 4 | while number in nums: 5 | number = number * 2 6 | 7 | return number -------------------------------------------------------------------------------- /2169. Count Operations to Obtain Zero (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countOperations(self, num1, num2): 3 | count = 0 4 | 5 | while num1 != 0 and num2 != 0: 6 | if num1 > num2: 7 | num1 -= num2 8 | count += 1 9 | elif num1 == num2: 10 | num1 = 0 11 | num2 = 0 12 | count += 1 13 | break 14 | else: 15 | num2 -= num1 16 | count += 1 17 | return count 18 | -------------------------------------------------------------------------------- /217. Contains Duplicate (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def containsDuplicate(self, nums: List[int]) -> bool: 3 | if sorted(list(set(nums))) == sorted(nums): 4 | return False 5 | else: 6 | return True -------------------------------------------------------------------------------- /2176. Count Equal and Divisible Pairs in an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countPairs(self, nums, k): 3 | count = 0 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | if nums[i] == nums[j]: 7 | if (i*j) % k == 0: 8 | count +=1 9 | return count -------------------------------------------------------------------------------- /2180. Count Integers With Even Digit Sum (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countEven(self, num: int) -> int: 3 | count = 0 4 | for i in range(1,num+1): 5 | number = str(i) 6 | amount = 0 7 | for char in number: 8 | amount += int(char) 9 | if amount % 2 == 0: 10 | count += 1 11 | return count -------------------------------------------------------------------------------- /2190. Most Frequent Number Following Key In an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mostFrequent(self, nums: List[int], key: int) -> int: 3 | dictionary = {} 4 | for i in range(len(nums) - 1): 5 | if nums[i] == key: 6 | number = nums[i + 1] 7 | if number not in dictionary: 8 | dictionary[number] = 1 9 | else: 10 | dictionary[number] += 1 11 | 12 | value = max(dictionary.values()) 13 | 14 | for k in dictionary: 15 | if dictionary[k] == value: 16 | return k 17 | -------------------------------------------------------------------------------- /2206. Divide Array Into Equal Pairs (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def divideArray(self, nums): 3 | for num in nums: 4 | if nums.count(num) % 2 !=0: 5 | return False 6 | return True 7 | -------------------------------------------------------------------------------- /2215. Find the Difference of Two Arrays (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]: 3 | output1 = [] 4 | output2 = [] 5 | 6 | c1 = nums1 7 | c2 = nums2 8 | 9 | for num in nums1: 10 | if num in c2: 11 | continue 12 | else: 13 | if num not in output1: 14 | output1.append(num) 15 | 16 | for num in nums2: 17 | if num in c1: 18 | continue 19 | else: 20 | if num not in output2: 21 | output2.append(num) 22 | return [output1, output2] 23 | -------------------------------------------------------------------------------- /222. Count Complete Tree Nodes (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def countNodes(self, root): 9 | if not root: 10 | return 0 11 | 12 | return 1 + self.countNodes(root.left) + self.countNodes(root.right) 13 | 14 | 15 | -------------------------------------------------------------------------------- /2221. Find Triangular Sum of an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def triangularSum(self, nums): 3 | 4 | while len(nums) > 1: 5 | items = [] 6 | for i in range(len(nums) - 1): 7 | value = (nums[i] + nums[i + 1]) % 10 8 | items.append(value) 9 | nums = items 10 | return nums[0] 11 | -------------------------------------------------------------------------------- /2224. Minimum Number of Operations to Convert Time (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def convertTime(self, current, correct): 3 | t1 = current.split(":") 4 | t2 = correct.split(":") 5 | t1 = (int(t1[0])*60) + int(t1[1]) 6 | t2 = (int(t2[0])*60) + int(t2[1]) 7 | time = t2-t1 8 | count = 0 9 | while time >0: 10 | if time >=60: 11 | time-=60 12 | count +=1 13 | elif time >=15: 14 | time-=15 15 | count +=1 16 | elif time >=5: 17 | time-=5 18 | count +=1 19 | elif time >=1: 20 | time-=1 21 | count += 1 22 | return count 23 | 24 | s = Solution() 25 | s.convertTime("02:30","04:35") 26 | 27 | -------------------------------------------------------------------------------- /2235. Add Two Integers (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sum(self, num1: int, num2: int) -> int: 3 | return num1 + num2 -------------------------------------------------------------------------------- /225. Implement Stack using Queues/main.py: -------------------------------------------------------------------------------- 1 | class MyStack: 2 | 3 | def __init__(self): 4 | self.list = [] 5 | 6 | def push(self, x: int) -> None: 7 | self.list.append(x) 8 | 9 | def pop(self) -> int: 10 | return self.list.pop() 11 | 12 | def top(self) -> int: 13 | return self.list[-1] 14 | 15 | def empty(self) -> bool: 16 | if len(self.list) == 0: 17 | return True 18 | else: 19 | return False 20 | 21 | # Your MyStack object will be instantiated and called as such: 22 | # obj = MyStack() 23 | # obj.push(x) 24 | # param_2 = obj.pop() 25 | # param_3 = obj.top() 26 | # param_4 = obj.empty() 27 | -------------------------------------------------------------------------------- /226. Invert Binary Tree (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def invertTree(self, root): 9 | if not root: 10 | return 11 | 12 | left = self.invertTree(root.left) 13 | right = self.invertTree(root.right) 14 | root.left = right 15 | root.right = left 16 | return root 17 | 18 | 19 | -------------------------------------------------------------------------------- /2278. Percentage of Letter in String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def percentageLetter(self, s: str, letter: str) -> int: 3 | return (len([1 for char in s if char == letter]) * 100) // len(s) 4 | 5 | -------------------------------------------------------------------------------- /229. Majority Element II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def majorityElement(self, nums): 3 | dictionary = {} 4 | for num in nums: 5 | if num in dictionary: 6 | dictionary[num] += 1 7 | else: 8 | dictionary[num] =1 9 | amount = len(nums)/3 10 | output = [] 11 | for key in dictionary: 12 | if dictionary[key] > amount: 13 | output.append(key) 14 | return output 15 | 16 | -------------------------------------------------------------------------------- /23. Merge k Sorted Lists (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]: 8 | values = [] 9 | for l in lists: 10 | head = l 11 | node = head 12 | while node is not None: 13 | values.append(node.val) 14 | node = node.next 15 | 16 | values = sorted(values) 17 | 18 | head = ListNode(0) 19 | index = 0 20 | node = head 21 | 22 | while index != len(values): 23 | new_node = ListNode(values[index]) 24 | node.next = new_node 25 | index += 1 26 | node = node.next 27 | 28 | return head.next 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /2309. Greatest English Letter in Upper and Lower Case (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def greatestLetter(self, s: str) -> str: 3 | upper = [] 4 | for char in s: 5 | if char.lower() in s and char.upper() in s: 6 | upper.append(char.upper()) 7 | 8 | if len(upper) == 0: 9 | return "" 10 | else: 11 | return max(upper) 12 | -------------------------------------------------------------------------------- /231. Power of Two (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | if 0 >= n: 4 | return False 5 | is_true = True 6 | while is_true: 7 | if n> 1: 8 | remainder = n % 2 9 | if remainder != 0: 10 | return False 11 | else: 12 | n /= 2 13 | elif n == 1: 14 | return True -------------------------------------------------------------------------------- /232. Implement Queue using Stacks/main.py: -------------------------------------------------------------------------------- 1 | class MyQueue: 2 | 3 | def __init__(self): 4 | self.list = [] 5 | 6 | def push(self, x: int) -> None: 7 | self.list.append(x) 8 | 9 | def pop(self) -> int: 10 | return self.list.pop(0) 11 | 12 | def peek(self) -> int: 13 | return self.list[0] 14 | 15 | def empty(self) -> bool: 16 | if len(self.list) == 0: 17 | return True 18 | else: 19 | return False 20 | 21 | # Your MyQueue object will be instantiated and called as such: 22 | # obj = MyQueue() 23 | # obj.push(x) 24 | # param_2 = obj.pop() 25 | # param_3 = obj.peek() 26 | # param_4 = obj.empty() 27 | -------------------------------------------------------------------------------- /2339. All the Matches of the League/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def find_all_matches(teams: pd.DataFrame) -> pd.DataFrame: 5 | df = teams 6 | teams = [] 7 | 8 | for index, row in df.iterrows(): 9 | teams.append(row["team_name"]) 10 | 11 | homes = [] 12 | aways = [] 13 | for team in teams: 14 | for t in teams: 15 | if t != team: 16 | homes.append(team) 17 | aways.append(t) 18 | 19 | return pd.DataFrame({"home_team": homes, "away_team": aways}) 20 | -------------------------------------------------------------------------------- /234. Palindrome Linked List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def isPalindrome(self, head: Optional[ListNode]) -> bool: 8 | values = [] 9 | 10 | node = head 11 | 12 | while node is not None: 13 | values.append(node.val) 14 | node = node.next 15 | 16 | return values == values[::-1] -------------------------------------------------------------------------------- /2351. First Letter to Appear Twice (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def repeatedCharacter(self, s): 3 | letters = list(s) 4 | dict = {} 5 | for letter in letters: 6 | if letter not in dict: 7 | dict[letter] = 1 8 | else: 9 | return letter 10 | -------------------------------------------------------------------------------- /2356. Number of Unique Subjects Taught by Each Teacher (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def count_unique_subjects(teacher: pd.DataFrame) -> pd.DataFrame: 5 | dictionary = {} 6 | 7 | for index, row in teacher.iterrows(): 8 | subject = row["subject_id"] 9 | t = row["teacher_id"] 10 | if t not in dictionary: 11 | dictionary[t] = [subject] 12 | else: 13 | if subject not in dictionary[t]: 14 | dictionary[t].append(subject) 15 | 16 | ids = [] 17 | count = [] 18 | for t in dictionary: 19 | ids.append(t) 20 | count.append(len(dictionary[t])) 21 | 22 | return pd.DataFrame({"teacher_id": ids, "cnt": count}) -------------------------------------------------------------------------------- /2363. Merge Similar Items (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]: 3 | dictionary = {} 4 | 5 | for item in items1: 6 | if item[0] in dictionary: 7 | dictionary[item[0]] += item[1] 8 | else: 9 | dictionary[item[0]] = item[1] 10 | 11 | for item in items2: 12 | if item[0] in dictionary: 13 | dictionary[item[0]] += item[1] 14 | else: 15 | dictionary[item[0]] = item[1] 16 | 17 | keys = sorted(dictionary) 18 | result = [] 19 | 20 | for key in keys: 21 | value = [key] 22 | value.append(dictionary[key]) 23 | 24 | result.append(value) 25 | 26 | return result 27 | -------------------------------------------------------------------------------- /2367. Number of Arithmetic Triplets (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arithmeticTriplets(self, nums: List[int], diff: int) -> int: 3 | count = 0 4 | for i in range(0,len(nums)): 5 | for j in range(i+1,len(nums)): 6 | for k in range(i+2,len(nums)): 7 | if nums[j] - nums[i] == diff and nums[k] - nums[j] == diff: 8 | count += 1 9 | return count 10 | -------------------------------------------------------------------------------- /237. Delete Node in a Linked List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def deleteNode(self, node): 9 | node.val = node.next.val 10 | node.next = node.next.next 11 | -------------------------------------------------------------------------------- /238. Product of Array Except Self (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def productExceptSelf(self, nums: List[int]) -> List[int]: 3 | if nums.count(0) >= 2: 4 | return len(nums) * [0] 5 | elif nums.count(0) == 1: 6 | product = 1 7 | index = None 8 | for i in range(len(nums)): 9 | num = nums[i] 10 | if num != 0: 11 | product *= num 12 | nums[i] = 0 13 | elif num == 0: 14 | index = i 15 | nums[index] = product 16 | else: 17 | product = 1 18 | for i in range(len(nums)): 19 | product *= nums[i] 20 | 21 | for i in range(len(nums)): 22 | nums[i] = product // nums[i] 23 | 24 | return nums 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /2390. Removing Stars From a String/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeStars(self, s: str) -> str: 3 | result = [] 4 | 5 | for char in s: 6 | if char == "*": 7 | if result: 8 | result.pop() 9 | else: 10 | result.append(char) 11 | 12 | return "".join(result) 13 | -------------------------------------------------------------------------------- /2396. Strictly Palindromic Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isStrictlyPalindromic(self, n: int) -> bool: 3 | for i in range(2,n-1): 4 | new_string = self.convert(n,i) 5 | result = self.check_palindrome(new_string) 6 | if result == False: 7 | return False 8 | return True 9 | 10 | def convert(self,number:int,base:int)->str: 11 | output = [] 12 | 13 | while number != 0: 14 | remainder = number % base 15 | number = number//base 16 | output.append(str(remainder)) 17 | string = "".join(output[::-1]) 18 | return string 19 | 20 | def check_palindrome(self,string:str): 21 | if string == string[::-1]: 22 | return True 23 | else: 24 | return False -------------------------------------------------------------------------------- /240. Search a 2D Matrix II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def searchMatrix(self, matrix, target): 3 | for i in range(len(matrix)): 4 | for j in range(len(matrix[i])): 5 | if matrix[i][j] == target: 6 | return True 7 | return False 8 | -------------------------------------------------------------------------------- /2404. Most Frequent Even Element/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def mostFrequentEven(self, nums): 3 | dictionary = {} 4 | for num in nums: 5 | parity = num % 2 6 | if parity == 0: 7 | if num not in dictionary: 8 | dictionary[num] = 1 9 | else: 10 | dictionary[num] +=1 11 | if len(dictionary) == 0: 12 | return -1 13 | else: 14 | result = sorted(dictionary.keys(),key= lambda x: (-dictionary[x],x)) 15 | return result[0] -------------------------------------------------------------------------------- /2413. Smallest Even Multiple (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestEvenMultiple(self, n: int) -> int: 3 | if n % 2 == 0: 4 | return n 5 | else: 6 | return n *2 -------------------------------------------------------------------------------- /242.Valid Anagram (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isAnagram(self, s: str, t: str) -> bool: 3 | s_dict = {} 4 | t_dict = {} 5 | if len(s) != len(t): 6 | return False 7 | for i in range(len(s)): 8 | char_s = s[i] 9 | char_t = t[i] 10 | if char_s not in s_dict: 11 | s_dict[char_s] = 1 12 | else: 13 | s_dict[char_s] += 1 14 | 15 | if char_t not in t_dict: 16 | t_dict[char_t] = 1 17 | else: 18 | t_dict[char_t] += 1 19 | if s_dict == t_dict: 20 | return True 21 | else: 22 | return False -------------------------------------------------------------------------------- /2427. Number of Common Factors (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def commonFactors(self, a: int, b: int) -> int: 3 | return len([i for i in range(1,min(a,b)+1) if a % i == 0 and b % i == 0]) -------------------------------------------------------------------------------- /2455. Average Value of Even Numbers That Are Divisible by Three (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def averageValue(self, nums: List[int]) -> int: 3 | nums = [num for num in nums if num % 6 == 0] 4 | try: 5 | return int(sum(nums)/len(nums)) 6 | except ZeroDivisionError: 7 | return 0 -------------------------------------------------------------------------------- /2455. Average Value of Even Numbers That Are Divisible by Three (Leetcode)/main1.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def averageValue(self, nums: List[int]) -> int: 3 | nums = [num for num in nums if num % 3 == 0 and num % 2 == 0] 4 | try: 5 | return int(sum(nums)/len(nums)) 6 | except ZeroDivisionError: 7 | return 0 -------------------------------------------------------------------------------- /2465. Number of Distinct Averages (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumAverage(self, nums: List[int]) -> float: 3 | averages = [] 4 | n = len(nums)//2 5 | for i in range(n): 6 | greatest = max(nums) 7 | smallest = min(nums) 8 | nums.remove(greatest) 9 | nums.remove(smallest) 10 | average = (greatest+smallest)/2 11 | averages.append(average) 12 | return min(averages) 13 | -------------------------------------------------------------------------------- /2469. Convert the Temperature (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertTemperature(self, celsius: float) -> List[float]: 3 | return [celsius+273.15,(celsius*1.8)+32] 4 | -------------------------------------------------------------------------------- /2475. Number of Unequal Triplets in Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def unequalTriplets(self, nums: List[int]) -> int: 3 | count = 0 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | for k in range(i+2,len(nums)): 7 | if j > i and k > j: 8 | if nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]: 9 | count += 1 10 | return count -------------------------------------------------------------------------------- /2481. Minimum Cuts to Divide a Circle (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numberOfCuts(self, n): 3 | if n == 1: 4 | return 0 5 | if n %2 == 0: 6 | return n/2 7 | else: 8 | return n -------------------------------------------------------------------------------- /2485. Find the Pivot Integer/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotInteger(self, n: int) -> int: 3 | nums = [i for i in range(1, n + 1)] 4 | for i in range(n): 5 | left_sum = sum(nums[0:i + 1]) 6 | right_sum = sum(nums[i:]) 7 | print(left_sum, right_sum) 8 | if left_sum == right_sum: 9 | return i + 1 10 | return -1 11 | 12 | -------------------------------------------------------------------------------- /2490. Circular Sentence/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isCircularSentence(self, sentence: str) -> bool: 3 | words = sentence.split() 4 | if len(words) == 1: 5 | if sentence[0] == sentence[-1]: 6 | return True 7 | else: 8 | return False 9 | 10 | for i in range(0, len(words)): 11 | if i == len(words) - 1: 12 | if words[len(words) - 1][-1] != words[0][0]: 13 | return False 14 | else: 15 | if words[i][-1] != words[i + 1][0]: 16 | return False 17 | 18 | return True 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /2500. Delete Greatest Value in Each Row (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def deleteGreatestValue(self, grid): 3 | total = 0 4 | while len(grid[0]) !=0: 5 | items = [] 6 | for i in range(len(grid)): 7 | current = grid[i] 8 | greatest = max(grid[i]) 9 | grid[i].remove(greatest) 10 | items.append(greatest) 11 | total += max(items) 12 | return total -------------------------------------------------------------------------------- /2520. Count the Digits That Divide a Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countDigits(self, num: int) -> int: 3 | return sum([1 for item in list(str(num)) if num % int(item) == 0]) 4 | 5 | -------------------------------------------------------------------------------- /2529. Maximum Count of Positive Integer and Negative Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def maximumCount(self, nums): 3 | negative = 0 4 | positive = 0 5 | for num in nums: 6 | if num >0: 7 | positive += 1 8 | elif num < 0: 9 | negative += 1 10 | return max(negative,positive) -------------------------------------------------------------------------------- /2535. Difference Between Element Sum and Digit Sum of an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def differenceOfSum(self, nums: List[int]) -> int: 3 | element_sum = sum(nums) 4 | digit_sum = 0 5 | for num in nums: 6 | for char in str(num): 7 | digit_sum += int(char) 8 | return abs(element_sum-digit_sum) -------------------------------------------------------------------------------- /2540. Minimum Common Value (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getCommon(self, nums1: List[int], nums2: List[int]) -> int: 3 | s1 = set(nums1) 4 | s2 = set(nums2) 5 | common = list(s1&s2) 6 | if len(common) == 0: 7 | return -1 8 | else: 9 | return min(common) -------------------------------------------------------------------------------- /2544. Alternating Digit Sum (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def alternateDigitSum(self, n: int) -> int: 3 | total = 0 4 | for i in range(len(str(n))): 5 | if i % 2 == 0: 6 | total += int(str(n)[i]) 7 | else: 8 | total += (-1 *int(str(n)[i])) 9 | return total 10 | -------------------------------------------------------------------------------- /2578. Split With Minimum Sum (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def splitNum(self, num: int) -> int: 3 | numbers = [int(number) for number in list(str(num))] 4 | num1 = "" 5 | num2 = "" 6 | for i in range(0,len(numbers)): 7 | number = min(numbers) 8 | numbers.remove(number) 9 | if i % 2 ==0: 10 | num1 += str(number) 11 | else: 12 | num2 += str(number) 13 | return int(num1)+int(num2) 14 | -------------------------------------------------------------------------------- /258. Add Digits (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addDigits(self, num: int) -> int: 3 | total = 0 4 | number = str(num) 5 | while len(number) >1: 6 | number = [int(item) for item in list(number)] 7 | total = sum(number) 8 | number = str(total) 9 | number = int(number) 10 | return number -------------------------------------------------------------------------------- /258. Add Digits/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addDigits(self, num: int) -> int: 3 | while num >= 10: 4 | number = list(str(num)) 5 | 6 | num = sum([int(i) for i in number]) 7 | 8 | return num -------------------------------------------------------------------------------- /2582. Pass the Pillow (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def passThePillow(self, n: int, time: int) -> int: 3 | array = [i for i in range(1, n + 1)] 4 | index = 0 5 | forward = True 6 | 7 | for i in range(time): 8 | if forward: 9 | index += 1 10 | else: 11 | index -= 1 12 | item = array[index] 13 | 14 | if item == n: 15 | forward = False 16 | 17 | if item == 1: 18 | forward = True 19 | return index + 1 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /2586. Count the Number of Vowel Strings in Range (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def vowelStrings(self, words: List[str], left: int, right: int) -> int: 3 | vowels = ["a","e","i","o","u"] 4 | count = 0 5 | words = words[left:right+1] 6 | for i in range(len(words)): 7 | word = words[i] 8 | if len(word) == 1 and word in vowels: 9 | count += 1 10 | else: 11 | if word[0] in vowels and word[-1] in vowels: 12 | count += 1 13 | return count 14 | -------------------------------------------------------------------------------- /2595. Number of Even and Odd Bits (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def evenOddBit(self, n): 3 | bits = [0,0] 4 | count = 0 5 | while n != 0: 6 | remainder = n %2 7 | n = n//2 8 | if count %2 == 0 and remainder == 1: 9 | bits[0] +=1 10 | count +=1 11 | elif count %2 == 1 and remainder == 1: 12 | bits[1] += 1 13 | count +=1 14 | else: 15 | count +=1 16 | return bits 17 | 18 | -------------------------------------------------------------------------------- /26. Remove Duplicates from Sorted Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, nums: List[int]) -> int: 3 | for num in nums: 4 | count = nums.count(num) 5 | for i in range(count-1): 6 | nums.remove(num) -------------------------------------------------------------------------------- /260. Single Number III (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums: List[int]) -> List[int]: 3 | numbers = [] 4 | for num in nums: 5 | if nums.count(num) == 1: 6 | numbers.append(num) 7 | if len(numbers) == 2: 8 | return numbers -------------------------------------------------------------------------------- /263. Ugly Number/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isUgly(self, n: int) -> bool: 3 | if n == 0: 4 | return False 5 | while n!= 1: 6 | if n % 2 == 0: 7 | n = n//2 8 | print(n) 9 | elif n % 3 == 0: 10 | n = n//3 11 | print(n) 12 | elif n % 5 == 0: 13 | n = n//5 14 | print(n) 15 | else: 16 | return False 17 | return True 18 | -------------------------------------------------------------------------------- /2651. Calculate Delayed Arrival Time (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int: 3 | new_time = arrivalTime + delayedTime 4 | while new_time >= 24: 5 | new_time -= 24 6 | return new_time 7 | -------------------------------------------------------------------------------- /2652. Sum Multiples (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfMultiples(self, n: int) -> int: 3 | return sum([i for i in range(1,n+1) if i%3 == 0 or i%5==0 or i%7==0]) 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /2678. Number of Senior Citizens (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countSeniors(self, details): 3 | count = 0 4 | for detail in details: 5 | tens = int(detail[11]) * 10 6 | ones = int(detail[12]) 7 | age = tens+ones 8 | if age > 60: 9 | count +=1 10 | return count 11 | -------------------------------------------------------------------------------- /2679. Sum in a Matrix (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def matrixSum(self, nums): 3 | score = 0 4 | while len(nums[-1]) != 0: 5 | items = [] 6 | for i in range(len(nums)): 7 | numbers = nums[i] 8 | greatest = max(numbers) 9 | items.append(greatest) 10 | numbers.remove(greatest) 11 | score += max(items) 12 | return score 13 | 14 | -------------------------------------------------------------------------------- /268. Missing Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def missingNumber(self, nums): 3 | n = len(nums) 4 | total = (n * (n+1))/2 5 | for num in nums: 6 | total -= num 7 | return total 8 | -------------------------------------------------------------------------------- /27. Remove Element (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeElement(self, nums: List[int], val: int) -> int: 3 | count = 0 4 | for num in nums: 5 | if num == val: 6 | count += 1 7 | for i in range(count): 8 | nums.remove(val) 9 | nums.append(nums) 10 | return len(nums) - count 11 | -------------------------------------------------------------------------------- /270. Closest Binary Search Tree Value (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def closestValue(self, root, target): 9 | self.values = [] 10 | 11 | self.traverse(root, target) 12 | result = sorted(self.values, key=lambda x: (abs(x), -x)) 13 | 14 | return int(target - result[0]) 15 | 16 | def traverse(self, node, value): 17 | if not node: 18 | return 19 | 20 | self.traverse(node.left, value) 21 | self.values.append(value - node.val) 22 | self.traverse(node.right, value) 23 | 24 | -------------------------------------------------------------------------------- /2706. Buy Two Chocolates (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def buyChoco(self, prices: List[int], money: int) -> int: 3 | smallest = min(prices) 4 | prices.remove(smallest) 5 | next_smallest = min(prices) 6 | if money >= smallest+next_smallest: 7 | return (money-(smallest+next_smallest)) 8 | else: 9 | return money -------------------------------------------------------------------------------- /2710. Remove Trailing Zeros From a String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeTrailingZeros(self, num: str) -> str: 3 | return str(int(num[::-1]))[::-1] -------------------------------------------------------------------------------- /2733. Neither Minimum nor Maximum (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNonMinOrMax(self, nums: List[int]) -> int: 3 | for n in nums: 4 | if n != min(nums) and n!=max(nums): 5 | return n 6 | return -1 -------------------------------------------------------------------------------- /2739. Total Distance Traveled (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distanceTraveled(self, mainTank: int, additionalTank: int) -> int: 3 | distance = 0 4 | 5 | while mainTank >0: 6 | if mainTank >=5: 7 | if additionalTank >=1: 8 | additionalTank -= 1 9 | mainTank -= 4 10 | distance += 50 11 | else: 12 | mainTank -= 5 13 | distance += 50 14 | else: 15 | distance += (mainTank)*10 16 | mainTank = 0 17 | return distance -------------------------------------------------------------------------------- /274. H-Index (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hIndex(self, citations: List[int]) -> int: 3 | length = len(citations) 4 | for i in range(length,-1,-1): 5 | if sum(1 for n in citations if n >= i) >= i: 6 | return i 7 | -------------------------------------------------------------------------------- /275. H-Index II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hIndex(self, citations: List[int]) -> int: 3 | dictionary = {} 4 | 5 | for i,num in enumerate(citations): 6 | if num not in dictionary: 7 | dictionary[num] = 1 8 | else: 9 | dictionary[num] += 1 10 | 11 | h_index = len(citations) 12 | for i in range(len(citations),0,-1): 13 | total = 0 14 | for key,value in dictionary.items(): 15 | if key >= h_index: 16 | total += value 17 | 18 | if total >=h_index: 19 | return h_index 20 | else: 21 | h_index -= 1 22 | return 0 -------------------------------------------------------------------------------- /2769. Find the Maximum Achievable Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def theMaximumAchievableX(self, num, t): 3 | return (t*2) + num 4 | -------------------------------------------------------------------------------- /2778. Sum of Squares of Special Elements (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfSquares(self, nums: List[int]) -> int: 3 | return sum([pow(nums[i],2) for i in range(0,len(nums)) if len(nums) % (i+1) == 0]) -------------------------------------------------------------------------------- /2788. Split Strings by Separator (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def splitWordsBySeparator(self, words: list[str], separator: str) -> list[str]: 3 | output = [] 4 | for word in words: 5 | results = word.split(separator) 6 | for result in results: 7 | if result != '': 8 | output.append(result) 9 | return output 10 | 11 | 12 | -------------------------------------------------------------------------------- /2798. Number of Employees Who Met the Target (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int: 3 | return len([hour for hour in hours if hour >=target]) 4 | -------------------------------------------------------------------------------- /28. Find the Index of the First Occurrence in a String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def strStr(self, haystack, needle): 3 | sentence = haystack.replace(needle, "$") 4 | for i in range(len(sentence)): 5 | if sentence[i] == "$": 6 | return i 7 | 8 | return -1 -------------------------------------------------------------------------------- /2824. Count Pairs Whose Sum is Less than Target (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countPairs(self, nums, target): 3 | count = 0 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | if nums[i]+nums[j] < target: 7 | count +=1 8 | return count 9 | -------------------------------------------------------------------------------- /2828. Check if a String Is an Acronym of Words (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isAcronym(self, words, s): 3 | return s == "".join([w[0] for w in words]) 4 | -------------------------------------------------------------------------------- /283. Move Zeroes (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def moveZeroes(self, nums): 3 | # My first solution 4 | # for num in nums: 5 | # if num == 0: 6 | # nums.remove(num) 7 | # nums.append(0) 8 | # return nums 9 | 10 | #Efficient Solution- My new solution 11 | j = 0 12 | for i in range(len(nums)): 13 | if nums[i] != 0: 14 | nums[j] = nums[i] 15 | j += 1 16 | for i in range(j,len(nums)): 17 | nums[i] = 0 18 | -------------------------------------------------------------------------------- /2864. Maximum Odd Binary Number/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumOddBinaryNumber(self, s: str) -> str: 3 | zero_count = s.count("0") 4 | 5 | one_count = s.count("1") 6 | 7 | if one_count == 1: 8 | return ("0"*zero_count)+"1" 9 | else: 10 | return ("1"*(one_count-1))+("0"*zero_count) + "1" 11 | -------------------------------------------------------------------------------- /287. Find the Duplicate Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findDuplicate(self, nums): 3 | seen = set() 4 | for num in nums: 5 | if num in seen: 6 | return num 7 | else: 8 | seen.add(num) 9 | -------------------------------------------------------------------------------- /2873. Maximum Value of an Ordered Triplet I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumTripletValue(self, nums: List[int]) -> int: 3 | greatest = 0 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | for k in range(i+2,len(nums)): 7 | if k > j and j > i: 8 | if nums[i] > 0 and nums[j] > 0 and nums[k] > 0: 9 | product = (nums[i]-nums[j]) * nums[k] 10 | if product > greatest: 11 | greatest = product 12 | return greatest -------------------------------------------------------------------------------- /2877. Create a DataFrame from List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas 2 | 3 | def createDataframe(student_data: List[List[int]]) -> pd.DataFrame: 4 | ids = [] 5 | ages = [] 6 | for data in student_data: 7 | ids.append(data[0]) 8 | ages.append(data[1]) 9 | dictionary = { 10 | "student_id":ids, 11 | "age":ages 12 | } 13 | return pandas.DataFrame(dictionary) 14 | -------------------------------------------------------------------------------- /2878. Get the Size of a DataFrame (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def getDataframeSize(players: pd.DataFrame) -> List[int]: 4 | x = players.shape 5 | return [x[0],x[1]] 6 | -------------------------------------------------------------------------------- /2879. Display the First Three Rows (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def selectFirstRows(employees: pd.DataFrame) -> pd.DataFrame: 4 | return employees.head(3) 5 | -------------------------------------------------------------------------------- /2880. Select Data (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def selectData(students: pd.DataFrame) -> pd.DataFrame: 5 | name = [] 6 | age = [] 7 | for index, row in students.iterrows(): 8 | if row["student_id"] == 101: 9 | name.append(row['name']) 10 | age.append(row['age']) 11 | break 12 | 13 | return pd.DataFrame({"name": name, "age": age}) -------------------------------------------------------------------------------- /2881. Create a New Column (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def createBonusColumn(employees: pd.DataFrame) -> pd.DataFrame: 5 | name = [] 6 | salary = [] 7 | bonus = [] 8 | for (index, row) in employees.iterrows(): 9 | name.append(row["name"]) 10 | salary.append(row["salary"]) 11 | bonus.append(2 * row["salary"]) 12 | return pd.DataFrame({"name": name, "salary": salary, "bonus": bonus}) 13 | -------------------------------------------------------------------------------- /2882. Drop Duplicate Rows (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def dropDuplicateEmails(customers: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | emails = [] 7 | for (index,row) in customers.iterrows(): 8 | email = row["email"] 9 | if email not in emails: 10 | ids.append(row["customer_id"]) 11 | names.append(row["name"]) 12 | emails.append(email) 13 | 14 | return pd.DataFrame({"customer_id":ids,"name":names,"email":emails}) -------------------------------------------------------------------------------- /2883. Drop Missing Data (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def dropMissingData(students: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | ages = [] 7 | for (index,row) in students.iterrows(): 8 | if pd.isnull(row["name"]): 9 | pass 10 | else: 11 | ids.append(row["student_id"]) 12 | names.append(row["name"]) 13 | ages.append(row["age"]) 14 | return pd.DataFrame({"student_id":ids,"name":names,"age":ages}) -------------------------------------------------------------------------------- /2884. Modify Columns (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def modifySalaryColumn(employees: pd.DataFrame) -> pd.DataFrame: 4 | names = [] 5 | salaries = [] 6 | for (index,row) in employees.iterrows(): 7 | names.append(row["name"]) 8 | salaries.append(row["salary"]*2) 9 | 10 | return pd.DataFrame({"name":names,"salary":salaries}) -------------------------------------------------------------------------------- /2885. Rename Columns (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def renameColumns(students: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | surnames = [] 7 | ages = [] 8 | for (index,row) in students.iterrows(): 9 | ids.append(row["id"]) 10 | names.append(row["first"]) 11 | surnames.append(row["last"]) 12 | ages.append(row["age"]) 13 | return pd.DataFrame({"student_id":ids,"first_name":names,"last_name":surnames,"age_in_years":ages}) 14 | -------------------------------------------------------------------------------- /2886. Change Data Type (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def changeDatatype(students: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | ages = [] 7 | grades = [] 8 | for (index,row) in students.iterrows(): 9 | ids.append(row["student_id"]) 10 | names.append(row["name"]) 11 | ages.append(row["age"]) 12 | grades.append(int(row["grade"])) 13 | return pd.DataFrame({"student_id":ids,"name":names,"age":ages,"grade":grades}) -------------------------------------------------------------------------------- /2887. Fill Missing Data (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def fillMissingValues(products: pd.DataFrame) -> pd.DataFrame: 4 | names = [] 5 | quantities = [] 6 | prices = [] 7 | for (index,row) in products.iterrows(): 8 | names.append(row["name"]) 9 | prices.append(row["price"]) 10 | if pd.isnull(row["quantity"]): 11 | quantities.append(0) 12 | else: 13 | quantities.append(row["quantity"]) 14 | return pd.DataFrame({"name":names,"quantity":quantities,"price":prices}) -------------------------------------------------------------------------------- /2888. Reshape Data: Concatenate (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def concatenateTables(df1: pd.DataFrame, df2: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | ages = [] 7 | for (index,row) in df1.iterrows(): 8 | ids.append(row["student_id"]) 9 | names.append(row["name"]) 10 | ages.append(row["age"]) 11 | 12 | for (index,row) in df2.iterrows(): 13 | ids.append(row["student_id"]) 14 | names.append(row["name"]) 15 | ages.append(row["age"]) 16 | 17 | return pd.DataFrame({"student_id":ids,"name":names,"age":ages}) 18 | -------------------------------------------------------------------------------- /2891. Method Chaining (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def findHeavyAnimals(animals: pd.DataFrame) -> pd.DataFrame: 4 | names = [] 5 | weights = [] 6 | for (index,row) in animals.iterrows(): 7 | if int(row["weight"]) > 100: 8 | names.append(row["name"]) 9 | weights.append(row["weight"]) 10 | new_weights = sorted(weights,reverse=True) 11 | new_names = [names[weights.index(weight)] for weight in new_weights] 12 | return pd.DataFrame({"name":new_names}) -------------------------------------------------------------------------------- /2894. Divisible and Non-divisible Sums Difference (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def differenceOfSums(self, n: int, m: int) -> int: 3 | num1 = [i for i in range(1,n+1) if i%m !=0] 4 | num2 = [i for i in range(1,n+1) if i%m ==0] 5 | return num1-num2 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /2903. Find Indices With Index and Value Difference I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findIndices(self, nums: List[int], indexDifference: int, valueDifference: int) -> List[int]: 3 | for i in range(len(nums)): 4 | for j in range(i,len(nums)): 5 | if abs(i-j) >= indexDifference and abs(nums[i]-nums[j]) >= valueDifference: 6 | return [i,j] 7 | return [-1,-1] 8 | -------------------------------------------------------------------------------- /2908. Minimum Sum of Mountain Triplets I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumSum(self, nums: List[int]) -> int: 3 | sums = [] 4 | for i in range(len(nums)): 5 | for j in range(i+1,len(nums)): 6 | for k in range(i+2,len(nums)): 7 | if k > j and j > i: 8 | if nums[i] < nums[j] and nums[k] < nums[j]: 9 | sums.append(nums[i] + nums[j] + nums[k]) 10 | if len(sums) ==0: 11 | return -1 12 | else: 13 | return min(sums) 14 | 15 | -------------------------------------------------------------------------------- /2942. Find Words Containing Character (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findWordsContaining(self, words: List[str], x: str) -> List[int]: 3 | return [i for i in range(len(words)) if x in words[i]] -------------------------------------------------------------------------------- /295. Find Median from Data Stream/main.py: -------------------------------------------------------------------------------- 1 | class MedianFinder: 2 | 3 | def __init__(self): 4 | self.list = [] 5 | self.length = 0 6 | 7 | def addNum(self, num: int) -> None: 8 | self.list.append(num) 9 | self.length += 1 10 | 11 | def findMedian(self) -> float: 12 | self.list = sorted(self.list) 13 | if self.length % 2 == 0: 14 | return (self.list[self.length // 2] + self.list[(self.length // 2) - 1]) / 2 15 | else: 16 | return self.list[self.length // 2] 17 | 18 | # Your MedianFinder object will be instantiated and called as such: 19 | # obj = MedianFinder() 20 | # obj.addNum(num) 21 | # param_2 = obj.findMedian() 22 | -------------------------------------------------------------------------------- /2951. Find the Peaks (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findPeaks(self, mountain): 3 | peaks = [] 4 | for i in range(1,len(mountain)-1): 5 | previous_item = mountain[i-1] 6 | current_item = mountain[i] 7 | next_item = mountain[i+1] 8 | if current_item > previous_item and current_item > next_item: 9 | peaks.append(i) 10 | return peaks 11 | -------------------------------------------------------------------------------- /2965. Find Missing and Repeated Values (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMissingAndRepeatedValues(self, grid: List[List[int]]) -> List[int]: 3 | numbers = [i for i in range(1,pow(len(grid),2)+1)] 4 | grid = [grid[i][j] for i in range(len(grid)) for j in range(len(grid[i]))] 5 | repeated_item = None 6 | missing_item = None 7 | for number in numbers: 8 | if number not in grid: 9 | missing_item = number 10 | elif grid.count(number) == 2: 11 | repeated_item = number 12 | return [repeated_item,missing_item] 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /2974. Minimum Number Game (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberGame(self, nums: List[int]) -> List[int]: 3 | arr = [] 4 | while len(nums) != 0: 5 | s1 = min(nums) 6 | nums.remove(s1) 7 | s2 = min(nums) 8 | nums.remove(s2) 9 | arr.append(s2) 10 | arr.append(s1) 11 | return arr -------------------------------------------------------------------------------- /2980. Check if Bitwise OR Has Trailing Zeros (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hasTrailingZeros(self, nums): 3 | count = 0 4 | for num in nums: 5 | if num % 2 == 0: 6 | count += 1 7 | if count == 2: 8 | return True 9 | return False 10 | -------------------------------------------------------------------------------- /2985. Calculate Compressed Mean (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def compressed_mean(orders: pd.DataFrame) -> pd.DataFrame: 4 | sigma_sum = 0 5 | total = 0 6 | for index,row in orders.iterrows(): 7 | order = row["order_occurrences"] 8 | count = row["item_count"] 9 | total += order 10 | sigma_sum += (order*count) 11 | return pd.DataFrame({"average_items_per_order":[round(sigma_sum/total,2)]}) -------------------------------------------------------------------------------- /2989. Class Performance (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def class_performance(scores: pd.DataFrame) -> pd.DataFrame: 5 | score_list = [] 6 | 7 | for index, row in scores.iterrows(): 8 | score = row["assignment1"] + row["assignment2"] + row["assignment3"] 9 | score_list.append(score) 10 | 11 | return pd.DataFrame({"difference_in_score": [max(score_list) - min(score_list)]}) -------------------------------------------------------------------------------- /3019. Number of Changing Keys (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countKeyChanges(self, s: str) -> int: 3 | count = 0 4 | char = s[0] 5 | for i in range(1,len(s)): 6 | new_char = s[i] 7 | if char.lower() != new_char.lower(): 8 | count += 1 9 | char = new_char 10 | return count 11 | -------------------------------------------------------------------------------- /3024. Type of Triangle (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def triangleType(self, nums: List[int]) -> str: 3 | if len([item for item in nums if item == nums[0]]) == 3: 4 | return "equilateral" 5 | 6 | for i in range(len(nums)): 7 | item = nums[i] 8 | nums.pop(i) 9 | if sum(nums) <= item: 10 | return "none" 11 | else: 12 | nums.insert(i,item) 13 | 14 | for num in nums: 15 | if nums.count(num) ==2: 16 | return "isosceles" 17 | return "scalene" -------------------------------------------------------------------------------- /3028. Ant on the Boundary (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def returnToBoundaryCount(self, nums: List[int]) -> int: 3 | location = 0 4 | count = 0 5 | for num in nums: 6 | if num >0: 7 | location += num 8 | elif num < 0: 9 | location += (num) 10 | 11 | if location == 0: 12 | count += 1 13 | 14 | return count -------------------------------------------------------------------------------- /303. Range Sum Query - Immutable/main.py: -------------------------------------------------------------------------------- 1 | class NumArray: 2 | 3 | def __init__(self, nums: List[int]): 4 | self.array = nums 5 | 6 | def sumRange(self, left: int, right: int) -> int: 7 | return sum(self.array[left:right + 1]) 8 | 9 | # Your NumArray object will be instantiated and called as such: 10 | # obj = NumArray(nums) 11 | # param_1 = obj.sumRange(left,right) -------------------------------------------------------------------------------- /3033. Modify the Matrix (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def modifiedMatrix(self, matrix): 3 | for i in range(len(matrix)): 4 | for j in range(len(matrix[i])): 5 | if matrix[i][j] == -1: 6 | elements = [m[j] for m in matrix] 7 | matrix[i][j] = max(elements) 8 | return matrix 9 | -------------------------------------------------------------------------------- /3046. Split the Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isPossibleToSplit(self, nums): 3 | dict = {} 4 | for num in nums: 5 | if num not in dict: 6 | dict[num] = 1 7 | else: 8 | dict[num] += 1 9 | if dict[num] > 2: 10 | return False 11 | return True 12 | 13 | -------------------------------------------------------------------------------- /3051. Find Candidates for Data Scientist Position/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def find_candidates(candidates: pd.DataFrame) -> pd.DataFrame: 5 | employees = {} 6 | ids = [] 7 | for index, row in candidates.iterrows(): 8 | candidate = row["candidate_id"] 9 | skill = row["skill"] 10 | if candidate in employees: 11 | employees[candidate].append(skill) 12 | if "Python" in employees[candidate] and "PostgreSQL" in employees[candidate] and "Tableau" in employees[ 13 | candidate]: 14 | if candidate not in ids: 15 | ids.append(candidate) 16 | else: 17 | employees[candidate] = [skill] 18 | 19 | ids = sorted(ids) 20 | 21 | return pd.DataFrame({"candidate_id": ids}) 22 | -------------------------------------------------------------------------------- /3062. Winner of the Linked List Game/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def gameResult(self, head: Optional[ListNode]) -> str: 8 | odd_points = 0 9 | even_points = 0 10 | node = head 11 | while node is not None: 12 | even = node.val 13 | odd = node.next.val 14 | if odd > even: 15 | odd_points += 1 16 | else: 17 | even_points += 1 18 | 19 | node = node.next.next 20 | 21 | if odd_points == even_points: 22 | return "Tie" 23 | elif odd_points > even_points: 24 | return "Odd" 25 | else: 26 | return "Even" 27 | -------------------------------------------------------------------------------- /3065. Minimum Operations to Exceed Threshold Value I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | return len([num for num in nums if num < k]) -------------------------------------------------------------------------------- /3079. Find the Sum of Encrypted Integers (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfEncryptedInt(self, nums: list[int]) -> int: 3 | total = 0 4 | for num in nums: 5 | numbers = [int(char) for char in list(str(num))] 6 | length = len(numbers) 7 | largest = str(max(numbers)) 8 | total += int(largest*length) 9 | return total 10 | 11 | 12 | -------------------------------------------------------------------------------- /3099. Harshad Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfTheDigitsOfHarshadNumber(self, x: int) -> int: 3 | number = str(x) 4 | total = 0 5 | for i in range(len(number)): 6 | total += int(number[i]) 7 | if x % total ==0: 8 | return total 9 | else: 10 | return -1 11 | -------------------------------------------------------------------------------- /3110. Score of a String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def scoreOfString(self, s: str) -> int: 3 | total = 0 4 | for i in range(0,len(s)-1): 5 | char1 = s[i] 6 | char2 = s[i+1] 7 | score1 = ord(char1) 8 | score2 = ord(char2) 9 | total += abs(score1-score2) 10 | return total -------------------------------------------------------------------------------- /3115. Maximum Prime Difference/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximumPrimeDifference(self, nums: list[int]) -> int: 3 | numbers = [i for i in range(len(nums)) if self.prime(nums[i]) == True] 4 | 5 | if len(numbers) == 1: 6 | return 0 7 | else: 8 | return max(numbers) - min(numbers) 9 | 10 | def prime(self, number: int) -> bool: 11 | if number == 1: 12 | return False 13 | elif number == 2: 14 | return True 15 | elif number % 2 == 0: 16 | return False 17 | else: 18 | for i in range(3, int(number ** 0.5) + 1): 19 | if number % i == 0: 20 | return False 21 | return True 22 | 23 | -------------------------------------------------------------------------------- /3150. Invalid Tweets II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def find_invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame: 5 | ids = [] 6 | for index, row in tweets.iterrows(): 7 | identifier = row["tweet_id"] 8 | tweet = row["content"] 9 | if len(tweet) > 140: 10 | ids.append(identifier) 11 | elif tweet.count("#") > 3 or tweet.count("@") > 3: 12 | ids.append(identifier) 13 | 14 | return pd.DataFrame({"tweet_id": sorted(ids)}) 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /3151. Special Array I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArraySpecial(self, nums: List[int]) -> bool: 3 | paritiess = [] 4 | if len(nums) == 1: 5 | return True 6 | elif len(nums) == 2: 7 | if nums[0] %2 == nums[1] %2: 8 | return False 9 | else: 10 | return True 11 | else: 12 | for i in range(1,len(nums)-1): 13 | previous = nums[i-1] % 2 14 | current = nums[i] % 2 15 | later = nums[i+1] %2 16 | if previous == current or current == later: 17 | return False 18 | return True -------------------------------------------------------------------------------- /3151. Special Array I (Leetcode)/main1.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def isArraySpecial(self, nums): 3 | for i in range(0,len(nums)-1): 4 | p1 = nums[i] %2 5 | p2 = nums[i+1] %2 6 | if p1 == p2: 7 | return False 8 | return True -------------------------------------------------------------------------------- /3158. Find the XOR of Numbers Which Appear Twice (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def duplicateNumbersXOR(self, nums): 3 | dictionary = {} 4 | 5 | for num in nums: 6 | if num not in dictionary: 7 | dictionary[num] = 1 8 | else: 9 | dictionary[num] += 1 10 | numbers = [] 11 | for key in dictionary: 12 | if dictionary[key] == 2: 13 | numbers.append(key) 14 | 15 | if len(numbers) == 0: 16 | return 0 17 | elif len(numbers) == 1: 18 | return numbers[0] 19 | else: 20 | total = numbers[0] 21 | for number in numbers[1:]: 22 | total ^= number 23 | return total 24 | 25 | -------------------------------------------------------------------------------- /3162. Find the Number of Good Pairs I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfPairs(self, nums1: List[int], nums2: List[int], k: int) -> int: 3 | count = 0 4 | for i in range(len(nums1)): 5 | for j in range(len(nums2)): 6 | if nums1[i] % (nums2[j]*k) == 0: 7 | count += 1 8 | return count 9 | -------------------------------------------------------------------------------- /3168. Minimum Number of Chairs in a Waiting Room (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def minimumChairs(self, s): 3 | occupancy = [] 4 | count = 0 5 | for char in s: 6 | if char == "E": 7 | count += 1 8 | occupancy.append(count) 9 | elif char == "L": 10 | count -=1 11 | occupancy.append(count) 12 | return max(occupancy) 13 | 14 | -------------------------------------------------------------------------------- /3178. Find the Child Who Has the Ball After K Seconds (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfChild(self, n: int, k: int) -> int: 3 | index = 0 4 | forward = True 5 | for i in range(k): 6 | if forward: 7 | index += 1 8 | else: 9 | index -= 1 10 | if index == 0: 11 | forward = True 12 | 13 | if index == n - 1: 14 | forward = False 15 | return index 16 | -------------------------------------------------------------------------------- /3190. Find Minimum Operations to Make All Elements Divisible by Three (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumOperations(self, nums: List[int]) -> int: 3 | count = 0 4 | for num in nums: 5 | if num % 3 == 0: 6 | count += 0 7 | elif num % 3 == 1: 8 | count += 1 9 | elif num % 3 == 2: 10 | count += 1 11 | return count 12 | -------------------------------------------------------------------------------- /3194. Minimum Average of Smallest and Largest Elements (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minimumAverage(self, nums: List[int]) -> float: 3 | averages = [] 4 | n = len(nums)//2 5 | for i in range(n): 6 | greatest = max(nums) 7 | smallest = min(nums) 8 | nums.remove(greatest) 9 | nums.remove(smallest) 10 | average = (greatest+smallest)/2 11 | averages.append(average) 12 | return min(averages) -------------------------------------------------------------------------------- /3210. Find the Encrypted String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getEncryptedString(self, s: str, k: int) -> str: 3 | result = "" 4 | length = len(s) 5 | for i in range(length): 6 | index = (i+k)%length 7 | result += s[index] 8 | return result 9 | 10 | 11 | -------------------------------------------------------------------------------- /3232. Find if Digit Game Can Be Won (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canAliceWin(self, nums: List[int]) -> bool: 3 | single_digit = 0 4 | double_digit = 0 5 | for num in nums: 6 | string = str(num) 7 | if len(string) == 2: 8 | double_digit += num 9 | else: 10 | single_digit += num 11 | 12 | if single_digit == double_digit: 13 | return False 14 | else: 15 | return True -------------------------------------------------------------------------------- /3248. Snake in Matrix/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def finalPositionOfSnake(self, n: int, commands: List[str]) -> int: 3 | total = (n**2)-1 4 | current = 0 5 | for command in commands: 6 | if command == "UP": 7 | current -= n 8 | elif command == "DOWN": 9 | current += n 10 | elif command == "LEFT": 11 | current -= 1 12 | else: 13 | current += 1 14 | return current 15 | -------------------------------------------------------------------------------- /326. Power of Three (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfThree(self, n: int) -> bool: 3 | if 0 >=n: 4 | return False 5 | else: 6 | while n>1: 7 | remainder = n%3 8 | print(remainder) 9 | n = n/3 10 | if remainder != 0: 11 | print(False) 12 | 13 | print(True) 14 | 15 | 16 | sol = Solution() 17 | sol.isPowerOfThree(27) -------------------------------------------------------------------------------- /3263. Convert Doubly Linked List to Array I/main.py: -------------------------------------------------------------------------------- 1 | """ 2 | # Definition for a Node. 3 | class Node: 4 | def __init__(self, val, prev=None, next=None): 5 | self.val = val 6 | self.prev = prev 7 | self.next = next 8 | """ 9 | 10 | 11 | class Solution: 12 | def toArray(self, root: 'Optional[Node]') -> List[int]: 13 | values = [] 14 | values.append(root.val) 15 | 16 | node = root.next 17 | while node is not None: 18 | print(values) 19 | values.append(node.val) 20 | node = node.next 21 | 22 | return values -------------------------------------------------------------------------------- /3280. Convert Date to Binary/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertDateToBinary(self, date: str) -> str: 3 | numbers = date.split("-") 4 | numbers = [int(n) for n in numbers] 5 | output_list = [] 6 | 7 | for n in numbers: 8 | binary_list = [] 9 | 10 | while n > 0: 11 | remainder = n % 2 12 | binary_list.append(str(remainder)) 13 | n = n // 2 14 | 15 | output_list.append("".join(binary_list[::-1])) 16 | 17 | return "-".join(output_list) 18 | -------------------------------------------------------------------------------- /3289. The Two Sneaky Numbers of Digitville/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSneakyNumbers(self, nums: List[int]) -> List[int]: 3 | items = set() 4 | for num in nums: 5 | if nums.count(num) == 2: 6 | items.add(num) 7 | 8 | return list(items) 9 | -------------------------------------------------------------------------------- /3294. Convert Doubly Linked List to Array II/main.py: -------------------------------------------------------------------------------- 1 | """ 2 | # Definition for a Node. 3 | class Node: 4 | def __init__(self, val, prev=None, next=None): 5 | self.val = val 6 | self.prev = prev 7 | self.next = next 8 | """ 9 | 10 | 11 | class Solution: 12 | def toArray(self, node: 'Optional[Node]') -> List[int]: 13 | values = [] 14 | current_node = node 15 | while current_node is not None: 16 | print(current_node.val) 17 | if current_node.prev is None: 18 | break 19 | else: 20 | current_node = current_node.prev 21 | 22 | while current_node is not None: 23 | values.append(current_node.val) 24 | current_node = current_node.next 25 | 26 | return values 27 | -------------------------------------------------------------------------------- /3340. Check Balanced String (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isBalanced(self, num: str) -> bool: 3 | return sum([int(char) for char in num[0::2]]) == sum([int(char) for char in num[1::2]]) -------------------------------------------------------------------------------- /3345. Smallest Divisible Digit Product I (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestNumber(self, n: int, t: int) -> int: 3 | not_found = True 4 | while not_found: 5 | num = n 6 | product = 1 7 | for n in str(n): 8 | product *= int(n) 9 | 10 | if product % t == 0: 11 | return num 12 | else: 13 | num += 1 14 | n = num 15 | -------------------------------------------------------------------------------- /3368. First Letter Capitalization (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def process_text(user_content: pd.DataFrame) -> pd.DataFrame: 5 | original = [] 6 | converted = [] 7 | ids = [] 8 | for index, row in user_content.iterrows(): 9 | ids.append(row['content_id']) 10 | new = row['content_text'] 11 | original.append(new) 12 | new = " ".join([n.title() for n in new.split(" ")]) 13 | converted.append(new) 14 | 15 | return pd.DataFrame({'content_id': ids, "original_text": original, 'converted_text': converted}) 16 | -------------------------------------------------------------------------------- /338. Counting Bits (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def countBits(self, n): 3 | result = [] 4 | 5 | for i in range(0,n+1): 6 | if i == 0: 7 | result.append(0) 8 | else: 9 | count = 0 10 | while i != 0: 11 | remainder = i % 2 12 | i = i//2 13 | if remainder == 1: 14 | count +=1 15 | result.append(count) 16 | return result 17 | 18 | -------------------------------------------------------------------------------- /342. Power of Four (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfFour(self, n: int) -> bool: 3 | if n <= 0: 4 | return False 5 | else: 6 | 7 | while n != 1: 8 | remainder = n % 4 9 | n = n/4 10 | 11 | if remainder != 0: 12 | return False 13 | return True 14 | 15 | 16 | -------------------------------------------------------------------------------- /344. Reverse String/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s: List[str]) -> None: 3 | length = len(s) 4 | 5 | count = length // 2 6 | 7 | for i in range(count): 8 | first = s[i] 9 | last = s[length - 1 - i] 10 | s[i], s[length - 1 - i] = last, first 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /346. Moving Average from Data Stream/main.py: -------------------------------------------------------------------------------- 1 | class MovingAverage: 2 | 3 | def __init__(self, size: int): 4 | self.size = size 5 | self.list = [] 6 | 7 | def isFull(self): 8 | if len(self.list) == self.size: 9 | return True 10 | return False 11 | 12 | def next(self, val: int) -> float: 13 | if self.isFull(): 14 | self.list.pop(0) 15 | self.list.append(val) 16 | return sum(self.list) / self.size 17 | else: 18 | self.list.append(val) 19 | return sum(self.list) / (len(self.list)) -------------------------------------------------------------------------------- /347. Top K Frequent Elements/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def topKFrequent(self, nums: List[int], k: int) -> List[int]: 3 | numbers = {} 4 | for n in nums: 5 | if n not in numbers: 6 | numbers[n] = 1 7 | else: 8 | numbers[n] += 1 9 | print(numbers) 10 | result = sorted(numbers.keys(), key=lambda x: -numbers[x]) 11 | 12 | return result[0:k] 13 | 14 | -------------------------------------------------------------------------------- /349. Intersection of Two Arrays (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | output = set() 4 | for item in nums1: 5 | if item in nums2: 6 | output.add(item) 7 | return list(output) -------------------------------------------------------------------------------- /350. Intersection of Two Arrays II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: 3 | dict1 = {} 4 | dict2 = {} 5 | 6 | for num in nums1: 7 | if num not in dict1: 8 | dict1[num] = 1 9 | else: 10 | dict1[num] += 1 11 | 12 | for num in nums2: 13 | if num not in dict2: 14 | dict2[num] = 1 15 | else: 16 | dict2[num] += 1 17 | possible = [] 18 | for num in nums1: 19 | if num in nums2: 20 | if num not in possible: 21 | count1 = dict1[num] 22 | count2 = dict2[num] 23 | for i in range(min(count1, count2)): 24 | possible.append(num) 25 | return possible 26 | 27 | 28 | -------------------------------------------------------------------------------- /3512. Minimum Operations to Make Array Sum Divisible by K (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minOperations(self, nums: List[int], k: int) -> int: 3 | return sum(nums)%k -------------------------------------------------------------------------------- /3516. Find Closest Person (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findClosest(self, x: int, y: int, z: int) -> int: 3 | x_amount = abs(z-x) 4 | y_amount = abs(z-y) 5 | if x_amount == y_amount: 6 | return 0 7 | elif x_amount < y_amount: 8 | return 1 9 | else: 10 | return 2 -------------------------------------------------------------------------------- /367. Valid Perfect Square/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPerfectSquare(self, num: int) -> bool: 3 | found = False 4 | count = 1 5 | while not found: 6 | 7 | square = count ** 2 8 | 9 | if square == num: 10 | return True 11 | elif square < num: 12 | count += 1 13 | else: 14 | return False 15 | 16 | 17 | -------------------------------------------------------------------------------- /369. Plus One Linked List/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def plusOne(self, head: ListNode) -> ListNode: 8 | numbers = [] 9 | node = head 10 | 11 | while node is not None: 12 | numbers.append(str(node.val)) 13 | node = node.next 14 | 15 | number = int(str("".join(numbers))) + 1 16 | number = str(number) 17 | print(number) 18 | new_list = [int(n) for n in list(number)] 19 | 20 | new_head = ListNode(0) 21 | 22 | node = new_head 23 | 24 | for i in range(len(new_list)): 25 | new_node = ListNode(new_list[i]) 26 | node.next = new_node 27 | node = node.next 28 | 29 | return new_head.next 30 | -------------------------------------------------------------------------------- /374. Guess Number Higher or Lower (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # The guess API is already defined for you. 2 | # @param num, your guess 3 | # @return -1 if num is higher than the picked number 4 | # 1 if num is lower than the picked number 5 | # otherwise return 0 6 | # def guess(num: int) -> int: 7 | 8 | class Solution: 9 | def guessNumber(self, n: int) -> int: 10 | left = 1 11 | right = n 12 | 13 | while left <= right: 14 | middle = (left+right)//2 15 | x = guess(middle) 16 | if x == 0: 17 | return middle 18 | elif x == 1: 19 | left = middle + 1 20 | else: 21 | right = middle - 1 -------------------------------------------------------------------------------- /379. Design Phone Directory/main.py: -------------------------------------------------------------------------------- 1 | class PhoneDirectory: 2 | 3 | def __init__(self, maxNumbers: int): 4 | self.nums = [-1 for n in range(maxNumbers)] 5 | 6 | def get(self) -> int: 7 | for i in range(len(self.nums)): 8 | num = self.nums[i] 9 | if num == -1: 10 | self.nums[i] = i 11 | return i 12 | return -1 13 | 14 | def check(self, number: int) -> bool: 15 | if self.nums[number] == -1: 16 | return True 17 | else: 18 | return False 19 | 20 | def release(self, number: int) -> None: 21 | self.nums[number] = -1 22 | return None 23 | 24 | # Your PhoneDirectory object will be instantiated and called as such: 25 | # obj = PhoneDirectory(maxNumbers) 26 | # param_1 = obj.get() 27 | # param_2 = obj.check(number) 28 | # obj.release(number) -------------------------------------------------------------------------------- /384. Shuffle an Array/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def __init__(self, nums: List[int]): 4 | self.original = nums 5 | self.nums = nums 6 | 7 | def reset(self) -> List[int]: 8 | self.nums = self.original 9 | return self.nums 10 | 11 | def shuffle(self) -> List[int]: 12 | # shuffled = nums: shuffled is just a new name for nums, so they both refer to the same list. 13 | #shuffled = nums[:]: shuffled is a new list that is a shallow copy of nums, so changes to one list won't affect the other. 14 | shuffled = self.nums[:] 15 | for i in range(len(shuffled) - 1, 0, -1): 16 | j = random.randint(0, i) 17 | shuffled[i], shuffled[j] = shuffled[j], shuffled[i] 18 | return shuffled 19 | 20 | 21 | # Your Solution object will be instantiated and called as such: 22 | # obj = Solution(nums) 23 | # param_1 = obj.reset() 24 | # param_2 = obj.shuffle() -------------------------------------------------------------------------------- /387. First Unique Character in a String/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstUniqChar(self, s: str) -> int: 3 | original_characters = list(s) 4 | characters = [] 5 | for char in original_characters: 6 | if char not in characters: 7 | characters.append(char) 8 | 9 | for char in characters: 10 | if s.count(char) == 1: 11 | return original_characters.index(char) 12 | 13 | return -1 14 | -------------------------------------------------------------------------------- /398. Random Pick Index (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import random 2 | class Solution: 3 | 4 | def __init__(self, nums: List[int]): 5 | self.dictionary = {} 6 | for i in range(len(nums)): 7 | num = nums[i] 8 | if num not in self.dictionary: 9 | self.dictionary[num] = [i] 10 | else: 11 | self.dictionary[num].append(i) 12 | 13 | def pick(self, target: int) -> int: 14 | indices = self.dictionary[target] 15 | return random.choice(indices) 16 | 17 | 18 | # Your Solution object will be instantiated and called as such: 19 | # obj = Solution(nums) 20 | # param_1 = obj.pick(target) -------------------------------------------------------------------------------- /412. Fizz Buzz (Leetcode)/main.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector fizzBuzz(int n) { 4 | vector result; 5 | for (int i=1;i<=n;i++){ 6 | if (i % 15 == 0){ 7 | result.push_back("FizzBuzz"); 8 | } else if (i % 5 == 0){ 9 | result.push_back("Buzz"); 10 | } else if (i % 3 == 0){ 11 | result.push_back("Fizz"); 12 | } else{ 13 | result.push_back(to_string(i)); 14 | } 15 | } 16 | return result; 17 | } 18 | }; -------------------------------------------------------------------------------- /412. Fizz Buzz (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fizzBuzz(self, n: int) -> List[str]: 3 | list = [] 4 | for i in range(1,n+1): 5 | if i % 15 == 0: 6 | list.append("FizzBuzz") 7 | continue 8 | elif i % 5 == 0: 9 | list.append("Buzz") 10 | continue 11 | elif i % 3 == 0: 12 | list.append("Fizz") 13 | continue 14 | else: 15 | list.append(str(i)) 16 | return list 17 | 18 | -------------------------------------------------------------------------------- /414. Third Maximum Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def thirdMax(self, nums): 3 | nums = list(set(nums)) 4 | if len(nums) < 3: 5 | return max(nums) 6 | else: 7 | nums.sort() 8 | return nums[-3] 9 | -------------------------------------------------------------------------------- /415.Add Strings (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addStrings(self, num1: str, num2: str) -> str: 3 | number1 = 0 4 | number2 = 0 5 | num1 = num1[::-1] 6 | num2 = num2[::-1] 7 | for i in range(len(num1)): 8 | number1 += int(num1[i]) * pow(10,i) 9 | print(number1) 10 | for i in range(len(num2)): 11 | number2 += int(num2[i]) * pow(10,i) 12 | print(number2) 13 | return number1 + number2 14 | 15 | 16 | 17 | sol = Solution() 18 | sol.addStrings("11","123") 19 | -------------------------------------------------------------------------------- /434. Number of Segments in a String/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSegments(self, s: str) -> int: 3 | return len(s.split()) 4 | 5 | -------------------------------------------------------------------------------- /448. Find All Numbers Disappeared in an Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDisappearedNumbers(self, nums: List[int]) -> List[int]: 3 | dictionary = {i: 0 for i in range(1, len(nums) + 1)} 4 | for num in nums: 5 | dictionary[num] += 1 6 | 7 | nums = [n for n in dictionary if dictionary[n] == 0] 8 | 9 | return nums 10 | -------------------------------------------------------------------------------- /451. Sort Characters By Frequency/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def frequencySort(self, s: str) -> str: 3 | dictionary = {} 4 | for char in s: 5 | if char not in dictionary: 6 | dictionary[char] = 1 7 | else: 8 | dictionary[char] += 1 9 | 10 | result = sorted(dictionary.keys(), key=lambda x: -dictionary[x]) 11 | output = "" 12 | for char in result: 13 | amount = dictionary[char] 14 | if amount > 1: 15 | for i in range(amount): 16 | output += char 17 | else: 18 | output += char 19 | 20 | return output 21 | -------------------------------------------------------------------------------- /453. Minimum Moves to Equal Array Elements (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves(self, nums: List[int]) -> int: 3 | if len(nums) == 1: 4 | return 0 5 | total = 0 6 | greatest = max(nums) 7 | smallest = min(nums) 8 | total += greatest-smallest 9 | nums.remove(greatest) 10 | nums.remove(smallest) 11 | for num in nums: 12 | total += num-smallest 13 | return total -------------------------------------------------------------------------------- /459. Repeated Substring Pattern (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def repeatedSubstringPattern(self, s): 3 | length = len(s) 4 | for i in range(len(s) // 2): 5 | string = s[:i + 1] 6 | string_length = i + 1 7 | if string * (length // string_length) == s: 8 | return True 9 | 10 | return False -------------------------------------------------------------------------------- /462. Minimum Moves to Equal Array Elements II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves2(self, nums: List[int]) -> int: 3 | nums = sorted(nums) 4 | length = len(nums) 5 | if length % 2 == 0: 6 | median = (nums[length // 2] + nums[(length // 2) - 1]) // 2 7 | else: 8 | median = nums[length // 2] 9 | 10 | count = 0 11 | for num in nums: 12 | count += abs(num - median) 13 | return count 14 | -------------------------------------------------------------------------------- /476. Number Complement (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findComplement(self, num: int) -> int: 3 | bits = [] 4 | while num > 0: 5 | remainder = num % 2 6 | num = num // 2 7 | bits.append(remainder) 8 | total = 0 9 | for i in range(len(bits)): 10 | if bits[i] == 0: 11 | total += pow(2,i) 12 | return total 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /485. Max Consecutive Ones (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def findMaxConsecutiveOnes(self, nums): 3 | greatest = 0 4 | count = 0 5 | for num in nums: 6 | if num == 1: 7 | count +=1 8 | if count > greatest: 9 | greatest = count 10 | else: 11 | count = 0 12 | return greatest -------------------------------------------------------------------------------- /49. Group Anagrams (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def groupAnagrams(self, strs: List[str]) -> List[List[str]]: 3 | 4 | dictionary = {} 5 | for st in strs: 6 | result = [] 7 | for char in st: 8 | result.append(ord(char)) 9 | result = tuple(sorted(result)) 10 | if result not in dictionary: 11 | dictionary[result] = [st] 12 | else: 13 | dictionary[result].append(st) 14 | 15 | print(dictionary) 16 | if strs == ['']: 17 | return [['']] 18 | else: 19 | output = [] 20 | for key, value in dictionary.items(): 21 | output.append(value) 22 | return output -------------------------------------------------------------------------------- /50. Pow(x, n) (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def myPow(self, x: float, n: int) -> float: 3 | if n == 0: 4 | return 1 5 | elif n < 0: 6 | return 1 / self.myPow(x, -n) 7 | else: 8 | half_pow = self.myPow(x, n // 2) 9 | if n % 2 == 0: 10 | return half_pow * half_pow 11 | else: 12 | return half_pow * half_pow * x -------------------------------------------------------------------------------- /501. Find Mode in Binary Search Tree (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def findMode(self, root: Optional[TreeNode]) -> List[int]: 9 | self.frequency = {} 10 | self.traverse(root) 11 | greatest = max(self.frequency.values()) 12 | return [n for n in self.frequency if self.frequency[n] == greatest] 13 | 14 | def traverse(self, root): 15 | if not root: 16 | return 17 | 18 | if root.val in self.frequency: 19 | self.frequency[root.val] += 1 20 | else: 21 | self.frequency[root.val] = 1 22 | 23 | self.traverse(root.left) 24 | self.traverse(root.right) 25 | 26 | -------------------------------------------------------------------------------- /504. Base 7 (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def convertToBase7(self, num): 3 | bits = [] 4 | if num == 0: 5 | return "0" 6 | if num < 0: 7 | num = -num 8 | while num != 0: 9 | remainder = num %7 10 | num = num//7 11 | 12 | bits.append(str(remainder)) 13 | bits.reverse() 14 | return "-" + "".join(bits) 15 | else: 16 | while num != 0: 17 | remainder = num % 7 18 | num = num // 7 19 | 20 | bits.append(str(remainder)) 21 | bits.reverse() 22 | return "".join(bits) 23 | -------------------------------------------------------------------------------- /507. Perfect Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkPerfectNumber(self, num: int) -> bool: 3 | if num == 1: 4 | return False 5 | factors = [1] 6 | for i in range(2,int(num**0.5)+1): 7 | if num % i == 0: 8 | factors.append(i) 9 | factors.append(num//i) 10 | 11 | return sum(factors) == num -------------------------------------------------------------------------------- /509. Fibonacci Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fib(self, n: int) -> int: 3 | if n == 0: 4 | return 0 5 | elif n < 3: 6 | return 1 7 | else: 8 | a = 1 9 | b = 1 10 | for i in range(n-2): 11 | c = b 12 | b = a+b 13 | a = c 14 | return b 15 | 16 | s 17 | -------------------------------------------------------------------------------- /511. Game Play Analysis I/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def game_analysis(activity: pd.DataFrame) -> pd.DataFrame: 5 | player_dictionary = {} 6 | 7 | for index, row in activity.iterrows(): 8 | player = row["player_id"] 9 | if player not in player_dictionary: 10 | player_dictionary[player] = row["event_date"] 11 | else: 12 | current_date = pd.Timestamp(player_dictionary[player]) 13 | new_date = pd.Timestamp(row["event_date"]) 14 | if new_date < current_date: 15 | player_dictionary[player] = row["event_date"] 16 | 17 | ids = [] 18 | logins = [] 19 | 20 | for d in player_dictionary: 21 | ids.append(d) 22 | logins.append(player_dictionary[d]) 23 | 24 | return pd.DataFrame({"player_id": ids, "first_login": logins}) 25 | -------------------------------------------------------------------------------- /520. Detect Capital/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def detectCapitalUse(self, word: str) -> bool: 3 | length = len(word) 4 | # check if all are upper-case 5 | count_total = sum([1 for char in word if char.isupper()]) 6 | if count_total == length: 7 | return True 8 | 9 | # check if none are upper case 10 | count = sum([1 for char in word if char.islower()]) 11 | if count == length: 12 | return True 13 | 14 | # check if only the first letter is capital 15 | if count_total == 1 and word[0].isupper(): 16 | return True 17 | 18 | return False 19 | 20 | -------------------------------------------------------------------------------- /530. Minimum Absolute Difference in BST (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def getMinimumDifference(self, root: Optional[TreeNode]) -> int: 9 | self.values = [] 10 | 11 | self.traverse(root) 12 | 13 | output = [] 14 | result = sorted(self.values) 15 | for i in range(1, len(result)): 16 | output.append(abs(result[i] - result[i - 1])) 17 | 18 | return min(output) 19 | 20 | def traverse(self, root): 21 | if not root: 22 | return 23 | 24 | self.values.append(root.val) 25 | 26 | self.traverse(root.left) 27 | self.traverse(root.right) -------------------------------------------------------------------------------- /557. Reverse Words in a String III (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | return " ".join([word[::-1] for word in s.split()]) 4 | -------------------------------------------------------------------------------- /58. Length of Last Word (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s: str) -> int: 3 | length = len(s.strip().split()[-1]) 4 | return length 5 | -------------------------------------------------------------------------------- /584. Find Customer Referee (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def find_customer_referee(customer: pd.DataFrame) -> pd.DataFrame: 4 | names = [] 5 | for (index,row) in customer.iterrows(): 6 | if pd.isnull(row["referee_id"]) or row["referee_id"] != 2: 7 | names.append(row["name"]) 8 | return pd.DataFrame({"name":names}) -------------------------------------------------------------------------------- /586. Customer Placing the Largest Number of Orders/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | 4 | def largest_orders(orders: pd.DataFrame) -> pd.DataFrame: 5 | dictionary = {} 6 | for index, row in orders.iterrows(): 7 | number = row["customer_number"] 8 | if number not in dictionary: 9 | dictionary[number] = 1 10 | else: 11 | dictionary[number] += 1 12 | 13 | greatest_count = 0 14 | number = 0 15 | print(dictionary) 16 | for n in dictionary: 17 | if dictionary[n] > greatest_count: 18 | greatest_count = dictionary[n] 19 | number = n 20 | 21 | if number == 0: 22 | return pd.DataFrame({"customer_number": []}) 23 | return pd.DataFrame({"customer_number": [number]}) 24 | -------------------------------------------------------------------------------- /595. Big Countries (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def big_countries(world: pd.DataFrame) -> pd.DataFrame: 4 | name = [] 5 | population = [] 6 | area = [] 7 | for (index,row) in world.iterrows(): 8 | if int(row["area"]) >= 3000000 or int(row["population"]) >= 25000000: 9 | name.append(row["name"]) 10 | population.append(row["population"]) 11 | area.append(row["area"]) 12 | return pd.DataFrame({"name":name,"population":population,"area":area}) -------------------------------------------------------------------------------- /596. Classes More Than 5 Students (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def find_classes(courses: pd.DataFrame) -> pd.DataFrame: 4 | count = {} 5 | for (index,row) in courses.iterrows(): 6 | course = row["class"] 7 | if course not in count: 8 | count[course] = 1 9 | else: 10 | count[course] += 1 11 | classes = [] 12 | for key,value in count.items(): 13 | if value >= 5: 14 | classes.append(key) 15 | return pd.DataFrame({"class":classes}) -------------------------------------------------------------------------------- /599. Minimum Index Sum of Two Lists (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]: 3 | 4 | common = [r for r in list1 if r in list2] 5 | 6 | item = common[0] 7 | items = [item] 8 | index = list1.index(item) + list2.index(item) 9 | 10 | for rating in common[1:]: 11 | new_index = list1.index(rating) + list2.index(rating) 12 | if new_index < index: 13 | items = [] 14 | items.append(rating) 15 | index = new_index 16 | elif new_index == index: 17 | items.append(rating) 18 | 19 | return items 20 | -------------------------------------------------------------------------------- /6. Zigzag Conversion (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convert(self, s: str, numRows: int) -> str: 3 | if numRows == 1: 4 | return s 5 | arrays = [] 6 | for i in range(numRows): 7 | arrays.append([]) 8 | down = True 9 | index = 0 10 | 11 | for char in s: 12 | arrays[index].append(char) 13 | 14 | 15 | if index == numRows-1: 16 | down = False 17 | 18 | if index == 0: 19 | down = True 20 | 21 | if down: 22 | index += 1 23 | else: 24 | index -= 1 25 | output_string = "" 26 | for arr in arrays: 27 | output_string += "".join(arr) 28 | return output_string 29 | 30 | 31 | 32 | sol = Solution() 33 | sol.convert('PAYPALISHIRING',4) -------------------------------------------------------------------------------- /60. Permutation Sequence (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getPermutation(self, n: int, k: int) -> str: 3 | total = factorial(n) 4 | array = [i for i in range(1,n+1)] 5 | divisor = total//n 6 | output = "" 7 | k -= 1 8 | while array: 9 | index = k//divisor 10 | digit = array.pop(index) 11 | output += f"{digit}" 12 | 13 | k = k % divisor 14 | n = n-1 15 | if array: 16 | divisor = factorial(n)//n 17 | 18 | return output 19 | 20 | def factorial(n)->int: 21 | if n == 0: 22 | return 1 23 | elif n > 0: 24 | return n * factorial(n-1) -------------------------------------------------------------------------------- /607. Sales Person (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def sales_person(sales_person: pd.DataFrame, company: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame: 4 | red_id = "" 5 | for (index,row) in company.iterrows(): 6 | if row["name"] == "RED": 7 | red_id = row["com_id"] 8 | 9 | ids = [] 10 | for (index,row) in orders.iterrows(): 11 | if row["com_id"] == red_id: 12 | ids.append(row["sales_id"]) 13 | 14 | names = [] 15 | for (index,row) in sales_person.iterrows(): 16 | if row["sales_id"] in ids: 17 | continue 18 | else: 19 | names.append(row["name"]) 20 | return pd.DataFrame({"name":names}) 21 | -------------------------------------------------------------------------------- /627. Swap Salary (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | 3 | def swap_salary(salary: pd.DataFrame) -> pd.DataFrame: 4 | ids = [] 5 | names = [] 6 | genders = [] 7 | salaries = [] 8 | for (index,row) in salary.iterrows(): 9 | gender = row["sex"] 10 | ids.append(row["id"]) 11 | names.append(row["name"]) 12 | salaries.append(row["salary"]) 13 | if gender == "m": 14 | genders.append("f") 15 | else: 16 | genders.append("m") 17 | return pd.DataFrame({'id':ids,"name":names,"sex":genders,"salary":salaries}) 18 | -------------------------------------------------------------------------------- /66. Plus One (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def plusOne(self, digits: List[int]) -> List[int]: 3 | digits = [str(d) for d in digits] 4 | number = int("".join(digits)) 5 | number = str(number + 1) 6 | return [int(n) for n in list(number)] -------------------------------------------------------------------------------- /67. Add Binary (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addBinary(self, a: str, b: str) : 3 | total = 0 4 | a_char = "".join(list(a)[::-1]) 5 | b_char = "".join(list(b)[::-1]) 6 | for i in range(len(a_char)): 7 | if a_char[i] == "1": 8 | total += pow(2,i) 9 | for i in range(len(b_char)): 10 | if b_char[i] == "1": 11 | total += pow(2,i) 12 | 13 | if total == 0: 14 | return "0" 15 | else: 16 | result = [] 17 | while total >0: 18 | remainder = total % 2 19 | total = total //2 20 | 21 | result.append(str(remainder)) 22 | number = "".join(result[::-1]) 23 | return number 24 | -------------------------------------------------------------------------------- /682. Baseball Game (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def calPoints(self, operations): 3 | numbers = "0123456789" 4 | record = [] 5 | 6 | for op in operations: 7 | if op == "+": 8 | one = int(record[-1]) 9 | two = int(record[-2]) 10 | record.append(one+two) 11 | elif op == "D": 12 | one = int(record[-1]) 13 | record.append(one*2) 14 | elif op == "C": 15 | record.pop() 16 | else: 17 | record.append(int(op)) 18 | return sum(record) 19 | -------------------------------------------------------------------------------- /7. Reverse Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverse(self, x: int) -> int: 3 | lower_limit = pow(-2,31) 4 | upper_limit = pow(2,31)-1 5 | if x <0: 6 | number = -1*int("".join(list(str(x))[1::][::-1])) 7 | if number < lower_limit: 8 | return 0 9 | else: 10 | return number 11 | else: 12 | number = int("".join(list(str(x))[::-1])) 13 | if number > upper_limit: 14 | return 0 15 | else: 16 | return number 17 | 18 | 19 | s = Solution() 20 | x = s.reverse(x=123) 21 | print(x) -------------------------------------------------------------------------------- /70. Climbing Stairs (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def climbStairs(self, n): 3 | if n == 1: 4 | return 1 5 | if n == 2: 6 | return 2 7 | 8 | 9 | a = 1 10 | b = 2 11 | for i in range(2,n): 12 | c = a+b 13 | a = b 14 | b = c 15 | return b 16 | -------------------------------------------------------------------------------- /705. Design HashSet/main.py: -------------------------------------------------------------------------------- 1 | class MyHashSet: 2 | 3 | def __init__(self): 4 | self.hashset = [] 5 | 6 | def add(self, key: int) -> None: 7 | if self.contains(key) == False: 8 | self.hashset.append(key) 9 | 10 | def remove(self, key: int) -> None: 11 | if self.contains(key): 12 | self.hashset.remove(key) 13 | 14 | def contains(self, key: int) -> bool: 15 | if key in self.hashset: 16 | return True 17 | return False 18 | 19 | # Your MyHashSet object will be instantiated and called as such: 20 | # obj = MyHashSet() 21 | # obj.add(key) 22 | # obj.remove(key) 23 | # param_3 = obj.contains(key) -------------------------------------------------------------------------------- /706. Design HashMap/main.py: -------------------------------------------------------------------------------- 1 | class MyHashMap: 2 | 3 | def __init__(self): 4 | self.hm = {} 5 | 6 | def put(self, key: int, value: int) -> None: 7 | self.hm[key] = value 8 | 9 | def get(self, key: int) -> int: 10 | if key in self.hm: 11 | return self.hm[key] 12 | else: 13 | return -1 14 | 15 | def remove(self, key: int) -> None: 16 | if key in self.hm: 17 | return self.hm.pop(key) 18 | 19 | # Your MyHashMap object will be instantiated and called as such: 20 | # obj = MyHashMap() 21 | # obj.put(key,value) 22 | # param_2 = obj.get(key) 23 | # obj.remove(key) -------------------------------------------------------------------------------- /709. To Lower Case (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toLowerCase(self, s: str) -> str: 3 | return s.lower() -------------------------------------------------------------------------------- /724. Find Pivot Index/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pivotIndex(self, nums: List[int]) -> int: 3 | for i in range(len(nums)): 4 | if i == 0: 5 | left_sum = 0 6 | right_sum = sum(nums[i + 1:]) 7 | if left_sum == right_sum: 8 | return 0 9 | elif i == len(nums) - 1: 10 | left_sum = sum(nums[0:i]) 11 | right_sum = 0 12 | if left_sum == right_sum: 13 | return i 14 | else: 15 | left_sum = sum(nums[0:i]) 16 | right_sum = sum(nums[i + 1:]) 17 | if left_sum == right_sum: 18 | return i 19 | return -1 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /728. Self Dividing Numbers (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def selfDividingNumbers(self, left, right): 3 | output = [] 4 | for i in range(left,right+1): 5 | digits = [int(digit) for digit in list(str(i))] 6 | if 0 not in digits: 7 | if len(list(str(i))) == len([digit for digit in digits if i % digit == 0]): 8 | output.append(i) 9 | return output 10 | -------------------------------------------------------------------------------- /73. Set Matrix Zeroes (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def setZeroes(self, matrix: List[List[int]]) -> None: 3 | """ 4 | Do not return anything, modify matrix in-place instead. 5 | """ 6 | rows = [] 7 | columns = [] 8 | for i in range(len(matrix)): 9 | for j in range(len(matrix[i])): 10 | if matrix[i][j] == 0: 11 | if i not in rows: 12 | rows.append(i) 13 | if j not in columns: 14 | columns.append(j) 15 | 16 | for i in range(len(matrix)): 17 | if i in rows: 18 | for j in range(len(matrix[i])): 19 | matrix[i][j] = 0 20 | 21 | for row in matrix: 22 | for col in columns: 23 | row[col] = 0 24 | -------------------------------------------------------------------------------- /74. Search a 2D Matrix (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def searchMatrix(self, matrix, target): 3 | for i in range(len(matrix)): 4 | for j in range(len(matrix[i])): 5 | if matrix[i][j] == target: 6 | return True 7 | return False 8 | 9 | -------------------------------------------------------------------------------- /747. Largest Number At Least Twice of Others (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dominantIndex(self, nums: List[int]) -> int: 3 | greatest = max(nums) 4 | index = nums.index(greatest) 5 | nums.remove(greatest) 6 | second = max(nums) 7 | if greatest >= 2*second: 8 | return index 9 | return -1 -------------------------------------------------------------------------------- /75. Sort Colors/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortColors(self, nums: List[int]) -> None: 3 | zero_range = range(0, nums.count(0)) 4 | index = nums.count(0) 5 | one_range = range(index, nums.count(1) + index) 6 | index += nums.count(1) 7 | two_range = range(index, nums.count(2) + index) 8 | for i in zero_range: 9 | nums[i] = 0 10 | 11 | for j in one_range: 12 | nums[j] = 1 13 | 14 | for k in two_range: 15 | nums[k] = 2 -------------------------------------------------------------------------------- /771. Jewels and Stones (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def numJewelsInStones(self, jewels, stones): 3 | jewel_items = list(jewels) 4 | stone_list = list(stones) 5 | count = 0 6 | for item in stone_list: 7 | if item in jewel_items: 8 | count += 1 9 | return count 10 | 11 | -------------------------------------------------------------------------------- /783. Minimum Distance Between BST Nodes (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def minDiffInBST(self, root: Optional[TreeNode]) -> int: 9 | self.values = [] 10 | 11 | self.traverse(root) 12 | 13 | output = [] 14 | result = sorted(self.values) 15 | for i in range(1, len(result)): 16 | output.append(abs(result[i] - result[i - 1])) 17 | 18 | return min(output) 19 | 20 | def traverse(self, root): 21 | if not root: 22 | return 23 | 24 | self.values.append(root.val) 25 | 26 | self.traverse(root.left) 27 | self.traverse(root.right) -------------------------------------------------------------------------------- /82. Remove Duplicates from Sorted List II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | values = [] 9 | node = head 10 | while node is not None: 11 | values.append(node.val) 12 | node = node.next 13 | 14 | values = [v for v in values if values.count(v) == 1] 15 | values = sorted(values) 16 | 17 | head = ListNode(0) 18 | index = 0 19 | 20 | node = head 21 | 22 | while index != len(values): 23 | new_node = ListNode(values[index]) 24 | node.next = new_node 25 | index += 1 26 | node = node.next 27 | 28 | return head.next 29 | 30 | -------------------------------------------------------------------------------- /832. Flipping an Image (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def flipAndInvertImage(self, image: List[List[int]]) -> List[List[int]]: 3 | for i in range(len(image)): 4 | image[i] = image[i][::-1] 5 | 6 | for i in range(len(image)): 7 | for j in range(len(image[i])): 8 | if image[i][j] == 0: 9 | image[i][j] = 1 10 | else: 11 | image[i][j] = 0 12 | return image -------------------------------------------------------------------------------- /844. Backspace String Compare/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def backspaceCompare(self, s: str, t: str) -> bool: 3 | result = [] 4 | 5 | for char in s: 6 | if char == "#": 7 | if result: 8 | result.pop() 9 | else: 10 | result.append(char) 11 | 12 | output = [] 13 | for char in t: 14 | if char == "#": 15 | if output: 16 | output.pop() 17 | else: 18 | output.append(char) 19 | 20 | return result == output 21 | -------------------------------------------------------------------------------- /876. Middle of the Linked List (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, val=0, next=None): 4 | # self.val = val 5 | # self.next = next 6 | class Solution: 7 | def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: 8 | length = 0 9 | 10 | node = head 11 | while node is not None: 12 | length += 1 13 | node = node.next 14 | 15 | position = (length // 2) + 1 16 | 17 | if length == 1: 18 | return head 19 | 20 | node = head 21 | count = 1 22 | while node is not None: 23 | if position - 1 == count: 24 | return node.next 25 | else: 26 | count += 1 27 | node = node.next -------------------------------------------------------------------------------- /884. Uncommon Words from Two Sentences (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uncommonFromSentences(self, s1: str, s2: str) -> list[str]: 3 | word_count = {} 4 | s1 = s1.split() 5 | s2 = s2.split() 6 | words = s1+s2 7 | for word in words: 8 | if word not in word_count: 9 | word_count[word] = 1 10 | else: 11 | word_count[word] += 1 12 | chosen = [] 13 | for key in word_count: 14 | if word_count[key] == 1: 15 | chosen.append(key) 16 | return chosen 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | sol = Solution() 25 | sol.uncommonFromSentences("apple apple","banana") -------------------------------------------------------------------------------- /896. Monotonic Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isMonotonic(self, nums: List[int]) -> bool: 3 | if nums == sorted(nums) or nums == sorted(nums,reverse= True): 4 | return True 5 | else: 6 | return False -------------------------------------------------------------------------------- /9. Palindrome Number (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x: int) -> bool: 3 | if x < 0: 4 | return False 5 | string_form = str(x) 6 | new_string = "" 7 | for i in range(len(string_form) - 1, -1, -1): 8 | new_string += string_form[i] 9 | if string_form == new_string: 10 | return True 11 | else: 12 | return False 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /905. Sort Array By Parity (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortArrayByParity(self, nums: List[int]) -> List[int]: 3 | even = [n for n in nums if n %2 == 0] 4 | odd = [n for n in nums if n %2 == 1] 5 | return even+odd -------------------------------------------------------------------------------- /922. Sort Array By Parity II (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortArrayByParityII(self, nums: List[int]) -> List[int]: 3 | result = [] 4 | even = [n for n in nums if n %2 == 0] 5 | odd = [n for n in nums if n %2 == 1] 6 | for i in range(len(odd)): 7 | n1 = even[0] 8 | n2 = odd[0] 9 | result.append(n1) 10 | result.append(n2) 11 | even.pop(0) 12 | odd.pop(0) 13 | return result 14 | -------------------------------------------------------------------------------- /933. Number of Recent Calls/main.py: -------------------------------------------------------------------------------- 1 | class RecentCounter: 2 | 3 | def __init__(self): 4 | self.requests = [] 5 | 6 | def ping(self, t: int) -> int: 7 | self.requests.append(t) 8 | lower_bound = t - 3000 9 | upper_bound = t 10 | result = 0 11 | for r in self.requests: 12 | if lower_bound <= r <= upper_bound: 13 | result += 1 14 | 15 | return result 16 | 17 | # Your RecentCounter object will be instantiated and called as such: 18 | # obj = RecentCounter() 19 | # param_1 = obj.ping(t) -------------------------------------------------------------------------------- /938. Range Sum of BST (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode: 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution: 8 | def rangeSumBST(self, root: Optional[TreeNode], low: int, high: int) -> int: 9 | self.total = 0 10 | self.traverse(root, low, high) 11 | return self.total 12 | 13 | def traverse(self, root, low, high): 14 | if not root: 15 | return 16 | 17 | if low <= root.val <= high: 18 | self.total += root.val 19 | 20 | self.traverse(root.left, low, high) 21 | self.traverse(root.right, low, high) -------------------------------------------------------------------------------- /94. Binary Tree Inorder Traversal (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode(object): 3 | # def __init__(self, val=0, left=None, right=None): 4 | # self.val = val 5 | # self.left = left 6 | # self.right = right 7 | class Solution(object): 8 | def inorderTraversal(self, root): 9 | self.result = [] 10 | 11 | self.inorderTraversal1(root) 12 | return self.result 13 | 14 | def inorderTraversal1(self, root): 15 | if not root: 16 | return None 17 | 18 | self.inorderTraversal1(root.left) 19 | self.result.append(root.val) 20 | self.inorderTraversal1(root.right) 21 | -------------------------------------------------------------------------------- /977. Squares of a Sorted Array (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def sortedSquares(self, nums): 3 | return sorted([pow(num, 2) for num in nums]) 4 | 5 | -------------------------------------------------------------------------------- /989. Add to Array-Form of Integer (Leetcode)/main.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | sys.set_int_max_str_digits(100000) 4 | 5 | 6 | class Solution: 7 | def addToArrayForm(self, num: List[int], k: int) -> List[int]: 8 | new_num = int("".join([str(n) for n in num])) 9 | new_num += k 10 | return [int(n) for n in list(str(new_num))] --------------------------------------------------------------------------------