├── .gitignore ├── 0001. Two Sum ├── Solution1.java ├── solution1.c ├── solution1.cpp ├── solution1.js ├── solution1.py └── solution2.js ├── 0002. Add Two Numbers └── solution1.java ├── 0003. Longest Substring Without Repeating Characters ├── solution1.c ├── solution1.cpp ├── solution1.java └── solution1.js ├── 0005. Longest Palindromic Substring ├── solution1.class └── solution1.java ├── 0007. Reverse Integer ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0008. String to Integer (atoi) └── solution1.java ├── 0009. Palindrome Number └── solution1.cpp ├── 0011. Container With Most Water └── solution1.cpp ├── 0012. Integer to Roman └── solution1.cpp ├── 0013. Roman to Integer └── solution1.cpp ├── 0014. Longest Common Prefix └── Solution1.java ├── 0015. 3Sum └── solution1.cpp ├── 0016. 3Sum Closet └── solution1.cpp ├── 0017. Letter Combinations of a Phone Number ├── solution1.cpp └── solution1.java ├── 0018. 4Sum ├── solution1.cpp ├── solution2.cpp └── solution3.cpp ├── 0019. Remove Nth Node from End of List └── solution1.c ├── 0020. Valid Parentheses └── solution1.cpp ├── 0021. Merge Two Sorted List ├── solution1.cpp └── solution2.cpp ├── 0022. Generate Parentheses └── solution1.cpp ├── 0023. Merge K Sorted Lists ├── solution1.cpp └── solution2.cpp ├── 0024. Swap Nodes in Pairs └── solution1.cpp ├── 0028. Find the Index of the First Occurrence in a String └── solution1.cpp ├── 0029. Divide Two Integers └── solution1.cpp ├── 0033. Search in Rotated Sorted Array └── solution1.cpp ├── 0034. Find First and Last Position of Element in Sorted Array ├── solution1.java ├── solution1.js └── solution1.py ├── 0037. Sudoku Solver └── solution1.cpp ├── 0039. Combination Sum ├── Solution1.java └── solution1.cpp ├── 0040. Combination Sum II └── solution1.cpp ├── 0041. First Missing Positive └── solution1.java ├── 0042. Trapping Rain Water ├── solution1.cpp ├── solution1.java └── solution2.java ├── 0043. Multiply Strings ├── solution1.java ├── solution1.js └── solution1.py ├── 0045. Jump Game II └── solution1.cpp ├── 0046. Permutations ├── solution1.cpp └── solution2.cpp ├── 0047. Permutations II └── solution1.cpp ├── 0048. Rotate Image └── solution1.cpp ├── 0049. Group Anagrams ├── solution1.cpp └── solution2.cpp ├── 0050. Pow(x,n) └── solution1.c ├── 0051. N-Queens ├── solution1.cpp └── solution2.cpp ├── 0053. Maximum Subarray ├── solution1.cpp └── solution1.js ├── 0054. Spiral Matrix └── solution1.cpp ├── 0055. Jump Game └── solution1.cpp ├── 0058 .Length of Last Word ├── solution1.c ├── solution1.cpp ├── solution1.java └── solution2.java ├── 0061. Rotate List └── solution1.cpp ├── 0062. Unique Paths ├── solution1.cpp └── solution2.cpp ├── 0064. Minimum Path Sum └── solution1.cpp ├── 0066. Plus One └── solution1.java ├── 0067. Add Binary └── solution1.cpp ├── 0069. Sqrt(x) └── solution1.c ├── 0070. Climbing Stairs └── solution1.cpp ├── 0073. Set Matrix Zeroes ├── Solution1.java ├── solution1.cpp ├── solution2.cpp └── solution3.cpp ├── 0074. Search a 2D Matrix ├── solution1.cpp └── solution2.cpp ├── 0075. Sort Colors ├── solution1.cpp ├── solution2.cpp ├── solution2.java └── solution3.cpp ├── 0078. Subsets └── solution1.cpp ├── 0079. Word Search └── solution1.java ├── 0081. Search in Rotated Sorted Array II └── solution1.cpp ├── 0083. Remove Duplicates From Sorted List ├── solution1.c └── solution1.cpp ├── 0088. Merge Sorted Array ├── solution1.cpp ├── solution1.java └── solution2.cpp ├── 0090. Subsets II └── solution1.cpp ├── 0092. Reverse Linked List II └── solution1.cpp ├── 0100. Same Tree └── solution1.cpp ├── 0101. Symmetric Tree └── solution1.cpp ├── 0102. Binary Tree Level Order Traversal └── solution1.cpp ├── 0103. Binary Tree Zigzag Level Order Traversal └── solution1.cpp ├── 0104. Maximum Depth of Binary Tree └── solution1.cpp ├── 0110. Balanced Binary Tree └── solution1.c ├── 0118. Pascal's Triangle ├── Solution1.java ├── solution1.cpp └── solution2.cpp ├── 0119. Pascal's Triangle II └── solution1.cpp ├── 0121. Best Time to Buy and Sell Stock └── solution1.c ├── 0125. Valid Palindrome ├── solution1.cpp └── solution1.java ├── 0128. Longest Consecutive Sequence ├── Solution1.java └── Solution2.java ├── 0129. Sum Root to Leaf Numbers └── solution1.cpp ├── 0130. Surrounded Regions └── solution1.cpp ├── 0131. Palindrome Partitioning └── solution1.cpp ├── 0136. Single Number └── solution1.java ├── 0137. Single Number II └── solution1.cpp ├── 0141. Linked List Cycle ├── solution1.cpp └── solution2.cpp ├── 0142. Linked List Cycle II └── solution1.cpp ├── 0144. Binary Tree Preorder Traversal └── solution1.cpp ├── 0145. Binary Tree Postorder Traversal └── solution1.cpp ├── 0148. Sort List ├── solution1.cpp └── solution1.java ├── 0151. Reverse Words in a String └── solution1.java ├── 0160. Intersection of Two Linked Lists ├── solution1.cpp ├── solution2.cpp └── solution3.cpp ├── 0162. Find Peak Element └── solution1.cpp ├── 0168. Excel Sheet Column Title └── solution1.cpp ├── 0169. Majority Element ├── Solution1.java └── solution1.c ├── 0171. Excel Sheet Column Number └── solution1.cpp ├── 0175. Combine Two Tables └── solution 1.sql ├── 0176. Second Highest Salary ├── solution1.sql ├── solution2.sql └── solution3.sql ├── 0178. Rank Scores ├── solution1.sql └── solution2.sql ├── 0179. Largest Number ├── solution1.cpp ├── solution1.java ├── solution2.java └── solution3.java ├── 0181. Employees Earning More Than Their Managers ├── solution1.sql └── solution2.sql ├── 0182. Duplicate Emails └── solution1.sql ├── 0183. Customers Who Never Order └── solution1.sql ├── 0184. Department Highest Salary └── solution1.sql ├── 0185. Department Top Three Salaries └── solution1.sql ├── 0189. Rotate Array └── solution1.cpp ├── 0191. Number of 1 Bits ├── solution.js ├── solution1.cpp └── solution2.cpp ├── 0196. Delete Duplicate Emails └── solution1.sql ├── 0197. Rising Temperature └── solution1.sql ├── 0198. House Robber ├── solution1.cpp └── solution2.cpp ├── 0200. Number of Islands └── solution1.cpp ├── 0202. Happy Number ├── solution1.cpp ├── solution2.cpp └── solution3.cpp ├── 0203. Remove Linked List Elements └── solution1.cpp ├── 0204. Count Primes ├── solution1.cpp └── solution1.java ├── 0205. Isomorphic Strings ├── solution1.c └── solution1.cpp ├── 0206. Reverse Linked List ├── solution1.c ├── solution1.cpp └── solution2.c ├── 0207. Course Schedule └── solution1.cpp ├── 0213. House Robber II └── solution1.cpp ├── 0215. Kth Largest Element in an Array ├── solution1.cpp └── solution1.java ├── 0217. Contains Duplicate └── solution1.java ├── 0219. Contains Duplicate II └── solution1.java ├── 0222. Count Complete Tree Nodes ├── solution1.cpp └── solution2.cpp ├── 0231. Power of Two ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0234. Palindrome Linked List ├── solution1.cpp └── solution1.java ├── 0242. Valid Anagram ├── solution1.cpp ├── solution1.js └── solution2.cpp ├── 0258. Add Digits └── solution1.java ├── 0260. Single Number III └── solution1.cpp ├── 0263. Ugly Number ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0268. Missing Number ├── solution1.c └── solution2.c ├── 0283. Move Zeroes └── solution1.c ├── 0290. Word Pattern └── solution1.cpp ├── 0326. Power of Three ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0328. Odd Even Linked List └── solution1.cpp ├── 0338. Counting Bits ├── solution1.cpp └── solution2.cpp ├── 0342. Power of Four ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0344. Reverse String └── solution1.java ├── 0345. Reverse Vowels of a String └── solution1.java ├── 0349. Intersection of Two Arrays ├── solution1.cpp ├── solution1.java └── solution2.cpp ├── 0367. Valid Perfect Square ├── solution1.c ├── solution1.cpp └── solution1.java ├── 0374. Guess Number Higher or Lower └── solution1.cpp ├── 0383. Ransom Note └── solution1.cpp ├── 0387. First Unique Character in a String ├── solution1.cpp ├── solution1.java └── solution2.java ├── 0389. Find the Difference └── solultion1.java ├── 0392. Is Subsequence └── solution1.cpp ├── 0404. Sum of Left Leaves ├── solution1.cpp └── solution2.cpp ├── 0409. Longest Palindrome └── solution1.cpp ├── 0412. Fizz Buzz └── solution1.cpp ├── 0415. Add Strings ├── solution1.cpp ├── solution1.java └── solution2.cpp ├── 0438. Find All Anagrams in a String └── solution1.cpp ├── 0500. Keyboard Row └── solution1.cpp ├── 0507. Perfect Number └── solution1.cpp ├── 0509. Fibonacci Number ├── solution1.cpp └── solution1.java ├── 0511. Game Play Analysis I └── solution1.sql ├── 0520. Detect Capital └── solution1.java ├── 0524. Longest Word in Dictionary through Deleting ├── solution1.cpp └── solution1.java ├── 0540. Single Element in a Sorted Array └── solution1.cpp ├── 0542. 01 Matrix ├── solution1.cpp └── solution1.java ├── 0543. Diameter of Binary Tree └── solution1.cpp ├── 0550. Game Play Analysis IV └── solution1.sql ├── 0560. Subarray Sum Equals K ├── solution1.cpp └── solution1.java ├── 0567. Permutation in String ├── solution1.cpp └── solution1.java ├── 0570. Managers with at Least 5 Direct Reports └── solution1.sql ├── 0577. Employee Bonus └── solution1.sql ├── 0584. Find Customer Referee └── solution1.sql ├── 0585. Investments in 2016 └── solution1.sql ├── 0586. Customer Placing the Largest Number of Orders └── solution1.sql ├── 0589. N-ary Tree Preorder Traversal └── solution1.cpp ├── 0595. Big Countries └── solution1.sql ├── 0596. Classes More Than 5 Students └── solution1.sql ├── 0602. Friend Requests II Who Has the Most Friends └── solution1.sql ├── 0607. Sales Person └── solution1.sql ├── 0610. Triangle Judgement └── solution1.sql ├── 0617. Merge Two Binary Trees └── solution1.c ├── 0619. Biggest Single Number └── solution1.sql ├── 0620. Not Boring Movies └── solution1.sql ├── 0627. Swap Salary └── solution1.sql ├── 0633. Sum of Square Numbers └── solution1.java ├── 0671. Second Minimum Node In a Binary Tree └── solution1.cpp ├── 0686. Repeated String Match ├── solution1.cpp └── solution1.exe ├── 0700. Search in a Binary Search Tree └── solution1.cpp ├── 0704. Binary Search └── solution1.java ├── 0709. To Lower Case └── solution1.java ├── 0728. Self Dividing Numbers └── solution1.cpp ├── 0733. Flood Fill └── solution1.cpp ├── 0785. Is Graph Bipartite └── solution1.cpp ├── 0788. Rotated Digits └── solution1.java ├── 0859. Buddy Strings └── solution1.cpp ├── 0876. Middle of the Linked List └── solution1.cpp ├── 0896. Monotonic Array └── solution1.cpp ├── 0916. Word Subsets └── solution1.java ├── 0921. Minimum Add to Make Parentheses Valid └── solution1.java ├── 0977. Squares of a Sorted Array └── solution1.cpp ├── 0987. Vertical Order Traversal of a Binary Tree └── solution1.cpp ├── 0994. Rotting Oranges └── solution1.cpp ├── 1015. Smallest Integer Divisible by K └── solution1.java ├── 1020. Number of Enclaves └── solution1.cpp ├── 1045. Customers Who Bought All Products └── solution1.sql ├── 1050. Actors and Directors Who Cooperated At Least Three Times └── solution1.sql ├── 1068. Product Sales Analysis I └── solution1.sql ├── 1070. Product Sales Analysis III └── solution1.sql ├── 1075. Project Employees I └── solution1.sql ├── 1084. Sales Analysis III ├── solution1.sql └── solution2.sql ├── 1089. Duplicate Zeros ├── solution1.cpp └── solution2.cpp ├── 1141. User Activity for the Past 30 Days I └── solution1.sql ├── 1148. Article Views I └── solution1.sql ├── 1160. Find Words That Can Be Formed by Characters └── solution1.cpp ├── 1174. Immediate Food Delivery II ├── solution1.sql ├── solution2.sql └── solution3.sql ├── 1179. Reformat Department Table └── solution1.sql ├── 1193. Monthly Transactions I ├── solution1.sql └── solution2.sql ├── 1211. Queries Quality and Percentage └── solution1.sql ├── 1232. Check If It Is a Straight Line └── solution1.cpp ├── 1251. Average Selling Price └── solution1.sql ├── 1280. Students and Examinations └── solution1.sql ├── 1287. Element Appearing More Than 25% In Sorted Array ├── solution1.cpp └── solution2.cpp ├── 1290. Convert Binary Number in a Linked List to Integer ├── solution1.c └── solution1.cpp ├── 1291. Sequential Digits ├── solution1.cpp ├── solution1.java └── solution2.java ├── 1323. Maximum 69 Number └── solution1.java ├── 1327. List the Products Ordered in a Period └── solution1.sql ├── 1337. The K Weakest Rows in a Matrix └── solution1.cpp ├── 1347. Minimum Number of Steps to Make Two Strings Anagram ├── solution1.java └── solution2.java ├── 1351. Count Negative Numbers in a Sorted Matrix ├── solution1.cpp └── solution2.cpp ├── 1356. Sort Integers By The Number of 1 Bits ├── solution1.cpp └── solution2.cpp ├── 1378. Replace Employee ID With The Unique Identifier └── solution1.sql ├── 1407. Top Travellers └── solution1.sql ├── 1464. Maximum Product of Two Elements in an Array ├── solution1.cpp └── solution2.cpp ├── 1480. Running Sum of 1d Array └── solution1.java ├── 1484. Group Sold Products By The Date └── solution1.sql ├── 1491. Average Salary Excluding the Minimum and Maximum Salary └── solution1.cpp ├── 1512. Number of Good Pairs ├── solution1.c └── solution2.c ├── 1517. Find Users With Valid E-Mails └── solution1.sql ├── 1523. Count Odd Numbers in an Interval Range └── solution1.c ├── 1527. Patients With a Condition └── solution1.sql ├── 1528. Shuffle String └── solution1.cpp ├── 1581. Customer Who Visited but Did Not Make Any Transactions └── solution1.sql ├── 1582. Special Positions in a Binary Matrix └── solution1.cpp ├── 1587. Bank Account Summary II └── solution1.sql ├── 1633. Percentage of Users Attended a Contest └── solution1.sql ├── 1661. Average Time of Process per Machine └── solution1.sql ├── 1662. Check If Two String Arrays are Equivalent ├── solution1.cpp └── solution1.js ├── 1667. Fix Names in a Table └── solution1.sql ├── 1683. Invalid Tweets └── solution1.sql ├── 1693. Daily Leads and Partners └── solution1.sql ├── 1729. Find Followers Count └── solution1.sql ├── 1731. The Number of Employees Which Report to Each Employee └── solution1.sql ├── 1757. Recyclable and Low Fat Products └── solution1.sql ├── 1768. Merge Strings Alternately ├── solution1.cpp └── solution1.exe ├── 1789. Primary Department for Each Employee └── solution1.sql ├── 1790. Check if One String Swap Can Make Strings Equal ├── solution1.c └── solution2.c ├── 1791. Find Center of Star Graph └── solution1.cpp ├── 1816. Truncate Sentence └── solution1.java ├── 1859. Sorting the Sentence └── solution1.cpp ├── 1873. Calculate Special Bonus ├── solution1.sql └── solution2.sql ├── 1890. The Latest Login in 2020 └── solution1.sql ├── 1907. Count Salary Categories └── solution1.sql ├── 1929. Concatenation of Array └── solution1.cpp ├── 1978. Employees Whose Manager Left the Company ├── solution1.sql └── solution2.sql ├── 2264. Largest 3-Same-Digit Number in String └── solution1.cpp ├── 2325. Decode the Message ├── solution1.cpp ├── solution1.java └── solution2.java ├── 2356. Number of Unique Subjects Taught by Each Teacher └── solution1.sql ├── 2390. Removing Stars From a String └── solution1.cpp ├── 2396. Strictly Palindromic Number ├── solution1.java └── solution2.java ├── 2418. Sort the People └── solution1.cpp ├── 2427. Number of Common Factors └── solution1.java ├── 2520. Count the Digits That Divide a Number └── solution1.java ├── 2619. Array Prototype Last └── solution1.js ├── 2620. Counter └── approach1.js ├── 2621. Sleep └── solution1.js ├── 2626. Array Reduce Transformation └── solution1.js ├── 2629. Function Composition └── solution1.js ├── 2634. Filter Elements from Array └── solution1.js ├── 2635. Apply Transform Over Each Element in Array └── solution1.js ├── 2648. Generate Fiboacci Sequence └── solution1.js ├── 2660. Determine the Winner of a Bowling Game └── solution1.cpp ├── 2665. Counter II └── solution1.js ├── 2666. Allow One Function Call └── solution1.js ├── 2667. Create Hello World Function └── solution1.js ├── 2677. Chunk Array └── solution1.js ├── 2703. Return Length of Arguments Passed └── solution1.js ├── 2704. To Be Or Not To Be └── solution1.js ├── 2785. Sort Vowels in a String └── solution1.cpp └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /.vscode 2 | *.exe 3 | *.class 4 | /bin 5 | .classpath 6 | .project 7 | tempCodeRunnerFile.cpp 8 | demo.cpp 9 | demo.js 10 | demo.java 11 | tempCodeRunnerFile.java 12 | Main.java -------------------------------------------------------------------------------- /0001. Two Sum/Solution1.java: -------------------------------------------------------------------------------- 1 | public class Solution1 2 | { 3 | public static int[] twoSum(int[] nums,int target) 4 | { 5 | for(int i=0;i 2 | #include 3 | int* twoSum(int* nums,int n,int target,int returnSize) 4 | { 5 | returnSize = 2; 6 | int *ans = malloc(sizeof(int)*2); 7 | for(int i=0;i 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | vector twoSum(vector &nums,int target) 7 | { 8 | int n = nums.size(); 9 | int k; 10 | vector v; 11 | unordered_map m; 12 | if(n==0) return v; 13 | for(int i=0;i nums = {2,7,11,15}; 30 | int target = 9; 31 | Solution s; 32 | vector ans = s.twoSum(nums,target); 33 | cout< 2 | #include 3 | #include 4 | #include 5 | 6 | int lengthOfLongestSubstring(char * s) 7 | { 8 | int n = strlen(s); 9 | int *visited = (int*)calloc(256,sizeof(int)); 10 | int maxLength = 0; 11 | int start = 0; 12 | for(int i=0;i maxLength) maxLength = currentLength; 24 | } 25 | return maxLength; 26 | } 27 | 28 | int main() 29 | { 30 | char s[100]; 31 | scanf("%[^\n]%*c",s); 32 | printf("%d",lengthOfLongestSubstring(s)); 33 | } -------------------------------------------------------------------------------- /0003. Longest Substring Without Repeating Characters/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int lengthOfLongestSubstring(string s) 7 | { 8 | vector str; 9 | int output=0; 10 | for(int i=0;i output) output = str.size(); // check for maximum length 20 | } 21 | return output; 22 | } 23 | }; 24 | int main() 25 | { 26 | string s = "abcabc"; 27 | Solution sol; 28 | cout<0;i--) 19 | { 20 | for(int j=0;j 2 | #include 3 | 4 | int reverse(int x){ 5 | 6 | long sum=0; 7 | while(x) 8 | { 9 | sum = (sum*10) + (x%10); 10 | x/=10; 11 | } 12 | if(sum>INT_MIN && sum 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int reverse(int x) { 7 | long sum=0; 8 | while(x) 9 | { 10 | sum = (sum*10) + (x%10); 11 | x/=10; 12 | } 13 | if(sum>INT_MIN && sum 0) { 11 | reverse = reverse * 10 + x % 10; 12 | x /= 10; 13 | } 14 | if (reverse > Integer.MAX_VALUE) { 15 | return 0; 16 | } 17 | return (int) (isNegative ? -reverse : reverse); 18 | } 19 | } 20 | public class solution1 21 | { 22 | public static void main(String[] args) 23 | { 24 | Solution sol = new Solution(); 25 | System.out.println(sol.reverse(123)); 26 | } 27 | } -------------------------------------------------------------------------------- /0009. Palindrome Number/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | bool isPalindrome(int x) 7 | { 8 | long sum=0; 9 | int temp=x; 10 | if(x<0) return false; 11 | if(x>INT_MIN && x 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int maxArea(vector &container) 8 | { 9 | int left = 0; 10 | int right = container.size()-1; 11 | int maximum = 0; 12 | while(left < right) 13 | { 14 | int width = right - left; 15 | int height = min(container[left],container[right]); 16 | int area = height * width; 17 | maximum = max(maximum,area); 18 | if(container[left] < container[right]) left++; 19 | else if(container[left] > container[right]) right--; 20 | else 21 | { 22 | left++; 23 | right--; 24 | } 25 | } 26 | return maximum; 27 | } 28 | }; 29 | int main() 30 | { 31 | int n; 32 | cin>>n; 33 | vector container(n); 34 | for(int i=0;i>container[i]; 37 | } 38 | Solution sol; 39 | cout< 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | string integerToRoman(int num) 8 | { 9 | string ones[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"}; 10 | string tens[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; 11 | string hundreds[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; 12 | string thousands[] = {"","M","MM","MMM"}; 13 | 14 | return thousands[num/1000] + hundreds[(num%1000)/100] + tens[(num%100)/10] + ones[num%10]; 15 | } 16 | }; 17 | int main() 18 | { 19 | int n; 20 | cin>>n; 21 | Solution sol; 22 | cout< 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int romantoInt(string s) 7 | { 8 | unordered_map roman 9 | {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; 10 | int integer=0; 11 | for(int i=0;i 2 | #include 3 | 4 | typedef struct ListNode 5 | { 6 | int data; 7 | struct ListNode *next; 8 | }ListNode; 9 | 10 | ListNode *head = NULL; 11 | 12 | Node* createNode(int data) 13 | { 14 | ListNode* newNode = (Node*)malloc(sizeof(Node)); 15 | newNode->data = data; 16 | newNode->next = NULL; 17 | return newNode; 18 | } 19 | void insertAtBeginning(int data) 20 | { 21 | ListNode* newNode = createNode(data); 22 | if(head==NULL) 23 | { 24 | head = newNode; 25 | return; 26 | } 27 | newNode->next = head; 28 | head = newNode; 29 | } 30 | void insertAtEnd(int data) 31 | { 32 | if(head==NULL) 33 | { 34 | insertAtBeginning(data); 35 | return; 36 | } 37 | ListNode* newNode = createNode(data); 38 | ListNode* temp = head; 39 | while(temp->next != NULL) temp = temp->next; 40 | temp->next = newNode; 41 | newNode->next = NULL; 42 | } -------------------------------------------------------------------------------- /0028. Find the Index of the First Occurrence in a String/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int strStr(string s,string h) 7 | { 8 | int index=-1; 9 | int ind1=-1,ind2=0; 10 | for(int i=0;i=h.size()) break; 19 | ind1++; 20 | ind2++; 21 | } 22 | } 23 | if(ind2>=h.size()) break; 24 | else 25 | { 26 | ind2=0; 27 | index=-1; 28 | } 29 | } 30 | return index; 31 | } 32 | }; 33 | int main() 34 | { 35 | string s,h; 36 | cin>>s>>h; 37 | Solution sol; 38 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int divide(int x, int y) { 6 | if(x == INT_MIN && y == -1) 7 | { 8 | return INT_MAX; 9 | } 10 | return x/y; 11 | } 12 | }; 13 | int main() 14 | { 15 | int x,y; 16 | cin>>x>>y; 17 | Solution sol; 18 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int search(vector& nums, int target) { 7 | for(int i=0;i nums = {4,5,6,7,0,1,2}; 18 | int target = 0; 19 | Solution sol; 20 | cout<=0;i--) if(nums[i] == target) {b = i; break;} 6 | return new int[] {a,b}; 7 | } 8 | } 9 | public class solution1 10 | { 11 | public static void main(String[] args) 12 | { 13 | Solution sol = new Solution(); 14 | int[] nums = {5,7,7,8,8,10}; 15 | int target = 8; 16 | int[] result = sol.searchRange(nums, target); 17 | for(int i: result) System.out.print(i + " "); 18 | } 19 | } -------------------------------------------------------------------------------- /0034. Find First and Last Position of Element in Sorted Array/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @param {number} target 4 | * @return {number[]} 5 | */ 6 | var searchRange = function(nums, target) { 7 | return [nums.indexOf(target),nums.lastIndexOf(target)]; 8 | }; 9 | 10 | let nums = [5,7,7,8,8,10] 11 | let target = 8 12 | 13 | console.log(searchRange(nums,target)); -------------------------------------------------------------------------------- /0034. Find First and Last Position of Element in Sorted Array/solution1.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def searchRange(self, nums, target): 3 | result = [] 4 | for i in range(0,len(nums)): 5 | if(nums[i]==target): 6 | result.append(i) 7 | break 8 | for i in range(len(nums)-1,-1,-1): 9 | if(nums[i]==target): 10 | result.append(i) 11 | break 12 | if(len(result) == 0): 13 | result = [-1,-1] 14 | return result 15 | 16 | -------------------------------------------------------------------------------- /0041. First Missing Positive/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int firstMissingPositive(int[] nums) { 5 | int[] freq = new int[nums.length+1]; 6 | Arrays.fill(freq, -1); 7 | for(int i=0;i 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int min(int a,int b) 7 | { 8 | return ab?a:b; 13 | } 14 | int trap(vector &v) 15 | { 16 | int n = v.size(); 17 | int water = 0; 18 | if(n==0) return 0; 19 | int leftMax[n],rightMax[n]; 20 | leftMax[0] = v[0]; 21 | for(int i=1;i=0;i--) 27 | { 28 | rightMax[i] = max(v[i],rightMax[i+1]); 29 | } 30 | for(int i=0;i height = {0,1,0,2,1,0,1,3,2,1,2,1}; 40 | Solution s; 41 | cout<max) 13 | { 14 | max = a[i]; 15 | } 16 | leftMax[i] = max; 17 | } 18 | max=-1; 19 | for(int i=n-1;i>=0;i--) 20 | { 21 | if(a[i]>max) 22 | { 23 | max = a[i]; 24 | } 25 | rightMax[i] = max; 26 | } 27 | for(int i=0;i rightMax = new Stack<>(); 9 | rightMax.push(a[n-1]); 10 | for(int i=n-2;i>=2;i--) 11 | { 12 | int temp = Math.max(rightMax.peek(),a[i]); 13 | rightMax.push(temp); 14 | } 15 | int totWater=0; 16 | for(int i=1;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int jump(vector &nums) 8 | { 9 | for(int i=1;i>n; 28 | vector nums(n); 29 | for(int i=0;i>nums[i]; 30 | Solution sol; 31 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void func(int start,int end,vector &nums,vector> &ans) 7 | { 8 | if(start==end) 9 | { 10 | ans.push_back(nums); 11 | return; 12 | } 13 | else 14 | { 15 | for(int i=start;i<=end;i++) 16 | { 17 | swap(nums[start],nums[i]); 18 | func(start+1,end,nums,ans); 19 | swap(nums[start],nums[i]); 20 | } 21 | } 22 | } 23 | vector> permute(vector& nums) 24 | { 25 | vector> ans; 26 | func(0,nums.size()-1,nums,ans); 27 | return ans; 28 | } 29 | }; 30 | int main() 31 | { 32 | Solution s; 33 | vector nums = {1,2,3}; 34 | vector> ans = s.permute(nums); 35 | for(auto i: ans) 36 | { 37 | for(auto j:i) 38 | { 39 | cout< 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | vector> groupAnagrams(vector &s) 7 | { 8 | vector> ans; 9 | unordered_map> m; 10 | for(int i=0;i>n; 27 | vector s(n); 28 | for(int i=0;i>s[i]; 29 | Solution sol; 30 | vector> ans = sol.groupAnagrams(s); 31 | for(auto i: ans) 32 | { 33 | for(auto j:i) 34 | { 35 | cout< 2 | #include 3 | 4 | double myPow(double x, int n) 5 | { 6 | return pow(x,n); 7 | } 8 | int main() 9 | { 10 | double x = 2.00000; 11 | int n = 10; 12 | printf("%lf",myPow(x,n)); 13 | } -------------------------------------------------------------------------------- /0053. Maximum Subarray/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int maxSubArray(vector &nums) 8 | { 9 | int maxValue = INT_MIN; 10 | int sum=0; 11 | for(int i=0;i nums = {-2,1,-3,4,-1,2,1,-5,4}; 22 | Solution s; 23 | cout< 2 | #include 3 | using namespace std; 4 | class Solution 5 | { 6 | public: 7 | bool canJump(vector &nums) 8 | { 9 | vector v(nums.size(),-1); 10 | v[0]=nums[0]; 11 | for(int i=1;i0) v[i] = max(v[i-1]-1,nums[i]); 14 | else v[i]=-1; 15 | } 16 | if(v[nums.size()-1]!=-1) return true; 17 | return false; 18 | } 19 | }; 20 | int main() 21 | { 22 | vector v = {2,5,0,0}; 23 | Solution s; 24 | if(s.canJump(v)) cout<<"TRUE"; 25 | else cout<<"FALSE"; 26 | } -------------------------------------------------------------------------------- /0058 .Length of Last Word/solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int lengthOfLastWord(char * s) 4 | { 5 | int count=0; 6 | int n = strlen(s)-1; 7 | while(s[n]==' ') n--; 8 | for(int i=n;i>=0;i--) 9 | { 10 | if(s[i]!=' ') count++; 11 | else break; 12 | } 13 | return count; 14 | } 15 | int main() 16 | { 17 | char s[100]; 18 | scanf("%[^\n]%*c",s); 19 | printf("%d",lengthOfLastWord(s)); 20 | } -------------------------------------------------------------------------------- /0058 .Length of Last Word/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int lengthOfLastWord(string s) { 7 | int count=0,flag=1; 8 | int len = size(s)-1; 9 | while(flag) 10 | { 11 | if(s[len]==' ') 12 | { 13 | len--; 14 | } 15 | else 16 | { 17 | flag=0; 18 | } 19 | } 20 | for(int i=0;i<=len;i++) 21 | { 22 | if(s[i]==' ') 23 | count=0; 24 | else 25 | count++; 26 | } 27 | return count; 28 | 29 | } 30 | }; 31 | int main() 32 | { 33 | string s; 34 | cin>>s; 35 | Solution sol; 36 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int recursion(int m,int n,int a,int b,vector> &v) 6 | { 7 | if(v[m][n] != -1) return v[m][n]; 8 | 9 | if(m==a || n==b) return 1; 10 | 11 | v[m][n] = recursion(m+1,n,a,b,v) + recursion(m,n+1,a,b,v); 12 | 13 | return v[m][n]; 14 | } 15 | int uniquePaths(int m, int n) 16 | { 17 | vector> v(m,vector(n,-1)); 18 | return recursion(0,0,m-1,n-1,v); 19 | } 20 | }; 21 | int main() 22 | { 23 | Solution sol; 24 | int m,n 25 | cin>>m>>n; 26 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int uniquePaths(int m, int n,int i=0,int j=0) 6 | { 7 | if(i>=m || j>=n) return 0; 8 | if(i == m-1 && j== n-1) return 1; 9 | return uniquePaths(m,n,i+1,j) + uniquePaths(m,n,i,j+1); 10 | } 11 | }; 12 | int main() 13 | { 14 | Solution sol; 15 | int m,n; 16 | cin>>m>>n; 17 | cout< 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int minPathSum(vector> &grid) 8 | { 9 | int m = grid.size(); 10 | int n = grid[0].size(); 11 | 12 | for(int i=1;i> grid {{1,3,1}, 34 | {1,5,1}, 35 | {4,2,1}}; 36 | 37 | cout<=0;i--) 9 | { 10 | if(digits[i]<9) 11 | { 12 | digits[i]++; 13 | return digits; 14 | } 15 | digits[i]=0; 16 | } 17 | int[] newNumber = new int[n+1]; 18 | newNumber[0] = 1; 19 | return newNumber; 20 | 21 | } 22 | } 23 | public class solution1 24 | { 25 | public static void main(String[] args) 26 | { 27 | Solution sol = new Solution(); 28 | int[] nums = {1,2,3}; 29 | int[] result = sol.plusOne(nums); 30 | for(int i=0;i 2 | 3 | int mySqrt(int x){ 4 | if(x==0 || x==1) return x; 5 | 6 | int start = 0; 7 | int end = x; 8 | int mid = -1; 9 | 10 | while(start <= end) 11 | { 12 | mid = start + (end - start) / 2; 13 | 14 | if((double)mid * mid > (double)x) end = mid - 1; 15 | else if(mid * mid == x) return mid; 16 | else start = mid + 1; 17 | } 18 | 19 | return (int)end; 20 | } 21 | int main() 22 | { 23 | int x; 24 | scanf("%d", &x); 25 | int result = mySqrt(x); 26 | printf("%d", result); 27 | } -------------------------------------------------------------------------------- /0070. Climbing Stairs/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int climbStairs(int n) 8 | { 9 | if(n==1) return 1; 10 | if(n==2) return 2; 11 | int dp[n+1]; 12 | dp[1] = 1; 13 | dp[2] = 2; 14 | for(int i=3;i<=n;i++) 15 | { 16 | dp[i] = dp[i-1] + dp[i-2]; 17 | } 18 | return dp[n]; 19 | } 20 | }; 21 | int main() 22 | { 23 | int n=5; 24 | Solution s; 25 | cout< 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool searchMatrix(vector> &matrix,int target) 8 | { 9 | for(auto i: matrix) 10 | { 11 | for(auto j: i) 12 | { 13 | if(j == target) return true; 14 | } 15 | } 16 | return false; 17 | } 18 | }; 19 | int main() 20 | { 21 | vector> matrix = {{1,3,5,7}, 22 | {10,11,16,20}, 23 | {23,30,34,60}}; 24 | int target = 3; 25 | 26 | Solution sol; 27 | if(sol.searchMatrix(matrix,target)) cout<<"TRUE"; 28 | else cout<<"FALSE"; 29 | } -------------------------------------------------------------------------------- /0074. Search a 2D Matrix/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool searchMatrix(vector> &matrix,int target) 8 | { 9 | int row = matrix.size(); 10 | int col = matrix[0].size(); 11 | int start = 0; 12 | int end = row*col-1; 13 | 14 | while(start <= end) 15 | { 16 | int mid = start + (end-start)/2; 17 | int element = matrix[mid/col][mid%col]; 18 | if(element == target) return true; 19 | else if(target < element) end = mid - 1; 20 | else if(target > element) start = mid + 1; 21 | } 22 | return false; 23 | } 24 | }; 25 | int main() 26 | { 27 | vector> matrix = {{1,3,5,7}, 28 | {10,11,16,20}, 29 | {23,30,34,60}}; 30 | int target = 3; 31 | 32 | Solution sol; 33 | if(sol.searchMatrix(matrix,target)) cout<<"TRUE"; 34 | else cout<<"FALSE"; 35 | } -------------------------------------------------------------------------------- /0075. Sort Colors/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { //NOT RECOMMENDED 5 | public: 6 | void sortColors(vector& nums) { 7 | multiset s; 8 | for(auto i: nums) 9 | s.insert(i); 10 | nums.clear(); 11 | for(auto i: s) 12 | nums.push_back(i); 13 | } 14 | }; 15 | 16 | int main() { 17 | 18 | } -------------------------------------------------------------------------------- /0075. Sort Colors/solution2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public void sortColors(int[] nums) { //NOT RECOMMENDED 5 | List l = new ArrayList<>(); 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void sortColors(vector& nums) { 7 | int low=0,mid=0,high=nums.size()-1; 8 | while(mid <= high) { 9 | if(nums[mid] == 0) { 10 | swap(nums[low], nums[mid]); 11 | mid++; 12 | low++; 13 | } else if(nums[mid] == 1) { 14 | mid++; 15 | } else if(nums[mid] == 2) { 16 | swap(nums[mid], nums[high]); 17 | high--; 18 | } 19 | } 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | int n; 26 | cin>>n; 27 | vector nums(n); 28 | for(int i=0;i>nums[i]; 29 | Solution sol; 30 | sol.sortColors(nums); 31 | for(auto i: nums) cout< O(N) 45 | Space Complexity -> O(1) 46 | */ -------------------------------------------------------------------------------- /0078. Subsets/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | void func(int index,vector &nums,vector &path,vector> &ans) 7 | { 8 | if(index==nums.size()) 9 | { 10 | ans.push_back(path); 11 | return; 12 | } 13 | path.push_back(nums[index]); 14 | func(index+1,nums,path,ans); 15 | path.pop_back(); 16 | func(index+1,nums,path,ans); 17 | } 18 | vector> Subsets(vector &nums) 19 | { 20 | vector> ans; 21 | vector path; 22 | func(0,nums,path,ans); 23 | return ans; 24 | } 25 | }; 26 | int main() 27 | { 28 | vector nums = {1,2,3}; 29 | Solution s; 30 | vector> ans = s.Subsets(nums); 31 | for(auto i: ans) 32 | { 33 | for(auto j:i) 34 | { 35 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool search(vector& nums, int target) { 7 | for(int i=0;i nums = {2,5,6,0,0,1,2}; 17 | int target = 0; 18 | Solution sol; 19 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void merge(vector& nums1, int m, vector& nums2, int n) { 7 | int i = m; 8 | int j=-1; 9 | while(i nums1 = {1,2,3,0,0,0}; 19 | int m = 3; 20 | vector nums2 = {2,5,6}; 21 | int n = 3; 22 | Solution sol; 23 | sol.merge(nums1,m,nums2,n); 24 | for(auto i: nums1) cout<= 0) { 7 | if(i >=0 && nums1[i] >= nums2[j]) { 8 | nums1[k--] = nums1[i--]; 9 | } else { 10 | nums1[k--] = nums2[j--]; 11 | } 12 | } 13 | } 14 | } 15 | public class solution1 { 16 | public static void main(String[] args) { 17 | int[] nums1 = {1,2,3,0,0,0}; 18 | int[] nums2 = {2,5,6}; 19 | Solution sol = new Solution(); 20 | sol.merge(nums1, 3, nums2, 3); 21 | for(Integer i: nums1) System.out.print(i + " "); 22 | } 23 | } -------------------------------------------------------------------------------- /0088. Merge Sorted Array/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void merge(vector& nums1, int m, vector& nums2, int n) { 7 | int i = m - 1; 8 | int j = n - 1; 9 | int k = m + n - 1; 10 | while(j >= 0) { 11 | if(i >= 0 && nums1[i] > nums2[j]) { 12 | nums1[k--] = nums1[i--]; 13 | } else { 14 | nums1[k--] = nums2[j--]; 15 | } 16 | } 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | vector nums1 = {1,2,3,0,0,0}; 23 | int m = 3; 24 | vector nums2 = {2,5,6}; 25 | int n = 3; 26 | Solution sol; 27 | sol.merge(nums1,m,nums2,n); 28 | for(auto i: nums1) cout< 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | void func(int index,vector &nums,vector &path,vector> &ans) 7 | { 8 | ans.push_back(path); 9 | for(int i=index;i> subsetsWithDup(vector &nums) 19 | { 20 | sort(nums.begin(),nums.end()); 21 | vector> ans; 22 | vector path; 23 | func(0,nums,path,ans); 24 | return ans; 25 | } 26 | }; 27 | int main() 28 | { 29 | vector nums = {1,2,2}; 30 | Solution s; 31 | vector> ans = s.subsetsWithDup(nums); 32 | for(auto i: ans) 33 | { 34 | for(auto j:i) 35 | { 36 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector> generate(int n) 7 | { 8 | vector> output(n); 9 | for(int i=0;i> ans = sol.generate(n); 25 | for(auto i: ans) 26 | { 27 | for(auto j: i) 28 | { 29 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector> generate(int n) 7 | { 8 | vector> output(n); 9 | for(int i=0;i> ans = sol.generate(n); 35 | for(auto i: ans) 36 | { 37 | for(auto j: i) 38 | { 39 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | vector getRow(int n) 6 | { 7 | vector ans(n+1),prev(n+1); 8 | 9 | for(int i=0;i<=n;i++) 10 | { 11 | for(int j=0;j<=i;j++) 12 | { 13 | if(j==0 || j==i) ans[j] = 1; 14 | else ans[j] = prev[j] + prev[j-1]; 15 | } 16 | prev = ans; 17 | } 18 | return ans; 19 | } 20 | }; 21 | int main() 22 | { 23 | Solution sol; 24 | int n=4; 25 | vector ans = sol.getRow(n); 26 | for(auto i:ans) cout< 2 | #include 3 | int maxProfit(int* prices, int n) 4 | { 5 | int min=INT_MAX,ans=0,sum=0; 6 | for(int i=0;i ans) 13 | ans = sum; 14 | } 15 | return ans; 16 | } 17 | int main() 18 | { 19 | int prices[] = {7,1,5,3,6,4}; 20 | printf("%d",maxProfit(prices,7)); 21 | } -------------------------------------------------------------------------------- /0125. Valid Palindrome/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | bool isPalindrome(string s) { 6 | string x = ""; 7 | int start,end; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int singleNumber(vector& nums) 7 | { 8 | unordered_map m; 9 | for(int i=0;i nums{1,1,1,2,2,2,5}; 23 | Solution sol; 24 | cout< l = new ArrayList<>(); 5 | while(temp != null) 6 | { 7 | l.add(temp.val); 8 | temp = temp.next; 9 | } 10 | temp = head; 11 | Collections.sort(l); 12 | for(Object o: l) 13 | { 14 | temp.val = (int)o; 15 | temp = temp.next; 16 | } 17 | return head; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /0160. Intersection of Two Linked Lists/solution1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *getIntersectionNode(ListNode *A, ListNode *B) { 4 | typedef ListNode Node; 5 | unordered_set s; 6 | while(A != NULL) 7 | { 8 | s.insert(A); 9 | A = A->next; 10 | } 11 | while(B != NULL) 12 | { 13 | if(s.insert(B).second == false) 14 | return B; 15 | 16 | B = B->next; 17 | } 18 | return NULL; 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /0160. Intersection of Two Linked Lists/solution2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *getIntersectionNode(ListNode *A, ListNode *B) { 4 | typedef ListNode Node; 5 | stack s1; 6 | stack s2; 7 | Node* temp = NULL; 8 | 9 | while(A != NULL) 10 | { 11 | s1.push(A); 12 | A = A->next; 13 | } 14 | while(B != NULL) 15 | { 16 | s2.push(B); 17 | B = B->next; 18 | } 19 | while((!s1.empty()) && (!s2.empty())) 20 | { 21 | if(s1.top() == s2.top()) 22 | { 23 | temp = s1.top(); 24 | s1.pop(); 25 | s2.pop(); 26 | } 27 | else 28 | return temp; 29 | } 30 | return temp; 31 | } 32 | }; -------------------------------------------------------------------------------- /0160. Intersection of Two Linked Lists/solution3.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode *getIntersectionNode(ListNode *A, ListNode *B) { 4 | typedef ListNode Node; 5 | Node *tempA,*tempB; 6 | tempA = A; 7 | tempB = B; 8 | while(tempA != NULL) 9 | { 10 | tempB = B; 11 | while(tempB != NULL) 12 | { 13 | if(tempA == tempB) 14 | return tempA; 15 | 16 | tempB = tempB->next; 17 | } 18 | tempA = tempA->next; 19 | } 20 | return NULL; 21 | } 22 | }; -------------------------------------------------------------------------------- /0162. Find Peak Element/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int findPeakElement(vector& nums) { 7 | int maxIndex = 0; 8 | int maxValue = INT_MIN; 9 | for(int i=0;i maxValue) 12 | { 13 | maxValue = nums[i]; 14 | maxIndex = i; 15 | } 16 | } 17 | return maxIndex; 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | vector nums = {1,2,3,4,5,6}; 24 | Solution sol; 25 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string convertToTitle(int n) 7 | { 8 | string ans; 9 | while(n) 10 | { 11 | if(n%26==0) 12 | { 13 | ans += 'Z'; 14 | n--; 15 | } 16 | else 17 | { 18 | ans += (n%26-1)+'A'; 19 | } 20 | n/=26; 21 | } 22 | reverse(ans.begin(),ans.end()); 23 | return ans; 24 | } 25 | }; 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | Solution sol; 31 | string ans = sol.convertToTitle(n); 32 | cout< 2 | 3 | int majorityElement(int* a, int n){ 4 | int voter=a[0]; 5 | int voteCount=1; 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int titleToNumber(string s) 7 | { 8 | int sum=0; 9 | int p=s.size()-1; 10 | if(s.size() <= 1) 11 | { 12 | return (int)(s[0]-64); 13 | } 14 | for(int i=0;i>s; 25 | Solution sol; 26 | int ans = sol.titleToNumber(s); 27 | cout<= S2.Score) AS Ranking 19 | FROM Scores S2 20 | ORDER BY Ranking; -------------------------------------------------------------------------------- /0179. Largest Number/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | static bool compare(int a, int b) 8 | { 9 | return to_string(a) + to_string(b) > to_string(b) + to_string(a); 10 | } 11 | string largestNumber(vector& nums) 12 | { 13 | sort(nums.begin(), nums.end(), compare); 14 | string result = ""; 15 | int index=0; 16 | while(index < nums.size() && nums[index] == 0) index++; 17 | while(index < nums.size()) { 18 | result += to_string(nums[index]); 19 | index++; 20 | } 21 | if(result=="") return "0"; 22 | return result; 23 | } 24 | }; 25 | int main() 26 | { 27 | vector nums = {3,30,34,5,9}; 28 | Solution sol; 29 | cout< stringList = new ArrayList<>(); 6 | for(Integer i: nums) { 7 | stringList.add(i.toString()); 8 | } 9 | Collections.sort(stringList, new Comparator() { 10 | public int compare(String a, String b) { 11 | return (b + a).compareTo(a + b); 12 | } 13 | }); 14 | 15 | int index=0; 16 | String result=""; 17 | while(index < stringList.size() && stringList.get(index).equals("0")) index++; 18 | while(index < stringList.size()) { 19 | result += stringList.get(index); 20 | index++; 21 | } 22 | if(result.equals("")) return "0"; 23 | return result; 24 | } 25 | } 26 | public class solution1 { 27 | public static void main(String[] args) { 28 | int[] nums = {3,30,34,5,9}; 29 | Solution sol = new Solution(); 30 | System.out.println(sol.largestNumber(nums)); 31 | } 32 | } -------------------------------------------------------------------------------- /0179. Largest Number/solution2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public static boolean compare(String a, String b) { 5 | return a.concat(b).compareTo(b.concat(a)) > 0; 6 | } 7 | public String largestNumber(int[] nums) { 8 | List stringList = new ArrayList<>(); 9 | for(Integer i: nums) stringList.add(Integer.toString(i)); 10 | Collections.sort(stringList, (a,b) -> compare(a, b) ? -1 : 1); 11 | StringBuilder result = new StringBuilder(); 12 | int index=0; 13 | while(index < stringList.size() && stringList.get(index).equals("0")) index++; 14 | while(index < stringList.size()) { 15 | result.append(stringList.get(index)); 16 | index++; 17 | } 18 | if(result.toString().equals("")) return "0"; 19 | return result.toString(); 20 | } 21 | } 22 | public class solution2 { 23 | public static void main(String[] args) { 24 | int[] nums = {3,30,34,5,9}; 25 | Solution sol = new Solution(); 26 | System.out.println(sol.largestNumber(nums)); 27 | } 28 | } -------------------------------------------------------------------------------- /0181. Employees Earning More Than Their Managers/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE employee 2 | ( 3 | id INT PRIMARY KEY, 4 | ename VARCHAR(20), 5 | salary INT, 6 | managerId INT 7 | ); 8 | 9 | INSERT INTO employee VALUES(1,'Joe',70000,3); 10 | INSERT INTO employee VALUES(2,'Henry',80000,4); 11 | INSERT INTO employee VALUES(3,'Sam',60000,NULL); 12 | INSERT INTO employee VALUES(4,'Max',90000,NULL); 13 | 14 | SELECT * FROM employee; 15 | 16 | SELECT e.ename AS Employee FROM employee AS e 17 | WHERE salary > (SELECT salary FROM employee WHERE e.managerId = id); 18 | 19 | -------------------------------------------------------------------------------- /0181. Employees Earning More Than Their Managers/solution2.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE employee 2 | ( 3 | id INT PRIMARY KEY, 4 | ename VARCHAR(20), 5 | salary INT, 6 | managerId INT 7 | ); 8 | 9 | INSERT INTO employee VALUES(1,'Joe',70000,3); 10 | INSERT INTO employee VALUES(2,'Henry',80000,4); 11 | INSERT INTO employee VALUES(3,'Sam',60000,NULL); 12 | INSERT INTO employee VALUES(4,'Max',90000,NULL); 13 | 14 | SELECT * FROM employee; 15 | 16 | SELECT a.ename AS Employee 17 | FROM employee AS a JOIN employee AS b 18 | ON a.managerId = b.id AND a.salary > b.salary; 19 | -------------------------------------------------------------------------------- /0182. Duplicate Emails/solution1.sql: -------------------------------------------------------------------------------- 1 | 2 | -- LEETCODE 182 Duplicate Emails 3 | CREATE TABLE Person 4 | ( 5 | id INT PRIMARY KEY, 6 | email VARCHAR(20) 7 | ); 8 | 9 | INSERT INTO Person VALUES(1,'a@b.com'); 10 | INSERT INTO Person VALUES(2,'c@d.com'); 11 | INSERT INTO Person VALUES(3,'a@b.com'); 12 | 13 | SELECT email FROM Person 14 | GROUP BY email 15 | HAVING COUNT(*) > 1; -------------------------------------------------------------------------------- /0183. Customers Who Never Order/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Customers 2 | ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(20) 5 | ); 6 | CREATE TABLE orders 7 | ( 8 | id INT PRIMARY KEY, 9 | customerId INT 10 | ); 11 | 12 | INSERT INTO Customers VALUES 13 | (1,"Joe"), 14 | (2,"Henry"), 15 | (3,"Sam"), 16 | (4,"Max"); 17 | 18 | SELECT Customers.name AS Customers FROM Customers 19 | LEFT JOIN orders ON customers.id = orders.customerId 20 | WHERE orders.customerId IS NULL; -------------------------------------------------------------------------------- /0184. Department Highest Salary/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Employee 2 | ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(30), 5 | salary INT, 6 | departmentId INT, 7 | CONSTRAINT fk_departmentId FOREIGN KEY (departmentId) 8 | REFERENCES Department(id) 9 | ); 10 | 11 | CREATE TABLE Department 12 | ( 13 | id INT PRIMARY KEY, 14 | name VARCHAR(30) 15 | ); 16 | 17 | INSERT INTO employee (id, name, salary, departmentId) 18 | VALUES 19 | (1, 'Joe', 70000, 1), 20 | (2, 'Jim', 90000, 1), 21 | (3, 'Henry', 80000, 2), 22 | (4, 'Sam', 60000, 2), 23 | (5, 'Max', 90000, 1); 24 | 25 | INSERT INTO Department VALUES 26 | (1,'IT'), 27 | (2,'Sales'); 28 | 29 | SELECT d.name as Department, e.name AS Employee, e.salary AS Salary 30 | FROM Employee AS e 31 | INNER JOIN Department AS d 32 | ON e.departmentId = d.id 33 | WHERE (e.departmentId, e.salary) IN 34 | ( 35 | SELECT departmentId, MAX(salary) 36 | FROM Employee 37 | GROUP BY departmentId 38 | ); 39 | -------------------------------------------------------------------------------- /0189. Rotate Array/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void rotate(vector& v, int k) { 7 | int n = v.size(); 8 | k = k % n; 9 | reverse(v.begin(),v.end()-k); 10 | reverse(v.end()-k,v.end()); 11 | reverse(v.begin(),v.end()); 12 | } 13 | }; 14 | 15 | int main() { 16 | int n; 17 | cin>>n; 18 | vector nums(n); 19 | for(int i=0;i>nums[i]; 20 | int k; cin>>k; 21 | Solution sol; 22 | sol.rotate(nums,k); 23 | for(auto i: nums) cout<>1; 8 | } 9 | return count; 10 | }; 11 | 12 | let n = 8; 13 | console.log(hammingWeight(n)); -------------------------------------------------------------------------------- /0191. Number of 1 Bits/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int hammingWeight(uint32_t n) 7 | { 8 | int count=0; 9 | while(n!=0) 10 | { 11 | count++; 12 | n = n & (n-1); 13 | } 14 | return count; 15 | } 16 | }; 17 | int main() 18 | { 19 | uint32_t n = 00000000000000000000000000001011; 20 | Solution s; 21 | cout< 2 | using namespace std; 3 | class Solution 4 | { 5 | public: 6 | int hammingWeight(uint32_t n) 7 | { 8 | int count=0; 9 | for(int i=1;i<=32;i++) 10 | { 11 | count += n&1; 12 | n = n>>1; 13 | } 14 | return count; 15 | } 16 | }; 17 | int main() 18 | { 19 | uint32_t n = 00000000000000000000000000001011; 20 | Solution s; 21 | cout< p2.id; 13 | 14 | SELECT * FROM Person; -------------------------------------------------------------------------------- /0197. Rising Temperature/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Weather 2 | ( 3 | id INT PRIMARY KEY, 4 | recordDate DATE, 5 | temperature INT 6 | ); 7 | 8 | INSERT INTO Weather VALUES(1,'2015-01-01',10); 9 | INSERT INTO Weather VALUES(2,'2015-01-02',25); 10 | INSERT INTO Weather VALUES(3,'2015-01-03',20); 11 | INSERT INTO Weather VALUES(4,'2015-01-04',30); 12 | 13 | SELECT * FROM weather; 14 | 15 | SELECT w1.id 16 | FROM Weather w1 17 | JOIN Weather w2 ON w1.recordDate = DATE_ADD(w2.recordDate, INTERVAL 1 DAY) 18 | WHERE w1.temperature > w2.temperature; 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /0198. House Robber/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int maximumNonAdjacentSum(vector &nums) 9 | { 10 | int n = nums.size(); 11 | int prev = nums[0]; 12 | int prev2 = 0; 13 | for(int i=1;i1) 17 | take += prev2; 18 | 19 | int notTake = prev; 20 | 21 | int curIndex = max(take,notTake); 22 | prev2 = prev; 23 | prev = curIndex; 24 | } 25 | return prev; 26 | } 27 | }; 28 | int main() 29 | { 30 | vector nums = {1,2,3,4,5}; 31 | Solution s; 32 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | class Solution 6 | { 7 | public: 8 | int rec(vector &nums,int index,vector &dp) 9 | { 10 | if(index==0) return nums[index]; 11 | if(index<0) return 0; 12 | 13 | if(dp[index]!=-1) return dp[index]; 14 | 15 | int pick = nums[index] + rec(nums,index-2,dp); 16 | int notpick = 0 + rec(nums,index-1,dp); 17 | dp[index] = max(pick,notpick); 18 | return dp[index]; 19 | } 20 | int maximumNonAdjacentSum(vector &nums) 21 | { 22 | int n = nums.size(); 23 | vector dp(n,-1); 24 | return rec(nums,n-1,dp); 25 | } 26 | }; 27 | int main() 28 | { 29 | vector nums = {1,2,3,4,5}; 30 | Solution s; 31 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int solve(int n) 6 | { 7 | int ans=0; 8 | while(n) 9 | { 10 | int digit = n%10; 11 | ans += digit*digit; 12 | n/=10; 13 | } 14 | return ans; 15 | } 16 | bool isHappy(int n) 17 | { 18 | while(n>5) 19 | { 20 | n = solve(n); 21 | } 22 | if(n==1) return true; 23 | return false; 24 | } 25 | }; 26 | int main() 27 | { 28 | int n; 29 | cin>>n; 30 | Solution sol; 31 | cout< 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int solve(int n,int &sum) 6 | { 7 | if(n==0 && sum==1) return sum; 8 | if(n==0 && sum>5) 9 | { 10 | n=sum; 11 | sum=0; 12 | } 13 | if(n==0) return sum; 14 | sum += (n%10) * (n%10); 15 | solve(n/10,sum); 16 | return sum; 17 | } 18 | bool isHappy(int n) 19 | { 20 | int sum=0; 21 | int output = solve(n,sum); 22 | if(output==1) return true; 23 | return false; 24 | } 25 | }; 26 | int main() 27 | { 28 | int n = 82; 29 | Solution sol; 30 | sol.isHappy(n); 31 | } -------------------------------------------------------------------------------- /0202. Happy Number/solution3.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | int solve(int n) 6 | { 7 | if(n==0) return 0; 8 | return (n%10)*(n%10) + solve(n/10); 9 | } 10 | bool isHappy(int n) 11 | { 12 | int ans = solve(n); 13 | while(ans>5) 14 | { 15 | ans = solve(ans); 16 | } 17 | if(ans==1) return true; 18 | return false; 19 | } 20 | }; 21 | int main() 22 | { 23 | int n = 82; 24 | Solution sol; 25 | sol.isHappy(n); 26 | } -------------------------------------------------------------------------------- /0204. Count Primes/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | //Sieve of Eratosthenes 5 | class Solution { 6 | public: 7 | int countPrimes(int n) { 8 | 9 | if(n<=2) return 0; 10 | 11 | vector isPrime(n, false); 12 | isPrime[2] = true; 13 | 14 | for(int i=3;i 2 | #include 3 | #include 4 | 5 | bool isIsomorphic(char * s, char * t) 6 | { 7 | if(strlen(s) < strlen(t) || (strlen(s) > strlen(t))) return false; 8 | int s_count[strlen(s)]; 9 | int t_count[strlen(t)]; 10 | int count1[27] = {0}; 11 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isIsomorphic(string s, string t) 7 | { 8 | vector freq(255); 9 | for(int i=0;i>s>>t; 34 | Solution sol; 35 | if(sol.isIsomorphic(s,t)) cout<<"TRUE"; 36 | else cout<<"FALSE"; 37 | } -------------------------------------------------------------------------------- /0215. Kth Largest Element in an Array/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int findKthLargest(vector& nums, int k) { 7 | priority_queue q(nums.begin(),nums.end()); 8 | while(k > 1) 9 | { 10 | q.pop(); 11 | k--; 12 | } 13 | return q.top(); 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | vector nums = {3,2,1,5,6,4}; 20 | int k = 2; 21 | Solution sol; 22 | cout< left = new HashMap(); 6 | for (int i = 0; i < nums.length; i ++){ 7 | if (left.containsKey(nums[i])){ 8 | if (i - left.get(nums[i])<= k){ 9 | return true; 10 | } 11 | } 12 | left.put(nums[i], i); 13 | }//for i 14 | return false; 15 | } 16 | } 17 | public class solution1 { 18 | public static void main(String[] args) { 19 | int[] nums = new int[]{1,2,3,1}; 20 | int k = 3; 21 | Solution sol = new Solution(); 22 | if(sol.containsNearbyDuplicate(nums, k)) System.out.println("TRUE"); 23 | else System.out.println("FALSE"); 24 | } 25 | } -------------------------------------------------------------------------------- /0222. Count Complete Tree Nodes/solution1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNodes(TreeNode* root) { 4 | vector v; 5 | stack s; 6 | TreeNode* currNode = root; 7 | while(1) 8 | { 9 | while(currNode != NULL) 10 | { 11 | s.push(currNode); 12 | currNode = currNode->left; 13 | } 14 | if(s.empty()) break; 15 | TreeNode* temp = s.top(); 16 | s.pop(); 17 | v.push_back(temp->val); 18 | currNode = temp->right; 19 | } 20 | return v.size(); 21 | } 22 | }; -------------------------------------------------------------------------------- /0222. Count Complete Tree Nodes/solution2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countNodes(TreeNode* root) { 4 | if(root == NULL) return 0; 5 | 6 | int leftPart = countNodes(root->left); 7 | int rightPart = countNodes(root->right); 8 | return leftPart + rightPart + 1; 9 | } 10 | }; -------------------------------------------------------------------------------- /0231. Power of Two/solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | bool isPowerOfTwo(int n){ 6 | for(int i=0;i<=40;i++) 7 | { 8 | long x = pow(2,i); 9 | if(x==n) 10 | { 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | int main() 17 | { 18 | int n = 16; 19 | if(isPowerOfTwo(n)) printf("TRUE"); 20 | else printf("FALSE"); 21 | } -------------------------------------------------------------------------------- /0231. Power of Two/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isPowerOfTwo(int n) { 7 | if(n>=INT_MIN && n<=INT_MAX) 8 | { 9 | for(int i=0;i<=40;i++) 10 | { 11 | if(pow(2,i)==n) 12 | return true; 13 | } 14 | } 15 | return false; 16 | } 17 | }; 18 | int main() 19 | { 20 | int n = 16; 21 | Solution sol; 22 | if(sol.isPowerOfTwo(n)) cout<<"TRUE"; 23 | else cout<<"FALSE"; 24 | } -------------------------------------------------------------------------------- /0231. Power of Two/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | for(int i=0;i<=50;i++) 4 | { 5 | if(Math.pow(2,i)==n) 6 | { 7 | return true; 8 | } 9 | } 10 | return false; 11 | 12 | } 13 | } 14 | public class solution1 { 15 | public static void main(String[] args) { 16 | int n = 16; 17 | Solution sol = new Solution(); 18 | if(sol.isPowerOfTwo(n)) System.out.println("TRUE"); 19 | else System.out.println("FALSE"); 20 | } 21 | } -------------------------------------------------------------------------------- /0234. Palindrome Linked List/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(ListNode head) { 3 | ListNode temp = head; 4 | Stack s = new Stack<>(); 5 | while(temp != null) 6 | { 7 | s.push(temp.val); 8 | temp = temp.next; 9 | } 10 | temp = head; 11 | while(temp != null) 12 | { 13 | if(temp.val != s.peek()) return false; 14 | temp = temp.next; 15 | s.pop(); 16 | } 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /0242. Valid Anagram/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | bool isAnagram(string s, string t) 6 | { 7 | sort(s.begin(),s.end()); 8 | sort(t.begin(),t.end()); 9 | int l = s.size(); 10 | int k = t.size(); 11 | if(l!=k) return false; 12 | for(int i=0;i 2 | using namespace std; 3 | class Solution { 4 | public: 5 | bool isAnagram(string s, string t) 6 | { 7 | unordered_map m; 8 | if(s.size()!=t.size()) return false; 9 | for(int i=0;i 0) return false; 12 | return true; 13 | } 14 | }; 15 | int main() 16 | { 17 | string s = "anagram"; 18 | string t = "nagaram"; 19 | Solution sol; 20 | if(sol.isAnagram(s,t)) cout<<"TRUE"; 21 | else cout<<"FALSE"; 22 | } -------------------------------------------------------------------------------- /0258. Add Digits/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int addDigits(int num) { 5 | int sum=0; 6 | while(num>0) 7 | { 8 | sum += num%10; 9 | num /=10; 10 | 11 | if(num==0 && sum>9) 12 | { 13 | num=sum; 14 | sum=0; 15 | } 16 | } 17 | return sum; 18 | 19 | } 20 | } 21 | public class solution1 { 22 | public static void main(String[] args) { 23 | Scanner sc = new Scanner(System.in); 24 | int num = sc.nextInt(); 25 | Solution sol = new Solution(); 26 | System.out.println(sol.addDigits(num)); 27 | sc.close(); 28 | } 29 | } -------------------------------------------------------------------------------- /0260. Single Number III/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | class Solution { 4 | public: 5 | vector singleNumber(vector& nums) { 6 | unordered_map m; 7 | vector ans; 8 | for(int i=0;i nums{1,2,1,3,2,5}; 21 | vector ans = sol.singleNumber(nums); 22 | for(auto i: ans) cout< 2 | 3 | 4 | 5 | bool isUgly(int n){ 6 | if(n==1) 7 | return true; 8 | else if(n==0) 9 | return false; 10 | 11 | while(n!=1) 12 | { 13 | if(n%2==0) 14 | n/=2; 15 | else if(n%3==0) 16 | n/=3; 17 | else if(n%5==0) 18 | n/=5; 19 | else 20 | return false; 21 | } 22 | if(n==1) 23 | return true; 24 | 25 | return false; 26 | 27 | } 28 | int main() 29 | { 30 | int n=6; 31 | if(isUgly(n)) printf("TRUE"); 32 | else printf("FALSE"); 33 | } -------------------------------------------------------------------------------- /0263. Ugly Number/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isUgly(int n) { 7 | if(n==1) 8 | return true; 9 | else if(n==0) 10 | return false; 11 | 12 | while(n!=1) 13 | { 14 | if(n%2==0) 15 | n/=2; 16 | else if(n%3==0) 17 | n/=3; 18 | else if(n%5==0) 19 | n=n/5; 20 | else 21 | return false; 22 | } 23 | if(n==1) 24 | return true; 25 | 26 | return false; 27 | } 28 | }; 29 | int main() 30 | { 31 | int n = 6; 32 | Solution sol; 33 | if(sol.isUgly(n)) cout<<"TRUE"; 34 | else cout<<"FALSE"; 35 | } -------------------------------------------------------------------------------- /0263. Ugly Number/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public boolean isUgly(int n) { 5 | if(n==1) 6 | return true; 7 | else if(n==0) 8 | return false; 9 | 10 | while(n!=1) 11 | { 12 | if(n%2==0) 13 | n/=2; 14 | else if(n%3==0) 15 | n/=3; 16 | else if(n%5==0) 17 | n/=5; 18 | else 19 | return false; 20 | } 21 | if(n==1) 22 | return true; 23 | 24 | return false; 25 | } 26 | } 27 | public class solution1 28 | { 29 | public static void main(String[] args) 30 | { 31 | int n = 6; 32 | Solution sol = new Solution(); 33 | if(sol.isUgly(n)) System.out.println("TRUE"); 34 | else System.out.println("FALSE"); 35 | } 36 | } -------------------------------------------------------------------------------- /0268. Missing Number/solution1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int missingNumber(int* nums, int n){ 4 | int sum1 = 0; 5 | int sum2 = 0; 6 | sum1 = (n*(n+1))/2; 7 | for(int i=0;i 2 | 3 | int missingNumber(int* nums, int n){ 4 | int sum1 = 0; 5 | int sum2 = 0; 6 | for(int i=1;i<=n;i++) sum1 += i; 7 | for(int i=0;i 2 | 3 | void moveZeroes(int* arr, int n) 4 | { 5 | int index=-1; 6 | for(int i=0;i 2 | using namespace std; 3 | class Solution { 4 | public: 5 | bool wordPattern(string pattern, string s) 6 | { 7 | vector v; 8 | string t = ""; 9 | for(int i=0;i 2 | #include 3 | 4 | bool isPowerOfThree(int n){ 5 | for(int i=0;i<50;i++) 6 | { 7 | if(pow(3,i)==n) 8 | return true; 9 | } 10 | return false; 11 | 12 | } 13 | 14 | int main() 15 | { 16 | int n = 27; 17 | if(isPowerOfThree(n)) printf("TRUE"); 18 | else printf("FALSE"); 19 | } 20 | -------------------------------------------------------------------------------- /0326. Power of Three/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isPowerOfThree(int n) { 7 | for(int i=0;i<50;i++) 8 | { 9 | if(pow(3,i)==n) 10 | return true; 11 | } 12 | return false; 13 | 14 | } 15 | }; 16 | 17 | int main() 18 | { 19 | int n = 27; 20 | Solution sol; 21 | if(sol.isPowerOfThree(n)) cout<<"TRUE"; 22 | else cout<<"FALSE"; 23 | } -------------------------------------------------------------------------------- /0326. Power of Three/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfThree(int n) { 3 | for(int i=0;i<50;i++) 4 | { 5 | if(Math.pow(3,i)==n) 6 | return true; 7 | } 8 | return false; 9 | 10 | } 11 | } 12 | 13 | public class solution1 { 14 | public static void main(String[] args) { 15 | int n = 27; 16 | Solution sol = new Solution(); 17 | if(sol.isPowerOfThree(n)) System.out.println("TRUE"); 18 | else System.out.println("FALSE"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /0338. Counting Bits/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int calculateBits(int n) { 7 | int count = 0; 8 | while(n) { 9 | int k = n & 1; 10 | count += k; 11 | n = n >> 1; 12 | } 13 | return count; 14 | } 15 | vector countBits(int n) { 16 | vector result; 17 | for(int i=0;i<=n;i++) { 18 | result.push_back(calculateBits(i)); 19 | } 20 | return result; 21 | } 22 | }; 23 | 24 | int main() 25 | { 26 | int n; 27 | cin>>n; 28 | Solution sol; 29 | vector result = sol.countBits(n); 30 | for(auto i: result) cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int calculateBits(int n) { 7 | int count = 0; 8 | while(n) { 9 | count += n%2; 10 | n/=2; 11 | } 12 | return count; 13 | } 14 | vector countBits(int n) { 15 | vector result; 16 | for(int i=0;i<=n;i++) { 17 | result.push_back(calculateBits(i)); 18 | } 19 | return result; 20 | } 21 | }; 22 | 23 | int main() 24 | { 25 | int n; 26 | cin>>n; 27 | Solution sol; 28 | vector result = sol.countBits(n); 29 | for(auto i: result) cout< 2 | #include 3 | 4 | bool isPowerOfFour(int n){ 5 | for(int i=0;i<50;i++) 6 | { 7 | if(pow(4,i)==n) 8 | return true; 9 | } 10 | return false; 11 | } 12 | int main() 13 | { 14 | int n = 16; 15 | if(isPowerOfFour(n)) printf("TRUE"); 16 | else printf("FALSE"); 17 | } -------------------------------------------------------------------------------- /0342. Power of Four/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isPowerOfFour(int n) { 7 | for(int i=0;i<50;i++) 8 | { 9 | if(pow(4,i)==n) 10 | return true; 11 | } 12 | return false; 13 | 14 | } 15 | }; 16 | int main() 17 | { 18 | int n = 16; 19 | Solution sol; 20 | if(sol.isPowerOfFour(n)) cout<<"TRUE"; 21 | else cout<<"FALSE"; 22 | } -------------------------------------------------------------------------------- /0342. Power of Four/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int n) { 3 | for(int i=0;i<50;i++) 4 | { 5 | if(Math.pow(4,i)==n) 6 | return true; 7 | } 8 | return false; 9 | 10 | } 11 | } 12 | public class solution1 { 13 | public static void main(String[] args) { 14 | int n = 16; 15 | Solution sol = new Solution(); 16 | if(sol.isPowerOfFour(n)) System.out.println("TRUE"); 17 | else System.out.println("FALSE"); 18 | } 19 | } -------------------------------------------------------------------------------- /0344. Reverse String/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int start = 0; 4 | int end = s.length-1; 5 | while(start < end) 6 | { 7 | char temp = s[start]; 8 | s[start] = s[end]; 9 | s[end] = temp; 10 | start++; 11 | end--; 12 | } 13 | } 14 | } 15 | public class solution1 { 16 | public static void main(String[] args) { 17 | char[] s = {'h','e','l','l','o'}; 18 | Solution sol = new Solution(); 19 | sol.reverseString(s); 20 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector intersection(vector& nums1, vector& nums2) { 7 | vector result; 8 | for(int i=0;i nums1 = {1,2,2,1}; 24 | vector nums2 = {2,2}; 25 | Solution sol; 26 | vector result = sol.intersection(nums1, nums2); 27 | for(int i: result) cout< a = new HashSet<>(); 6 | HashSet b = new HashSet<>(); 7 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector intersection(vector& nums1, vector& nums2) { 7 | map map1, map2; 8 | vector result; 9 | for(int i=0;i nums1 = {1,2,2,1}; 22 | vector nums2 = {2,2}; 23 | Solution sol; 24 | vector result = sol.intersection(nums1, nums2); 25 | for(int i: result) cout< 2 | #include 3 | #include 4 | 5 | bool isPerfectSquare(int num){ 6 | if(num>INT_MIN && INT_MAX>num) 7 | { 8 | for(long i=0;i<=num;i++) 9 | { 10 | if(i*i==num) 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | int main() 17 | { 18 | int n = 16; 19 | if(isPerfectSquare(n)) printf("TRUE"); 20 | else printf("FALSE"); 21 | } -------------------------------------------------------------------------------- /0367. Valid Perfect Square/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isPerfectSquare(int num) { 7 | if(num>INT_MIN && numInteger.MIN_VALUE && num 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int guess(int n) 7 | { 8 | int pick = 1240808008; 9 | if(pick == n) return 0; 10 | else if(pick > n) return 1; 11 | else if(pick < n) return -1; 12 | return 111; 13 | } 14 | int guessNumber(int n) 15 | { 16 | while(n!=0) 17 | { 18 | if(guess(n)==-1) 19 | { 20 | while(guess(n)==-1) n--; 21 | } 22 | else if(guess(n) == 1) n++; 23 | else if(guess(n) == 0) return n; 24 | } 25 | return n; 26 | } 27 | }; 28 | int main() 29 | { 30 | int n = 1792997410; 31 | Solution sol; 32 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool canConstruct(string a, string b) { 7 | int arr[26] = {0}; 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int firstUniqChar(string s) { 7 | unordered_map map; 8 | for(int i=0;i map = new HashMap<>(); 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isSubsequence(string s, string t) { 7 | if(s.size() == 0) return true; 8 | if(s.size() == 1 && t.size() == 1) 9 | { 10 | if(s[0] == t[0]) return true; 11 | else return false; 12 | } 13 | int index = 0; 14 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | void leftLeavesSum(TreeNode* root,bool isleft,int &sum) 7 | { 8 | if(root==NULL) return; 9 | if(!root->left && !root->right && isleft) sum+=root->val; 10 | leftLeavesSum(root->left,1,sum); 11 | leftLeavesSum(root->right,0,sum); 12 | } 13 | int sumOfLeftLeaves(TreeNode* root) { 14 | int sum=0; 15 | leftLeavesSum(root,0,sum); 16 | return sum; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /0404. Sum of Left Leaves/solution2.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int sum=0; 4 | void findLeftLeaves(TreeNode* root,bool isLeft) 5 | { 6 | if(root==NULL) return; 7 | if(isLeft && root->left==NULL && root->right==NULL) 8 | { 9 | sum += root->val; 10 | } 11 | findLeftLeaves(root->left,true); 12 | findLeftLeaves(root->right,false); 13 | } 14 | int sumOfLeftLeaves(TreeNode* root) { 15 | if(root==NULL) return 0; 16 | findLeftLeaves(root,false); 17 | return sum; 18 | } 19 | }; -------------------------------------------------------------------------------- /0409. Longest Palindrome/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int longestPalindrome(string s) 7 | { 8 | unordered_map map; 9 | int odd = false; 10 | int result = 0; 11 | for(int i=0;i 1) { 18 | result += i.second - (i.second % 2); 19 | } 20 | } 21 | return result; 22 | } 23 | }; 24 | 25 | int main() 26 | { 27 | string s; 28 | cin>>s; 29 | Solution sol; 30 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector fizzBuzz(int n) { 7 | vector v; 8 | for(int i=1;i<=n;i++) 9 | { 10 | if(i%3==0 && i%5==0) v.push_back("FizzBuzz"); 11 | else if(i%3==0) v.push_back("Fizz"); 12 | else if(i%5==0) v.push_back("Buzz"); 13 | else v.push_back(to_string(i)); 14 | } 15 | return v; 16 | } 17 | }; 18 | int main() 19 | { 20 | int n=3; 21 | Solution sol; 22 | vector result = sol.fizzBuzz(n); 23 | for(auto i: result) cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string addStrings(string num1, string num2) { 7 | string res=""; 8 | int i=num1.size()-1,j=num2.size()-1,carry=0; 9 | while(i>=0 || j>=0) 10 | { 11 | int sum = carry; 12 | if(i>=0) sum += num1[i--]-'0'; 13 | if(j>=0) sum += num2[j--]-'0'; 14 | res += (sum%10)+'0'; 15 | carry = sum/10; 16 | } 17 | if(carry>0) res+=(carry % 10)+'0'; 18 | reverse(res.begin(),res.end()); 19 | return res; 20 | } 21 | }; 22 | int main() 23 | { 24 | string num1 = "11"; 25 | string num2 = "123"; 26 | Solution sol; 27 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | std::string addStrings(std::string num1, std::string num2) { 7 | std::string res; 8 | int i = num1.size() - 1, j = num2.size() - 1, carry = 0; 9 | while (i >= 0 || j >= 0) { 10 | int sum = carry; 11 | if (i >= 0) { 12 | sum += num1[i] - '0'; 13 | i--; 14 | } 15 | if (j >= 0) { 16 | sum += num2[j] - '0'; 17 | j--; 18 | } 19 | res += (sum % 10) + '0'; 20 | carry = sum / 10; 21 | } 22 | if (carry > 0) { 23 | res += (carry % 10) + '0'; 24 | } 25 | std::reverse(res.begin(), res.end()); 26 | return res; 27 | } 28 | }; 29 | int main() 30 | { 31 | string num1 = "11"; 32 | string num2 = "123"; 33 | Solution sol; 34 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool checkPerfectNumber(int num) { 7 | long sum=0; 8 | if(num>INT_MIN && num<99999999) 9 | { 10 | for(int i=1;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int fib(int n) { 7 | if(n==0 || n==1) return n; 8 | return fib(n-1) + fib(n-2); 9 | } 10 | }; 11 | int main() 12 | { 13 | int n = 2; 14 | Solution sol; 15 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool checkDictionary(string s, string t) { 7 | int index1=0,index2=0; 8 | while(index1 < s.size() && index2 < t.size()) { 9 | if(s[index1] == t[index2]) { 10 | index2++; 11 | } 12 | index1++; 13 | } 14 | return index2 == t.size(); 15 | } 16 | string findLongestWord(string s, vector& dictionary) { 17 | 18 | string result = ""; 19 | for(int i=0;i result.size() || (dictionary[i].size() == result.size() && dictionary[i] < result)) 22 | result = dictionary[i]; 23 | } 24 | } 25 | return result; 26 | } 27 | }; 28 | 29 | int main() 30 | { 31 | string s = "aaa"; 32 | vector dictionary = {"aaa", "aa", "a"}; 33 | Solution sol; 34 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int singleNonDuplicate(vector& nums) { 7 | unordered_map map; 8 | for(int i=0;i nums = {1,1,2,3,3,4,4,5,5}; 17 | Solution sol; 18 | cout<left, diameter); 16 | int right = dfs(node->right, diameter); 17 | diameter = max(diameter, left + right); 18 | return max(left, right) + 1; 19 | } 20 | }; -------------------------------------------------------------------------------- /0560. Subarray Sum Equals K/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int subarraySum(vector& nums, int k) { 7 | unordered_map map; 8 | map[0] = 1; 9 | int sum=0,count=0; 10 | for(int i=0;i nums = {1,1,1}; 23 | int k = 2; 24 | Solution sol; 25 | cout< map = new HashMap<>(); 6 | map.put(0,1); 7 | int prefixSum=0, count=0; 8 | for(int i=0;i= 5; 39 | 40 | -------------------------------------------------------------------------------- /0577. Employee Bonus/solution1.sql: -------------------------------------------------------------------------------- 1 | 2 | -- Leetcode 577 Employee Bonus 3 | CREATE TABLE Employee 4 | ( 5 | empId INT PRIMARY KEY, 6 | ename VARCHAR(20), 7 | supervisor INT, 8 | salary INT 9 | ); 10 | 11 | CREATE TABLE Bonus 12 | ( 13 | empid INT PRIMARY KEY, 14 | bonus INT, 15 | CONSTRAINT fk_empid FOREIGN KEY (empid) REFERENCES Employee(empId) 16 | ); 17 | INSERT INTO Employee VALUES(3,'Brad',null,4000); 18 | INSERT INTO Employee VALUES(1,'John',3,1000); 19 | INSERT INTO Employee VALUES(2,'Dan',3,2000); 20 | INSERT INTO Employee VALUES(4,'Thomas',3,4000); 21 | 22 | INSERT INTO Bonus VALUES(2,500); 23 | INSERT INTO Bonus VALUES(4,2000); 24 | 25 | 26 | SELECT employee.ename,bonus.bonus 27 | FROM employee 28 | LEFT JOIN bonus ON employee.empid = bonus.empid 29 | WHERE bonus < 1000 OR bonus IS NULL; 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /0584. Find Customer Referee/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Customer 2 | ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(30), 5 | referee_id INT 6 | ); 7 | 8 | INSERT INTO Customer VALUES 9 | (1,"Will",null), 10 | (2,"Jane",null), 11 | (3,"Alex",2), 12 | (4,"Bill",null), 13 | (5,"Zack",1), 14 | (6,"Mark",2); 15 | 16 | SELECT name FROM Customer 17 | WHERE referee_id <> 2 OR referee_id IS NULL; -------------------------------------------------------------------------------- /0585. Investments in 2016/solution1.sql: -------------------------------------------------------------------------------- 1 | Create Table If Not Exists Insurance 2 | ( 3 | pid int PRIMARY KEY, 4 | tiv_2015 float, 5 | tiv_2016 float, 6 | lat float NOT NULL, 7 | lon float NOT NULL 8 | ); 9 | 10 | insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('1', '10', '5', '10', '10'); 11 | insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('2', '20', '20', '20', '20'); 12 | insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('3', '10', '30', '20', '20'); 13 | insert into Insurance (pid, tiv_2015, tiv_2016, lat, lon) values ('4', '10', '40', '40', '40'); 14 | 15 | SELECT ROUND(SUM(DISTINCT i1.tiv_2016),2) AS tiv_2016 16 | FROM insurance i1, insurance i2 17 | WHERE i1.tiv_2015 = i2.tiv_2015 18 | AND i1.pid <> i2.pid 19 | AND i1.pid NOT IN 20 | ( 21 | SELECT DISTINCT a.pid 22 | FROM insurance a, insurance b 23 | WHERE a.pid <> b.pid 24 | AND a.lat = b.lat 25 | AND a.lon = b.lon 26 | ); 27 | 28 | 29 | -------------------------------------------------------------------------------- /0586. Customer Placing the Largest Number of Orders/solution1.sql: -------------------------------------------------------------------------------- 1 | USE Leetcode; 2 | 3 | CREATE TABLE Orders 4 | ( 5 | order_number INT PRIMARY KEY, 6 | customer_number INT 7 | ); 8 | 9 | INSERT INTO Orders VALUES(1,1); 10 | INSERT INTO Orders VALUES(2,2); 11 | INSERT INTO Orders VALUES(3,3); 12 | INSERT INTO Orders VALUES(4,3); 13 | 14 | DELETE FROM Orders; 15 | 16 | SELECT (SELECT MAX(customer_number) FROM Orders); 17 | 18 | SELECT MAX(customer_number) AS customer_number FROM Orders; 19 | 20 | SELECT customer_number FROM Orders 21 | GROUP BY customer_number 22 | ORDER BY COUNT(customer_number) DESC LIMIT 1; 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /0589. N-ary Tree Preorder Traversal/solution1.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | // Definition for a Node. 3 | class Node { 4 | public: 5 | int val; 6 | vector children; 7 | 8 | Node() {} 9 | 10 | Node(int _val) { 11 | val = _val; 12 | } 13 | 14 | Node(int _val, vector _children) { 15 | val = _val; 16 | children = _children; 17 | } 18 | }; 19 | */ 20 | 21 | class Solution { 22 | public: 23 | vector preorder(Node* root) 24 | { 25 | stack s; 26 | vector preorder; 27 | if(root==NULL) return preorder; 28 | s.push(root); 29 | while(!s.empty()) 30 | { 31 | Node* temp = s.top(); 32 | s.pop(); 33 | preorder.push_back(temp->val); 34 | for(int i=temp->children.size()-1;i>=0;i--) 35 | { 36 | s.push(temp->children[i]); 37 | } 38 | } 39 | return preorder; 40 | } 41 | }; -------------------------------------------------------------------------------- /0595. Big Countries/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE World 2 | ( 3 | name VARCHAR(20) PRIMARY KEY, 4 | continent VARCHAR(20), 5 | area INT, 6 | population INT, 7 | gdp BIGINT 8 | ); 9 | 10 | INSERT INTO World (name, continent, area, population, gdp) 11 | VALUES 12 | ('Afghanistan', 'Asia', 652230, 25500100, 20343000000), 13 | ('Albania', 'Europe', 28748, 2831741, 12960000000), 14 | ('Algeria', 'Africa', 2381741, 37100000, 188681000000), 15 | ('Andorra', 'Europe', 468, 78115, 3712000000), 16 | ('Angola', 'Africa', 1246700, 20609294, 100990000000); 17 | 18 | SELECT * FROM World; 19 | 20 | SELECT name,population,area FROM World 21 | WHERE area >= 3000000 OR 22 | population >= 25000000; 23 | -------------------------------------------------------------------------------- /0596. Classes More Than 5 Students/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Courses 2 | ( 3 | Student VARCHAR(20) PRIMARY KEY, 4 | class VARCHAR(20) 5 | ); 6 | 7 | INSERT INTO Courses VALUES 8 | ('A','Math'), 9 | ('B','English'), 10 | ('C','Math'), 11 | ('D','Biology'), 12 | ('E','Math'), 13 | ('F','Computer'), 14 | ('G','Math'), 15 | ('H','Math'), 16 | ('I','Math'); 17 | 18 | SELECT * FROM Courses; 19 | 20 | SELECT class FROM Courses 21 | GROUP BY class 22 | HAVING COUNT(class) > 4; -------------------------------------------------------------------------------- /0602. Friend Requests II Who Has the Most Friends/solution1.sql: -------------------------------------------------------------------------------- 1 | SELECT id, COUNT(id) AS num 2 | FROM ( 3 | SELECT requester_id AS id FROM RequestAccepted 4 | UNION ALL 5 | SELECT accepter_id AS id FROM RequestAccepted 6 | ) AS combined 7 | GROUP BY id 8 | ORDER BY num DESC 9 | LIMIT 1; -------------------------------------------------------------------------------- /0610. Triangle Judgement/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Triangle 2 | ( 3 | x INT, 4 | y INT, 5 | z INT, 6 | PRIMARY KEY(x,y,z) 7 | ); 8 | 9 | INSERT INTO Triangle VALUES(13,15,30),(10,20,15); 10 | 11 | 12 | SELECT x,y,z, 13 | CASE 14 | WHEN x+y<=z THEN 'No' 15 | WHEN y+z<=x THEN 'No' 16 | WHEN x+z<=y THEN 'No' 17 | ELSE 'Yes' END AS 'Triangle' 18 | FROM Triangle; 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /0617. Merge Two Binary Trees/solution1.c: -------------------------------------------------------------------------------- 1 | 2 | struct TreeNode* mergeTrees(struct TreeNode* root1, struct TreeNode* root2) 3 | { 4 | if(root1==NULL) return root2; 5 | if(root2==NULL) return root1; 6 | root1->val += root2->val; 7 | root1->left = mergeTrees(root1->left,root2->left); 8 | root1->right = mergeTrees(root1->right,root2->right); 9 | return root1; 10 | } -------------------------------------------------------------------------------- /0619. Biggest Single Number/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE MyNumbers 2 | ( 3 | num INT 4 | ); 5 | 6 | INSERT INTO MyNumbers VALUES 7 | (8), 8 | (8), 9 | (3), 10 | (3), 11 | (1), 12 | (4) 13 | (5), 14 | (6); 15 | 16 | SELECT MAX(num) AS num 17 | FROM MyNumbers 18 | WHERE num IN ( 19 | SELECT num FROM MyNumbers 20 | GROUP BY num 21 | HAVING COUNT(*)=1 22 | ); -------------------------------------------------------------------------------- /0620. Not Boring Movies/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Cinema 2 | ( 3 | id INT PRIMARY KEY, 4 | movie VARCHAR(20), 5 | descriptions VARCHAR(30), 6 | rating DECIMAL(2,1) 7 | ); 8 | 9 | INSERT INTO Cinema VALUES 10 | (1,'War','Great 3D',8.9), 11 | (2,'Science','fiction',8.5), 12 | (3,'irish','boring',6.2), 13 | (4,'Ice song','Fantacy',8.6), 14 | (5,'House card','Interesting',9.1); 15 | 16 | SELECT * FROM Cinema 17 | WHERE id%2 <> 0 AND descriptions <> 'Boring' 18 | ORDER BY rating DESC; 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /0627. Swap Salary/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Salary 2 | ( 3 | id INT PRIMARY KEY, 4 | name VARCHAR(20), 5 | sex ENUM('m','f'), 6 | salary INT 7 | ); 8 | 9 | INSERT INTO Salary VALUES(1,'A','m',2500); 10 | INSERT INTO Salary VALUES(2,'B','f',1500); 11 | INSERT INTO Salary VALUES(3,'C','m',5500); 12 | INSERT INTO Salary VALUES(4,'D','f',500); 13 | 14 | UPDATE Salary 15 | SET sex = CASE 16 | WHEN sex = 'm' THEN 'f' 17 | WHEN sex = 'f' THEN 'm' 18 | END; 19 | 20 | SELECT * FROM salary; -------------------------------------------------------------------------------- /0633. Sum of Square Numbers/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public boolean judgeSquareSum(int c) { 5 | for(int a=(int)Math.sqrt(c);a>=0;a--) { 6 | int b = (int)Math.floor(Math.sqrt(c - (a*a))); 7 | System.out.println(a*a + " " + b*b); 8 | if(a*a + b*b == c) return true; 9 | } 10 | return false; 11 | } 12 | } 13 | 14 | public class solution1 { 15 | public static void main(String[] args) { 16 | int c = 3; 17 | Solution sol = new Solution(); 18 | System.out.println(sol.judgeSquareSum(c)); 19 | } 20 | } -------------------------------------------------------------------------------- /0671. Second Minimum Node In a Binary Tree/solution1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | void insert(TreeNode* root, set &s) 4 | { 5 | if(root==NULL) return; 6 | insert(root->left,s); 7 | insert(root->right,s); 8 | s.insert(root->val); 9 | } 10 | int findSecondMinimumValue(TreeNode* root) { 11 | set s; 12 | insert(root,s); 13 | int l=1; 14 | int ans; 15 | if(s.size()==1) return -1; 16 | for(auto i: s) 17 | { 18 | ans = i; 19 | if(l-- == 0) break; 20 | } 21 | return ans; 22 | } 23 | }; -------------------------------------------------------------------------------- /0686. Repeated String Match/solution1.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itisameerkhan/Leetcode/78ba4654b814d17ff728d661c9536990c3292291/0686. Repeated String Match/solution1.exe -------------------------------------------------------------------------------- /0700. Search in a Binary Search Tree/solution1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * struct TreeNode { 4 | * int val; 5 | * TreeNode *left; 6 | * TreeNode *right; 7 | * TreeNode() : val(0), left(nullptr), right(nullptr) {} 8 | * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} 9 | * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} 10 | * }; 11 | */ 12 | class Solution { 13 | public: 14 | TreeNode* searchBST(TreeNode* root, int val) { 15 | if(root==NULL) return NULL; 16 | if(root->val == val) return root; 17 | if(val < root->val) root = searchBST(root->left,val); 18 | else if(val > root->val) root = searchBST(root->right,val); 19 | return root; 20 | } 21 | }; -------------------------------------------------------------------------------- /0704. Binary Search/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int left = 0, right = nums.length - 1; 4 | while(left <= right) { 5 | int mid = left + (right - left) / 2; 6 | 7 | if(nums[mid] == target) return mid; 8 | else if(nums[mid] < target) left = mid + 1; 9 | else if(nums[mid] > target) right = mid - 1; 10 | } 11 | return -1; 12 | } 13 | } 14 | public class solution1 { 15 | public static void main(String[] args) { 16 | int[] nums = {-1,0,3,5,9,12}; 17 | Solution sol = new Solution(); 18 | System.out.println(sol.search(nums, 13)); 19 | } 20 | } -------------------------------------------------------------------------------- /0709. To Lower Case/solution1.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String toLowerCase(String s) { 3 | String res = ""; 4 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool selfDivide(int num) 8 | { 9 | int temp = num; 10 | while (temp) 11 | { 12 | int digit = temp % 10; 13 | temp /= 10; 14 | if(digit == 0) return false; 15 | if (num % digit != 0) 16 | return false; 17 | } 18 | return true; 19 | } 20 | vector selfDividingNumbers(int left, int right) 21 | { 22 | vector result; 23 | for (int i = left; i <= right; i++) 24 | { 25 | if (selfDivide(i)) 26 | { 27 | result.push_back(i); 28 | } 29 | } 30 | return result; 31 | } 32 | }; 33 | 34 | int main() 35 | { 36 | int left = 1; 37 | int right = 22; 38 | Solution sol; 39 | vector result = sol.selfDividingNumbers(left, right); 40 | for (auto i : result) 41 | cout << i << " "; 42 | } -------------------------------------------------------------------------------- /0788. Rotated Digits/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public boolean validateRotation(int n) { 5 | int[] freq = new int[10]; 6 | Arrays.fill(freq, 0); 7 | while(n > 0) { 8 | int digit=n%10; 9 | freq[digit] = 1; 10 | n/=10; 11 | } 12 | boolean valid = false; 13 | if(freq[2]==1 || freq[5]==1 || freq[6]==1 || freq[9]==1) valid = true; 14 | if(freq[3]==1 || freq[4]==1 || freq[7]==1) valid = false; 15 | return valid; 16 | } 17 | public int rotatedDigits(int n) { 18 | int index = 1, result=0; 19 | while(index <= n) { 20 | if(validateRotation(index)) result++; 21 | index++; 22 | } 23 | return result; 24 | } 25 | } 26 | public class solution1 { 27 | public static void main(String[] args) { 28 | int n = 857; 29 | Solution sol = new Solution(); 30 | System.out.println(sol.rotatedDigits(n)); 31 | } 32 | } -------------------------------------------------------------------------------- /0859. Buddy Strings/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool buddyStrings(string a, string b) { 7 | for(int i=0;i>a>>b; 24 | Solution sol; 25 | if(sol.buddyStrings(a,b)) cout<<"true"; 26 | else cout<<"false"; 27 | } -------------------------------------------------------------------------------- /0876. Middle of the Linked List/solution1.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | ListNode* middleNode(ListNode* head) { 4 | typedef ListNode Node; 5 | Node *fast, *slow; 6 | fast = slow = head; 7 | while(fast != NULL) 8 | { 9 | fast = fast->next; 10 | if(fast != NULL) 11 | { 12 | fast = fast->next; 13 | slow = slow->next; 14 | } 15 | } 16 | return slow; 17 | } 18 | }; -------------------------------------------------------------------------------- /0896. Monotonic Array/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool isMonotonic(vector& nums) { 7 | int increase=1,decrease=1, prev = nums[0]; 8 | for(int i=1;i nums[i]){ 14 | decrease++; 15 | prev = nums[i]; 16 | } 17 | else { 18 | increase++; 19 | decrease++; 20 | prev = nums[i]; 21 | } 22 | } 23 | return increase == nums.size() || decrease == nums.size(); 24 | } 25 | }; 26 | int main() 27 | { 28 | vector nums = {1,3,2}; 29 | Solution sol; 30 | if(sol.isMonotonic(nums)) cout<<"TRUE"; 31 | else cout<<"FALSE"; 32 | } -------------------------------------------------------------------------------- /0921. Minimum Add to Make Parentheses Valid/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int minAddToMakeValid(String s) { 5 | Stack stack = new Stack<>(); 6 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector sortedSquares(vector& nums) { 7 | multiset m; 8 | for(auto i: nums) 9 | m.insert(i*i); 10 | 11 | vector v; 12 | for(auto i: m) 13 | v.push_back(i); 14 | 15 | return v; 16 | } 17 | }; 18 | 19 | int main() 20 | { 21 | vector nums = {-4,-1,0,3,10}; 22 | Solution sol; 23 | vector result = sol.sortedSquares(nums); 24 | for(auto i: result) cout<= 3; -------------------------------------------------------------------------------- /1068. Product Sales Analysis I/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Sales 2 | ( 3 | sale_id INT PRIMARY KEY, 4 | product_id INT, 5 | year INT, 6 | quantity INT, 7 | price INT, 8 | CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES Product(product_id) 9 | ); 10 | 11 | CREATE TABLE Product 12 | ( 13 | product_id INT PRIMARY KEY, 14 | product_name VARCHAR(20) 15 | ); 16 | 17 | INSERT INTO Sales VALUES 18 | (1,100,2008,10,5000), 19 | (2,100,2009,12,5000), 20 | (7,200,2011,15,9000); 21 | 22 | INSERT INTO Product VALUES 23 | (100,"Nokia"), 24 | (200,"Apple"), 25 | (300,"Samsung"); 26 | 27 | SELECT product.product_name,sales.year,sales.price 28 | FROM sales 29 | LEFT JOIN product 30 | ON sales.product_id = product.product_id; -------------------------------------------------------------------------------- /1075. Project Employees I/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE project 2 | ( 3 | project_id INT, 4 | employee_id INT, 5 | PRIMARY KEY (project_id,employee_id), 6 | CONSTRAINT fk_employee_id FOREIGN KEY (employee_id) REFERENCES Employee(employee_id) 7 | ); 8 | 9 | CREATE TABLE Employee 10 | ( 11 | employee_id INT PRIMARY KEY, 12 | name VARCHAR(20), 13 | experience_years INT NOT NULL 14 | ); 15 | 16 | INSERT INTO project VALUES 17 | (1,1), 18 | (1,2), 19 | (1,3), 20 | (2,1), 21 | (2,4); 22 | 23 | INSERT INTO employee VALUES 24 | (1,"Khaled",3), 25 | (2,"Ali",2), 26 | (3,"John",1), 27 | (4,"Doe",2); 28 | 29 | SELECT project.project_id,ROUND(AVG(experience_years),2) AS average_years 30 | FROM project 31 | LEFT JOIN employee 32 | ON project.employee_id = employee.employee_id 33 | GROUP BY project_id; 34 | 35 | -------------------------------------------------------------------------------- /1084. Sales Analysis III/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Product 2 | ( 3 | product_id INT PRIMARY KEY, 4 | product_name VARCHAR(20), 5 | unit_price INT 6 | ); 7 | 8 | CREATE TABLE sales 9 | ( 10 | seller_id INT, 11 | product_id INT, 12 | buyer_id INT, 13 | sale_date DATE, 14 | quantity INT, 15 | price INT, 16 | CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES product(product_id) 17 | ); 18 | 19 | INSERT INTO product VALUES 20 | (1,"S8",1000), 21 | (2,"G4",800), 22 | (3,"iPhone",1400); 23 | 24 | INSERT INTO sales VALUES 25 | (1,1,1,"2019-01-21",2,2000), 26 | (1,2,2,"2019-02-17",1,800), 27 | (2,2,3,"2019-06-02",1,800), 28 | (3,3,4,"2019-05-12",2,2800); 29 | 30 | SELECT product_id,product_name FROM sales 31 | INNER JOIN product USING(product_id) 32 | GROUP BY product_id 33 | HAVING SUM(IF(sale_date BETWEEN "2019-01-01" 34 | AND "2019-03-31",1,0)) = SUM(IF(sale_date,1,0)); -------------------------------------------------------------------------------- /1084. Sales Analysis III/solution2.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Product 2 | ( 3 | product_id INT PRIMARY KEY, 4 | product_name VARCHAR(20), 5 | unit_price INT 6 | ); 7 | 8 | CREATE TABLE sales 9 | ( 10 | seller_id INT, 11 | product_id INT, 12 | buyer_id INT, 13 | sale_date DATE, 14 | quantity INT, 15 | price INT, 16 | CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES product(product_id) 17 | ); 18 | 19 | INSERT INTO product VALUES 20 | (1,"S8",1000), 21 | (2,"G4",800), 22 | (3,"iPhone",1400); 23 | 24 | INSERT INTO sales VALUES 25 | (1,1,1,"2019-01-21",2,2000), 26 | (1,2,2,"2019-02-17",1,800), 27 | (2,2,3,"2019-06-02",1,800), 28 | (3,3,4,"2019-05-12",2,2800); 29 | 30 | SELECT product_id,product_name FROM product NATURAL JOIN sales 31 | GROUP BY product_id 32 | HAVING MIN(sale_date) >= "2019-01-01" AND MAX(sale_date) <= "2019-03-31"; 33 | 34 | -------------------------------------------------------------------------------- /1089. Duplicate Zeros/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | void duplicateZeros(vector &nums) 8 | { 9 | vector result; 10 | 11 | for (int i = 0; i < nums.size(); i++) 12 | { 13 | if (result.size() >= nums.size()) 14 | break; 15 | 16 | if (nums[i] == 0) 17 | { 18 | result.push_back(0); 19 | 20 | if (result.size() >= nums.size()) 21 | break; 22 | 23 | result.push_back(0); 24 | } 25 | else 26 | { 27 | result.push_back(nums[i]); 28 | } 29 | } 30 | for (int i = 0; i < result.size(); i++) 31 | { 32 | nums[i] = result[i]; 33 | } 34 | } 35 | }; 36 | int main() 37 | { 38 | vector nums = {8,4,5,0,0,0,0,7}; 39 | Solution sol; 40 | sol.duplicateZeros(nums); 41 | for (auto i : nums) 42 | cout << i << " "; 43 | } -------------------------------------------------------------------------------- /1089. Duplicate Zeros/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | void duplicateZeros(vector &nums) 8 | { 9 | int n = nums.size(); 10 | for (int i = 0; i < nums.size(); i++) 11 | { 12 | if (nums[i] == 0) 13 | { 14 | nums.insert(nums.begin() + i, 0); 15 | i++; 16 | } 17 | } 18 | nums.resize(n); 19 | } 20 | }; 21 | int main() 22 | { 23 | vector nums = {8, 4, 5, 0, 0, 0, 0, 7}; 24 | Solution sol; 25 | sol.duplicateZeros(nums); 26 | for (auto i : nums) 27 | cout << i << " "; 28 | } -------------------------------------------------------------------------------- /1141. User Activity for the Past 30 Days I/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE activity 2 | ( 3 | user_id INT, 4 | session_id INT, 5 | activity_date DATE, 6 | activity_type ENUM('open_session','end_session', 7 | 'scroll_down','send_message') 8 | ); 9 | 10 | INSERT INTO activity (user_id, session_id, activity_date, activity_type) 11 | VALUES 12 | (1, 1, '2019-07-20', 'open_session'), 13 | (1, 1, '2019-07-20', 'scroll_down'), 14 | (1, 1, '2019-07-20', 'end_session'), 15 | (2, 4, '2019-07-20', 'open_session'), 16 | (2, 4, '2019-07-21', 'send_message'), 17 | (2, 4, '2019-07-21', 'end_session'), 18 | (3, 2, '2019-07-21', 'open_session'), 19 | (3, 2, '2019-07-21', 'send_message'), 20 | (3, 2, '2019-07-21', 'end_session'), 21 | (4, 3, '2019-06-25', 'open_session'), 22 | (4, 3, '2019-06-25', 'end_session'); 23 | 24 | SELECT * FROM activity; 25 | 26 | SELECT DISTINCT activity_date AS day, COUNT(DISTINCT user_id) AS active_users 27 | FROM activity 28 | WHERE activity_date BETWEEN '2019-06-28' AND '2019-07-27' 29 | GROUP BY activity_date; -------------------------------------------------------------------------------- /1148. Article Views I/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Views 2 | ( 3 | article_id INT, 4 | author_id INT, 5 | viewer_id INT, 6 | view_date DATE 7 | ); 8 | 9 | INSERT INTO views (article_id, author_id, viewer_id, view_date) 10 | VALUES 11 | (1, 3, 5, '2019-08-01'), 12 | (1, 3, 6, '2019-08-02'), 13 | (2, 7, 7, '2019-08-01'), 14 | (2, 7, 6, '2019-08-02'), 15 | (4, 7, 1, '2019-07-22'), 16 | (3, 4, 4, '2019-07-21'), 17 | (3, 4, 4, '2019-07-21'); 18 | 19 | SELECT DISTINCT(author_id) AS id 20 | FROM views 21 | WHERE author_id = viewer_id 22 | ORDER BY author_id; 23 | -------------------------------------------------------------------------------- /1179. Reformat Department Table/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Department 2 | ( 3 | id INT, 4 | revenue INT, 5 | month VARCHAR(3), 6 | PRIMARY KEY(id,month) 7 | ); 8 | 9 | INSERT INTO Department VALUES 10 | (1,8000,"Jan"), 11 | (2,9000,"Jan"), 12 | (3,10000,"Feb"), 13 | (1,7000,"Feb"), 14 | (1,6000,"Mar"); 15 | 16 | SELECT id, 17 | SUM(IF(month="Jan",revenue,null)) AS Jan_Revenue, 18 | SUM(IF(month="Feb",revenue,null)) AS Feb_Revenue, 19 | SUM(IF(month="Mar",revenue,null)) AS Mar_Revenue, 20 | SUM(IF(month="Apr",revenue,null)) AS Apr_Revenue, 21 | SUM(IF(month="May",revenue,null)) AS May_Revenue, 22 | SUM(IF(month="Jun",revenue,null)) AS Jun_Revenue, 23 | SUM(IF(month="Jul",revenue,null)) AS Jul_Revenue, 24 | SUM(IF(month="Aug",revenue,null)) AS Aug_Revenue, 25 | SUM(IF(month="Sep",revenue,null)) AS Sep_Revenue, 26 | SUM(IF(month="Oct",revenue,null)) AS Oct_Revenue, 27 | SUM(IF(month="Nov",revenue,null)) AS Nov_Revenue, 28 | SUM(IF(month="Dec",revenue,null)) AS Dec_Revenue 29 | FROM Department 30 | GROUP BY id; 31 | 32 | -------------------------------------------------------------------------------- /1193. Monthly Transactions I/solution2.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Transactions ( 2 | id INT PRIMARY KEY, 3 | country VARCHAR(255), 4 | state ENUM('approved','declined'), 5 | amount INT, 6 | trans_date DATE 7 | ); 8 | 9 | insert into Transactions (id, country, state, amount, trans_date) values ('121', 'US', 'approved', '1000', '2018-12-18'); 10 | insert into Transactions (id, country, state, amount, trans_date) values ('122', 'US', 'declined', '2000', '2018-12-19'); 11 | insert into Transactions (id, country, state, amount, trans_date) values ('123', 'US', 'approved', '2000', '2019-01-01'); 12 | insert into Transactions (id, country, state, amount, trans_date) values ('124', 'DE', 'approved', '2000', '2019-01-07'); 13 | 14 | SELECT 15 | DATE_FORMAT(trans_date, '%Y-%m') AS month, 16 | country, 17 | COUNT(*) AS trans_count, 18 | SUM(state = 'approved') AS approved_count, 19 | SUM(amount) AS trans_total_amount, 20 | SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount 21 | FROM transactions 22 | GROUP BY 23 | DATE_FORMAT(trans_date, '%Y-%m'), 24 | country; 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /1211. Queries Quality and Percentage/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Queries 2 | ( 3 | query_name VARCHAR(50), 4 | result VARCHAR(50), 5 | position INT, 6 | rating INT 7 | ); 8 | 9 | INSERT INTO queries VALUES 10 | ('Dog','Golden Retriever',1,5), 11 | ('Dog','German Shepard',2,5), 12 | ('Dog','Mule',200,1), 13 | ('Cat','Shirazi',5,2), 14 | ('Cat','Siamese',3,3), 15 | ('Cat','Sphynx',7,4); 16 | 17 | SELECT query_name, 18 | ROUND(AVG(CAST(rating AS DECIMAL)/position),2) AS quality, 19 | ROUND(COUNT(CASE WHEN rating < 3 THEN 1 END)*100 / COUNT(*),2) 20 | AS poor_query_percentage 21 | FROM queries 22 | WHERE query_name IS NOT NULL 23 | GROUP BY query_name; 24 | 25 | -------------------------------------------------------------------------------- /1232. Check If It Is a Straight Line/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | bool checkStraightLine(vector> &coordinates) 8 | { 9 | int x0 = coordinates[0][0]; 10 | int y0 = coordinates[0][1]; 11 | int x1 = coordinates[1][0]; 12 | int y1 = coordinates[1][1]; 13 | 14 | for(int i=2;i>n; 28 | vector> coordinates; 29 | for(int i=0;i>x>>y; 33 | coordinates.push_back({x,y}); 34 | } 35 | Solution sol; 36 | if(sol.checkStraightLine(coordinates)) cout<<"TRUE"; 37 | else cout<<"FALSE"; 38 | } 39 | -------------------------------------------------------------------------------- /1251. Average Selling Price/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Prices 2 | ( 3 | product_id INT, 4 | start_date DATE, 5 | end_date DATE, 6 | price INT, 7 | PRIMARY KEY(product_id,start_date,end_date) 8 | ); 9 | 10 | CREATE TABLE UnitsSold 11 | ( 12 | product_id INT, 13 | purchase_date DATE, 14 | units INT 15 | ); 16 | 17 | INSERT INTO prices (product_id, start_date, end_date, price) 18 | VALUES 19 | (1, '2019-02-17', '2019-02-28', 5), 20 | (1, '2019-03-01', '2019-03-22', 20), 21 | (2, '2019-02-01', '2019-02-20', 15), 22 | (2, '2019-02-21', '2019-03-31', 30); 23 | 24 | INSERT INTO unitssold (product_id, purchase_date, units) 25 | VALUES 26 | (1, '2019-02-25', 100), 27 | (1, '2019-03-01', 15), 28 | (2, '2019-02-10', 200), 29 | (2, '2019-03-22', 30); 30 | 31 | SELECT p.product_id, 32 | IFNULL(ROUND(SUM(u.units * p.price)/SUM(u.units),2),0) AS average_price 33 | FROM prices AS p 34 | JOIN unitssold u 35 | ON (p.product_id = u.product_id 36 | AND u.purchase_date BETWEEN p.start_date AND p.end_date) 37 | GROUP BY p.product_id; 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /1287. Element Appearing More Than 25% In Sorted Array/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int findSpecialInteger(vector& nums) { 7 | int maxValue = -1; 8 | int count=1,max=nums[0]; 9 | for(int i=0;i maxValue) { 15 | maxValue = value; 16 | max = nums[i]; 17 | } 18 | if(current != next) count = 1; 19 | } 20 | return max; 21 | } 22 | }; 23 | int main() 24 | { 25 | vector nums = {1,2,2,6,6,6,6,7,10}; 26 | Solution sol; 27 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int findSpecialInteger(vector& nums) { 7 | unordered_map map; 8 | for(int i=0;i maxValue) { 14 | maxValue = i.second; 15 | result = i.first; 16 | } 17 | } 18 | return result; 19 | } 20 | }; 21 | int main() 22 | { 23 | vector nums = {1,2,2,6,6,6,6,7,10}; 24 | Solution sol; 25 | cout<next; 16 | } 17 | temp = head; 18 | long long int decimal = 0; 19 | while(temp != NULL) 20 | { 21 | decimal = decimal + (temp->val * pow(2,--index)); 22 | temp = temp->next; 23 | } 24 | return decimal; 25 | } -------------------------------------------------------------------------------- /1290. Convert Binary Number in a Linked List to Integer/solution1.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode() : val(0), next(nullptr) {} 7 | * ListNode(int x) : val(x), next(nullptr) {} 8 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 9 | * }; 10 | */ 11 | class Solution { 12 | public: 13 | int getDecimalValue(ListNode* head) { 14 | typedef ListNode Node; 15 | Node *temp = head; 16 | long long int decimal = 0; 17 | int i=-1; 18 | stack s; 19 | while(temp != NULL) 20 | { 21 | s.push(temp->val); 22 | temp = temp->next; 23 | } 24 | while(!s.empty()) 25 | { 26 | decimal = decimal + (s.top() * pow(2,++i)); 27 | s.pop(); 28 | } 29 | return decimal; 30 | } 31 | }; -------------------------------------------------------------------------------- /1291. Sequential Digits/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector sequentialDigits(int low, int high) { 7 | vector result; 8 | for(int i=1;i<=9;i++) { 9 | int num = i; 10 | int nextDigit = i + 1; 11 | while(num <= high && nextDigit <= 9 ) { 12 | num = num*10 + nextDigit; 13 | nextDigit++; 14 | if(low <= num && num<=high) result.push_back(num); 15 | } 16 | } 17 | sort(result.begin(), result.end()); 18 | return result; 19 | } 20 | }; 21 | int main() 22 | { 23 | int low = 100, high = 300; 24 | Solution sol; 25 | vector result = sol.sequentialDigits(low, high); 26 | for(auto i: result) cout< sequentialDigits(int low, int high) { 5 | List result = new ArrayList<>(); 6 | for(int i=1;i<=9;i++) { 7 | int num = i; 8 | int nextDigit = i + 1; 9 | while(num <= high && nextDigit <= 9) { 10 | num = num*10 + nextDigit; 11 | nextDigit++; 12 | if(low <= num && num<=high) result.add(num); 13 | } 14 | } 15 | Collections.sort(result); 16 | return result; 17 | } 18 | } 19 | public class solution1 { 20 | public static void main(String[] args) { 21 | int low=100, high=300; 22 | Solution sol = new Solution(); 23 | List result = sol.sequentialDigits(low, high); 24 | for(Integer i: result) System.out.print(i + " "); 25 | } 26 | } -------------------------------------------------------------------------------- /1291. Sequential Digits/solution2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public List sequentialDigits(int low, int high) { 5 | List result = new ArrayList<>(); 6 | int[] numList = {12,23,34,45,56,67,78,89,123,234,345,456,567,678,789,1234, 7 | 2345,3456,4567,5678,6789,12345,23456,34567,45678,56789,123456,234567, 8 | 345678,456789,1234567,2345678,3456789,12345678,23456789,123456789}; 9 | 10 | for(int i=0;i result = sol.sequentialDigits(low, high); 21 | for(Integer i: result) System.out.print(i + " "); 22 | } 23 | } -------------------------------------------------------------------------------- /1323. Maximum 69 Number/solution1.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int maximum69Number (int num) { 5 | String s = Integer.toString(num); 6 | char[] ch = s.toCharArray(); 7 | for(int i=0;i 0) freq[s.charAt(i)-'a']--; 11 | else if(freq[s.charAt(i) - 'a'] == 0) count++; 12 | } 13 | return count; 14 | } 15 | } 16 | public class solution1 { 17 | public static void main(String[] args) { 18 | String s = "leetcode"; 19 | String t = "practice"; 20 | Solution sol = new Solution(); 21 | System.out.println(sol.minSteps(s, t)); 22 | } 23 | } -------------------------------------------------------------------------------- /1347. Minimum Number of Steps to Make Two Strings Anagram/solution2.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class Solution { 4 | public int minSteps(String s, String t) { 5 | int count=0; 6 | int[] freq = new int[26]; 7 | Arrays.fill(freq, 0); 8 | for(int i=0;i 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int countNegatives(vector>& grid) 7 | { 8 | int count=0; 9 | for(auto i: grid) 10 | { 11 | for(auto j:i) 12 | { 13 | if(j<0) count++; 14 | } 15 | } 16 | return count; 17 | } 18 | }; 19 | 20 | int main() 21 | { 22 | vector> grid {{4,3,2,-1},{3,2,1,-1},{1,1,-1,-2},{-1,-1,-2,-3}}; 23 | Solution sol; 24 | cout< 2 | 3 | class Solution { 4 | public: 5 | int countNegatives(vector>& grid) 6 | { 7 | int row = grid.size()-1; 8 | int col = grid[0].size()-1; 9 | int count=0; 10 | while(grid[row][col] < 0) 11 | { 12 | count++; 13 | col--; 14 | if(col==-1) 15 | { 16 | row--; 17 | col = grid[0].size(); 18 | } 19 | } 20 | return count; 21 | } 22 | }; 23 | int main() 24 | { 25 | 26 | } -------------------------------------------------------------------------------- /1356. Sort Integers By The Number of 1 Bits/solution2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | static int countBits(int num) { 7 | int count = 0; 8 | while(num) { 9 | count += num & 1; 10 | num >>= 1; 11 | } 12 | return count; 13 | } 14 | static bool compare(int a, int b) { 15 | int countA = countBits(a); 16 | int countB = countBits(b); 17 | 18 | if(countA == countB) return a < b; 19 | 20 | return countA < countB; 21 | 22 | } 23 | vector sortByBits(vector& arr) { 24 | sort(arr.begin(),arr.end(), compare); 25 | return arr; 26 | } 27 | }; 28 | 29 | int main() { 30 | int n; 31 | cin>>n; 32 | vector arr(n); 33 | for(int i=0;i>arr[i]; 34 | Solution sol; 35 | vector result = sol.sortByBits(arr); 36 | for(auto i: result) cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxProduct(vector& nums) { 7 | int maxValue = 0; 8 | for(int i=0;i maxValue) maxValue = temp; 12 | } 13 | } 14 | return maxValue; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | vector nums = {3,4,5,2}; 21 | Solution sol; 22 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int maxProduct(vector& nums) { 7 | int currentMax = nums[0]; 8 | int secondMax = 0; 9 | for(int i=1;i= currentMax) { 11 | secondMax = currentMax; 12 | currentMax = nums[i]; 13 | } else if(nums[i] >= secondMax) { 14 | secondMax = nums[i]; 15 | } 16 | } 17 | return (currentMax - 1) * (secondMax - 1); 18 | } 19 | }; 20 | 21 | int main() 22 | { 23 | vector nums = {3,4,5,2}; 24 | Solution sol; 25 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | double average(vector& salary) { 7 | sort(salary.begin(),salary.end()); 8 | double ans = 0; 9 | int t=0; 10 | for(int i=1;i salary = {1000, 2000, 3000}; 21 | Solution sol; 22 | cout< 2 | 3 | int numIdenticalPairs(int* nums, int n){ 4 | int count=0; 5 | for(int i=0;i 2 | 3 | int numIdenticalPairs(int* nums, int n){ 4 | int count=0; 5 | for(int i=0;i 2 | 3 | int countOdds(int low, int high) 4 | { 5 | int n = high-low+1; 6 | if(low%2==1 && high%2==1) return (n/2)+1; 7 | else if(low%2==0 && high%2==0) return n/2; 8 | else if((low%2==1 && high%2==0) || (low%2==0 && high%2==1)) return n/2; 9 | return high-low+1; 10 | } 11 | int main() 12 | { 13 | int low = 3; 14 | int high = 7; 15 | printf("%d",countOdds(low, high)); 16 | } -------------------------------------------------------------------------------- /1527. Patients With a Condition/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Patients 2 | ( 3 | patient_id INT PRIMARY KEY, 4 | patient_name VARCHAR(30), 5 | conditions VARCHAR(50) 6 | ); 7 | 8 | INSERT INTO Patients VALUES(1,'Daniel','YFEV COUGH'); 9 | INSERT INTO Patients(patient_id,patient_name) VALUES(2,'Alice'); 10 | INSERT INTO Patients VALUES(3,'Bob','DIAB100 MYOP'); 11 | INSERT INTO Patients VALUES(4,'George','ACNE DIAB100'); 12 | INSERT INTO Patients VALUES(5,'Alain','DIAB201'); 13 | 14 | SELECT * FROM patients 15 | WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'; 16 | 17 | 18 | -------------------------------------------------------------------------------- /1528. Shuffle String/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution 5 | { 6 | public: 7 | int shuffled_string(string s) 8 | { 9 | vector num = {"one","zero","two","three","four","five","six","seven","eight","nine","ten"}; 10 | map m; 11 | for(int i=0;i>t; 26 | Solution sol; 27 | while(t--) 28 | { 29 | string s; 30 | cin>>s; 31 | int ans = sol.shuffled_string(s); 32 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int checkDigit(vector> &mat, int row, int col, int rowSize,int colSize) { 7 | for(int i=0;i>& mat) { 16 | int result = 0; 17 | for(int i=0;i> mat = {{1,0,0}, 31 | {0,0,1}, 32 | {1,0,0}}; 33 | Solution sol; 34 | cout< 10000; 38 | 39 | -------------------------------------------------------------------------------- /1633. Percentage of Users Attended a Contest/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Users 2 | ( 3 | user_id INT PRIMARY KEY, 4 | user_name VARCHAR(20) 5 | ); 6 | 7 | CREATE TABLE Register 8 | ( 9 | contest_id INT, 10 | user_id INT, 11 | PRIMARY KEY(contest_id,user_id) 12 | ); 13 | 14 | INSERT INTO users (user_id, user_name) VALUES 15 | (6, 'Alice'), 16 | (2, 'Bob'), 17 | (7, 'Alex'); 18 | 19 | INSERT INTO register (contest_id, user_id) 20 | VALUES 21 | (215, 6), 22 | (209, 2), 23 | (208, 2), 24 | (210, 6), 25 | (208, 6), 26 | (209, 7), 27 | (209, 6), 28 | (215, 7), 29 | (208, 7), 30 | (210, 2), 31 | (207, 2), 32 | (210, 7); 33 | 34 | 35 | SELECT contest_id, ROUND((COUNT(DISTINCT user_id)/(SELECT COUNT(user_id) FROM Users)*100),2) AS percentage 36 | FROM Register 37 | GROUP BY contest_id 38 | ORDER BY percentage DESC,contest_id ASC; -------------------------------------------------------------------------------- /1661. Average Time of Process per Machine/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Activity 2 | ( 3 | machine_id INT, 4 | process_id INT, 5 | activity_type ENUM('start','end'), 6 | timestamp FLOAT, 7 | PRIMARY KEY(machine_id,process_id,activity_type) 8 | ); 9 | 10 | INSERT INTO activity (machine_id, process_id, activity_type, timestamp) 11 | VALUES 12 | (0, 0, 'start', 0.712), 13 | (0, 0, 'end', 1.520), 14 | (0, 1, 'start', 3.140), 15 | (0, 1, 'end', 4.120), 16 | (1, 0, 'start', 0.550), 17 | (1, 0, 'end', 1.550), 18 | (1, 1, 'start', 0.430), 19 | (1, 1, 'end', 1.420), 20 | (2, 0, 'start', 4.100), 21 | (2, 0, 'end', 4.512), 22 | (2, 1, 'start', 2.500), 23 | (2, 1, 'end', 5.000); 24 | 25 | SELECT a1.machine_id, ROUND(AVG(a2.timestamp - a1.timestamp),3) AS processing_time 26 | FROM activity a1 27 | JOIN activity a2 28 | ON a1.process_id = a2.process_id 29 | AND a1.machine_id = a2.machine_id 30 | AND a1.timestamp < a2.timestamp 31 | GROUP BY a1.machine_id; 32 | -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | bool arrayStringsAreEqual(vector& word1, vector& word2) { 7 | string s1 = "",s2=""; 8 | for(int i=0;i word1 = {"ab", "c"}; 16 | vector word2 = {"a", "bc"}; 17 | Solution sol; 18 | if(sol.arrayStringsAreEqual(word1,word2)) cout<<"TRUE"; 19 | else cout<<"FALSE"; 20 | } -------------------------------------------------------------------------------- /1662. Check If Two String Arrays are Equivalent/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {string[]} word1 3 | * @param {string[]} word2 4 | * @return {boolean} 5 | */ 6 | var arrayStringsAreEqual = function(word1, word2) { 7 | const result1 = word1.join(''); 8 | const result2 = word2.join(''); 9 | return result1 === result2; 10 | }; 11 | 12 | const word1 = ["ab", "c"]; 13 | const word2 = ["a", "bc"]; 14 | console.log(arrayStringsAreEqual(word1, word2)); -------------------------------------------------------------------------------- /1667. Fix Names in a Table/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Users 2 | ( 3 | user_id INT PRIMARY KEY, 4 | name VARCHAR(30) 5 | ); 6 | 7 | INSERT INTO Users VALUES(1,'aLice'),(2,'bOB'); 8 | 9 | SELECT user_id, CONCAT(UCASE(SUBSTRING(name,1,1)),LCASE(SUBSTRING(name,2))) AS name 10 | FROM Users 11 | ORDER BY user_id; -------------------------------------------------------------------------------- /1683. Invalid Tweets/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Tweets 2 | ( 3 | tweet_id INT PRIMARY KEY, 4 | content VARCHAR(50) 5 | ); 6 | 7 | INSERT INTO Tweets VALUES(1,'Vote for Biden'); 8 | INSERT INTO Tweets VALUES(2,'Let us make America great again'); 9 | 10 | SELECT tweet_id FROM Tweets 11 | WHERE LENGTH(content) > 15; -------------------------------------------------------------------------------- /1693. Daily Leads and Partners/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE DailySales 2 | ( 3 | date_id DATE, 4 | make_name VARCHAR(30), 5 | lead_id INT, 6 | partner_id INT 7 | ); 8 | 9 | INSERT INTO DailySales (date_id, make_name, lead_id, partner_id) 10 | VALUES 11 | ('2020-12-8', 'toyota', 0, 1), 12 | ('2020-12-8', 'toyota', 1, 0), 13 | ('2020-12-8', 'toyota', 1, 2), 14 | ('2020-12-7', 'toyota', 0, 2), 15 | ('2020-12-7', 'toyota', 0, 1), 16 | ('2020-12-8', 'honda', 1, 2), 17 | ('2020-12-8', 'honda', 2, 1), 18 | ('2020-12-7', 'honda', 0, 1), 19 | ('2020-12-7', 'honda', 1, 2), 20 | ('2020-12-7', 'honda', 2, 1); 21 | 22 | SELECT date_id, make_name, COUNT(DISTINCT lead_id) AS unique_leads, 23 | COUNT(DISTINCT partner_id) AS unique_partners 24 | FROM DailySales 25 | GROUP BY date_id, make_name; 26 | 27 | -------------------------------------------------------------------------------- /1729. Find Followers Count/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Followers 2 | ( 3 | user_id INT, 4 | follower_id INT, 5 | PRIMARY KEY(user_id,follower_id) 6 | ); 7 | 8 | INSERT INTO Followers VALUES(0,1),(1,0),(2,0),(2,1); 9 | 10 | SELECT user_id, COUNT(follower_id) AS follower_count 11 | FROM Followers 12 | GROUP BY user_id 13 | ORDER BY user_id; 14 | 15 | use leetcode; -------------------------------------------------------------------------------- /1731. The Number of Employees Which Report to Each Employee/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Employees 2 | ( 3 | employee_id INT PRIMARY KEY, 4 | name VARCHAR(30), 5 | reports_to INT NULL, 6 | age INT 7 | ); 8 | INSERT INTO employees (employee_id, name, reports_to, age) 9 | VALUES 10 | (9, 'Hercy', NULL, 43), 11 | (6, 'Alice', 9, 41), 12 | (4, 'Bob', 9, 36), 13 | (2, 'Winston', NULL, 37); 14 | 15 | 16 | select e1.employee_id, e1.name, COUNT(e2.reports_to) AS reports_count, 17 | ROUND(AVG(e2.age*1.00),0) AS average_age 18 | from employees as e1 19 | join employees e2 20 | on e1.employee_id = e2.reports_to 21 | GROUP BY e1.employee_id, e1.name 22 | HAVING COUNT(e2.reports_to) >= 1 23 | ORDER BY employee_id; 24 | 25 | 26 | -------------------------------------------------------------------------------- /1757. Recyclable and Low Fat Products/solution1.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT product_id FROM Products 3 | WHERE low_fats = 'Y' AND recyclable = 'Y'; -------------------------------------------------------------------------------- /1768. Merge Strings Alternately/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string mergeAlternately(string word1, string word2) 7 | { 8 | string result = ""; 9 | int flag = 1; 10 | int i=0,j=0; 11 | for(int index=0;index 2 | #include 3 | 4 | bool areAlmostEqual(char * s1, char * s2) 5 | { 6 | if(strcmp(s1,s2)==0) return true; 7 | if(strlen(s1)!=strlen(s2)) return false; 8 | int left = 0; 9 | int right = strlen(s1)-1; 10 | while(s1[left]==s2[left]) left++; 11 | while(s1[right]==s2[right]) right--; 12 | char t = s1[left]; 13 | s1[left] = s1[right]; 14 | s1[right] = t; 15 | if(strcmp(s1,s2)==0) return true; 16 | return false; 17 | } 18 | int main() 19 | { 20 | char *s1 = "bank"; 21 | char *s2 = "kanb"; 22 | printf(areAlmostEqual(s1,s2)); 23 | } -------------------------------------------------------------------------------- /1790. Check if One String Swap Can Make Strings Equal/solution2.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | bool areAlmostEqual(char * s1, char * s2) 5 | { 6 | if(strlen(s1)==1 && strlen(s2)==1) 7 | { 8 | if(strcmp(s1,s2)==0) return true; 9 | else return false; 10 | } 11 | if(strlen(s1)!=strlen(s2)) return false; 12 | int i=0,j=1; 13 | while(s1[i]!='\0') 14 | { 15 | char t = s1[i]; 16 | s1[i] = s1[j]; 17 | s1[j] = t; 18 | if(strcmp(s1,s2)==0) return true; 19 | else 20 | { 21 | t = s1[i]; 22 | s1[i] = s1[j]; 23 | s1[j] = t; 24 | } 25 | j++; 26 | if(j==strlen(s1)) 27 | { 28 | i++; 29 | j=0; 30 | } 31 | } 32 | return false; 33 | } 34 | int main() 35 | { 36 | char *s1 = "bank"; 37 | char *s2 = "kanb"; 38 | printf(areAlmostEqual(s1,s2)); 39 | } -------------------------------------------------------------------------------- /1791. Find Center of Star Graph/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | int findCenter(vector>& edges) 7 | { 8 | if(edges[0][0] == edges[1][0] || edges[0][0]==edges[1][1]) 9 | return edges[0][0]; 10 | return edges[0][1]; 11 | } 12 | }; 13 | 14 | int main() 15 | { 16 | vector> edges = {{1,2}, {2,3}, {4,2}}; 17 | Solution sol; 18 | cout<0) 8 | { 9 | res += arr[index++]; 10 | if(k>0) res += " "; 11 | } 12 | return res; 13 | } 14 | } 15 | public class solution1 { 16 | public static void main(String[] args) { 17 | String s = "Hello how are you Contestant"; 18 | int k = 4; 19 | Solution sol = new Solution(); 20 | System.out.println(sol.truncateSentence(s, k)); 21 | } 22 | } -------------------------------------------------------------------------------- /1859. Sorting the Sentence/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string sortSentence(string s) { 7 | map m; 8 | string temp = ""; 9 | for(int i=0;i='0' && s[i]<='9') 12 | { 13 | m[s[i]-'0'] = temp; 14 | temp = ""; 15 | } 16 | else if(s[i]!=' ') 17 | temp = temp + s[i]; 18 | } 19 | string res = ""; 20 | for(auto i: m) 21 | res = res + i.second + " "; 22 | res.erase(res.length()-1); 23 | return res; 24 | } 25 | }; 26 | int main() 27 | { 28 | string s = "is2 sentence4 This1 a3"; 29 | Solution sol; 30 | cout< 0 AND name NOT LIKE 'M%' THEN salary 18 | ELSE 0 19 | END AS bonus 20 | FROM employees 21 | ORDER BY employee_id; 22 | 23 | 24 | -------------------------------------------------------------------------------- /1873. Calculate Special Bonus/solution2.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Employees 2 | ( 3 | employee_id INT PRIMARY KEY, 4 | name VARCHAR(30), 5 | salary INT 6 | ); 7 | 8 | INSERT INTO employees (employee_id, name, salary) 9 | VALUES (2, 'Meir', 3000), 10 | (3, 'Michael', 3800), 11 | (7, 'Addilyn', 7400), 12 | (8, 'Juan', 6100), 13 | (9, 'Kannon', 7700); 14 | 15 | SELECT employee_id, salary * (name NOT LIKE 'M%' )* (employee_id % 2 <> 0) 16 | AS bonus 17 | FROM Employees 18 | ORDER BY employee_id; 19 | -------------------------------------------------------------------------------- /1890. The Latest Login in 2020/solution1.sql: -------------------------------------------------------------------------------- 1 | Create table If Not Exists Logins (user_id int, time_stamp datetime, PRIMARY KEY (user_id, time_stamp)); 2 | 3 | insert into Logins (user_id, time_stamp) values ('6', '2020-06-30 15:06:07'); 4 | insert into Logins (user_id, time_stamp) values ('6', '2021-04-21 14:06:06'); 5 | insert into Logins (user_id, time_stamp) values ('6', '2019-03-07 00:18:15'); 6 | insert into Logins (user_id, time_stamp) values ('8', '2020-02-01 05:10:53'); 7 | insert into Logins (user_id, time_stamp) values ('8', '2020-12-30 00:46:50'); 8 | insert into Logins (user_id, time_stamp) values ('2', '2020-01-16 02:49:50'); 9 | insert into Logins (user_id, time_stamp) values ('2', '2019-08-25 07:59:08'); 10 | insert into Logins (user_id, time_stamp) values ('14', '2019-07-14 09:00:00'); 11 | insert into Logins (user_id, time_stamp) values ('14', '2021-01-06 11:59:59'); 12 | 13 | SELECT * FROM logins; 14 | 15 | SELECT user_id, MAX(time_stamp) AS last_stamp 16 | FROM logins 17 | WHERE time_stamp 18 | BETWEEN "2020-01-01 00:00:00" 19 | AND "2020-12-31 23:59:59" 20 | GROUP BY user_id; -------------------------------------------------------------------------------- /1907. Count Salary Categories/solution1.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE Accounts ( 2 | account_id INT PRIMARY KEY, 3 | income INT 4 | ); 5 | 6 | insert into Accounts (account_id, income) values ('3', '108939'); 7 | insert into Accounts (account_id, income) values ('2', '12747'); 8 | insert into Accounts (account_id, income) values ('8', '87709'); 9 | insert into Accounts (account_id, income) values ('6', '91796'); 10 | 11 | SELECT 'Low Salary' AS category, COUNT(account_id) AS accounts_count 12 | FROM accounts 13 | WHERE income < 20000 14 | 15 | UNION 16 | 17 | SELECT 'Average Salary' AS category, COUNT(account_id) AS accounts_count 18 | FROM accounts 19 | WHERE income BETWEEN 20000 AND 50000 20 | 21 | UNION 22 | 23 | SELECT 'High Salary' AS category, COUNT(account_id) AS accounts_count 24 | FROM accounts 25 | WHERE income > 50000; 26 | 27 | -------------------------------------------------------------------------------- /1929. Concatenation of Array/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector getConcatenation(vector& nums) { 7 | vector v; 8 | for(auto i: nums) 9 | v.push_back(i); 10 | for(auto i: v) 11 | nums.push_back(i); 12 | return nums; 13 | } 14 | }; 15 | 16 | int main() 17 | { 18 | vector nums = {1,2,1}; 19 | Solution sol; 20 | vector result = sol.getConcatenation(nums); 21 | for(auto i: result) cout< ALL ( 17 | SELECT employee_id 18 | FROM Employees 19 | ) 20 | AND salary < 3000 21 | ORDER BY employee_id; 22 | 23 | -------------------------------------------------------------------------------- /2264. Largest 3-Same-Digit Number in String/solution1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string largestGoodInteger(string num) { 7 | int result = 0; 8 | string output = ""; 9 | for(int i=0;i "< "<= result) { 18 | result = nums; 19 | output = s; 20 | } 21 | } 22 | } 23 | return output; 24 | } 25 | }; 26 | 27 | int main() 28 | { 29 | string s = "2300019"; 30 | Solution sol; 31 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | string removeStars(string s) 7 | { 8 | string result = ""; 9 | int count = 0; 10 | for(int i=s.size()-1;i>=0;i--) 11 | { 12 | if(s[i] != '*' && count > 0) 13 | { 14 | count--; 15 | continue; 16 | } 17 | if(s[i] == '*') 18 | { 19 | count++; 20 | continue; 21 | } 22 | else 23 | { 24 | result += s[i]; 25 | } 26 | } 27 | reverse(result.begin(),result.end()); 28 | return result; 29 | } 30 | }; 31 | int main() 32 | { 33 | string s = "leet**cod*e"; 34 | Solution sol; 35 | cout< 2 | using namespace std; 3 | 4 | class Solution { 5 | public: 6 | vector sortPeople(vector& names, vector& heights) 7 | { 8 | for(int i=0;i names = {"Mary","John","Emma"}; 27 | vector heights = {180,165,170}; 28 | Solution sol; 29 | vector result = sol.sortPeople(names, heights); 30 | for(auto i: result) cout< 0) { 8 | int digit = temp % 10; 9 | temp /= 10; 10 | if(num % digit == 0) { 11 | result++; 12 | } 13 | } 14 | return result; 15 | } 16 | } 17 | public class solution1 { 18 | public static void main(String[] args) { 19 | int num = 121; 20 | Solution sol = new Solution(); 21 | System.out.println(sol.countDigits(num)); 22 | } 23 | } -------------------------------------------------------------------------------- /2619. Array Prototype Last/solution1.js: -------------------------------------------------------------------------------- 1 | Array.prototype.last = function() { 2 | if(this.length === 0) return -1; 3 | return this[this.length-1]; 4 | }; 5 | 6 | nums = [12,3,4,5,5,5,999]; 7 | 8 | console.log(nums.last()); -------------------------------------------------------------------------------- /2620. Counter/approach1.js: -------------------------------------------------------------------------------- 1 | let n = 10; 2 | 3 | let createCounter = function(n) 4 | { 5 | let counter = n; 6 | return function() 7 | { 8 | counter++; 9 | return counter-1; 10 | }; 11 | }; 12 | 13 | let output = createCounter(n); 14 | console.log(output()); 15 | console.log(output()); 16 | console.log(output()); -------------------------------------------------------------------------------- /2621. Sleep/solution1.js: -------------------------------------------------------------------------------- 1 | async function sleep(millis) 2 | { 3 | await new Promise((resolve,reject) => { 4 | setTimeout(resolve,millis); 5 | }); 6 | return millis; 7 | } 8 | 9 | sleep(100).then(()=>console.log("hello")); -------------------------------------------------------------------------------- /2626. Array Reduce Transformation/solution1.js: -------------------------------------------------------------------------------- 1 | var reduce = function(nums, fn, init) { 2 | let val = init; 3 | for(let i=0;i x + 1, x => 2 * x]) 17 | * fn(4) // 9 18 | */ -------------------------------------------------------------------------------- /2634. Filter Elements from Array/solution1.js: -------------------------------------------------------------------------------- 1 | var filter = function(arr, fn) { 2 | let output = arr.filter(fn); 3 | return output; 4 | }; 5 | 6 | function greaterThan10(n) 7 | { 8 | return n > 10; 9 | } 10 | 11 | let arr = [0,10,20,30]; 12 | let output = filter(arr,greaterThan10); 13 | console.log(output) -------------------------------------------------------------------------------- /2635. Apply Transform Over Each Element in Array/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} arr 3 | * @param {Function} fn 4 | * @return {number[]} 5 | */ 6 | var map = function(arr, fn) { 7 | arr = arr.map((element, index) => fn(element, index)); 8 | return arr; 9 | }; -------------------------------------------------------------------------------- /2648. Generate Fiboacci Sequence/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return {Generator} 3 | */ 4 | var fibGenerator = function*() { 5 | let prev = 0; 6 | let curr = 1; 7 | yield prev; 8 | yield curr; 9 | for(let i=2;i<=50;i++) 10 | { 11 | const next = prev + curr; 12 | yield next; 13 | prev = curr; 14 | curr = next; 15 | } 16 | }; 17 | 18 | /** 19 | * const gen = fibGenerator(); 20 | * gen.next().value; // 0 21 | * gen.next().value; // 1 22 | */ -------------------------------------------------------------------------------- /2665. Counter II/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {integer} init 3 | * @return { increment: Function, decrement: Function, reset: Function } 4 | */ 5 | var createCounter = function(init) { 6 | let nextValue = init; 7 | function increment() { return ++nextValue } 8 | function decrement() { return --nextValue } 9 | function reset() { return nextValue=init } 10 | 11 | return { increment, decrement, reset } 12 | }; 13 | 14 | /** 15 | * const counter = createCounter(5) 16 | * counter.increment(); // 6 17 | * counter.reset(); // 5 18 | * counter.decrement(); // 4 19 | */ 20 | 21 | /* 22 | Time 23 | 61ms 24 | Beats 41.72% of users with JavaScript 25 | 26 | Memory 27 | 44.24MB 28 | Beats 82.33% of users with JavaScript 29 | */ -------------------------------------------------------------------------------- /2666. Allow One Function Call/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Function} fn 3 | * @return {Function} 4 | */ 5 | var once = function(fn) { 6 | 7 | let hasBeenCalled = false; 8 | let result; 9 | return function(...args) { 10 | if(!hasBeenCalled) { 11 | result = fn(...args); 12 | hasBeenCalled = true; 13 | return result; 14 | } else { 15 | return undefined; 16 | } 17 | } 18 | }; 19 | 20 | /** 21 | * let fn = (a,b,c) => (a + b + c) 22 | * let onceFn = once(fn) 23 | * 24 | * onceFn(1,2,3); // 6 25 | * onceFn(2,3,6); // returns undefined without calling fn 26 | */ 27 | -------------------------------------------------------------------------------- /2667. Create Hello World Function/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return {Function} 3 | */ 4 | var createHelloWorld = function() { 5 | 6 | return function(...args) { 7 | return "Hello World" 8 | } 9 | }; 10 | 11 | /** 12 | * const f = createHelloWorld(); 13 | * f(); // "Hello World" 14 | */ -------------------------------------------------------------------------------- /2677. Chunk Array/solution1.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {Array} arr 3 | * @param {number} size 4 | * @return {Array} 5 | */ 6 | var chunk = function(arr, size) { 7 | let result = []; 8 | for(let i=0;i { 8 | if(val === val2) return true; 9 | else throw new Error("Not Equal"); 10 | }, 11 | notToBe: (val2) => { 12 | if(val !== val2) return true; 13 | else throw new Error("Equal"); 14 | } 15 | } 16 | }; 17 | 18 | /** 19 | * expect(5).toBe(5); // true 20 | * expect(5).notToBe(5); // throws "Equal" 21 | */ --------------------------------------------------------------------------------