├── 0001-two-sum ├── NOTES.md ├── 0001-two-sum.cpp └── README.md ├── 0202-happy-number ├── NOTES.md └── 0202-happy-number.cpp ├── 0258-add-digits ├── NOTES.md ├── 0258-add-digits.cpp └── README.md ├── 0041-first-missing-positive ├── NOTES.md ├── 0041-first-missing-positive.cpp └── README.md ├── 3280-convert-date-to-binary ├── NOTES.md └── 3280-convert-date-to-binary.cpp ├── 2109-adding-spaces-to-a-string ├── NOTES.md ├── 2109-adding-spaces-to-a-string.cpp └── README.md ├── 2351-first-letter-to-appear-twice ├── NOTES.md └── 2351-first-letter-to-appear-twice.cpp ├── 0540-single-element-in-a-sorted-array ├── NOTES.md ├── 0540-single-element-in-a-sorted-array.cpp └── README.md ├── 1475-final-prices-with-a-special-discount-in-a-shop ├── NOTES.md ├── 1475-final-prices-with-a-special-discount-in-a-shop.cpp └── README.md ├── 0217-contains-duplicate ├── 0217-contains-duplicate.cpp └── README.md ├── 2292-counting-words-with-a-given-prefix ├── 2292-counting-words-with-a-given-prefix.cpp └── README.md ├── 0260-single-number-iii ├── 0260-single-number-iii.cpp └── README.md ├── 3997-1346-441-arranging-coins ├── 3997-1346-441-arranging-coins.cpp └── README.md ├── 0229-majority-element-ii ├── 0229-majority-element-ii.cpp └── README.md ├── 0238-product-of-array-except-self ├── 0238-product-of-array-except-self.cpp └── README.md ├── 0026-remove-duplicates-from-sorted-array ├── 0026-remove-duplicates-from-sorted-array.cpp └── README.md ├── 0349-intersection-of-two-arrays ├── 0349-intersection-of-two-arrays.cpp └── README.md ├── 0387-first-unique-character-in-a-string ├── 0387-first-unique-character-in-a-string.cpp └── README.md ├── 0058-length-of-last-word ├── 0058-length-of-last-word.cpp └── README.md ├── 0350-intersection-of-two-arrays-ii ├── 0350-intersection-of-two-arrays-ii.cpp └── README.md └── 0119-7-3289-the-two-sneaky-numbers-of-digitville ├── 0119-7-3289-the-two-sneaky-numbers-of-digitville.cpp └── README.md /0001-two-sum/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0202-happy-number/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0258-add-digits/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0041-first-missing-positive/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /3280-convert-date-to-binary/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2109-adding-spaces-to-a-string/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /2351-first-letter-to-appear-twice/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /1475-final-prices-with-a-special-discount-in-a-shop/NOTES.md: -------------------------------------------------------------------------------- 1 | ​ -------------------------------------------------------------------------------- /0258-add-digits/0258-add-digits.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int addDigits(int num) { 4 | int n=num; 5 | int k; 6 | int sum=0; 7 | while(n>0){ 8 | sum+=n%10; 9 | n=n/10; 10 | } 11 | while(sum>=10){ 12 | sum=(sum-10)+1; 13 | } 14 | return sum; 15 | 16 | } 17 | }; -------------------------------------------------------------------------------- /0217-contains-duplicate/0217-contains-duplicate.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool containsDuplicate(vector& nums) { 4 | int n=nums.size(); 5 | unordered_mapmap; 6 | for(int i=0;i=2){ 11 | return true; 12 | } 13 | } 14 | return false; 15 | } 16 | }; -------------------------------------------------------------------------------- /2292-counting-words-with-a-given-prefix/2292-counting-words-with-a-given-prefix.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int prefixCount(vector& words, string pref) { 4 | int c = 0; 5 | int n = pref.length(); 6 | for (const string& w : words) { 7 | if (w.size() >= n && w.compare(0, n, pref) == 0) { 8 | c++; 9 | } 10 | } 11 | return c; 12 | } 13 | }; -------------------------------------------------------------------------------- /0260-single-number-iii/0260-single-number-iii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector singleNumber(vector& nums) { 4 | int n=nums.size(); 5 | int i,k; 6 | vectorarr; 7 | unordered_mapmap; 8 | for(i=0;i finalPrices(vector& A) { 4 | vector stack; 5 | for (int i = 0; i < A.size(); ++i) { 6 | while (stack.size() && A[stack.back()] >= A[i]) { 7 | A[stack.back()] -= A[i]; 8 | stack.pop_back(); 9 | } 10 | stack.push_back(i); 11 | } 12 | return A; 13 | } 14 | }; -------------------------------------------------------------------------------- /3997-1346-441-arranging-coins/3997-1346-441-arranging-coins.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int arrangeCoins(int n) { 4 | // n=5 n-i 5 | //index 1=> 5-1=4 n=4 6 | //index 2=> 4-2=2 n=2 7 | //index 3=> 2-3=-1 n=-1 8 | int i;//index 9 | int k=n; //passing the n valye to k 10 | int l; 11 | for(i=1;i<=n;i++){ 12 | k=k-i; 13 | if(k<0){ 14 | return i-1; 15 | } 16 | } 17 | return 1; 18 | } 19 | }; -------------------------------------------------------------------------------- /2351-first-letter-to-appear-twice/2351-first-letter-to-appear-twice.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char repeatedCharacter(string s) { 4 | unordered_mapmp; 5 | // v f 6 | int n=s.size(); 7 | int i;//index 8 | for(i=0;i majorityElement(vector& nums) { 4 | int n=nums.size(); 5 | unordered_mapmap; 6 | vectorans; 7 | int i; 8 | for(i=0;in/3){ 13 | ans.push_back(k.first); 14 | } 15 | } 16 | sort(ans.begin(),ans.end()); 17 | return ans; 18 | } 19 | }; -------------------------------------------------------------------------------- /0001-two-sum/0001-two-sum.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector twoSum(vector& nums, int target) { 4 | int n = nums.size(); 5 | int i = 0; 6 | unordered_map mp; 7 | for(int i = 0; i < n; i++){ 8 | mp[nums[i]] = i; 9 | } 10 | 11 | for(int i=0;i productExceptSelf(vector& nums) { 4 | int n,i; 5 | n=nums.size(); 6 | int arr[n]; 7 | int rig[n]; 8 | arr[0]=1; 9 | rig[n-1]=1; 10 | for(i=1;i& nums) { 4 | int i,j; 5 | unordered_mapmap; 6 | int n=nums.size(); 7 | vectorarr; 8 | for(i=0;i intersection(vector& nums1, vector& nums2) { 4 | unordered_mapmap;//empty 5 | unordered_setset;//empty 6 | int i;//index 7 | for(auto k:nums2){ 8 | map[k]++; 9 | } 10 | int n=nums1.size(); 11 | for(i=0;ians; 17 | for(auto l:set){ 18 | ans.push_back(l); 19 | } 20 | return ans; 21 | 22 | } 23 | }; -------------------------------------------------------------------------------- /0041-first-missing-positive/0041-first-missing-positive.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int firstMissingPositive(vector& nums) { 4 | /* 5 | unordered_map 6 | */ 7 | unordered_mapmap;//empty 8 | //v f==i 9 | int i;//index 10 | int n=nums.size();//size 11 | for(i=0;i& nums) { 4 | // bit 5 | // unordered map 6 | // generally basics 7 | 8 | /* 9 | xor operation (^) 10 | 1-> 8421= 0001 11 | 3-> 8421= 0011 12 | -------------- 13 | output: 14 | 0010 (2) 15 | 16 | 1-> 8421= 0001 3-> 8421 =0011 17 | 1-> 8421= 0001 3-> 8421 =0011 18 | 19 | 0000(0)*/ 20 | int s=0; 21 | int i;//index 22 | for(i=0;imp 5 | unordered_mapmp;//empty 6 | // v f 7 | int n=s.size();//size 8 | int i;//index 9 | for(i=0;i1 13 | e=>1->2->3 14 | t=>1 15 | c=>1 16 | o=>1 17 | d=>1*/ 18 | 19 | } 20 | for(i=0;i& spaces) { 4 | /* t.c s.c 5 | unordered_map O(n) O(n) 6 | string matching O(n+m) O(1) 7 | */ 8 | unordered_mapmap;//empty 9 | // v f 10 | int n=s.size(); 11 | int m=spaces.size(); 12 | int i;//index 13 | for(i=0;i=0;i--){ 15 | if(s[i]==' '){ 16 | m++; 17 | } 18 | else{ 19 | break; 20 | } 21 | } 22 | //space count 23 | for(i=n-1-m;i>=0;i--){ 24 | if(s[i]!=' '){ 25 | count++; 26 | } 27 | else{ 28 | break; 29 | } 30 | } 31 | return count; 32 | 33 | } 34 | }; -------------------------------------------------------------------------------- /0350-intersection-of-two-arrays-ii/0350-intersection-of-two-arrays-ii.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector intersect(vector& nums1, vector& nums2) { 4 | //unordered_mapmp; 5 | unordered_mapmp;//empty 6 | int i;//index 7 | int n=nums1.size(); 8 | for(auto i:nums2){ 9 | mp[i]++; 10 | /* v f 11 | 2=>1 12 | 2=>2 13 | example 2 14 | v f 15 | 9=>1->2 16 | 4=>1->2 17 | 8=>1*/ 18 | } 19 | vectorarr; 20 | for(i=0;iO(n) 12 | string str=""; 13 | while(d>0){ 14 | str+=(d%2+'0'); 15 | d=d/2; 16 | } 17 | str+='-'; 18 | while(m>0){ 19 | str+=(m%2+'0'); 20 | m=m/2; 21 | } 22 | str+='-'; 23 | while(y>0){ 24 | str+=(y%2+'0'); 25 | y=y/2; 26 | } 27 | reverse(str.begin(),str.end()); 28 | return str; 29 | } 30 | }; -------------------------------------------------------------------------------- /0202-happy-number/0202-happy-number.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isHappy(int n) { 4 | /* unordered_map*/ 5 | //n=18 6 | unordered_mapmap; 7 | /* 8 | 1. n update sum 9 | 19 => 82 n will store again 82 sum=0 10 | 82 => 68 n will store again 68 sum=0 11 | 68=>100 n will stores again 100 sum=0*/ 12 | /* 13 | 2. if(sum != map) 14 | return false;*/ 15 | /*n=19 16 | n%10=9 17 | n=n/10; 18 | n=1 19 | n%10=1; 20 | */ 21 | int sum=0; 22 | while(sum!=1){ 23 | sum=0; 24 | while(n>0){ 25 | sum+=pow(n%10,2); 26 | n=n/10; 27 | } 28 | //f (or) map.second 29 | if(map[sum]==2){ 30 | return false; 31 | } 32 | map[sum]++; 33 | n=sum; 34 | } 35 | return true; 36 | 37 | } 38 | }; -------------------------------------------------------------------------------- /0258-add-digits/README.md: -------------------------------------------------------------------------------- 1 |

258. Add Digits

Easy


Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: num = 38
 7 | Output: 2
 8 | Explanation: The process is
 9 | 38 --> 3 + 8 --> 11
10 | 11 --> 1 + 1 --> 2 
11 | Since 2 has only one digit, return it.
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: num = 0
17 | Output: 0
18 | 
19 | 20 |

 

21 |

Constraints:

22 | 23 |
    24 |
  • 0 <= num <= 231 - 1
  • 25 |
26 | 27 |

 

28 |

Follow up: Could you do it without any loop/recursion in O(1) runtime?

29 |
-------------------------------------------------------------------------------- /0540-single-element-in-a-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

540. Single Element in a Sorted Array

Medium


You are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once.

2 | 3 |

Return the single element that appears only once.

4 | 5 |

Your solution must run in O(log n) time and O(1) space.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,1,2,3,3,4,4,8,8]
10 | Output: 2
11 | 

Example 2:

12 |
Input: nums = [3,3,7,7,10,11,11]
13 | Output: 10
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 1 <= nums.length <= 105
  • 20 |
  • 0 <= nums[i] <= 105
  • 21 |
22 |
-------------------------------------------------------------------------------- /0349-intersection-of-two-arrays/README.md: -------------------------------------------------------------------------------- 1 |

349. Intersection of Two Arrays

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums1 = [1,2,2,1], nums2 = [2,2]
 7 | Output: [2]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
13 | Output: [9,4]
14 | Explanation: [4,9] is also accepted.
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 22 |
  • 0 <= nums1[i], nums2[i] <= 1000
  • 23 |
24 |
-------------------------------------------------------------------------------- /0229-majority-element-ii/README.md: -------------------------------------------------------------------------------- 1 |

229. Majority Element II

Medium


Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums = [3,2,3]
 7 | Output: [3]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: nums = [1]
13 | Output: [1]
14 | 
15 | 16 |

Example 3:

17 | 18 |
Input: nums = [1,2]
19 | Output: [1,2]
20 | 
21 | 22 |

 

23 |

Constraints:

24 | 25 |
    26 |
  • 1 <= nums.length <= 5 * 104
  • 27 |
  • -109 <= nums[i] <= 109
  • 28 |
29 | 30 |

 

31 |

Follow up: Could you solve the problem in linear time and in O(1) space?

32 |
-------------------------------------------------------------------------------- /0119-7-3289-the-two-sneaky-numbers-of-digitville/0119-7-3289-the-two-sneaky-numbers-of-digitville.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector getSneakyNumbers(vector& nums) { 4 | /* unordered_map 5 | general format 6 | 7 | unordered_mapmap; 8 | v f 9 | */ 10 | unordered_mapmap;//empity 11 | //v f 12 | int n=nums.size();//size 13 | int i;//index 14 | for(i=0;i1 1->1 2->1 19 | 0->2 1->2 20 | 1->3 21 | */ 22 | } 23 | vectorarr;//decleration of array dynamic 24 | // int arr[n];//decleration of array static 25 | for(auto l:map){ 26 | //we have v it has f ==2 27 | //map.first=>l.first(value) 28 | //map.second=>l.second(frequency) 29 | if(l.second==2){ 30 | arr.push_back(l.first); 31 | } 32 | } 33 | return arr; 34 | 35 | } 36 | }; -------------------------------------------------------------------------------- /3997-1346-441-arranging-coins/README.md: -------------------------------------------------------------------------------- 1 |

 3997 1346 2 | 441. Arranging Coins


You have n coins and you want to build a staircase with these coins. The staircase consists of k rows where the ith row has exactly i coins. The last row of the staircase may be incomplete.

3 | 4 |

Given the integer n, return the number of complete rows of the staircase you will build.

5 | 6 |

 

7 |

Example 1:

8 | 9 |
Input: n = 5
10 | Output: 2
11 | Explanation: Because the 3rd row is incomplete, we return 2.
12 | 
13 | 14 |

Example 2:

15 | 16 |
Input: n = 8
17 | Output: 3
18 | Explanation: Because the 4th row is incomplete, we return 3.
19 | 
20 | 21 |

 

22 |

Constraints:

23 | 24 |
    25 |
  • 1 <= n <= 231 - 1
  • 26 |
27 |
-------------------------------------------------------------------------------- /0041-first-missing-positive/README.md: -------------------------------------------------------------------------------- 1 |

41. First Missing Positive

Hard


Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.

2 | 3 |

You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,0]
 9 | Output: 3
10 | Explanation: The numbers in the range [1,2] are all in the array.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [3,4,-1,1]
16 | Output: 2
17 | Explanation: 1 is in the array but 2 is missing.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: nums = [7,8,9,11,12]
23 | Output: 1
24 | Explanation: The smallest positive integer 1 is missing.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= nums.length <= 105
  • 32 |
  • -231 <= nums[i] <= 231 - 1
  • 33 |
34 |
-------------------------------------------------------------------------------- /0260-single-number-iii/README.md: -------------------------------------------------------------------------------- 1 |

260. Single Number III

Medium


Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

2 | 3 |

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: nums = [1,2,1,3,2,5]
 9 | Output: [3,5]
10 | Explanation:  [5, 3] is also a valid answer.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: nums = [-1,0]
16 | Output: [-1,0]
17 | 
18 | 19 |

Example 3:

20 | 21 |
Input: nums = [0,1]
22 | Output: [1,0]
23 | 
24 | 25 |

 

26 |

Constraints:

27 | 28 |
    29 |
  • 2 <= nums.length <= 3 * 104
  • 30 |
  • -231 <= nums[i] <= 231 - 1
  • 31 |
  • Each integer in nums will appear twice, only two integers will appear once.
  • 32 |
33 |
-------------------------------------------------------------------------------- /0058-length-of-last-word/README.md: -------------------------------------------------------------------------------- 1 |

58. Length of Last Word

Easy


Given a string s consisting of words and spaces, return the length of the last word in the string.

2 | 3 |

A word is a maximal substring consisting of non-space characters only.

4 | 5 |

 

6 |

Example 1:

7 | 8 |
Input: s = "Hello World"
 9 | Output: 5
10 | Explanation: The last word is "World" with length 5.
11 | 
12 | 13 |

Example 2:

14 | 15 |
Input: s = "   fly me   to   the moon  "
16 | Output: 4
17 | Explanation: The last word is "moon" with length 4.
18 | 
19 | 20 |

Example 3:

21 | 22 |
Input: s = "luffy is still joyboy"
23 | Output: 6
24 | Explanation: The last word is "joyboy" with length 6.
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 1 <= s.length <= 104
  • 32 |
  • s consists of only English letters and spaces ' '.
  • 33 |
  • There will be at least one word in s.
  • 34 |
35 |
-------------------------------------------------------------------------------- /0350-intersection-of-two-arrays-ii/README.md: -------------------------------------------------------------------------------- 1 |

350. Intersection of Two Arrays II

Easy


Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
Input: nums1 = [1,2,2,1], nums2 = [2,2]
 7 | Output: [2,2]
 8 | 
9 | 10 |

Example 2:

11 | 12 |
Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
13 | Output: [4,9]
14 | Explanation: [9,4] is also accepted.
15 | 
16 | 17 |

 

18 |

Constraints:

19 | 20 |
    21 |
  • 1 <= nums1.length, nums2.length <= 1000
  • 22 |
  • 0 <= nums1[i], nums2[i] <= 1000
  • 23 |
24 | 25 |

 

26 |

Follow up:

27 | 28 |
    29 |
  • What if the given array is already sorted? How would you optimize your algorithm?
  • 30 |
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • 31 |
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
  • 32 |
33 |
-------------------------------------------------------------------------------- /0387-first-unique-character-in-a-string/README.md: -------------------------------------------------------------------------------- 1 |

387. First Unique Character in a String

Easy


Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
7 |

Input: s = "leetcode"

8 | 9 |

Output: 0

10 | 11 |

Explanation:

12 | 13 |

The character 'l' at index 0 is the first character that does not occur at any other index.

14 |
15 | 16 |

Example 2:

17 | 18 |
19 |

Input: s = "loveleetcode"

20 | 21 |

Output: 2

22 |
23 | 24 |

Example 3:

25 | 26 |
27 |

Input: s = "aabb"

28 | 29 |

Output: -1

30 |
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • 1 <= s.length <= 105
  • 37 |
  • s consists of only lowercase English letters.
  • 38 |
39 |
-------------------------------------------------------------------------------- /0217-contains-duplicate/README.md: -------------------------------------------------------------------------------- 1 |

217. Contains Duplicate

Easy


Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct.

2 | 3 |

 

4 |

Example 1:

5 | 6 |
7 |

Input: nums = [1,2,3,1]

8 | 9 |

Output: true

10 | 11 |

Explanation:

12 | 13 |

The element 1 occurs at the indices 0 and 3.

14 |
15 | 16 |

Example 2:

17 | 18 |
19 |

Input: nums = [1,2,3,4]

20 | 21 |

Output: false

22 | 23 |

Explanation:

24 | 25 |

All elements are distinct.

26 |
27 | 28 |

Example 3:

29 | 30 |
31 |

Input: nums = [1,1,1,3,3,4,3,2,4,2]

32 | 33 |

Output: true

34 |
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 1 <= nums.length <= 105
  • 41 |
  • -109 <= nums[i] <= 109
  • 42 |
43 |
-------------------------------------------------------------------------------- /0238-product-of-array-except-self/README.md: -------------------------------------------------------------------------------- 1 |

238. Product of Array Except Self

Medium


Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

2 | 3 |

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

4 | 5 |

You must write an algorithm that runs in O(n) time and without using the division operation.

6 | 7 |

 

8 |

Example 1:

9 |
Input: nums = [1,2,3,4]
10 | Output: [24,12,8,6]
11 | 

Example 2:

12 |
Input: nums = [-1,1,0,-3,3]
13 | Output: [0,0,9,0,0]
14 | 
15 |

 

16 |

Constraints:

17 | 18 |
    19 |
  • 2 <= nums.length <= 105
  • 20 |
  • -30 <= nums[i] <= 30
  • 21 |
  • The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.
  • 22 |
23 | 24 |

 

25 |

Follow up: Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

26 |
-------------------------------------------------------------------------------- /0001-two-sum/README.md: -------------------------------------------------------------------------------- 1 |

1. Two Sum

Easy


Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

2 | 3 |

You may assume that each input would have exactly one solution, and you may not use the same element twice.

4 | 5 |

You can return the answer in any order.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: nums = [2,7,11,15], target = 9
11 | Output: [0,1]
12 | Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
13 | 
14 | 15 |

Example 2:

16 | 17 |
Input: nums = [3,2,4], target = 6
18 | Output: [1,2]
19 | 
20 | 21 |

Example 3:

22 | 23 |
Input: nums = [3,3], target = 6
24 | Output: [0,1]
25 | 
26 | 27 |

 

28 |

Constraints:

29 | 30 |
    31 |
  • 2 <= nums.length <= 104
  • 32 |
  • -109 <= nums[i] <= 109
  • 33 |
  • -109 <= target <= 109
  • 34 |
  • Only one valid answer exists.
  • 35 |
36 | 37 |

 

38 | Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
-------------------------------------------------------------------------------- /2292-counting-words-with-a-given-prefix/README.md: -------------------------------------------------------------------------------- 1 |

2292. Counting Words With a Given Prefix

Easy


You are given an array of strings words and a string pref.

2 | 3 |

Return the number of strings in words that contain pref as a prefix.

4 | 5 |

A prefix of a string s is any leading contiguous substring of s.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
11 | Input: words = ["pay","attention","practice","attend"], pref = "at"
12 | Output: 2
13 | Explanation: The 2 strings that contain "at" as a prefix are: "attention" and "attend".
14 | 
15 | 16 |

Example 2:

17 | 18 |
19 | Input: words = ["leetcode","win","loops","success"], pref = "code"
20 | Output: 0
21 | Explanation: There are no strings that contain "code" as a prefix.
22 | 
23 | 24 |

 

25 |

Constraints:

26 | 27 |
    28 |
  • 1 <= words.length <= 100
  • 29 |
  • 1 <= words[i].length, pref.length <= 100
  • 30 |
  • words[i] and pref consist of lowercase English letters.
  • 31 |
32 | -------------------------------------------------------------------------------- /1475-final-prices-with-a-special-discount-in-a-shop/README.md: -------------------------------------------------------------------------------- 1 |

1475. Final Prices With a Special Discount in a Shop

Easy


You are given an integer array prices where prices[i] is the price of the ith item in a shop.

2 | 3 |

There is a special discount for items in the shop. If you buy the ith item, then you will receive a discount equivalent to prices[j] where j is the minimum index such that j > i and prices[j] <= prices[i]. Otherwise, you will not receive any discount at all.

4 | 5 |

Return an integer array answer where answer[i] is the final price you will pay for the ith item of the shop, considering the special discount.

6 | 7 |

 

8 |

Example 1:

9 | 10 |
Input: prices = [8,4,6,2,3]
11 | Output: [4,2,4,2,3]
12 | Explanation: 
13 | For item 0 with price[0]=8 you will receive a discount equivalent to prices[1]=4, therefore, the final price you will pay is 8 - 4 = 4.
14 | For item 1 with price[1]=4 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 4 - 2 = 2.
15 | For item 2 with price[2]=6 you will receive a discount equivalent to prices[3]=2, therefore, the final price you will pay is 6 - 2 = 4.
16 | For items 3 and 4 you will not receive any discount at all.
17 | 
18 | 19 |

Example 2:

20 | 21 |
Input: prices = [1,2,3,4,5]
22 | Output: [1,2,3,4,5]
23 | Explanation: In this case, for all items, you will not receive any discount at all.
24 | 
25 | 26 |

Example 3:

27 | 28 |
Input: prices = [10,1,1,6]
29 | Output: [9,0,1,6]
30 | 
31 | 32 |

 

33 |

Constraints:

34 | 35 |
    36 |
  • 1 <= prices.length <= 500
  • 37 |
  • 1 <= prices[i] <= 1000
  • 38 |
39 |
-------------------------------------------------------------------------------- /0119-7-3289-the-two-sneaky-numbers-of-digitville/README.md: -------------------------------------------------------------------------------- 1 |

 119 7 2 | 3289. The Two Sneaky Numbers of Digitville


In the town of Digitville, there was a list of numbers called nums containing integers from 0 to n - 1. Each number was supposed to appear exactly once in the list, however, two mischievous numbers sneaked in an additional time, making the list longer than usual.

3 | 4 |

As the town detective, your task is to find these two sneaky numbers. Return an array of size two containing the two numbers (in any order), so peace can return to Digitville.

5 | 6 |

 

7 |

Example 1:

8 | 9 |
10 |

Input: nums = [0,1,1,0]

11 | 12 |

Output: [0,1]

13 | 14 |

Explanation:

15 | 16 |

The numbers 0 and 1 each appear twice in the array.

17 |
18 | 19 |

Example 2:

20 | 21 |
22 |

Input: nums = [0,3,2,1,3,2]

23 | 24 |

Output: [2,3]

25 | 26 |

Explanation:

27 | 28 |

The numbers 2 and 3 each appear twice in the array.

29 |
30 | 31 |

Example 3:

32 | 33 |
34 |

Input: nums = [7,1,5,4,3,4,6,0,9,5,8,2]

35 | 36 |

Output: [4,5]

37 | 38 |

Explanation:

39 | 40 |

The numbers 4 and 5 each appear twice in the array.

41 |
42 | 43 |

 

44 |

Constraints:

45 | 46 |
    47 |
  • 2 <= n <= 100
  • 48 |
  • nums.length == n + 2
  • 49 |
  • 0 <= nums[i] < n
  • 50 |
  • The input is generated such that nums contains exactly two repeated elements.
  • 51 |
52 |
-------------------------------------------------------------------------------- /2109-adding-spaces-to-a-string/README.md: -------------------------------------------------------------------------------- 1 |

2109. Adding Spaces to a String

Medium


You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.

2 | 3 |
    4 |
  • For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y' and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy Your Coffee".
  • 5 |
6 | 7 |

Return the modified string after the spaces have been added.

8 | 9 |

 

10 |

Example 1:

11 | 12 |
Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
13 | Output: "Leetcode Helps Me Learn"
14 | Explanation: 
15 | The indices 8, 13, and 15 correspond to the underlined characters in "LeetcodeHelpsMeLearn".
16 | We then place spaces before those characters.
17 | 
18 | 19 |

Example 2:

20 | 21 |
Input: s = "icodeinpython", spaces = [1,5,7,9]
22 | Output: "i code in py thon"
23 | Explanation:
24 | The indices 1, 5, 7, and 9 correspond to the underlined characters in "icodeinpython".
25 | We then place spaces before those characters.
26 | 
27 | 28 |

Example 3:

29 | 30 |
Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
31 | Output: " s p a c i n g"
32 | Explanation:
33 | We are also able to place spaces before the first character of the string.
34 | 
35 | 36 |

 

37 |

Constraints:

38 | 39 |
    40 |
  • 1 <= s.length <= 3 * 105
  • 41 |
  • s consists only of lowercase and uppercase English letters.
  • 42 |
  • 1 <= spaces.length <= 3 * 105
  • 43 |
  • 0 <= spaces[i] <= s.length - 1
  • 44 |
  • All the values of spaces are strictly increasing.
  • 45 |
46 |
-------------------------------------------------------------------------------- /0026-remove-duplicates-from-sorted-array/README.md: -------------------------------------------------------------------------------- 1 |

26. Remove Duplicates from Sorted Array

Easy


Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.

2 | 3 |

Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:

4 | 5 |
    6 |
  • Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
  • 7 |
  • Return k.
  • 8 |
9 | 10 |

Custom Judge:

11 | 12 |

The judge will test your solution with the following code:

13 | 14 |
int[] nums = [...]; // Input array
15 | int[] expectedNums = [...]; // The expected answer with correct length
16 | 
17 | int k = removeDuplicates(nums); // Calls your implementation
18 | 
19 | assert k == expectedNums.length;
20 | for (int i = 0; i < k; i++) {
21 |     assert nums[i] == expectedNums[i];
22 | }
23 | 
24 | 25 |

If all assertions pass, then your solution will be accepted.

26 | 27 |

 

28 |

Example 1:

29 | 30 |
Input: nums = [1,1,2]
31 | Output: 2, nums = [1,2,_]
32 | Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
33 | It does not matter what you leave beyond the returned k (hence they are underscores).
34 | 
35 | 36 |

Example 2:

37 | 38 |
Input: nums = [0,0,1,1,1,2,2,3,3,4]
39 | Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
40 | Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
41 | It does not matter what you leave beyond the returned k (hence they are underscores).
42 | 
43 | 44 |

 

45 |

Constraints:

46 | 47 |
    48 |
  • 1 <= nums.length <= 3 * 104
  • 49 |
  • -100 <= nums[i] <= 100
  • 50 |
  • nums is sorted in non-decreasing order.
  • 51 |
52 |
--------------------------------------------------------------------------------