├── Images └── Hacktoberfest.jpg ├── C# └── README.md ├── Go └── README.md ├── PHP └── README.md ├── Ruby └── README.md ├── Rust └── README.md ├── Elixir └── README.md ├── Erlang └── README.md ├── Kotlin └── README.md ├── Racket └── README.md ├── Scala └── README.md ├── Swift └── README.md ├── Javascript └── README.md ├── Typescript └── README.md ├── C ├── README.md └── 2-add_two_numbers.c ├── Java ├── README.md ├── 9-palindrome_num.java ├── Reverse_Integers_Leetcode.java └── 1-two_sum.java ├── Python ├── README.md ├── 268-missing_number.py ├── 1-two_sum.py ├── 7-Reverse_Integer.py └── 3-Peak_element.py ├── .github └── pull_request_template.md ├── C++ ├── 268-missing-number.cpp ├── 55-jump_game.cpp ├── 198-House_Robber.cpp ├── 3-longest_substring_without_repeating_characters.cpp ├── README.md ├── 75-sort_colors.cpp ├── 48-Rotate_image.cpp ├── 1-two_sum.cpp ├── 503-next_greater_element_II.cpp ├── 11-Container_with_most_water.cpp ├── 162-find_peak_element.cpp ├── 31-Next_Permutation.cpp ├── 1014-best_sightseeing_pair.cpp ├── 21-merge_two_sorted_lists.cpp ├── 121-best_time_to_buy_and_sell_stock.cpp ├── 1769-Minimum_Number_of_Operations_to_Move_All_Balls_to_Each_Box.cpp ├── 15-3Sum.cpp ├── 2-add_two_numbers.cpp ├── 1964-Find_the_Longest_Valid _Obstacle_Course_at_Each_Position.cpp ├── 918-maximum_sum_circular_subarray.cpp ├── 6-ZigZag_Conversion.cpp ├── 59-Spiral_Matrix_II.cpp ├── 43-Multiply_Strings.cpp ├── 12-integer_to_roman.cpp ├── 1697-Checking_Existence_of_Edge_Length_Limited_Paths.cpp └── 8-string_to_integer_atoi.cpp ├── LICENSE ├── Steps_To_Raise_Pull_Request.md ├── NOTE FOR CONTRIBUTORS.md └── README.md /Images/Hacktoberfest.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/piyushsharma220699/LeetCode-Problems-Solution-Book/HEAD/Images/Hacktoberfest.jpg -------------------------------------------------------------------------------- /C#/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Go/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /PHP/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Ruby/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Rust/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Elixir/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Erlang/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Kotlin/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Racket/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Scala/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Swift/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Javascript/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /Typescript/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number -------------------------------------------------------------------------------- /C/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number 4 | 5 | 2: Add Two Numbers
6 | -------------------------------------------------------------------------------- /Java/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number 4 | 5 | 1 : Two Sum
6 | 7 : Reverse Integer
7 | 9 : Palindrome Number
8 | -------------------------------------------------------------------------------- /Python/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number 4 | 5 | 1 : Two Sum
6 | 2 : Missing Number
7 | 3 : Peak Element
8 | 7 : Reverse Integer
9 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | - Leetcode Question Id: 2 | - Language: 3 | 4 | CheckList: 5 | - [ ] I've read [Steps to contribute](https://github.dev/mukeshgurpude/Hacktoberfest-2021/Steps_To_Raise_Pull_Request.md) 6 | - [ ] Pull request title matches: [LeetCode_question_number]\_[Question_name separated by underscore]_[Language]** 7 | -------------------------------------------------------------------------------- /Java/9-palindrome_num.java: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/palindrome-number/ 2 | #12. Palindrome Number 3 | Given an integer x, return true if x is palindrome integer 4 | */ 5 | //Solution : 6 | 7 | public class Solution { 8 | public boolean isPalindrome(int x) { 9 | int sum = 0,target = x; 10 | while (x > 0) { 11 | int temp = x % 10; 12 | x /= 10; 13 | sum = sum * 10 + temp; 14 | } 15 | return sum == target; 16 | } 17 | } 18 | 19 | /* 20 | Sample Input 1 : x=121 21 | Sample output 1 : true 22 | 23 | Sample input 2 : x= -121 24 | Sample output 1 : false 25 | /* -------------------------------------------------------------------------------- /C++/268-missing-number.cpp: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/missing-number/ 2 | 268. Missing Number 3 | Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array.*/ 4 | //Solution : 5 | 6 | class Solution { 7 | public: 8 | int missingNumber(vector& a) { 9 | 10 | int i,n=a.size(); 11 | int j=n; 12 | 13 | for(i=0;i int: 8 | n =len(nums) 9 | for i in range(n+1): #range is [0,n] both inclusive 10 | if i not in nums: #return the index whenever it is not in the list, that index will be our answer 11 | return i 12 | 13 | ''' 14 | Input: nums = [3,0,1] 15 | Output: 2 16 | 17 | Input: nums = [0,1] 18 | Output: 2 19 | 20 | Time Complexity: O(n) 21 | Space COmplexity: O(1) 22 | ''' 23 | -------------------------------------------------------------------------------- /Python/1-two_sum.py: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/two-sum/ 2 | 1. Two Sum 3 | Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. 4 | */ 5 | //Solution : 6 | 7 | class Solution: 8 | def twoSum(self, nums: List[int], target: int) -> List[int]: 9 | map_t = {} 10 | for i in range(len(nums)): 11 | rem = target-nums[i] #calculate the number needs to meet the target (sum) 12 | 13 | if rem in map_t: 14 | return [map_t[rem],i] #return the position of the current element and position of reminder 15 | else: 16 | map_t[nums[i]] = i #Put the number as key as position as a value 17 | 18 | /* 19 | Sample Input : nums = [3, 4, 2], target = 6 20 | Sample output : [1,2] 21 | Time Complexity : O(N) 22 | Space Complexity : O(N) 23 | N = size of given array 24 | 25 | */ -------------------------------------------------------------------------------- /Java/Reverse_Integers_Leetcode.java: -------------------------------------------------------------------------------- 1 | /* 2 | Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. 3 | 4 | Assume the environment does not allow you to store 64-bit integers (signed or unsigned). 5 | 6 | 7 | 8 | Example 1: 9 | 10 | Input: x = 123 11 | Output: 321 12 | Example 2: 13 | 14 | Input: x = -123 15 | Output: -321 16 | Example 3: 17 | 18 | Input: x = 120 19 | Output: 21 20 | Example 4: 21 | 22 | Input: x = 0 23 | Output: 0 24 | */ 25 | 26 | class Reverse_Integer_Leetcode { 27 | public int reverse(int x) { 28 | int reversedNumber = 0; 29 | int n = Math.abs(x); 30 | while(n > 0){ 31 | reversedNumber *= 10; 32 | reversedNumber += n % 10; 33 | n /= 10; 34 | } 35 | if(x < 0) reversedNumber = -reversedNumber; 36 | return reversedNumber; 37 | } 38 | } -------------------------------------------------------------------------------- /C++/198-House_Robber.cpp: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/house-robber/ 2 | 198. House Robber 3 | ou are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night. 4 | Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police.*/ 5 | //Solution : 6 | 7 | class Solution { 8 | public: 9 | 10 | int rob(vector& a) 11 | { 12 | int n = a.size(); 13 | 14 | if(!n) return 0; 15 | if(n<2) return a[0]; 16 | 17 | a[1] = max(a[0], a[1]); 18 | for(int i=2;inum_map = new HashMap<>(); 11 | for(int i=0; i ump; 14 | for(int en=0; en=st) { 16 | //s[en] is already present in current substring 17 | //update start of current substring to next character 18 | st = ump[s[en]]; 19 | } 20 | //store the 1-based index of current character in hash map 21 | ump[s[en]] = en+1; 22 | ans = max(ans, en-st+1); 23 | } 24 | return ans; 25 | } 26 | }; 27 | 28 | /* 29 | Sample input : "abcabcbb" 30 | Sample output : 3 31 | 32 | Time Complexity : O(n) 33 | Space Complexity : O(n) 34 | n = length of string 35 | */ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Piyush Sharma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /C++/README.md: -------------------------------------------------------------------------------- 1 | # SOLVED PROBLEMS 2 | (If you are contributing your code in this language, don't forget to include it in the list below)
3 | ### NOTE : Include the code in the list in order of leetcode problem statement number 4 | 5 | 1 : Two Sum
6 | 2 : Add Two Numbers
7 | 3 : Longest Substring Without Repeating Characters
8 | 6 : ZigZag Conversion
9 | 8 : String to Integer (atoi)
10 | 11 : Container With Most Water
11 | 12 : Integer to Roman
12 | 15 : 3Sum
13 | 21 : Merge two sorted lists
14 | 31 : Next Permutation
15 | 43 : Multiply Strings
16 | 48 : Rotate Image
17 | 55 : Jump Game
18 | 59 : Spiral Matrix II
19 | 75 : Sort Colors
20 | 121 : Best Time To Buy And Sell Stock
21 | 162 : Find Peak Element
22 | 198 : House Robber
23 | 268 : Missing Number
24 | 503 : Next Greater Element II
25 | 918 : Maximum Sum Circular Subarray
26 | 1014 : Best Sightseeing Pair
27 | 1697 : Checking Existence of Edge Length Limited Paths
28 | 1769 : Minimum Number of Operations to Move All Balls to Each Box
29 | 1964 : Find the Longest Valid Obstacle Course at Each Position
30 | -------------------------------------------------------------------------------- /C++/75-sort_colors.cpp: -------------------------------------------------------------------------------- 1 | /*Link of Question : https://leetcode.com/problems/sort-colors/ 2 | 3 | Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white, and blue. 4 | 5 | We will use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively. 6 | 7 | You must solve this problem without using the library's sort function. 8 | */ 9 | //Solution 10 | class Solution { 11 | public: 12 | void sortColors(vector& nums) { 13 | // Everything before left is 0 and everythin after right is 2 14 | int left = -1, right = nums.size(), curr = 0; 15 | 16 | while(curr < right){ 17 | if(nums[curr] == 0){ 18 | swap(nums[++left], nums[curr]); 19 | curr++; 20 | } 21 | else if(nums[curr] == 2){ 22 | swap(nums[curr], nums[--right]); 23 | } 24 | else 25 | curr++; 26 | } 27 | } 28 | }; 29 | 30 | /* 31 | Sample Input : nums = [2,0,2,1,1,0] 32 | Sample Output : [0,0,1,1,2,2] 33 | Time Complexity : O(N) 34 | Space Complexity : O(N) 35 | */ 36 | -------------------------------------------------------------------------------- /C++/48-Rotate_image.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Leetcode - medium difficulty 3 | 48 - Rotate image 4 | 5 | Method :- 6 | 7 | first swap m[i][j] with m[j][i], then reverse up to down 8 | 9 | 1 2 3 3 2 1 3 6 9 10 | 4 5 6 => 6 5 4 => 2 5 8 11 | 7 8 9 9 8 7 1 4 7 12 | 13 | */ 14 | 15 | 16 | class Solution { 17 | public: 18 | void rotate(vector>& m) { 19 | 20 | int n = m.size(); 21 | 22 | //swapping 23 | for(int i=0; i twoSum(vector& nums, int target) { 10 | 11 | //unordered map for storing the element and index of that element 12 | unordered_mapmp; 13 | 14 | for(int i=0;is; 17 | vectorans(n,-1); 18 | 19 | for(int j=1;j<3;j++) 20 | { 21 | for(i = n-1 ;i>=0; i--) 22 | { 23 | while(!s.empty() && arr[i]>=s.top()) 24 | s.pop(); 25 | 26 | if(!s.empty()) 27 | ans[i] = s.top(); 28 | s.push(arr[i]); 29 | } 30 | } 31 | return ans; 32 | } 33 | }; 34 | 35 | /* 36 | Sample Input : nums = [1,2,1] 37 | Sample output : [2,-1,2] 38 | Time Complexity : O(N) 39 | Space Complexity : O(N) 40 | N = size of given array 41 | */ 42 | -------------------------------------------------------------------------------- /C++/11-Container_with_most_water.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Problem 11. Container With Most Water 3 | 4 | Given n non-negative integers a1, a2, ..., an , where each represents a point 5 | at coordinate (i, ai). n vertical lines are drawn such that the two endpoints 6 | of the line i is at (i, ai) and (i, 0). Find two lines, which, together with 7 | the x-axis forms a container, such that the container contains the most water. 8 | 9 | Notice that you may not slant the container. 10 | */ 11 | 12 | class Solution { 13 | public: 14 | int maxArea(vector& height) { 15 | //two pointer approach 16 | int it1 = 0,it2 = height.size()-1, ans = 0; 17 | while(it1& nums){ 12 | /* 13 | * Binary search implementation is the main idea to find the peak element in log(n) time complexity. 14 | 15 | * Initially left most index (l) = 0 and right most index = size of the vector - 1 because of zero based indexing. 16 | 17 | * Finding middle element with low + (high-low)/2 to avoid integer overflow. 18 | 19 | */ 20 | int low=0,high=nums.size()-1; 21 | while (lownums[mid+1]) 24 | high=mid; 25 | else 26 | low=mid+1; 27 | } 28 | return low; 29 | //returning l gives us the index of the peak element. 30 | } 31 | }; 32 | 33 | /* 34 | Sample Input : nums = [1,2,3,1] 35 | Sample output : 2 36 | Time Complexity : O(log(N)) 37 | Space Complexity : O(1) 38 | N = size of given vector. 39 | 40 | */ 41 | -------------------------------------------------------------------------------- /Python/7-Reverse_Integer.py: -------------------------------------------------------------------------------- 1 | """Link for the problem : https://leetcode.com/problems/reverse-integer 2 | 3 | Problem Statement : 4 | Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. 5 | """ 6 | """SOLUTION :""" 7 | class Solution: 8 | def reverse(self, x: int) -> int: 9 | res = 0 # initiating result variable 10 | if(x < 0): 11 | a = abs(x) # application for negative inputs 12 | b = 0 13 | while a>0: 14 | b*=10 15 | b += a%10 16 | a//=10 17 | res = -b 18 | else: 19 | a = (x) 20 | b = 0 21 | while a>0: 22 | b*=10 23 | b+=a%10 24 | a//=10 25 | res = b 26 | if res >= 2**31 or res < -2**31 : # with respect to the ranging condition of 32 bit result 27 | res =0 28 | return res 29 | 30 | """ Sample Input : 123 31 | Output : 321 32 | 33 | Input : 120 34 | Output : 21 35 | 36 | Input : -123 37 | Output : -321 38 | 39 | 40 | Time Complexity : O(log base 10 (n)) 41 | Space Complexity : O(1) 42 | 43 | """ 44 | -------------------------------------------------------------------------------- /C++/31-Next_Permutation.cpp: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/next-permutation/ 2 | 31. Next Permutation 3 | Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. 4 | 5 | If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). 6 | 7 | The replacement must be in place and use only constant extra memory. 8 | */ 9 | //Solution : 10 | 11 | /* Algorithm: 12 | Step 1: Find the largest index i, such that A[i]A[i]. 14 | Step 3: Swap A[i] and A[j]. 15 | Step 4: Reverse A[i+1] to the end. 16 | */ 17 | class Solution { 18 | public: 19 | void nextPermutation(vector < int > & A) { 20 | int i, j; 21 | int n = A.size(); 22 | for (i = n - 2; i >= 0; i--) { 23 | if (A[i] < A[i + 1]) break; 24 | } 25 | if (i < 0) { 26 | reverse(A.begin(), A.end()); 27 | return; 28 | } else { 29 | for (j = n - 1; j > i; j--) { 30 | if (A[j] > A[i]) { 31 | break; 32 | } 33 | } 34 | swap(A[j], A[i]); 35 | reverse(A.begin() + i + 1, A.end()); 36 | 37 | } 38 | return; 39 | } 40 | }; 41 | 42 | /* 43 | Sample Input: nums = [1,2,3] 44 | Sample Output: [1,3,2] 45 | Time Complexity : O(N) 46 | Space Complexity : O(1) 47 | */ 48 | -------------------------------------------------------------------------------- /C++/1014-best_sightseeing_pair.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Question link : https://leetcode.com/problems/best-sightseeing-pair/ 3 | 4 | 1014. You are given an integer array values where values[i] represents the value of the ith sightseeing spot. Two sightseeing spots i and j have a distance j - i between them. 5 | The score of a pair (i < j) of sightseeing spots is values[i] + values[j] + i - j: the sum of the values of the sightseeing spots, minus the distance between them. 6 | Return the maximum score of a pair of sightseeing spots. 7 | 8 | */ 9 | 10 | //Solution 11 | 12 | class Solution { 13 | public: 14 | int maxScoreSightseeingPair(vector& values) { 15 | int n = values.size(), ans = 0, xi = values[0]; 16 | 17 | /*values[i] + values[j] + i - j can be re written as : values[i]+i + values[j]-j. Let xi = values[i]+i and xj = values[j]-j. 18 | Since, we need to maximize the answer, we find max(values[i]+i) over all values[j]-j. One more constraint is i < j. 19 | That leaves us with only 1 for loop. 20 | */ 21 | for(int j = 1; j < n; j++){ 22 | ans = max(ans, xi + values[j]-j); 23 | 24 | //Keep on calculating max xi values before calculating the xj values which needs to be at the right of the chosen xi value. 25 | xi = max(xi, values[j]+j); 26 | } 27 | return ans; 28 | } 29 | }; 30 | 31 | /* 32 | Sample input : [8,1,5,2,6] 33 | Sample output : 11 34 | Time Complexity : O(n) 35 | Space Complexity : O(1) 36 | n = size of given array 37 | */ -------------------------------------------------------------------------------- /C++/21-merge_two_sorted_lists.cpp: -------------------------------------------------------------------------------- 1 | /*Link for Question : https://leetcode.com/problems/merge-two-sorted-lists/ 2 | 21. Merge Two Sorted Lists 3 | 4 | Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. 5 | */ 6 | //Solution : 7 | 8 | /** 9 | * Definition for singly-linked list. 10 | * struct ListNode { 11 | * int val; 12 | * ListNode *next; 13 | * ListNode() : val(0), next(nullptr) {} 14 | * ListNode(int x) : val(x), next(nullptr) {} 15 | * ListNode(int x, ListNode *next) : val(x), next(next) {} 16 | * }; 17 | */ 18 | class Solution { 19 | public: 20 | ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { 21 | 22 | if(l1 == NULL) { // if l1 is NULL return l2 23 | return l2; 24 | } 25 | 26 | if(l2 == NULL) { // if l2 is NULL return l1 27 | return l1; 28 | } 29 | 30 | ListNode* res; 31 | if(l1->val <= l2->val) { //if value of l1 is less than or equal to value of l2 32 | 33 | res = l1; 34 | res->next = mergeTwoLists(l1->next, l2); //point next of res to the return value of recurrsive function 35 | } 36 | 37 | else{ 38 | res = l2; 39 | res->next = mergeTwoLists(l1, l2->next); 40 | } 41 | 42 | return res; 43 | } 44 | }; 45 | 46 | /* 47 | Input: l1 = [1,2,4], l2 = [1,3,4] 48 | Output: [1,1,2,3,4,4] 49 | Time Complexity : O(N) 50 | Space Complexity : O(1) 51 | N = size of given array 52 | 53 | */ 54 | -------------------------------------------------------------------------------- /C++/121-best_time_to_buy_and_sell_stock.cpp: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 2 | 121. Best Time To Buy And Sell Stock 3 | 4 | You are given an array prices where prices[i] is the price of a given stock on the ith day. 5 | You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. 6 | Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0. 7 | */ 8 | 9 | //Solution : 10 | 11 | class Solution { 12 | public: 13 | int maxProfit(vector& prices) { 14 | int n = prices.size(); 15 | 16 | //check base case 17 | if(n == 1) 18 | return 0; 19 | 20 | //set initial buy price to be 1st element and initial profit = 0 21 | int buy = prices[0], profit = 0; 22 | 23 | for(int i = 1; i < n; i++){ 24 | 25 | //if current element is less than the buy price, we change buy price to current element, as that is the most intelligent choice. 26 | if(prices[i] < buy) 27 | { 28 | buy = prices[i]; 29 | } 30 | //if not, then we check if we sell at this price, what is the profit we are obtaining. We store the maximum profit. 31 | else 32 | { 33 | profit = max(profit, prices[i]-buy); 34 | } 35 | } 36 | return profit; 37 | } 38 | }; 39 | 40 | /* 41 | Sample Input : prices = [7,1,5,3,6,4] 42 | Sample output : 5 43 | Time Complexity : O(N) 44 | Space Complexity : O(1) 45 | N = size of given array 46 | 47 | */ -------------------------------------------------------------------------------- /Steps_To_Raise_Pull_Request.md: -------------------------------------------------------------------------------- 1 | 2 | Steps to Raise Pull Request 3 | --- 4 | 5 | --- 6 | 7 | #### 1. Fork [this](https://github.com/piyushsharma220699/Hacktoberfest-2021) repository. 8 | > Click on the Fork button at the top right corner. 9 | #### 2. Clone the forked repository into your local system. 10 | > Clone the repo by opening the terminal on your computer and running the command: 11 | 12 | > git clone https://github.com/Your-Github-Username/Hacktoberfest-2021 13 | 14 | #### 3. Navigate to the Repository directory 15 | > cd Hacktoberfest-2021 16 | 17 | #### 4. Add the Upstream 18 | > git remote add upstream https://github.com/piyushsharma220699/Hacktoberfest-2021 19 | 20 | #### 5. Connect this local repository to the remote one. 21 | > git remote -v 22 | 23 | #### 6. Pull the latest update from upstream to avoid conflict issues. 24 | > git pull upstream main 25 | 26 | #### 7. Create a new branch. 27 | > git checkout -b 28 | 29 | #### 8. Make your changes to the above branch and push it when completed. 30 | > git add . 31 | 32 | > git commit -m "commit_message" 33 | *Note: Please use relevant commit messages.* 34 | 35 | > git push -u origin 36 | 37 | #### 9. Create Pull request 38 | > Go to your forked repository, click on `Compare and pull requests`. 39 | 40 | > Confirm your changed files. Add appropriate title and description. 41 | 42 | **Note: PR Title should be in the following format [LeetCode_question_number]\_[Question_name separated by underscore]_[Language]** 43 | 44 | Ex : 174_Dungeon_Game_C++ 45 | 46 | > Click on `Create Pull Request`. 47 | 48 | #### 10. Wait for approval 49 | > Adjust the PR as per the review comments if any. 50 | -------------------------------------------------------------------------------- /C++/1769-Minimum_Number_of_Operations_to_Move_All_Balls_to_Each_Box.cpp: -------------------------------------------------------------------------------- 1 | /*Link of the question : https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/ 2 | 1769. 3 | You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith box is empty, and '1' if it contains one ball. 4 | In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes. 5 | Return an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box. 6 | Each answer[i] is calculated considering the initial state of the boxes. 7 | */ 8 | 9 | class Solution { 10 | public: 11 | vector minOperations(string boxes) { 12 | 13 | vector v;//this vector v will have operations for each of the ball 14 | int l = boxes.length(); //Calculate the length of the string 15 | 16 | 17 | for(int i=0;i> threeSum(vector& nums) { 10 | vector>ans; 11 | int n= nums.size(); 12 | sort(nums.begin(),nums.end()); 13 | /* Now fix the first element one by one and find the 14 | other two elements */ 15 | for(int i=0;i 0 && nums[i] == nums[i - 1]) /* Skipping as we will get same set of solutions */ 23 | continue; 24 | while(l{nums[i],nums[l],nums[r]}); 29 | while(l val + l2 -> val; 23 | curr -> next = new ListNode((carry + sum) % 10); 24 | curr = curr -> next; 25 | carry = (carry + sum) / 10; 26 | l1 = l1 -> next; 27 | l2 = l2 -> next; 28 | } 29 | 30 | // Might be the possibility that one list is exhausted other one isn't 31 | 32 | 33 | while(l1){ 34 | curr -> next = new ListNode((carry + l1 -> val) % 10); 35 | carry = (carry + l1 -> val) / 10; 36 | curr = curr -> next; 37 | l1 = l1 -> next; 38 | } 39 | 40 | while(l2){ 41 | curr -> next = new ListNode((carry + l2 -> val) % 10); 42 | carry = (carry + l2 -> val) / 10; 43 | curr = curr -> next; 44 | l2 = l2 -> next; 45 | } 46 | 47 | // At last if there is a carry present then create a new node for it. 48 | if(carry){ 49 | curr -> next = new ListNode(carry); 50 | } 51 | 52 | return newhead -> next; 53 | } 54 | }; 55 | 56 | /* 57 | Sample Input : l1 = [2,4,3], l2 = [5,6,4] 58 | Sample Output : [7, 0, 8] 59 | Time Complexity : O(N) 60 | Space Complexity : O(1) 61 | N = max(l1.length, l2.length) 62 | */ 63 | -------------------------------------------------------------------------------- /C++/1964-Find_the_Longest_Valid _Obstacle_Course_at_Each_Position.cpp: -------------------------------------------------------------------------------- 1 | /*Link Of Question : https://leetcode.com/problems/find-the-longest-valid-obstacle-course-at-each-position/ 2 | 1964. Find the Longest Valid Obstacle Course at Each Position 3 | 4 | You want to build some obstacle courses. You are given a 0-indexed integer array obstacles of length n, where obstacles[i] describes the height of the ith obstacle. 5 | 6 | For every index i between 0 and n - 1 (inclusive), find the length of the longest obstacle course in obstacles such that: 7 | 8 | You choose any number of obstacles between 0 and i inclusive. 9 | You must include the ith obstacle in the course. 10 | You must put the chosen obstacles in the same order as they appear in obstacles. 11 | Every obstacle (except the first) is taller than or the same height as the obstacle immediately before it. 12 | Return an array ans of length n, where ans[i] is the length of the longest obstacle course for index i as described above. 13 | 14 | Example 1: 15 | 16 | Input: obstacles = [1,2,3,2] 17 | Output: [1,2,3,3] 18 | Explanation: The longest valid obstacle course at each position is: 19 | - i = 0: [1], [1] has length 1. 20 | - i = 1: [1,2], [1,2] has length 2. 21 | - i = 2: [1,2,3], [1,2,3] has length 3. 22 | - i = 3: [1,2,3,2], [1,2,2] has length 3. 23 | */ 24 | //Solution : 25 | class Solution { 26 | public: 27 | vector longestObstacleCourseAtEachPosition(vector& obstacles) { 28 | vector ans; 29 | vector curr; 30 | int n = obstacles.size(); 31 | for(int i=0;i& A) { 12 | 13 | int n = A.size(); 14 | 15 | //check for base case 16 | if(n==0) return 0; 17 | if(n==1) return A[0]; 18 | 19 | /*Approach : we either can have maximum sum subarray in the linear part of array or we can have that subarray which includes both the ends of array and some middle part is not present. 20 | For this, we calculate maximum similar to what we'd have done in linear array case, and also minimum so that if we remove it from the entire array sum we get maximum subarray in circular case. 21 | Final answer is maximum of both the case. 22 | */ 23 | 24 | //initialize maximum, minimum, current max, current min and sum with the first element 25 | int cur_max=A[0],cur_min=A[0],mx=A[0],mn=A[0],sum=A[0]; 26 | for(int i=1;i> generateMatrix(int n) { 19 | int cnt=1; 20 | vector > result(n,vector(n)); 21 | int top=0,bottom=n-1,left=0,right=n-1;int dir=0; 22 | while(top<=bottom && left<=right){ 23 | if(dir==0){ 24 | for(int i=left;i<=right;i++){ 25 | result[top][i]=cnt++; 26 | } 27 | top++; 28 | dir=1; 29 | } 30 | if(dir==1){ 31 | for(int i=top;i<=bottom;i++){ 32 | result[i][right]=cnt++; 33 | } 34 | right--; 35 | dir=2; 36 | } 37 | if(dir==2){ 38 | for(int i=right;i>=left;i--){ 39 | result[bottom][i]=cnt++; 40 | } 41 | bottom--; 42 | dir=3; 43 | } 44 | if(dir==3){ 45 | for(int i=bottom;i>=top;i--){ 46 | result[i][left]=cnt++; 47 | } 48 | left++; 49 | dir=0; 50 | } 51 | } 52 | return result; 53 | } 54 | }; 55 | 56 | /* 57 | Sample input: 3 58 | Sample output: [[1,2,3],[8,9,4],[7,6,5]] 59 | Time Complexity: O(N*N) 60 | Space Complexity: O(N*N) 61 | */ 62 | -------------------------------------------------------------------------------- /NOTE FOR CONTRIBUTORS.md: -------------------------------------------------------------------------------- 1 | # GUIDELINES FOR CONTRIBUTORS 2 | 3 | ## Follow the following steps to contribute 4 | 5 | 1. Search for the code you want to contribute in the repository first. 6 | 2. If it doesn't exist, check the issues list. If in the existing issues list the problem is present, check if it's already assigned to someone or not. 7 | 3. If it's not assigned, comment on the issue along with the language you want to work on. 8 | 4. If the problem is not present in the existing issues, create a new issue. Don't forget to mention the language you want to work on. 9 | 5. WAIT PATIENTLY FOR THE ISSUE TO BE ASSIGNED TO YOU! 10 | 6. Create a new branch in your forked repository and start working on your code. 11 | 7. After you're done with the code, update the README.md file you find in that folder in order of LEETCODE PROBLEM NUMBER (Make sure you don't change the README.md in root of project but the README.md in folder of language you're working.)

12 | Add "Problem Number" + " : " + "Problem Name" in README.md
13 | Eg: 1 : Add Sum
14 | 8. You've done your job. Now wait for it to get reviewed and approved by maintainers :) 15 | 16 |
17 | 18 | ## MUST READ NOTES FOR THE CONTRIBUTOR 19 | 1. You cannot work on any issue that is not assigned to you. Issues will be assigned on a FIRST COME FIRST SERVE (FCFS) basis. The person who creates the issue gets the first priority. And then, issues are assigned based on who commented first. You just have to comment on the issue, asking to be assigned and the programming language you are going to use, and it will be done if found fit. 20 | 2. Don't forget to add the following things to your code (The PR will not get accepted if these are not present) - 21 | A. Proper comments wherever necessary.
22 | B. A block comment at the start of the code containing the problem number, problem name and problem statement.
23 | C. After the code, a block comment having sample input, sample output, time complexity of the solution and space complexity of the solution.
24 | 3. All PRs must be made from a Branch. Create a separate branch for every Issue you are working upon and then create a PR.
25 | 4. PLEASE DO NOT COPY THE CODE, PLAGIARIZED PR's WILL NOT GET ACCEPTED AT ALL! 26 | 27 |
28 | 29 | ## To understand what is Git & Github, Watch this (Thanks to Kunal Kushwaha for this amazing explanation). -------------------------------------------------------------------------------- /Python/3-Peak_element.py: -------------------------------------------------------------------------------- 1 | ''' 2 | Link of Question: https://leetcode.com/problems/find-peak-element/ 3 | 4 | Problem: A peak element is an element that is strictly greater than its neighbors.Given an integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks 5 | 6 | Approach: Since we have to give solution in log(n) time, Binary Search approach can be used with some enhancements. In below code we get an index say mid and compare it with neighbouing elements i.e mid-1 and mid+1, if (element at mid > element at mid-1 and element at mid+1), We simply return mid otherwise update the bounds, boundary conditions are handled seperately 7 | 8 | Disclaimer : This might not work in your system as input was handled by leetcode only, you can run this code as it is on the problem link 9 | ''' 10 | class Solution: 11 | def findPeakElement(self, nums: List[int]) -> int: 12 | n=len(nums) 13 | low = 0 14 | high = n-1 15 | if n==1: #return 0th element when length of array is 1(Single element is always a peak) 16 | return 0 17 | while low<=high: 18 | mid = (low+high)//2 19 | if mid>0 and mid< n-1: #checking if both the neighbours are present. 20 | left = mid-1 21 | right = mid+1 22 | if nums[mid]>nums[left] and nums[mid]>nums[right]: #return mid when conditions are satisfied 23 | return mid 24 | elif nums[left]>nums[mid]: #update/decrease higher bound 25 | high = mid -1 26 | elif nums[right]>nums[mid]: #update/increase lower bound 27 | low=mid+1 28 | 29 | elif mid==0: #if mid is 0th index then we can directly compare with element @1 30 | if nums[0]>nums[1]: 31 | return 0 32 | else: 33 | return 1 34 | elif mid==n-1: #if mid is n-1(last) index then we can directly compare with element @n-2 35 | if nums[n-1]>nums[n-2]: 36 | return n-1 37 | else: 38 | return n-2 39 | 40 | ''' 41 | Input: nums = [1,2,3,1] 42 | Output: 2 43 | 44 | Input: nums = [1,2,1,3,5,6,4] 45 | Output: 5 46 | 47 | Time Complexity = O(logN) 48 | Space Complexity= O(1) 49 | N = size of array 50 | 51 | ''' 52 | 53 | -------------------------------------------------------------------------------- /C++/43-Multiply_Strings.cpp: -------------------------------------------------------------------------------- 1 | /*Link of the question : https://leetcode.com/problems/multiply-strings/ 2 | 43. 3 | Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. 4 | Note: You must not use any built-in BigInteger library or convert the inputs to integer directly. 5 | */ 6 | 7 | 8 | class Solution { 9 | public: 10 | string multiply(string num1, string num2){ 11 | 12 | int length1 = num1.size(); //find the size of string 1 13 | int length2 = num2.size(); //find the size of string 1 14 | 15 | //If any of the string is 0 return zero 16 | if (length1 == 0 || length2 == 0){ 17 | return "0"; 18 | } 19 | 20 | vector result(length1 + length2, 0); 21 | 22 | int index1 = 0; //index for num1 traversal 23 | int index2 = 0; //index for num2 traversal 24 | 25 | 26 | //move right to left in num1 27 | for (int i=length1-1; i>=0; i--){ 28 | 29 | int carry = 0; 30 | int n1 = num1[i] - '0'; //getting integer value of current digit using typecasting 31 | 32 | index2 = 0; 33 | 34 | //move from right to left in num2 35 | for (int j=length2-1; j>=0; j--){ 36 | 37 | int n2 = num2[j] - '0';//getting integer value of current digit using typecasting 38 | 39 | int sum = n1*n2 + result[index1 + index2] + carry; //multiply n1 and n2 and add previously stored result at i+j th position in result vector and add carry 40 | 41 | carry = sum/10; //generate carry for next iteration 42 | 43 | result[index1 + index2] = sum % 10; //stroe the result 44 | 45 | index2++; 46 | } 47 | 48 | if (carry > 0){ 49 | result[index1 + index2] += carry; 50 | } 51 | 52 | index1++; 53 | //this plus ensures that position shifts towards left after every multiplication of a digit in num1 54 | } 55 | 56 | int i = result.size() - 1; 57 | while (i>=0 && result[i] == 0){ 58 | i--; 59 | }//ignore all zeros from right 60 | 61 | //if i = -1 it means that either of num1 or num2 was 0 or both 62 | if (i == -1){ 63 | return "0"; 64 | } 65 | 66 | string s = ""; 67 | 68 | while (i >= 0){ 69 | s += std::to_string(result[i--]); //create the string 70 | } 71 | 72 | return s; 73 | } 74 | 75 | 76 | }; 77 | 78 | /* 79 | Input: num1 = "123", num2 = "456" 80 | Output: "56088" 81 | 82 | Time complexity: O(m*n) 83 | Space Complexity: O(m+n) 84 | 85 | m and n are size of string 1 and 2 86 | 87 | */ 88 | 89 | -------------------------------------------------------------------------------- /C++/12-integer_to_roman.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Problem 12 : Integer to Roman 3 | 4 | Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. 5 | 6 | Symbol Value 7 | I 1 8 | V 5 9 | X 10 10 | L 50 11 | C 100 12 | D 500 13 | M 1000 14 | 15 | For example, 2 is written as II in Roman numeral, just two one's added together. 16 | 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, 17 | which is XX + V + II. 18 | 19 | Roman numerals are usually written largest to smallest from left to right. However, 20 | the numeral for four is not IIII. Instead, the number four is written as IV. Because 21 | the one is before the five we subtract it making four. The same principle applies to 22 | the number nine, which is written as IX. There are six instances where subtraction 23 | is used: 24 | 25 | I can be placed before V (5) and X (10) to make 4 and 9. 26 | X can be placed before L (50) and C (100) to make 40 and 90. 27 | C can be placed before D (500) and M (1000) to make 400 and 900. 28 | Given an integer, convert it to a roman numeral. 29 | */ 30 | 31 | class Solution { 32 | public: 33 | string intToRoman(int num) { 34 | //arrays corresponding each integer value to roman value 35 | vector val={1, 5, 10, 50, 100, 500, 1000}; 36 | vector romans={'I', 'V', 'X', 'L', 'C', 'D', 'M'}; 37 | string ans = ""; 38 | //traversing through arrays 39 | for(int i=6;i>=0;i--) { 40 | while(num>=val[i]) { 41 | //number is greater than current roman value 42 | //adding roman value to answer and subtracting integer value from number 43 | ans = ans+romans[i]; 44 | num -= val[i]; 45 | } 46 | if(i>0 && i%2!=0 && num>=val[i]-val[i-1]) { 47 | //current index is odd 48 | //checking if current value can be formed by subtracting a previous index from index 49 | ans = ans+romans[i-1]+romans[i]; 50 | num -= (val[i]-val[i-1]); 51 | } 52 | else if(i>0 && i%2==0 && num>=val[i]-val[i-2]) { 53 | //current index is even 54 | //checking if current value can be formed by subtracting a value at [i-2] index from index [i] 55 | //here at even index we can't check for subtracting [i-1] 56 | //because at every even index in our array, the correspong integer value is twice of prevoius value 57 | ans = ans+romans[i-2]+romans[i]; 58 | num -= (val[i]-val[i-2]); 59 | } 60 | } 61 | return ans; 62 | } 63 | }; 64 | 65 | /* 66 | Sample input : 58 67 | Sample Output : "LVIII" 68 | 69 | Time Complexity : O(n) 70 | Space Complexity : O(1) 71 | */ -------------------------------------------------------------------------------- /C/2-add_two_numbers.c: -------------------------------------------------------------------------------- 1 | /* Link Of Question: https://leetcode.com/problems/add-two-numbers/ 2 | 3 | 2. Add Two Numbers 4 | You are given two non-empty linked lists representing two non-negative integers. 5 | The digits are stored in reverse order, and each of their nodes contains a 6 | single digit. Add the two numbers and return the sum as a linked list. 7 | */ 8 | 9 | //Solution 10 | 11 | 12 | /* 13 | * Definition for singly-linked list. 14 | * struct ListNode { 15 | * int val; 16 | * struct ListNode *next; 17 | * }; 18 | */ 19 | 20 | 21 | struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){ 22 | int carry=0; 23 | struct ListNode *st,*t1,*t2; 24 | struct ListNode *start=(struct ListNode*)malloc(sizeof(struct ListNode)); 25 | t1=l1; 26 | t2=l2; 27 | st=start; 28 | while(t1!=NULL && t2!=NULL) 29 | { 30 | if((t1->val+t2->val+carry)>9) 31 | { 32 | start->val=(t1->val+t2->val+carry)%10; 33 | carry=1; 34 | } 35 | else 36 | { 37 | start->val=t1->val+t2->val+carry; 38 | carry=0; 39 | } 40 | t1=t1->next; 41 | t2=t2->next; 42 | if(t1==NULL && t2==NULL) 43 | start->next=NULL; 44 | else 45 | { 46 | start->next=(struct ListNode*)malloc(sizeof(struct ListNode)); 47 | start=start->next; 48 | } 49 | } 50 | if(t1!=NULL) 51 | { 52 | while(t1!=NULL) 53 | { 54 | if((t1->val+carry)>9) 55 | { 56 | start->val=(t1->val+carry)%10; 57 | carry=1; 58 | } 59 | else 60 | { 61 | start->val=t1->val+carry; 62 | carry=0; 63 | } 64 | t1=t1->next; 65 | if(t1==NULL) 66 | start->next=NULL; 67 | else 68 | { 69 | start->next=(struct ListNode*)malloc(sizeof(struct ListNode)); 70 | start=start->next; 71 | } 72 | } 73 | } 74 | else if(t2!=NULL) 75 | { 76 | while(t2!=NULL) 77 | { 78 | if((t2->val+carry)>9) 79 | { 80 | start->val=(t2->val+carry)%10; 81 | carry=1; 82 | } 83 | else 84 | { 85 | start->val=t2->val+carry; 86 | carry=0; 87 | } 88 | t2=t2->next; 89 | if(t2==NULL) 90 | start->next=NULL; 91 | else 92 | { 93 | start->next=(struct ListNode*)malloc(sizeof(struct ListNode)); 94 | start=start->next; 95 | } 96 | } 97 | } 98 | 99 | if(carry==1) 100 | { 101 | start->next=(struct ListNode*)malloc(sizeof(struct ListNode)); 102 | start=start->next; 103 | start->val=carry; 104 | start->next=NULL; 105 | } 106 | return st; 107 | } 108 | 109 | /* 110 | Sample Input: l1 = [2,4,3], l2 = [5,6,4] 111 | Sample Output: [7,0,8] 112 | Time Complexity: O(N) where N is the number of nodes in Linked List 113 | Space Complexity: O(N) 114 | */ 115 | -------------------------------------------------------------------------------- /C++/1697-Checking_Existence_of_Edge_Length_Limited_Paths.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prerequisites: 3 | Graph theory + DSU 4 | Problem Statement: 5 | An undirected graph of n nodes is defined by edgeList, where edgeList[i] = [ui, vi, disi] denotes an edge between 6 | nodes ui and vi with distance disi. Note that there may be multiple edges between two nodes. 7 | Given an array queries, where queries[j] = [pj, qj, limitj], your task is to determine for each queries[j] whether 8 | there is a path between pj and qj such that each edge on the path has a distance strictly less than limitj . 9 | Return a boolean array answer, where answer.length == queries.length and the jth value of answer is true if there is 10 | a path for queries[j] is true, and false otherwise. 11 | 12 | Example 1: 13 | Input: n = 3, edgeList = [[0,1,2],[1,2,4],[2,0,8],[1,0,16]], queries = [[0,1,2],[0,2,5]] 14 | Output: [false,true] 15 | Explanation: The above figure shows the given graph. Note that there are two overlapping edges between 0 and 1 with 16 | distances 2 and 16. 17 | For the first query, between 0 and 1 there is no path where each distance is less than 2, thus we return false for 18 | this query. 19 | For the second query, there is a path (0 -> 1 -> 2) of two edges with distances less than 5, thus we return true for 20 | this query. 21 | Example 2: 22 | Input: n = 5, edgeList = [[0,1,10],[1,2,5],[2,3,9],[3,4,13]], queries = [[0,4,14],[1,4,13]] 23 | Output: [true,false] 24 | Exaplanation: The above figure shows the given graph. 25 | 26 | Constraints: 27 | 2 <= n <= 105 28 | 1 <= edgeList.length, queries.length <= 105 29 | edgeList[i].length == 3 30 | queries[j].length == 3 31 | 0 <= ui, vi, pj, qj <= n - 1 32 | ui != vi 33 | pj != qj 34 | 1 <= disi, limitj <= 109 35 | There may be multiple edges between two nodes. 36 | */ 37 | 38 | /* 39 | Interesting thing in the problem: 40 | If we think in a simple manner and do some efforts for reducing the time complexity then we will reduce only till O((n+m)*q) but some 41 | more thinking to the problem can reduce it to O(q+n+m) which is required in the given task. 42 | */ 43 | 44 | // My code 45 | 46 | class Solution { 47 | public: 48 | 49 | int fa[100010]; 50 | 51 | int find_fa(int p){ 52 | int j, k; 53 | j = p; 54 | while (fa[j] != -1) j = fa[j]; 55 | while (p != j){ 56 | k = fa[p]; 57 | fa[p] = j; 58 | p = k; 59 | } 60 | 61 | return j; 62 | } 63 | 64 | vector distanceLimitedPathsExist(int n, vector>& el, vector>& qr) { 65 | vector>> e; 66 | vector, pair>> q; 67 | vector res; 68 | int i, p, x, y, l, lm, j; 69 | for (i = 0; i < el.size(); ++i) e.push_back({el[i][2],{el[i][0], el[i][1]}}); 70 | sort(e.begin(), e.end()); 71 | for (i = 0; i < n; ++i) fa[i] = -1; 72 | for (i = 0; i < qr.size(); ++i) { 73 | q.push_back({{qr[i][2], i}, {qr[i][0], qr[i][1]}}); 74 | res.push_back(false); 75 | } 76 | sort(q.begin(), q.end()); 77 | p = 0; 78 | for (i = 0; i < q.size(); ++i){ 79 | lm = q[i].first.first; 80 | j = q[i].first.second; 81 | while (p < e.size() && e[p].first < lm){ 82 | x = e[p].second.first; 83 | y = e[p].second.second; 84 | x = find_fa(x); 85 | y = find_fa(y); 86 | if (x != y) fa[y] = x; 87 | ++p; 88 | } 89 | 90 | x = find_fa(q[i].second.first); 91 | y = find_fa(q[i].second.second); 92 | res[j] = (x == y); 93 | } 94 | 95 | return res; 96 | } 97 | }; 98 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PR doesn't count as a valid PR for Hacktoberfest, shows Excluded/BAD Repository 2 | 3 | # HACKTOBERFEST 2021 4 | 5 | ![Open Source Love](https://img.shields.io/badge/Open%20Source-%E2%9D%A4-red.svg) 6 | [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://github.com/piyushsharma220699/Hacktoberfest-2021)  7 | [![first-timers-only](https://img.shields.io/badge/first--timers--only-friendly-blue.svg?style=flat-square)](https://hacktoberfest.netlify.com/)  8 | ![GitHub](https://img.shields.io/github/license/piyushsharma220699/Hacktoberfest-2021.svg) 9 | ![GitHub forks](https://img.shields.io/github/forks/piyushsharma220699/Hacktoberfest-2021.svg) 10 | ![GitHub issues](https://img.shields.io/github/issues/piyushsharma220699/Hacktoberfest-2021.svg) 11 | ![GitHub pull requests](https://img.shields.io/github/issues-pr/piyushsharma220699/Hacktoberfest-2021.svg) 12 | ![GitHub contributors](https://img.shields.io/github/contributors/piyushsharma220699/Hacktoberfest-2021.svg) 13 | ![](https://visitor-badge.glitch.me/badge?page_id=piyushsharma220699.Hacktoberfest-2021) 14 | [![Stars: piyushsharma220699](https://img.shields.io/github/stars/piyushsharma220699/Hacktoberfest-2021?color=gree&label=Star%20this%20Repo&logo=Star%20this%20Repo)](https://github.com/piyushsharma220699/Hacktoberfest-2021) 15 | [![Followers: piyushsharma220699](https://img.shields.io/github/followers/piyushsharma220699?label=Follow%20Me)](https://github.com/piyushsharma220699) 16 | 17 | Please don't forget to read the NOTE FOR CONTRIBUTORS.md file before making the contribution otherwise your PR will not get accepted! Also, ⭐ the repo to appreciate the work. 18 | 19 | Hacktoberfest 2021 20 | 21 | This is a beginner-friendly leetcoding project to help you get started with your hacktoberfest. This repository is actually made to help beginners step up and join hands with the open source community. Feel free to start contributing. There are no wrong contributions. If you don't know where to start, feel free to contact me. Please don't forget to read the contribution rules. Happy hacking <3 !! 22 | 23 | Don't forget to register yourself on hacktoberfest page 24 | 25 |
26 | 27 | ## EVENT DETAILS: 28 | 29 | 1. Hacktoberfest is a month-long challenge that happens in every year in the month of October, marking the celebration of Open Source Community. It helps the newbies to create their 1st meaningful PR. 30 | 2. It is hosted by Digital Ocean in partnership with Github and other companies like DEV, Intel etc. 31 | 3. All backgrounds and skill levels are encouraged to complete the challenge. 32 | 33 | To know more about the event, WATCH THIS 34 | 35 |
36 | 37 | ## HALL OF FAME (CHECK OUT OUR CONTRIBUTORS) 🏆🦸 38 | 39 | 40 | 41 | 42 | 43 |
44 | 45 | ## HERE COMES OUR FORKERS 🍴🦸 46 | 47 | [![Forkers repo roster for @piyushsharma220699/Hacktoberfest-2021](https://reporoster.com/forks/piyushsharma220699/Hacktoberfest-2021)](https://github.com/piyushsharma220699/Hacktoberfest-2021/network/members) 48 | 49 | ## OUR STARGAZERS ARE FEATURED HERE ⭐✨ 50 | [![Stargazers repo roster for @piyushsharma220699/Hacktoberfest-2021](https://reporoster.com/stars/piyushsharma220699/Hacktoberfest-2021)](https://github.com/piyushsharma220699/Hacktoberfest-2021/stargazers) 51 | 52 | ## ⭐⭐ Don't forget to drop a star if you like my work ⭐⭐ 53 | -------------------------------------------------------------------------------- /C++/8-string_to_integer_atoi.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Problem 8 : String to Integer (atoi) 3 | 4 | Implement the myAtoi(string s) function, which converts a string to a 32-bit signed 5 | integer (similar to C/C++'s atoi function). 6 | 7 | The algorithm for myAtoi(string s) is as follows: 8 | 9 | 1. Read in and ignore any leading whitespace. 10 | 2. Check if the next character (if not already at the end of the string) is '-' 11 | or '+'. Read this character in if it is either. This determines if the final 12 | result is negative or positive respectively. Assume the result is positive if 13 | neither is present. 14 | 3. Read in next the characters until the next non-digit charcter or the end of the 15 | input is reached. The rest of the string is ignored. 16 | 4. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no 17 | digits were read, then the integer is 0. Change the sign as necessary 18 | (from step 2). 19 | 5. If the integer is out of the 32-bit signed integer range [-231, 231 - 1], 20 | then clamp the integer so that it remains in the range. Specifically, integers 21 | less than -231 should be clamped to -231, and integers greater than 231 - 1 22 | should be clamped to 231 - 1. 23 | 6. Return the integer as the final result. 24 | 25 | Note: 26 | 27 | 1. Only the space character ' ' is considered a whitespace character. 28 | 2. Do not ignore any characters other than the leading whitespace or the rest of 29 | the string after the digits. 30 | */ 31 | 32 | class Solution { 33 | public: 34 | int myAtoi(string s) { 35 | int ans=0; 36 | bool flag = false, check = true; 37 | for(int i=0;i='0' && s[i]<='9'){ 50 | //digits have started 51 | //extract number from string 52 | while(i='0' && s[i]<='9'){ 54 | //current character is digit 55 | if(ans >= 214748364){ 56 | //checking if final answer could result out of 32-bit integer size 57 | if(flag){ 58 | //final answer is negative, check if it goes out of limit 59 | if((s[i]>'7' && ans==214748364) || ans>214748364){ 60 | return -2147483648; 61 | } 62 | } 63 | else{ 64 | if((s[i]>'6' && ans==214748364)||ans>214748364){ 65 | return 2147483647; 66 | } 67 | } 68 | } 69 | //add current digit to final answer 70 | ans = ans*10+(s[i]-'0'); 71 | } 72 | else{ 73 | //the current character is not a digit 74 | //so ignore the rest of string 75 | break; 76 | } 77 | i++; 78 | } 79 | //successfully extracted number from string 80 | //end the outer loop 81 | break; 82 | } 83 | else{ 84 | //current character is not a leading whitespace, not sign and not a digit 85 | //so return 0 86 | return 0; 87 | } 88 | } 89 | if(flag){ 90 | //answer is negative, change sign 91 | ans = 0-ans; 92 | } 93 | return ans; 94 | } 95 | }; 96 | 97 | /* 98 | Sample input : "42" 99 | Sample output : 42 100 | 101 | Time Complexity : O(n) 102 | Space Complexity : O(1) 103 | n = length of string 104 | */ --------------------------------------------------------------------------------